feat: run Velum project jobs in the background and process DOCX locally

This commit is contained in:
muena
2026-11-30 16:12:11 +01:00
parent 11ddd6d70f
commit d2ed7130f0
32 changed files with 2325 additions and 462 deletions

View File

@@ -1,7 +1,9 @@
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 { checkedImagePolicy, checkedImageReport, DEFAULT_IMAGE_POLICY, KEEP_IMAGE_WARNING, sameImagePolicyCoverage, summarizeImages } from "../src/lib/image-policy";
@@ -60,6 +62,7 @@ it("übermittelt dieselbe Bildoption in Vorschau, Anwendung und erneutem Abgleic
} },
"./preferences": { getPreferences: () => ({ velumBaseUrl: "https://velum.test" }) },
"./lib/image-policy": imagePolicy,
"./lib/xlsx-restoration": xlsxRestoration,
"./lib/project-capabilities": projectCapabilities,
};
const module = { exports: {} as { pseudonymizeDocument: (...args: unknown[]) => Promise<unknown>; requireProjectCapabilities: (images?: boolean) => Promise<void> } };
@@ -81,3 +84,32 @@ it("übermittelt dieselbe Bildoption in Vorschau, Anwendung und erneutem Abgleic
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<string, unknown> = {
"./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/xlsx-restoration": xlsxRestoration,
};
const module = { exports: {} as { depseudonymizeDocument: (name: string, data: Buffer, mapping: Record<string, { original: string; type: string }>) => Promise<Buffer> } };
new Function("require", "module", "exports", code)((id: string) => mocks[id] ?? require(id), module, module.exports);
const zip = zipSync({
"[Content_Types].xml": strToU8('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"/>'),
"xl/workbook.xml": strToU8('<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheets><sheet name="PERSON_01"/></sheets></workbook>'),
});
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);
});