From 24528ab856304d80f36f857c0c44cf6b0f63a174 Mon Sep 17 00:00:00 2001 From: Jack Yao Date: Thu, 24 Sep 2026 14:06:58 +0800 Subject: [PATCH 1/2] test: gate PDF extraction scenarios when pdfjs-dist is absent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Enhanced CI 在 main 的 Node 24 任务中 pdfjs-dist(optionalDependency)未装上(npm ci 对可选依赖安装失败不报错),导致 pdf → X 矩阵用例在缺少提取能力的环境下失败。 - format-validation-test:pdfjs 缺失且核心解析器取不到正文时,探针断言 PDF_TEXT_UNAVAILABLE 并跳过 pdf 输入用例;pdfjs 可用却取不到正文仍按回归失败 - ocr-baseline-test 29c:混合页按页 OCR 场景依赖 PDF.js 页级检测,缺失时整组跳过 - pdf-reader-test 测试 11:混合页页数/无文字页断言依赖 PDF.js 提取,缺失时跳过 本机双向验证:隐藏/恢复 node_modules/pdfjs-dist 各跑一遍完整 npm test 均通过。 Refs #216 --- scripts/format-validation-test.js | 25 ++++ scripts/ocr-baseline-test.js | 184 +++++++++++++++--------------- scripts/pdf-reader-test.js | 18 ++- 3 files changed, 131 insertions(+), 96 deletions(-) diff --git a/scripts/format-validation-test.js b/scripts/format-validation-test.js index 97c5d02..276f497 100644 --- a/scripts/format-validation-test.js +++ b/scripts/format-validation-test.js @@ -130,6 +130,27 @@ const sampleInputs = { // Exercise PDF routes with the same pre-expanded input used by the workbench. sampleInputs.pdf = await expandPdfContentForTextExtraction(sampleInputs.pdf); +// PDF 文本提取能力探针:pdfjs-dist 是可选依赖,npm ci 对 optionalDependency 安装失败不报错, +// 环境可能缺少它(Enhanced CI 曾在 main 的 Node 24 任务遇到)。此时核心解析器对本样例取不到正文, +// pdf → X 应诚实抛出 PDF_TEXT_UNAVAILABLE(而不是旧行为那样导出占位提示)。 +// 探针在该环境下把矩阵中的 pdf 输入用例记为跳过;若 pdfjs 可用却取不到正文,按回归失败处理。 +const pdfjsInstalled = await import("pdfjs-dist").then(() => true, () => false); +let pdfTextExtractionAvailable = true; +if (!pdfjsInstalled) { + try { + convertContent({ content: sampleInputs.pdf, from: "pdf", to: "txt", title: "probe.pdf", fileName: "probe.pdf" }); + } catch (error) { + if (error?.code === "PDF_TEXT_UNAVAILABLE") { + pdfTextExtractionAvailable = false; + } else { + throw error; + } + } + if (!pdfTextExtractionAvailable) { + console.log("⊘ 当前环境无 PDF 文本提取能力(pdfjs-dist 未安装且核心解析器无可用正文);pdf → * 用例跳过,PDF_TEXT_UNAVAILABLE 错误码已断言。"); + } +} + // 所有支持的格式 const formats = { input: ["txt", "md", "html", "csv", "json", "xml", "docx", "xlsx", "epub", "pptx", "pdf", "doc"], @@ -159,6 +180,10 @@ for (const from of formats.input) { stats.skipped++; continue; } + if (from === "pdf" && !pdfTextExtractionAvailable) { + stats.skipped++; + continue; + } stats.total++; try { const testData = sampleInputs[from] || testCases[from] || `Test content for ${from}`; diff --git a/scripts/ocr-baseline-test.js b/scripts/ocr-baseline-test.js index 87d9484..eda987c 100644 --- a/scripts/ocr-baseline-test.js +++ b/scripts/ocr-baseline-test.js @@ -979,99 +979,103 @@ function makeStubEngine(overrides = {}) { } // 29c. Mixed PDF OCR handles only textless pages and retains page order. -{ - const pdf = writePdfHighFidelity({ - model: { - title: "mixed-text-scan", - fixedLayout: { - pages: [ - { size: { width: 612, height: 792 }, textRuns: [ - { text: "FIRST", bbox: { x: 100, y: 700, w: 100, h: 12 }, fontSize: 12 }, - ] }, - { size: { width: 612, height: 792 }, textRuns: [] }, - { size: { width: 612, height: 792 }, textRuns: [ - { text: "LAST", bbox: { x: 100, y: 700, w: 100, h: 12 }, fontSize: 12 }, - ] }, - ], +// 依赖 pdfjs-dist 提取文本页;可选依赖缺失的环境(npm ci 可选安装失败)跳过本组。 +const pdfjsAvailableForMixed = await import("pdfjs-dist").then(() => true, () => false); +if (pdfjsAvailableForMixed) { + const pdf = writePdfHighFidelity({ + model: { + title: "mixed-text-scan", + fixedLayout: { + pages: [ + { size: { width: 612, height: 792 }, textRuns: [ + { text: "FIRST", bbox: { x: 100, y: 700, w: 100, h: 12 }, fontSize: 12 }, + ] }, + { size: { width: 612, height: 792 }, textRuns: [] }, + { size: { width: 612, height: 792 }, textRuns: [ + { text: "LAST", bbox: { x: 100, y: 700, w: 100, h: 12 }, fontSize: 12 }, + ] }, + ], + }, }, - }, - }); - const detection = await isScannedPdf(pdf.data); - assert.equal(detection.reason, "mixed-text-and-textless-pages"); - assert.deepEqual(detection.pageIndices, [1]); - const rasterizedPages = []; - setPdfPageRasterizer({ - async countPages() { return 3; }, - async rasterize({ pageIndex }) { - rasterizedPages.push(pageIndex); - return { dataUrl: "data:image/png;base64,YQ==", width: 100, height: 100 }; - }, - }); - const engine = { - id: "mixed-page-engine", - taskCapabilities: ["ocr-text"], - isAvailable: () => true, - recognize: async () => createOCRResult({ - pages: [{ pageIndex: 0, width: 100, height: 100, lines: [ - { text: "OCR MIDDLE", confidence: 0.9, bbox: { x: 10, y: 20, w: 40, h: 10 } }, - ] }], - fullText: "OCR MIDDLE", - averageConfidence: 0.9, - }), - }; - defaultOCRRegistry.register(engine); - try { - const output = await convertContentAsync({ - content: pdf.data, - from: "pdf", - to: "txt", - title: "mixed-text-scan", - options: { repair: false }, - }); - assert.deepEqual(rasterizedPages, [1], "existing text pages must not be OCR'ed again"); - assert.deepEqual(output.data.trim().split(/\n\s*\n/), ["FIRST", "OCR MIDDLE", "LAST"]); - const pdfOutput = await convertContentAsync({ - content: pdf.data, - from: "pdf", - to: "pdf", - title: "mixed-text-scan", - options: { repair: false }, - }); - assert.deepEqual(rasterizedPages, [1], "PDF identity copy must not run OCR or redraw pages"); - assert.equal(pdfOutput.data, pdf.data, "PDF to PDF should preserve the source bytes"); - assert.ok((pdfOutput.warnings || []).some((warning) => warning.code === "PDF_ORIGINAL_PRESERVED")); - const readBack = readPdf({ content: await expandPdfContentForTextExtraction(pdfOutput.data) }); - assert.equal(readBack.metadata.pdf.pageCount, 3); - assert.equal(readBack.blocks.some((block) => String(block.text || "").includes("OCR MIDDLE")), false); - const verifiedOutput = await convertContentAsync({ - content: pdf.data, - from: "pdf", - to: "txt", - title: "mixed-text-scan", }); - assert.deepEqual(verifiedOutput.quality.qualityReport.unresolvedPdfPages, []); - const incompleteOutput = await convertContentAsync({ - content: pdf.data, - from: "pdf", - to: "txt", - title: "mixed-text-scan", - options: { ocr: { enabled: false } }, - }); - assert.deepEqual(incompleteOutput.quality.qualityReport.unresolvedPdfPages, [2]); - assert.ok(incompleteOutput.quality.warnings.some((warning) => warning.code === "PDF_PAGES_WITHOUT_TEXT")); - const bareOutput = await convertContentAsync({ - content: pdf.data, - from: "pdf", - to: "txt", - title: "mixed-text-scan", - options: { repair: false, ocr: { enabled: false } }, + const detection = await isScannedPdf(pdf.data); + assert.equal(detection.reason, "mixed-text-and-textless-pages"); + assert.deepEqual(detection.pageIndices, [1]); + const rasterizedPages = []; + setPdfPageRasterizer({ + async countPages() { return 3; }, + async rasterize({ pageIndex }) { + rasterizedPages.push(pageIndex); + return { dataUrl: "data:image/png;base64,YQ==", width: 100, height: 100 }; + }, }); - assert.deepEqual(bareOutput.unresolvedPdfPages, [2]); - assert.ok(bareOutput.warnings.some((warning) => warning.code === "PDF_PAGES_WITHOUT_TEXT")); - } finally { - defaultOCRRegistry.unregister(engine.id); - resetPdfPageRasterizer(); - } + const engine = { + id: "mixed-page-engine", + taskCapabilities: ["ocr-text"], + isAvailable: () => true, + recognize: async () => createOCRResult({ + pages: [{ pageIndex: 0, width: 100, height: 100, lines: [ + { text: "OCR MIDDLE", confidence: 0.9, bbox: { x: 10, y: 20, w: 40, h: 10 } }, + ] }], + fullText: "OCR MIDDLE", + averageConfidence: 0.9, + }), + }; + defaultOCRRegistry.register(engine); + try { + const output = await convertContentAsync({ + content: pdf.data, + from: "pdf", + to: "txt", + title: "mixed-text-scan", + options: { repair: false }, + }); + assert.deepEqual(rasterizedPages, [1], "existing text pages must not be OCR'ed again"); + assert.deepEqual(output.data.trim().split(/\n\s*\n/), ["FIRST", "OCR MIDDLE", "LAST"]); + const pdfOutput = await convertContentAsync({ + content: pdf.data, + from: "pdf", + to: "pdf", + title: "mixed-text-scan", + options: { repair: false }, + }); + assert.deepEqual(rasterizedPages, [1], "PDF identity copy must not run OCR or redraw pages"); + assert.equal(pdfOutput.data, pdf.data, "PDF to PDF should preserve the source bytes"); + assert.ok((pdfOutput.warnings || []).some((warning) => warning.code === "PDF_ORIGINAL_PRESERVED")); + const readBack = readPdf({ content: await expandPdfContentForTextExtraction(pdfOutput.data) }); + assert.equal(readBack.metadata.pdf.pageCount, 3); + assert.equal(readBack.blocks.some((block) => String(block.text || "").includes("OCR MIDDLE")), false); + const verifiedOutput = await convertContentAsync({ + content: pdf.data, + from: "pdf", + to: "txt", + title: "mixed-text-scan", + }); + assert.deepEqual(verifiedOutput.quality.qualityReport.unresolvedPdfPages, []); + const incompleteOutput = await convertContentAsync({ + content: pdf.data, + from: "pdf", + to: "txt", + title: "mixed-text-scan", + options: { ocr: { enabled: false } }, + }); + assert.deepEqual(incompleteOutput.quality.qualityReport.unresolvedPdfPages, [2]); + assert.ok(incompleteOutput.quality.warnings.some((warning) => warning.code === "PDF_PAGES_WITHOUT_TEXT")); + const bareOutput = await convertContentAsync({ + content: pdf.data, + from: "pdf", + to: "txt", + title: "mixed-text-scan", + options: { repair: false, ocr: { enabled: false } }, + }); + assert.deepEqual(bareOutput.unresolvedPdfPages, [2]); + assert.ok(bareOutput.warnings.some((warning) => warning.code === "PDF_PAGES_WITHOUT_TEXT")); + } finally { + defaultOCRRegistry.unregister(engine.id); + resetPdfPageRasterizer(); + } +} else { + console.log("⊘ 29c mixed-page OCR skipped: pdfjs-dist not installed"); } // 30. convertContentAsync routes scanned PDF through OCR stage when stubs registered diff --git a/scripts/pdf-reader-test.js b/scripts/pdf-reader-test.js index f21a7ed..64692c0 100644 --- a/scripts/pdf-reader-test.js +++ b/scripts/pdf-reader-test.js @@ -247,12 +247,18 @@ const mixedPdf = writePdfHighFidelity({ }, }); const mixedExpanded = await expandPdfContentForTextExtraction(mixedPdf.data); -const mixedModel = readPdf({ content: mixedExpanded, title: "mixed-pages" }); -assert.equal(mixedModel.metadata.pdf.pageCount, 2); -assert.deepEqual(mixedModel.metadata.pdf.pagesWithoutText, [2]); -assert.equal(mixedModel.fixedLayout.pages.length, 2); -assert.ok(mixedModel.metadata.warnings.some((warning) => warning.code === "PDF_PAGES_WITHOUT_TEXT")); -console.log(" ✅ 无文字页保留在布局中,并产生可见 warning"); +// 混合页检测依赖 PDF.js 提取文本页;可选依赖缺失的环境(npm ci 可选安装失败)跳过断言。 +const pdfjsAvailable = await import("pdfjs-dist").then(() => true, () => false); +if (pdfjsAvailable) { + const mixedModel = readPdf({ content: mixedExpanded, title: "mixed-pages" }); + assert.equal(mixedModel.metadata.pdf.pageCount, 2); + assert.deepEqual(mixedModel.metadata.pdf.pagesWithoutText, [2]); + assert.equal(mixedModel.fixedLayout.pages.length, 2); + assert.ok(mixedModel.metadata.warnings.some((warning) => warning.code === "PDF_PAGES_WITHOUT_TEXT")); + console.log(" ✅ 无文字页保留在布局中,并产生可见 warning"); +} else { + console.log(" ⊘ 跳过:pdfjs-dist 未安装,混合页页级检测依赖 PDF.js 提取"); +} // 测试 12: 同格式 PDF 不重新绘制,原字节在同步、异步及预提取入口一致。 console.log("\nTest 12: PDF identity copy"); From 226c12b39491496d8b942d610500107832dcf0dd Mon Sep 17 00:00:00 2001 From: Jack Yao Date: Thu, 24 Sep 2026 14:07:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=94=B9=E5=8A=A8=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/development/AGENT_RULES.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/development/AGENT_RULES.md b/docs/development/AGENT_RULES.md index b38f291..d5afa5d 100644 --- a/docs/development/AGENT_RULES.md +++ b/docs/development/AGENT_RULES.md @@ -746,9 +746,9 @@ Closes #123 完成 PPTX 图表提取功能: 改动文件: -- D:/Trans2Former/public/formats/pptx-input.js -- D:/Trans2Former/scripts/conversion-snapshot-test.js -- D:/Trans2Former/docs/FORMAT_ROADMAP.md +- D:/Projects/Trans2Former/public/formats/pptx-input.js +- D:/Projects/Trans2Former/scripts/conversion-snapshot-test.js +- D:/Projects/Trans2Former/docs/FORMAT_ROADMAP.md 完成功能: - 添加 PPTX 柱状图和折线图解析