From 42b030266dde2e22c53ff7ce8bb19cc447a6d333 Mon Sep 17 00:00:00 2001 From: Alexander Wunschik Date: Sat, 27 Jan 2024 12:27:32 +0100 Subject: [PATCH] test: add tests for valid input formats --- PDFMergerBase.js | 6 +- package-lock.json | 109 ++++++++ package.json | 1 + ...ures.test.js => PDFMerger-browser.test.js} | 2 +- test/PDFMerger.test.js | 244 ++++++++++++++++++ test/fixtures.test.js | 169 ------------ test/issues.test.js | 1 + 7 files changed, 357 insertions(+), 175 deletions(-) rename test/{browser-fixtures.test.js => PDFMerger-browser.test.js} (99%) create mode 100644 test/PDFMerger.test.js delete mode 100644 test/fixtures.test.js diff --git a/PDFMergerBase.js b/PDFMergerBase.js index 29dafd4..86a525b 100644 --- a/PDFMergerBase.js +++ b/PDFMergerBase.js @@ -1,6 +1,6 @@ import { PDFDocument } from 'pdf-lib' -import { parsePagesString } from './parsePagesString' +import { parsePagesString } from './parsePagesString.js' /** * @typedef {Object} Metadata @@ -14,10 +14,6 @@ import { parsePagesString } from './parsePagesString' * @typedef {Uint8Array | ArrayBuffer | Blob | URL} PdfInput */ -/** - * @class PDFMergerBase - * @classdesc Base class for PDFMerger - */ export default class PDFMergerBase { /** * The internal pdf-lib document. diff --git a/package-lock.json b/package-lock.json index ade5dec..ae78a32 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "pdf-lib": "^1.17.1" }, "devDependencies": { + "cross-blob": "^3.0.2", "fs-extra": "^11.2.0", "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", @@ -1773,6 +1774,12 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "dev": true }, + "node_modules/blob-polyfill": { + "version": "7.0.20220408", + "resolved": "https://registry.npmjs.org/blob-polyfill/-/blob-polyfill-7.0.20220408.tgz", + "integrity": "sha512-oD8Ydw+5lNoqq+en24iuPt1QixdPpe/nUF8azTHnviCZYu9zUC+TwdzIp5orpblJosNlgNbVmmAb//c6d6ImUQ==", + "dev": true + }, "node_modules/brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -2087,6 +2094,19 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, + "node_modules/cross-blob": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/cross-blob/-/cross-blob-3.0.2.tgz", + "integrity": "sha512-u+7xq68MAjIqvoEKrdgIEupKJNBeU8MSl/cpfPmJ3rm9yvxrgbMPr8TkZS9qnwCgiVC8BsEt9kDkeD7He2zmNA==", + "dev": true, + "dependencies": { + "blob-polyfill": "^7.0.20220408", + "fetch-blob": "^3.2.0" + }, + "engines": { + "node": "^12.20 || >=14.13" + } + }, "node_modules/cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", @@ -3264,6 +3284,29 @@ "bser": "2.1.1" } }, + "node_modules/fetch-blob": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", + "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "paypal", + "url": "https://paypal.me/jimmywarting" + } + ], + "dependencies": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + }, + "engines": { + "node": "^12.20 || >= 14.13" + } + }, "node_modules/file-entry-cache": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz", @@ -5558,6 +5601,25 @@ "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==", "dev": true }, + "node_modules/node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "github", + "url": "https://paypal.me/jimmywarting" + } + ], + "engines": { + "node": ">=10.5.0" + } + }, "node_modules/node-fetch": { "version": "2.7.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", @@ -7301,6 +7363,15 @@ "makeerror": "1.0.12" } }, + "node_modules/web-streams-polyfill": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.2.tgz", + "integrity": "sha512-3pRGuxRF5gpuZc0W+EpwQRmCD7gRqcDOMt688KmdlDAgAyaB1XlN0zq2njfDNm44XVdIouE7pZ6GzbdyH47uIQ==", + "dev": true, + "engines": { + "node": ">= 8" + } + }, "node_modules/webidl-conversions": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", @@ -8958,6 +9029,12 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "dev": true }, + "blob-polyfill": { + "version": "7.0.20220408", + "resolved": "https://registry.npmjs.org/blob-polyfill/-/blob-polyfill-7.0.20220408.tgz", + "integrity": "sha512-oD8Ydw+5lNoqq+en24iuPt1QixdPpe/nUF8azTHnviCZYu9zUC+TwdzIp5orpblJosNlgNbVmmAb//c6d6ImUQ==", + "dev": true + }, "brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -9179,6 +9256,16 @@ "prompts": "^2.0.1" } }, + "cross-blob": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/cross-blob/-/cross-blob-3.0.2.tgz", + "integrity": "sha512-u+7xq68MAjIqvoEKrdgIEupKJNBeU8MSl/cpfPmJ3rm9yvxrgbMPr8TkZS9qnwCgiVC8BsEt9kDkeD7He2zmNA==", + "dev": true, + "requires": { + "blob-polyfill": "^7.0.20220408", + "fetch-blob": "^3.2.0" + } + }, "cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", @@ -10033,6 +10120,16 @@ "bser": "2.1.1" } }, + "fetch-blob": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", + "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", + "dev": true, + "requires": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + } + }, "file-entry-cache": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz", @@ -11732,6 +11829,12 @@ "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==", "dev": true }, + "node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", + "dev": true + }, "node-fetch": { "version": "2.7.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", @@ -12978,6 +13081,12 @@ "makeerror": "1.0.12" } }, + "web-streams-polyfill": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.2.tgz", + "integrity": "sha512-3pRGuxRF5gpuZc0W+EpwQRmCD7gRqcDOMt688KmdlDAgAyaB1XlN0zq2njfDNm44XVdIouE7pZ6GzbdyH47uIQ==", + "dev": true + }, "webidl-conversions": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", diff --git a/package.json b/package.json index eaa6d14..4b22110 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "pdf-lib": "^1.17.1" }, "devDependencies": { + "cross-blob": "^3.0.2", "fs-extra": "^11.2.0", "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", diff --git a/test/browser-fixtures.test.js b/test/PDFMerger-browser.test.js similarity index 99% rename from test/browser-fixtures.test.js rename to test/PDFMerger-browser.test.js index 51a1933..0c853c2 100644 --- a/test/browser-fixtures.test.js +++ b/test/PDFMerger-browser.test.js @@ -34,7 +34,7 @@ async function readFixtureAsUint8Array (file) { // Note: The browser tests differ from standard as all files are expected // to be generated or fetched before being passed into the merger. // For testing, they are retrieved with fs.ReadFile() and then passed in. -describe('PDFMerger', () => { +describe('PDFMerger for browser', () => { beforeAll(async () => { await fs.ensureDir(TMP_DIR) fileA = await readFixtureAsUint8Array('Testfile_A.pdf') diff --git a/test/PDFMerger.test.js b/test/PDFMerger.test.js new file mode 100644 index 0000000..fa9b1af --- /dev/null +++ b/test/PDFMerger.test.js @@ -0,0 +1,244 @@ +import path from 'path' + +import fs from 'fs-extra' +import Blob from 'cross-blob' +import pdfDiff from 'pdf-diff' +import { PDFDocument } from 'pdf-lib' +import { jest } from '@jest/globals' + +import PDFMerger from '../index' + +const __dirname = path.dirname(new URL(import.meta.url).pathname) +const FIXTURES_DIR = path.join(__dirname, 'fixtures') +const TMP_DIR = path.join(__dirname, 'tmp') + +jest.setTimeout(10000) + +describe('PDFMerger', () => { + beforeAll(async () => { + await fs.ensureDir(TMP_DIR) + }) + + test('merge two simple files', async () => { + const merger = new PDFMerger() + await merger.add(path.join(FIXTURES_DIR, 'Testfile_A.pdf')) + await merger.add(path.join(FIXTURES_DIR, 'Testfile_B.pdf')) + await merger.save(path.join(TMP_DIR, 'Testfile_AB.pdf')) + + const diff = await pdfDiff( + path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), + path.join(TMP_DIR, 'Testfile_AB.pdf') + ) + + expect(diff).toBeFalsy() + }) + + test('reset the internal document', async () => { + const merger = new PDFMerger() + await merger.add(path.join(FIXTURES_DIR, 'MergeDemo.pdf')) + merger.reset() + await merger.add(path.join(FIXTURES_DIR, 'Testfile_A.pdf')) + await merger.add(path.join(FIXTURES_DIR, 'Testfile_B.pdf')) + await merger.save(path.join(TMP_DIR, 'Testfile_AB.pdf')) + + const diff = await pdfDiff( + path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), + path.join(TMP_DIR, 'Testfile_AB.pdf') + ) + + expect(diff).toBeFalsy() + }) + + test('set title and check if title is set properly', async () => { + const merger = new PDFMerger() + await merger.add(path.join(FIXTURES_DIR, 'Testfile_A.pdf')) + + const testTitle = 'Test Title' + + await merger.setMetadata({ + title: testTitle + }) + + await merger.save(path.join(TMP_DIR, 'TestfileAMetadata.pdf')) + const pdfData = fs.readFileSync(path.join(TMP_DIR, 'TestfileAMetadata.pdf')) + + const outputDocument = await PDFDocument.load(pdfData, { ignoreEncryption: true }) + const outputTitle = outputDocument.getTitle() + + expect(testTitle).toBe(outputTitle) + }) + + describe('test valid inputs', () => { + test('provide input as Uint8Array', async () => { + const merger = new PDFMerger() + const TestfileAUint8Array = await fs.readFile(path.join(FIXTURES_DIR, 'Testfile_A.pdf')) + expect(TestfileAUint8Array).toBeInstanceOf(Uint8Array) + await merger.add(TestfileAUint8Array) + const TestfileBUint8Array = await fs.readFile(path.join(FIXTURES_DIR, 'Testfile_B.pdf')) + expect(TestfileBUint8Array).toBeInstanceOf(Uint8Array) + await merger.add(TestfileBUint8Array) + await merger.save(path.join(TMP_DIR, 'Testfile_AB.pdf')) + const diff = await pdfDiff( + path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), + path.join(TMP_DIR, 'Testfile_AB.pdf') + ) + expect(diff).toBeFalsy() + }) + + test('provide input as ArrayBuffer', async () => { + const merger = new PDFMerger() + const TestfileAUint8Array = await fs.readFile(path.join(FIXTURES_DIR, 'Testfile_A.pdf'), null) + const TestfileABuffer = TestfileAUint8Array.buffer + expect(TestfileABuffer).toBeInstanceOf(ArrayBuffer) + const TestfileBUint8Array = await fs.readFile(path.join(FIXTURES_DIR, 'Testfile_B.pdf'), null) + const TestfileBBuffer = TestfileBUint8Array.buffer + expect(TestfileBBuffer).toBeInstanceOf(ArrayBuffer) + await merger.add(TestfileABuffer) + await merger.add(TestfileBBuffer) + await merger.save(path.join(TMP_DIR, 'Testfile_AB.pdf')) + const diff = await pdfDiff( + path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), + path.join(TMP_DIR, 'Testfile_AB.pdf') + ) + expect(diff).toBeFalsy() + }) + + test('provide input as Blob', async () => { + const merger = new PDFMerger() + const TestfileAUint8Array = await fs.readFile(path.join(FIXTURES_DIR, 'Testfile_A.pdf')) + const TestfileABlob = new Blob([TestfileAUint8Array]) + expect(TestfileABlob).toBeInstanceOf(Blob) + await merger.add(TestfileABlob) + const TestfileBUint8Array = await fs.readFile(path.join(FIXTURES_DIR, 'Testfile_B.pdf')) + const TestfileBBlob = new Blob([TestfileBUint8Array]) + expect(TestfileBBlob).toBeInstanceOf(Blob) + await merger.add(TestfileBBlob) + await merger.save(path.join(TMP_DIR, 'Testfile_AB.pdf')) + const diff = await pdfDiff( + path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), + path.join(TMP_DIR, 'Testfile_AB.pdf') + ) + expect(diff).toBeFalsy() + }) + + test('provide input as URL', async () => { + const merger = new PDFMerger() + const TestfileAURL = new URL('https://raw.githubusercontent.com/nbesli/pdf-merger-js/master/test/fixtures/Testfile_A.pdf') + expect(TestfileAURL).toBeInstanceOf(URL) + await merger.add(TestfileAURL) + const TestfileBURL = new URL('https://raw.githubusercontent.com/nbesli/pdf-merger-js/master/test/fixtures/Testfile_B.pdf') + expect(TestfileBURL).toBeInstanceOf(URL) + await merger.add(TestfileBURL) + const diff = await pdfDiff( + path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), + path.join(TMP_DIR, 'Testfile_AB.pdf') + ) + expect(diff).toBeFalsy() + }) + }) + + describe('test valid output formats', () => { + test('saveAsBuffer returns a Buffer', async () => { + const merger = new PDFMerger() + await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf')) + const buffer = await merger.saveAsBuffer() + expect(buffer instanceof Buffer).toEqual(true) + }) + }) + + describe('test valid page definitions', () => { + test('combine pages from multiple books (array)', async () => { + const merger = new PDFMerger() + const tmpFile = 'MergeDemo1.pdf' + await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), [1]) + await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), [1, 2, 3]) + await merger.save(path.join(TMP_DIR, tmpFile)) + + const diff = await pdfDiff( + path.join(FIXTURES_DIR, 'MergeDemo.pdf'), + path.join(TMP_DIR, tmpFile) + ) + + expect(diff).toBeFalsy() + }) + + test('combine pages from multiple books (string - array)', async () => { + const merger = new PDFMerger() + const tmpFile = 'MergeDemo1.pdf' + await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), '1') + await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), [1, 2, 3]) + await merger.save(path.join(TMP_DIR, tmpFile)) + + const diff = await pdfDiff( + path.join(FIXTURES_DIR, 'MergeDemo.pdf'), + path.join(TMP_DIR, tmpFile) + ) + + expect(diff).toBeFalsy() + }) + + test('combine pages from multiple books (plain number - array)', async () => { + const merger = new PDFMerger() + const tmpFile = 'MergeDemo1.pdf' + await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), 1) + await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), [1, 2, 3]) + await merger.save(path.join(TMP_DIR, tmpFile)) + + const diff = await pdfDiff( + path.join(FIXTURES_DIR, 'MergeDemo.pdf'), + path.join(TMP_DIR, tmpFile) + ) + + expect(diff).toBeFalsy() + }) + + test('combine pages from multiple books (start - end)', async () => { + const merger = new PDFMerger() + const tmpFile = 'MergeDemo2.pdf' + await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), [1]) + await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), '1 - 3') + await merger.save(path.join(TMP_DIR, tmpFile)) + + const diff = await pdfDiff( + path.join(FIXTURES_DIR, 'MergeDemo.pdf'), + path.join(TMP_DIR, tmpFile) + ) + + expect(diff).toBeFalsy() + }) + + test('combine pages from multiple books (start to end)', async () => { + const merger = new PDFMerger() + const tmpFile = 'MergeDemo2.pdf' + await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), [1]) + await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), '1 to 3') + await merger.save(path.join(TMP_DIR, tmpFile)) + + const diff = await pdfDiff( + path.join(FIXTURES_DIR, 'MergeDemo.pdf'), + path.join(TMP_DIR, tmpFile) + ) + + expect(diff).toBeFalsy() + }) + + test('combine pages from multiple books (start-end)', async () => { + const merger = new PDFMerger() + const tmpFile = 'MergeDemo2.pdf' + await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), [1]) + await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), '1-3') + await merger.save(path.join(TMP_DIR, tmpFile)) + + const diff = await pdfDiff( + path.join(FIXTURES_DIR, 'MergeDemo.pdf'), + path.join(TMP_DIR, tmpFile) + ) + + expect(diff).toBeFalsy() + }) + }) + + afterAll(async () => { + await fs.remove(TMP_DIR) + }) +}) diff --git a/test/fixtures.test.js b/test/fixtures.test.js deleted file mode 100644 index cc00ee1..0000000 --- a/test/fixtures.test.js +++ /dev/null @@ -1,169 +0,0 @@ -import path from 'path' -import fs from 'fs-extra' -import pdfDiff from 'pdf-diff' -import { PDFDocument } from 'pdf-lib' -import { jest } from '@jest/globals' - -import PDFMerger from '../index' - -const __dirname = path.dirname(new URL(import.meta.url).pathname) -const FIXTURES_DIR = path.join(__dirname, 'fixtures') -const TMP_DIR = path.join(__dirname, 'tmp') - -jest.setTimeout(10000) - -describe('PDFMerger', () => { - beforeAll(async () => { - await fs.ensureDir(TMP_DIR) - }) - - test('merge two simple files', async () => { - const merger = new PDFMerger() - await merger.add(path.join(FIXTURES_DIR, 'Testfile_A.pdf')) - await merger.add(path.join(FIXTURES_DIR, 'Testfile_B.pdf')) - await merger.save(path.join(TMP_DIR, 'Testfile_AB.pdf')) - - const diff = await pdfDiff( - path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), - path.join(TMP_DIR, 'Testfile_AB.pdf') - ) - - expect(diff).toBeFalsy() - }) - - test('reset the internal document', async () => { - const merger = new PDFMerger() - await merger.add(path.join(FIXTURES_DIR, 'MergeDemo.pdf')) - merger.reset() - await merger.add(path.join(FIXTURES_DIR, 'Testfile_A.pdf')) - await merger.add(path.join(FIXTURES_DIR, 'Testfile_B.pdf')) - await merger.save(path.join(TMP_DIR, 'Testfile_AB.pdf')) - - const diff = await pdfDiff( - path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), - path.join(TMP_DIR, 'Testfile_AB.pdf') - ) - - expect(diff).toBeFalsy() - }) - - test('combine pages from multipel books (array)', async () => { - const merger = new PDFMerger() - const tmpFile = 'MergeDemo1.pdf' - await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), [1]) - await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), [1, 2, 3]) - await merger.save(path.join(TMP_DIR, tmpFile)) - - const diff = await pdfDiff( - path.join(FIXTURES_DIR, 'MergeDemo.pdf'), - path.join(TMP_DIR, tmpFile) - ) - - expect(diff).toBeFalsy() - }) - - test('combine pages from multipel books (string - array)', async () => { - const merger = new PDFMerger() - const tmpFile = 'MergeDemo1.pdf' - await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), '1') - await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), [1, 2, 3]) - await merger.save(path.join(TMP_DIR, tmpFile)) - - const diff = await pdfDiff( - path.join(FIXTURES_DIR, 'MergeDemo.pdf'), - path.join(TMP_DIR, tmpFile) - ) - - expect(diff).toBeFalsy() - }) - - test('combine pages from multipel books (plain number - array)', async () => { - const merger = new PDFMerger() - const tmpFile = 'MergeDemo1.pdf' - await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), 1) - await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), [1, 2, 3]) - await merger.save(path.join(TMP_DIR, tmpFile)) - - const diff = await pdfDiff( - path.join(FIXTURES_DIR, 'MergeDemo.pdf'), - path.join(TMP_DIR, tmpFile) - ) - - expect(diff).toBeFalsy() - }) - - test('combine pages from multipel books (start - end)', async () => { - const merger = new PDFMerger() - const tmpFile = 'MergeDemo2.pdf' - await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), [1]) - await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), '1 - 3') - await merger.save(path.join(TMP_DIR, tmpFile)) - - const diff = await pdfDiff( - path.join(FIXTURES_DIR, 'MergeDemo.pdf'), - path.join(TMP_DIR, tmpFile) - ) - - expect(diff).toBeFalsy() - }) - - test('combine pages from multipel books (start to end)', async () => { - const merger = new PDFMerger() - const tmpFile = 'MergeDemo2.pdf' - await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), [1]) - await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), '1 to 3') - await merger.save(path.join(TMP_DIR, tmpFile)) - - const diff = await pdfDiff( - path.join(FIXTURES_DIR, 'MergeDemo.pdf'), - path.join(TMP_DIR, tmpFile) - ) - - expect(diff).toBeFalsy() - }) - - test('combine pages from multipel books (start-end)', async () => { - const merger = new PDFMerger() - const tmpFile = 'MergeDemo2.pdf' - await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), [1]) - await merger.add(path.join(FIXTURES_DIR, 'UDHR.pdf'), '1-3') - await merger.save(path.join(TMP_DIR, tmpFile)) - - const diff = await pdfDiff( - path.join(FIXTURES_DIR, 'MergeDemo.pdf'), - path.join(TMP_DIR, tmpFile) - ) - - expect(diff).toBeFalsy() - }) - - test('saveAsBuffer returns a Buffer', async () => { - const merger = new PDFMerger() - await merger.add(path.join(FIXTURES_DIR, 'Testfile_AB.pdf')) - const buffer = await merger.saveAsBuffer() - expect(buffer instanceof Buffer).toEqual(true) - }) - - test('set title and check if title is set properly', async () => { - const merger = new PDFMerger() - await merger.add(path.join(FIXTURES_DIR, 'Testfile_A.pdf')) - - const testTitle = 'Test Title' - - await merger.setMetadata({ - title: testTitle - }) - - await merger.save(path.join(TMP_DIR, 'Testfile_A_Metadata.pdf')) - const pdfData = fs.readFileSync(path.join(TMP_DIR, 'Testfile_A_Metadata.pdf')) - - const outputDocument = await PDFDocument.load(pdfData, { ignoreEncryption: true }) - const outputTitle = outputDocument.getTitle() - - expect(testTitle).toBe(outputTitle) - }) - - afterAll(async () => { - await fs.remove(TMP_DIR) - }) -}) diff --git a/test/issues.test.js b/test/issues.test.js index 31ce131..f32cc80 100644 --- a/test/issues.test.js +++ b/test/issues.test.js @@ -1,4 +1,5 @@ import path from 'path' + import fs from 'fs-extra' import { jest } from '@jest/globals'