Skip to content

Commit 1cdfe9e

Browse files
authored
test(qa): cover PDF extraction dispatch (#118860)
1 parent a2ccd6e commit 1cdfe9e

2 files changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
title: PDF document extraction dispatch
2+
3+
scenario:
4+
id: pdf-document-extraction-dispatch
5+
surface: media
6+
category: media.media-intake-and-access
7+
coverage:
8+
primary:
9+
- media.pdf-document-extraction-dispatch
10+
objective: Verify PDF intake dispatches through the bundled document extractor to real clawpdf and reports disabled extraction explicitly.
11+
successCriteria:
12+
- The bundled document-extract plugin supplies the PDF extractor.
13+
- A parseable inline PDF yields its sentinel text through real clawpdf.
14+
- The document extractor runtime tags the result with extractor pdf.
15+
- Disabling document extraction produces the explicit unavailable error.
16+
docsRefs:
17+
- docs/plugins/reference/document-extract.md
18+
- docs/tools/pdf.md
19+
codeRefs:
20+
- src/media/pdf-extract.ts
21+
- src/media/document-extractors.runtime.ts
22+
- src/plugins/document-extractors.runtime.ts
23+
- extensions/document-extract/document-extractor.ts
24+
- test/e2e/qa-lab/media/pdf-document-extraction-dispatch.product.test.ts
25+
execution:
26+
kind: vitest
27+
path: test/e2e/qa-lab/media/pdf-document-extraction-dispatch.product.test.ts
28+
summary: Run a parseable inline PDF through bundled extractor discovery, real clawpdf extraction, and the disabled-plugin failure path.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)