feat(tools): 2.0 phase 5 wave 4 - office, ebooks, data, archives (14 tools) (#224)

This commit is contained in:
SnapOtter
2026-06-13 10:19:11 +08:00
parent 638288e196
commit fc7c1f850e
93 changed files with 7636 additions and 817 deletions
+67
View File
@@ -0,0 +1,67 @@
// Pure-regex unit test for the SSRF pre-scan in doc_html_pdf.py.
// Runs python3 against the actual module regexes (no weasyprint needed).
// Mirrors the step-3 verification matrix from the fix ticket.
import { spawnSync } from "node:child_process";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { hasPython } from "../../helpers/python-gate.js";
const SCRIPT_DIR = join(process.cwd(), "packages", "ai", "python");
/** Run a python3 snippet that imports regexes from the actual module and tests a case. */
function testRegex(html: string, patternName: string): boolean {
const code = [
"import sys, os",
`sys.path.insert(0, ${JSON.stringify(SCRIPT_DIR)})`,
"from doc_html_pdf import _REMOTE_REF_RE, _REMOTE_CSS_URL_RE",
`pat = ${patternName}`,
`m = pat.search(${JSON.stringify(html)})`,
"print('MATCH' if m else 'NO_MATCH')",
].join("; ");
const res = spawnSync("python3", ["-c", code], { encoding: "utf8", timeout: 5000 });
if (res.status !== 0) throw new Error(`python3 failed: ${res.stderr}`);
return res.stdout.trim() === "MATCH";
}
describe.skipIf(!hasPython)("SSRF pre-scan regexes (doc_html_pdf.py)", () => {
describe("_REMOTE_REF_RE", () => {
it("matches double-slash http img", () => {
expect(testRegex('<img src="http://h/x.png">', "_REMOTE_REF_RE")).toBe(true);
});
it("matches single-slash http img (pandoc rewrite)", () => {
expect(testRegex('<img src="http:/h/x.png">', "_REMOTE_REF_RE")).toBe(true);
});
it("matches double-slash https img", () => {
expect(testRegex('<img src="https://h/x.png">', "_REMOTE_REF_RE")).toBe(true);
});
it("matches single-slash https img", () => {
expect(testRegex('<img src="https:/h/x.png">', "_REMOTE_REF_RE")).toBe(true);
});
it("does NOT match data: URIs", () => {
expect(testRegex('<img src="data:image/png;base64,AA==">', "_REMOTE_REF_RE")).toBe(false);
});
it("does NOT match relative paths", () => {
expect(testRegex('<a href="page2.xhtml">', "_REMOTE_REF_RE")).toBe(false);
});
});
describe("_REMOTE_CSS_URL_RE", () => {
it("matches single-slash CSS url()", () => {
expect(testRegex("url(https:/h/a.css)", "_REMOTE_CSS_URL_RE")).toBe(true);
});
it("matches double-slash CSS url()", () => {
expect(testRegex("url(https://h/a.css)", "_REMOTE_CSS_URL_RE")).toBe(true);
});
it("does NOT match data: CSS url()", () => {
expect(testRegex("url(data:image/png;base64,AA==)", "_REMOTE_CSS_URL_RE")).toBe(false);
});
});
});
+27
View File
@@ -0,0 +1,27 @@
import { parseConvertTarget } from "@snapotter/doc-engine";
import { describe, expect, it } from "vitest";
describe("parseConvertTarget", () => {
it("returns a bare extension unchanged", () => {
const result = parseConvertTarget("pdf");
expect(result).toEqual({ ext: "pdf", convertTo: "pdf" });
});
it("splits on the first colon for qualified filter targets", () => {
const result = parseConvertTarget("docx:MS Word 2007 XML");
expect(result).toEqual({ ext: "docx", convertTo: "docx:MS Word 2007 XML" });
});
it("handles filter names with colons after the first", () => {
const result = parseConvertTarget("csv:Text - txt - csv (StarCalc):44,34,76,1");
expect(result).toEqual({
ext: "csv",
convertTo: "csv:Text - txt - csv (StarCalc):44,34,76,1",
});
});
it("handles single-character extensions", () => {
const result = parseConvertTarget("a:SomeFilter");
expect(result).toEqual({ ext: "a", convertTo: "a:SomeFilter" });
});
});
+34
View File
@@ -0,0 +1,34 @@
import { readFileSync } from "node:fs";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { pandocAvailable, runPandoc } from "@snapotter/doc-engine";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
describe("pandocAvailable", () => {
it("returns a boolean without throwing", () => {
expect(typeof pandocAvailable()).toBe("boolean");
});
});
describe.skipIf(!pandocAvailable())("runPandoc (requires pandoc)", () => {
let tmpDir: string;
beforeAll(async () => {
tmpDir = await mkdtemp(join(tmpdir(), "pandoc-test-"));
});
afterAll(async () => {
await rm(tmpDir, { recursive: true, force: true }).catch(() => {});
});
it("converts a trivial markdown file to html", async () => {
const mdPath = join(tmpDir, "test.md");
const htmlPath = join(tmpDir, "test.html");
await writeFile(mdPath, "# Hello\n\nWorld\n");
await runPandoc(mdPath, htmlPath, { extraArgs: ["--standalone"] });
const html = readFileSync(htmlPath, "utf8");
expect(html).toContain("Hello");
expect(html).toContain("<");
}, 30_000);
});
+5 -1
View File
@@ -24,7 +24,11 @@ describe("modality metadata", () => {
for (const tool of TOOLS) {
expect(validModalities).toContain(tool.modality);
expect(Array.isArray(tool.acceptedInputs)).toBe(true);
expect(tool.acceptedInputs.length).toBeGreaterThan(0);
// Empty acceptedInputs is valid for file-modality tools that accept any file
// (e.g. create-zip); the factory 415 gate skips when the list is empty.
if (tool.modality !== "file" || tool.acceptedInputs.length > 0) {
expect(tool.acceptedInputs.length).toBeGreaterThan(0);
}
expect(["fast", "long"]).toContain(tool.executionHint);
}
});