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

@@ -0,0 +1,92 @@
import { afterEach, expect, it } from "bun:test";
import { promises as fs } from "node:fs";
import { tmpdir } from "node:os";
import { basename, dirname, join } from "node:path";
import type { PlaceholderMapping } from "../src/types";
import { mappingExport, writeMappingExport } from "../src/lib/mapping-export";
const mapping: PlaceholderMapping = {
PERSON_02: { type: "PERSON", original: "Zweiter Name", merged_into: "PERSON_01" },
ORG_01: { type: "ORG", original: "Äpfel GmbH" },
PERSON_01: { type: "PERSON", original: "Anna Beispiel" },
PERSON_03: { type: "PERSON", original: "Anna Beispiel", merged_into: "PERSON_01" },
};
const roots: string[] = [];
afterEach(async () => { await Promise.all(roots.splice(0).map((path) => fs.rm(path, { recursive: true, force: true }))); });
async function temporaryDirectory() {
const path = await fs.mkdtemp(join(tmpdir(), "velum-mapping-export-"));
roots.push(path);
return path;
}
it("exportiert alle Originalbegriffe samt Varianten einmal und ohne Platzhalter", () => {
const snapshot = structuredClone(mapping);
expect(mappingExport(mapping, "terms")).toEqual({
text: "Anna Beispiel\nÄpfel GmbH\nZweiter Name\n", count: 3,
});
expect(mapping).toEqual(snapshot);
});
it("exportiert jede wirksame Ersetzung und bewahrt unterschiedliche Platzhalter desselben Originals", () => {
const result = mappingExport({ ...mapping,
BEGRIFF_01: { type: "BEGRIFF", original: "Anna Beispiel" },
}, "replacements");
expect(result.count).toBe(4);
expect(result.text.split("\n").filter(Boolean)).toEqual([
"Anna Beispiel → BEGRIFF_01", "Anna Beispiel → PERSON_01",
"Äpfel GmbH → ORG_01", "Zweiter Name → PERSON_01",
]);
expect(result.text).not.toContain("→ PERSON_02");
});
it("erhält Unicode, Klammer-Platzhalter sowie unterscheidbare Tabs und Zeilenumbrüche", () => {
const special = { "<PERSON_01>": { type: "PERSON", original: "Jörg\nMüller\r\nA\tB\\n" } };
expect(mappingExport(special, "terms").text).toBe("Jörg\\nMüller\\r\\nA\\tB\\\\n\n");
expect(mappingExport(special, "replacements").text).toBe("Jörg\\nMüller\\r\\nA\\tB\\\\n → <PERSON_01>\n");
});
it("berücksichtigt den übergebenen aktuellen Stand einschließlich neuer und gelöster Zuordnungen", () => {
const current = { ...mapping,
PERSON_02: { ...mapping.PERSON_02, merged_into: undefined },
EMAIL_01: { type: "EMAIL", original: "neu@example.test" },
};
const text = mappingExport(current, "replacements").text;
expect(text).toContain("Zweiter Name → PERSON_02\n");
expect(text).toContain("neu@example.test → EMAIL_01\n");
});
it("schreibt UTF-8 lokal mit privaten Rechten und überschreibt keine vorhandenen Exporte", async () => {
const directory = await temporaryDirectory();
const first = await writeMappingExport("Projekt", mapping, "terms", directory);
const second = await writeMappingExport("Projekt", mapping, "terms", directory);
const replacements = await writeMappingExport("Projekt", mapping, "replacements", directory);
expect(basename(first.path)).toBe("Projekt-begriffe.txt");
expect(basename(second.path)).toBe("Projekt-begriffe (2).txt");
expect(basename(replacements.path)).toBe("Projekt-ersetzungen.txt");
expect(await fs.readFile(first.path, "utf8")).toBe(mappingExport(mapping, "terms").text);
expect(await fs.readFile(replacements.path, "utf8")).toBe(mappingExport(mapping, "replacements").text);
for (const file of [first, second, replacements]) expect((await fs.stat(file.path)).mode & 0o777).toBe(0o600);
expect((await fs.readdir(directory)).length).toBe(3);
});
it("begrenzt Dateinamen auf das Exportverzeichnis und bewahrt parallele Exporte", async () => {
const directory = await temporaryDirectory();
const exports = await Promise.all(Array.from({ length: 4 }, () =>
writeMappingExport("../../Projekt\\Datei\n" + "漢".repeat(200), mapping, "terms", directory)));
expect(new Set(exports.map((file) => file.path)).size).toBe(4);
for (const file of exports) {
expect(dirname(file.path)).toBe(directory);
expect(Buffer.byteLength(basename(file.path))).toBeLessThan(255);
expect(await fs.readFile(file.path, "utf8")).toBe(mappingExport(mapping, "terms").text);
}
expect((await fs.readdir(directory)).length).toBe(4);
});
it("erzeugt bei leerer Tabelle oder defekten Gruppen keine unvollständige Exportdatei", async () => {
const directory = await temporaryDirectory();
expect(mappingExport({}, "terms")).toEqual({ text: "", count: 0 });
await expect(writeMappingExport("Leer", {}, "terms", directory)).rejects.toThrow("keine Begriffe");
const broken = { PERSON_01: { type: "PERSON", original: "Name", merged_into: "PERSON_02" } };
await expect(writeMappingExport("Defekt", broken, "replacements", directory)).rejects.toThrow("Hauptbegriff");
expect(await fs.readdir(directory)).toEqual([]);
});