Skip to content

Commit 4480160

Browse files
pdfPagesToBlob, splitPdf and load pdf unit tests
1 parent 27549c2 commit 4480160

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

test/unit/SplitPdfHook.test.ts

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { PDFDocument } from "pdf-lib";
2+
import { readFileSync } from "node:fs";
3+
4+
import { SplitPdfHook } from "../../src/hooks/custom/SplitPdfHook";
5+
6+
describe("SplitPdfHook", () => {
7+
let splitPdfHook: SplitPdfHook;
8+
const filename = "test/data/layout-parser-paper.pdf";
9+
let file: Buffer;
10+
let pdf: PDFDocument;
11+
12+
beforeEach(async () => {
13+
splitPdfHook = new SplitPdfHook();
14+
file = readFileSync(filename);
15+
pdf = await PDFDocument.load(file);
16+
});
17+
18+
afterEach(() => {
19+
jest.clearAllMocks();
20+
});
21+
22+
describe("pdfPagesToBlob", () => {
23+
it("should convert range of pages to a Blob object", async () => {
24+
const copyMock = jest.spyOn(PDFDocument.prototype, "copyPages");
25+
const saveMock = jest.spyOn(PDFDocument.prototype, "save");
26+
const addMock = jest.spyOn(PDFDocument.prototype, "addPage");
27+
28+
// Call the method
29+
const result = await splitPdfHook.pdfPagesToBlob(pdf, 4, 8);
30+
31+
// Verify the expected behavior
32+
expect(copyMock).toHaveBeenCalledWith(pdf, [3, 4, 5, 6, 7]);
33+
expect(saveMock).toHaveBeenCalled();
34+
expect(addMock).toHaveBeenCalledTimes(5);
35+
expect(result).toBeInstanceOf(Blob);
36+
expect(result.type).toEqual("application/pdf");
37+
});
38+
});
39+
40+
describe("splitPdf", () => {
41+
it("should split the PDF into one batch", async () => {
42+
const result = await splitPdfHook.splitPdf(pdf, 1);
43+
44+
expect(result).toHaveLength(1);
45+
expect(result[0]?.startPage).toBe(1);
46+
expect(result[0]?.endPage).toBe(16);
47+
});
48+
49+
it("should split the PDF into 3 batches", async () => {
50+
const result = await splitPdfHook.splitPdf(pdf, 3);
51+
52+
// Verify the expected behavior
53+
expect(result).toHaveLength(3);
54+
expect(result[0]?.startPage).toBe(1);
55+
expect(result[0]?.endPage).toBe(6);
56+
expect(result[1]?.startPage).toBe(7);
57+
expect(result[1]?.endPage).toBe(12);
58+
expect(result[2]?.startPage).toBe(13);
59+
expect(result[2]?.endPage).toBe(16);
60+
});
61+
62+
it("should split the PDF into 4 batches", async () => {
63+
const result = await splitPdfHook.splitPdf(pdf, 5);
64+
65+
// Verify the expected behavior
66+
expect(result).toHaveLength(4);
67+
expect(result[0]?.startPage).toBe(1);
68+
expect(result[0]?.endPage).toBe(4);
69+
expect(result[1]?.startPage).toBe(5);
70+
expect(result[1]?.endPage).toBe(8);
71+
expect(result[2]?.startPage).toBe(9);
72+
expect(result[2]?.endPage).toBe(12);
73+
expect(result[3]?.startPage).toBe(13);
74+
expect(result[3]?.endPage).toBe(16);
75+
});
76+
});
77+
78+
describe("loadPdf", () => {
79+
it("should return true, null, and 0 if the file is not a PDF", async () => {
80+
const result = await splitPdfHook.loadPdf(null);
81+
82+
expect(result).toEqual([true, null, 0]);
83+
});
84+
85+
it("should return true, null, and 0 if the file is not a PDF", async () => {
86+
const file = {
87+
name: "document.txt",
88+
content: jest.fn().mockResolvedValue(new ArrayBuffer(0)),
89+
};
90+
91+
const result = await splitPdfHook.loadPdf(file as any);
92+
93+
expect(result).toEqual([true, null, 0]);
94+
expect(file.content).not.toHaveBeenCalled();
95+
});
96+
97+
it("should return true, null, and 0 if there is an error while loading the PDF", async () => {
98+
const file = {
99+
name: "document.pdf",
100+
arrayBuffer: jest.fn().mockRejectedValue(new ArrayBuffer(0)),
101+
};
102+
103+
const result = await splitPdfHook.loadPdf(file as any);
104+
105+
expect(result).toEqual([true, null, 0]);
106+
expect(file.arrayBuffer).toHaveBeenCalled();
107+
});
108+
109+
it("should return false, PDFDocument object, and the number of pages if the PDF is loaded successfully", async () => {
110+
const file = readFileSync("test/data/layout-parser-paper-fast.pdf");
111+
const f = {
112+
name: "document.pdf",
113+
arrayBuffer: () => file.buffer,
114+
};
115+
116+
const loadMock = jest.spyOn(PDFDocument, "load");
117+
118+
const [error, _, pages] = await splitPdfHook.loadPdf(f as any);
119+
120+
expect(error).toBeFalsy();
121+
expect(pages).toEqual(2);
122+
expect(loadMock).toHaveBeenCalledTimes(1);
123+
expect(loadMock).toHaveBeenCalledWith(f.arrayBuffer());
124+
});
125+
});
126+
});

0 commit comments

Comments
 (0)