Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for issue #31 #85

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion test/issues.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const path = require('path')
const fs = require('fs-extra')
const fetch = require('node-fetch')

const PDFMerger = require('../index')

const FIXTURES_DIR = path.join(__dirname, 'fixtures')
const TMP_DIR = path.join(__dirname, 'tmp')

jest.setTimeout(10000)
jest.setTimeout(30000)

describe('issues', () => {
beforeAll(async () => {
Expand All @@ -31,6 +32,56 @@ describe('issues', () => {
await merger.save(path.join(TMP_DIR, 'issue-42_merged.pdf'))
})

test('check resulting file size (#31)', async () => {
function getFilenameFromUrl (url) {
const oUrl = new URL(url)
const filename = path.basename(oUrl.pathname)
return filename
}

async function downloadFile (url) {
const targetFile = path.join(FIXTURES_DIR, getFilenameFromUrl(url))
try {
await fs.stat(targetFile)
return true
} catch {
console.log('Big files not present. Downloading...')
}
const res = await fetch(url)
const aBuffer = await res.arrayBuffer()
await fs.writeFile(targetFile, Buffer.from(aBuffer))
}

// add some big pdf-files it doesn't realy matter witch ones!
const bigFiles = [
'https://www.ipcc.ch/report/ar6/wg3/downloads/report/IPCC_AR6_WGIII_SPM.pdf',
'https://www.ipcc.ch/report/ar6/wg1/downloads/report/IPCC_AR6_WGI_SPM_final.pdf',
'https://www.ipcc.ch/site/assets/uploads/sites/3/2019/12/SROCC_FullReport_FINAL.pdf'
]

// download some big files
try {
for (const url of bigFiles) {
await downloadFile(url)
await fs.stat(path.join(FIXTURES_DIR, getFilenameFromUrl(url)))
}
} catch (e) {
console.warn(`Could not download some big files to test issue #31. Please ensure the urls are still valid! ${e}`)
}

const merger = new PDFMerger()

for (const url of bigFiles) {
// add the first page of each pdf to the result
merger.add(path.join(FIXTURES_DIR, getFilenameFromUrl(url)), [1])
}

await merger.save(path.join(TMP_DIR, 'IPCC.pdf'))

const stats = await fs.stat(path.join(TMP_DIR, 'IPCC.pdf'))
expect(stats.size).toBeLessThan(2000000) // less than 2MB
})

afterAll(async () => {
await fs.remove(TMP_DIR)
})
Expand Down