Skip to content

Commit 9ce73ef

Browse files
committed
test: add tests for issue #31
1 parent a40cce6 commit 9ce73ef

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

test/issues.test.js

+52-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const path = require('path')
22
const fs = require('fs-extra')
3+
const fetch = require('node-fetch')
34

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

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

9-
jest.setTimeout(10000)
10+
jest.setTimeout(20000)
1011

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

35+
test('check resulting file size (#31)', async () => {
36+
function getFilenameFromUrl (url) {
37+
const oUrl = new URL(url)
38+
const filename = path.basename(oUrl.pathname)
39+
return filename
40+
}
41+
42+
async function downloadFile (url) {
43+
const targetFile = path.join(FIXTURES_DIR, getFilenameFromUrl(url))
44+
try {
45+
await fs.stat(targetFile)
46+
return true
47+
} catch {
48+
console.log('Big files not present. Downloading...')
49+
}
50+
const res = await fetch(url)
51+
const aBuffer = await res.arrayBuffer()
52+
await fs.writeFile(targetFile, Buffer.from(aBuffer))
53+
}
54+
55+
// add some big pdf-files it doesn't realy matter witch ones!
56+
const bigFiles = [
57+
'https://www.ipcc.ch/report/ar6/wg3/downloads/report/IPCC_AR6_WGIII_SPM.pdf',
58+
'https://www.ipcc.ch/report/ar6/wg1/downloads/report/IPCC_AR6_WGI_SPM_final.pdf',
59+
'https://www.ipcc.ch/site/assets/uploads/sites/3/2019/12/SROCC_FullReport_FINAL.pdf'
60+
]
61+
62+
// download some big files
63+
try {
64+
for (const url of bigFiles) {
65+
await downloadFile(url)
66+
await fs.stat(path.join(FIXTURES_DIR, getFilenameFromUrl(url)))
67+
}
68+
} catch (e) {
69+
console.warn(`Could not download some big files to test issue #31. Please ensure the urls are still valid! ${e}`)
70+
}
71+
72+
const merger = new PDFMerger()
73+
74+
for (const url of bigFiles) {
75+
// add the first page of each pdf to the result
76+
merger.add(path.join(FIXTURES_DIR, getFilenameFromUrl(url)), [1])
77+
}
78+
79+
await merger.save(path.join(TMP_DIR, 'IPCC.pdf'))
80+
81+
const stats = await fs.stat(path.join(TMP_DIR, 'IPCC.pdf'))
82+
expect(stats.size).toBeLessThan(2_000_000) // less than 2MB
83+
})
84+
3485
afterAll(async () => {
3586
await fs.remove(TMP_DIR)
3687
})

0 commit comments

Comments
 (0)