import { expect, it } from "bun:test"; import { readFileSync } from "node:fs"; import { createRequire } from "node:module"; import { zipSync, unzipSync, strToU8, strFromU8 } from "fflate"; import ts from "typescript"; import * as xlsxRestoration from "../src/lib/xlsx-restoration"; import * as imagePolicy from "../src/lib/image-policy"; import * as projectCapabilities from "../src/lib/project-capabilities"; import * as entityTypes from "../src/lib/entity-types"; import { checkedImagePolicy, checkedImageReport, DEFAULT_IMAGE_POLICY, KEEP_IMAGE_WARNING, sameImagePolicyCoverage, summarizeImages } from "../src/lib/image-policy"; const policies = ["placeholder", "remove", "keep"] as const; function response(policy = "placeholder", changes: Record = {}) { return { image_policy: policy, images_replaced: 2, images_removed: 0, media_parts_removed: 3, warnings: [], file_base64: "UFIDBA==", mapping: {}, hits: [], ...changes }; } it("verwendet Platzhalter als sicheren Standard und lehnt unbekannte Optionen ab", () => { expect(DEFAULT_IMAGE_POLICY).toBe("placeholder"); for (const policy of policies) expect(checkedImagePolicy(policy)).toBe(policy); expect(() => checkedImagePolicy("unknown")).toThrow(); }); it("verarbeitet Dokumente alter Stände und bei geänderter Bildoption erneut", () => { for (const selected of policies) { expect(sameImagePolicyCoverage(undefined, selected)).toBe(false); expect(sameImagePolicyCoverage({}, selected)).toBe(false); for (const saved of policies) expect(sameImagePolicyCoverage({ imagePolicy: saved }, selected)).toBe(saved === selected); } }); it("blockiert fehlende Bestätigung, ungültige Zähler und Warnungen", () => { for (const changes of [{ image_policy: undefined }, { image_policy: "keep" }, { images_removed: -1 }, { images_replaced: undefined }, { warnings: undefined }]) { expect(() => checkedImageReport(response("placeholder", changes), "placeholder")).toThrow(); } }); it("speichert nur Bildzähler und Warnungen im Dateibericht", () => { const checked = checkedImageReport(response(), "placeholder"); expect(Object.keys(checked).sort()).toEqual(["image_policy", "images_replaced", "images_removed", "media_parts_removed", "warnings"].sort()); expect("mapping" in checked).toBe(false); expect("file_base64" in checked).toBe(false); }); it("zeigt auch mit sonstigen Backend-Hinweisen bei keep eine Datenschutzwarnung", () => { const report = checkedImageReport(response("keep", { warnings: ["Anderer Hinweis"] }), "keep"); expect(report.warnings).toEqual([KEEP_IMAGE_WARNING, "Anderer Hinweis"]); expect(summarizeImages([{ imageReport: report }, { imageReport: report }])).toEqual({ replaced: 4, removed: 0, warnings: [KEEP_IMAGE_WARNING, "Anderer Hinweis"], }); }); it("übermittelt dieselbe Bildoption in Vorschau, Anwendung und erneutem Abgleich", async () => { const require = createRequire(import.meta.url); const code = ts.transpileModule(readFileSync(new URL("../src/velum.ts", import.meta.url), "utf8"), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2023 }, }).outputText; const calls: FormData[] = []; let returnedPolicy = "placeholder"; let supportsImages = true; const mocks: Record = { "./auth": { fetchWithAuth: async (url: string, init: RequestInit = {}) => { if (url.endsWith("capabilities")) return Response.json({ request_force_masks: true, document_hit_review: true, document_review_mapping: true, document_restoration: true, comment_author_review: true, revision_author_review: true, document_image_policy: supportsImages }); calls.push(init.body as FormData); return Response.json(response(returnedPolicy)); } }, "./preferences": { getPreferences: () => ({ velumBaseUrl: "https://velum.test" }) }, "./lib/image-policy": imagePolicy, "./lib/xlsx-restoration": xlsxRestoration, "./lib/project-capabilities": projectCapabilities, "./lib/entity-types": entityTypes, }; const module = { exports: {} as { pseudonymizeDocument: (...args: unknown[]) => Promise; requireProjectCapabilities: (images?: boolean) => Promise } }; new Function("require", "module", "exports", code)((id: string) => mocks[id] ?? require(id), module, module.exports); const api = module.exports; for (const policy of policies) { returnedPolicy = policy; for (const accepted of [undefined, ["hit-1"], undefined, ["new-hit-1"]]) { await api.pseudonymizeDocument("report.docx", Buffer.from("zip"), {}, {}, accepted, ["PERSON"], {}, policy); expect(calls.at(-1)!.get("image_policy")).toBe(policy); } } returnedPolicy = "placeholder"; await api.pseudonymizeDocument("report.docx", Buffer.from("zip"), {}, {}); expect(calls.at(-1)!.get("image_policy")).toBe("placeholder"); returnedPolicy = "keep"; await expect(api.pseudonymizeDocument("report.docx", Buffer.from("zip"), {}, {})).rejects.toThrow("bestätigt"); supportsImages = false; await expect(api.requireProjectCapabilities(true)).rejects.toThrow(); await api.requireProjectCapabilities(false); }); it("restores XLSX locally and keeps the API route for DOCX", async () => { const require = createRequire(import.meta.url); const code = ts.transpileModule(readFileSync(new URL("../src/velum.ts", import.meta.url), "utf8"), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2023 }, }).outputText; let requests = 0; const mocks: Record = { "./auth": { fetchWithAuth: async () => { requests++; return Response.json({ file_base64: Buffer.from("restored docx").toString("base64") }); } }, "./preferences": { getPreferences: () => ({ velumBaseUrl: "https://velum.test" }) }, "./lib/image-policy": imagePolicy, "./lib/project-capabilities": projectCapabilities, "./lib/entity-types": entityTypes, "./lib/xlsx-restoration": xlsxRestoration, }; const module = { exports: {} as { depseudonymizeDocument: (name: string, data: Buffer, mapping: Record) => Promise } }; new Function("require", "module", "exports", code)((id: string) => mocks[id] ?? require(id), module, module.exports); const zip = zipSync({ "[Content_Types].xml": strToU8(''), "xl/workbook.xml": strToU8(''), }); const restored = await module.exports.depseudonymizeDocument("book.XLSX", Buffer.from(zip), { PERSON_01: { original: "Anna", type: "PERSON" }, }); expect(requests).toBe(0); expect(strFromU8(unzipSync(restored)["xl/workbook.xml"])).toContain('name="Anna"'); expect(await module.exports.depseudonymizeDocument("book.docx", Buffer.from("docx"), {})).toEqual(Buffer.from("restored docx")); expect(requests).toBe(1); });