|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const unpdf = vi.hoisted(() => ({ |
| 4 | + extractText: vi.fn(), |
| 5 | + getDocumentProxy: vi.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +vi.mock("unpdf", () => unpdf); |
| 9 | + |
| 10 | +import { parseFile } from "../file"; |
| 11 | + |
| 12 | +describe("parseFile", () => { |
| 13 | + it("extracts text-layer PDFs into markdown-style page sections", async () => { |
| 14 | + const proxy = { destroy: vi.fn() }; |
| 15 | + unpdf.getDocumentProxy.mockResolvedValue(proxy); |
| 16 | + unpdf.extractText.mockResolvedValue({ |
| 17 | + totalPages: 2, |
| 18 | + text: [ |
| 19 | + "Agentic RL studies agents that learn through environment feedback.", |
| 20 | + "LLM RL usually optimizes language model behavior from preferences.", |
| 21 | + ], |
| 22 | + }); |
| 23 | + |
| 24 | + const file = new File([new Uint8Array([37, 80, 68, 70])], "paper.pdf", { |
| 25 | + type: "application/pdf", |
| 26 | + }); |
| 27 | + |
| 28 | + const parsed = await parseFile(file); |
| 29 | + |
| 30 | + expect(unpdf.getDocumentProxy).toHaveBeenCalledWith(expect.any(Uint8Array)); |
| 31 | + expect(unpdf.extractText).toHaveBeenCalledWith(proxy, { mergePages: false }); |
| 32 | + expect(proxy.destroy).toHaveBeenCalled(); |
| 33 | + expect(parsed).toEqual({ |
| 34 | + filename: "paper.pdf", |
| 35 | + format: "pdf", |
| 36 | + text: [ |
| 37 | + "# PDF: paper.pdf", |
| 38 | + "", |
| 39 | + "Source: PDF", |
| 40 | + "Pages: 2", |
| 41 | + "Extraction: embedded text", |
| 42 | + "", |
| 43 | + "## Page 1", |
| 44 | + "Agentic RL studies agents that learn through environment feedback.", |
| 45 | + "", |
| 46 | + "## Page 2", |
| 47 | + "LLM RL usually optimizes language model behavior from preferences.", |
| 48 | + ].join("\n"), |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it("marks image-heavy PDFs when little text is available", async () => { |
| 53 | + const proxy = { destroy: vi.fn() }; |
| 54 | + unpdf.getDocumentProxy.mockResolvedValue(proxy); |
| 55 | + unpdf.extractText.mockResolvedValue({ |
| 56 | + totalPages: 3, |
| 57 | + text: ["", " ", "Appendix"], |
| 58 | + }); |
| 59 | + |
| 60 | + const file = new File([new Uint8Array([37, 80, 68, 70])], "scan.pdf", { |
| 61 | + type: "application/pdf", |
| 62 | + }); |
| 63 | + |
| 64 | + const parsed = await parseFile(file); |
| 65 | + |
| 66 | + expect(parsed.format).toBe("pdf"); |
| 67 | + expect(parsed.text).toContain( |
| 68 | + "> Note: This PDF appears to be scanned or image-heavy. Text extraction is limited.", |
| 69 | + ); |
| 70 | + expect(parsed.text).toContain("Pages: 3"); |
| 71 | + expect(parsed.text).toContain("## Page 3\nAppendix"); |
| 72 | + }); |
| 73 | + |
| 74 | + it("does not mark concise text-layer PDFs as limited extraction", async () => { |
| 75 | + const proxy = { destroy: vi.fn() }; |
| 76 | + unpdf.getDocumentProxy.mockResolvedValue(proxy); |
| 77 | + unpdf.extractText.mockResolvedValue({ |
| 78 | + totalPages: 1, |
| 79 | + text: ["Paid. Total: $42."], |
| 80 | + }); |
| 81 | + |
| 82 | + const file = new File([new Uint8Array([37, 80, 68, 70])], "receipt.pdf", { |
| 83 | + type: "application/pdf", |
| 84 | + }); |
| 85 | + |
| 86 | + const parsed = await parseFile(file); |
| 87 | + |
| 88 | + expect(parsed.format).toBe("pdf"); |
| 89 | + expect(parsed.text).toContain("Extraction: embedded text"); |
| 90 | + expect(parsed.text).not.toContain("Text extraction is limited"); |
| 91 | + expect(parsed.text).toContain("## Page 1\nPaid. Total: $42."); |
| 92 | + }); |
| 93 | +}); |
0 commit comments