|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { extractDocumentContent } from "../../../../src/media/document-extractors.runtime.js"; |
| 3 | +import { extractPdfContent } from "../../../../src/media/pdf-extract.js"; |
| 4 | +import { resolvePluginDocumentExtractors } from "../../../../src/plugins/document-extractors.runtime.js"; |
| 5 | + |
| 6 | +const SENTINEL = "OPENCLAW_PDF_DISPATCH_SENTINEL"; |
| 7 | + |
| 8 | +function createInlinePdf(text: string): Buffer { |
| 9 | + const escapedText = text.replace(/[\\()]/gu, "\\$&"); |
| 10 | + const content = `BT |
| 11 | +/F1 18 Tf |
| 12 | +72 720 Td |
| 13 | +(${escapedText}) Tj |
| 14 | +ET |
| 15 | +`; |
| 16 | + const objects = [ |
| 17 | + "<< /Type /Catalog /Pages 2 0 R >>", |
| 18 | + "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", |
| 19 | + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>", |
| 20 | + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", |
| 21 | + `<< /Length ${Buffer.byteLength(content)} >> |
| 22 | +stream |
| 23 | +${content}endstream`, |
| 24 | + ]; |
| 25 | + |
| 26 | + let pdf = "%PDF-1.4\n% OpenClaw QA fixture\n"; |
| 27 | + const offsets: number[] = []; |
| 28 | + for (const [index, object] of objects.entries()) { |
| 29 | + offsets.push(Buffer.byteLength(pdf)); |
| 30 | + pdf += `${index + 1} 0 obj |
| 31 | +${object} |
| 32 | +endobj |
| 33 | +`; |
| 34 | + } |
| 35 | + |
| 36 | + const xrefOffset = Buffer.byteLength(pdf); |
| 37 | + pdf += `xref |
| 38 | +0 ${objects.length + 1} |
| 39 | +`; |
| 40 | + pdf += "0000000000 65535 f \n"; |
| 41 | + for (const offset of offsets) { |
| 42 | + pdf += `${String(offset).padStart(10, "0")} 00000 n \n`; |
| 43 | + } |
| 44 | + pdf += `trailer |
| 45 | +<< /Size ${objects.length + 1} /Root 1 0 R >> |
| 46 | +startxref |
| 47 | +${xrefOffset} |
| 48 | +%%EOF |
| 49 | +`; |
| 50 | + return Buffer.from(pdf, "ascii"); |
| 51 | +} |
| 52 | + |
| 53 | +describe("PDF document extraction dispatch", () => { |
| 54 | + it("uses the bundled PDF extractor and reports disabled extraction explicitly", async () => { |
| 55 | + const buffer = createInlinePdf(SENTINEL); |
| 56 | + const bundledPdfExtractor = resolvePluginDocumentExtractors().find( |
| 57 | + (extractor) => extractor.id === "pdf", |
| 58 | + ); |
| 59 | + |
| 60 | + expect(bundledPdfExtractor).toMatchObject({ |
| 61 | + id: "pdf", |
| 62 | + pluginId: "document-extract", |
| 63 | + mimeTypes: ["application/pdf"], |
| 64 | + }); |
| 65 | + |
| 66 | + const extracted = await extractDocumentContent({ |
| 67 | + buffer, |
| 68 | + mimeType: "application/pdf", |
| 69 | + maxPages: 1, |
| 70 | + maxPixels: 1_000_000, |
| 71 | + minTextChars: 1, |
| 72 | + }); |
| 73 | + expect(extracted).toMatchObject({ |
| 74 | + extractor: "pdf", |
| 75 | + text: SENTINEL, |
| 76 | + images: [], |
| 77 | + }); |
| 78 | + |
| 79 | + await expect( |
| 80 | + extractPdfContent({ |
| 81 | + buffer, |
| 82 | + maxPages: 1, |
| 83 | + maxPixels: 1_000_000, |
| 84 | + minTextChars: 1, |
| 85 | + }), |
| 86 | + ).resolves.toEqual({ |
| 87 | + text: SENTINEL, |
| 88 | + images: [], |
| 89 | + }); |
| 90 | + |
| 91 | + await expect( |
| 92 | + extractPdfContent({ |
| 93 | + buffer, |
| 94 | + maxPages: 1, |
| 95 | + maxPixels: 1_000_000, |
| 96 | + minTextChars: 1, |
| 97 | + config: { |
| 98 | + plugins: { |
| 99 | + entries: { |
| 100 | + "document-extract": { enabled: false }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }), |
| 105 | + ).rejects.toThrow( |
| 106 | + "PDF extraction disabled or unavailable: enable the document-extract plugin to process application/pdf files.", |
| 107 | + ); |
| 108 | + }); |
| 109 | +}); |
0 commit comments