import { afterEach, describe, expect, it, mock } from "bun:test"; import { promises as fs } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import type { RunFile } from "../src/lib/velum-project"; mock.module("@raycast/api", () => ({ environment: { supportPath: tmpdir() }, Form: {}, Icon: {}, Toast: { Style: {} }, showToast: async () => undefined, LocalStorage: {}, getPreferenceValues: () => ({}) })); const project = await import("../src/lib/velum-project"); const { mappingForRestoration } = await import("../src/lib/mapping-groups"); const { mergeMappingGroups } = await import("../src/lib/mapping-groups"); const roots: string[] = []; afterEach(async () => { await Promise.all(roots.splice(0).map((root) => fs.rm(root, { recursive: true, force: true }))); }); describe("Velum-Projektstände", () => { it("speichert Projektregeln und Bearbeitungsstände getrennt vom Quellordner", async () => { const root = await fs.mkdtemp(join(tmpdir(), "velum-project-test-")); roots.push(root); const source = join(root, "Quelle"); const support = join(root, "Raycast"); await fs.mkdir(source); await fs.writeFile(join(source, "Notiz.txt"), "Max Mustermann"); const rules = project.parseProjectRules("PERSON: Max Mustermann\nKundenSuite"); await project.saveProjectRules(source, rules, support); expect(await project.loadProjectRules(source, support)).toEqual(rules); await project.saveProjectWhitelist(source, [ { type: "PERSON", original: "Falscher Treffer" }, { type: "PERSON", original: "falscher treffer" }, ], support); expect(await project.loadProjectWhitelist(source, support)).toEqual([ { type: "PERSON", original: "falscher treffer" }, ]); expect(await fs.readFile(join(source, "Notiz.txt"), "utf8")).toBe("Max Mustermann"); const created = await project.createRun(source, support); created.run.mapping = { PERSON_01: { original: "Max Mustermann", type: "PERSON" } }; created.run.files.push({ source: "Notiz.txt", output: "PERSON_01.txt", kind: "text" }); await project.saveRun(created.directory, created.run); expect((await project.listRuns(source, support))[0].run.files[0].output).toBe("PERSON_01.txt"); expect(await fs.readFile(join(source, "Notiz.txt"), "utf8")).toBe("Max Mustermann"); }); it("ordnet erneute Läufe demselben Projektpfad zu und erkennt ältere Pfadschreibweisen", async () => { const root = await fs.mkdtemp(join(tmpdir(), "velum-project-identity-test-")); roots.push(root); const source = join(root, "Quelle"); const support = join(root, "Raycast"); await fs.mkdir(source); const first = await project.createRun(`${source}/`, support); first.run.projectPath = `${source}/`; await project.saveRun(first.directory, first.run); const second = await project.createRun(join(source, "."), support); await project.saveRun(second.directory, second.run); expect(project.projectDataRoot(source, support)).toBe(project.projectDataRoot(`${source}/`, support)); expect(first.run.id).not.toBe(second.run.id); expect((await project.listRuns(source, support)).map(({ run }) => run.id).sort()) .toEqual([first.run.id, second.run.id].sort()); }); it("legt pseudonymisierte Kopien neben dem Projekt ab und behält die Tabelle privat", async () => { const root = await fs.mkdtemp(join(tmpdir(), "velum-sibling-copy-test-")); roots.push(root); const source = join(root, "Quelle"); const support = join(root, "Raycast"); await fs.mkdir(source); const created = await project.createRun(source, support); created.run.mapping = { PERSON_01: { original: "Max Mustermann", type: "PERSON" } }; created.run.files = [{ source: "Unterordner/Name.txt", output: "Unterordner/PERSON_01.txt", kind: "text" }]; await fs.mkdir(join(created.directory, "files", "Unterordner")); await fs.writeFile(join(created.directory, "files", "Unterordner", "PERSON_01.txt"), "PERSON_01"); const copyPath = await project.publishProjectCopies(created.run, created.directory); created.run.copyPath = copyPath; await project.saveRun(created.directory, created.run); expect(copyPath.startsWith(join(root, "Quelle-Velum-"))).toBe(true); expect(project.runCopiesPath(created.run, created.directory)).toBe(copyPath); expect(await fs.readFile(join(copyPath, "Unterordner", "PERSON_01.txt"), "utf8")).toBe("PERSON_01"); expect(await fs.readdir(copyPath)).toEqual(["Unterordner"]); expect(await fs.readFile(join(created.directory, "run.json"), "utf8")).toContain("Max Mustermann"); await fs.writeFile(join(copyPath, "Antwort.txt"), "PERSON_01 antwortet"); const current = await project.scanRestorableCopyFiles(created.run, created.directory); expect(current.root).toBe(copyPath); expect(current.files.map((file) => file.relPath)).toEqual(["Antwort.txt", "Unterordner/PERSON_01.txt"]); const history = await project.scanRestorableCopyFiles(created.run, created.directory, false); expect(history.root).toBe(join(created.directory, "files")); expect(history.files.map((file) => file.relPath)).toEqual(["Unterordner/PERSON_01.txt"]); expect(project.restoreTextWithCount("PERSON_01 antwortet", created.run.mapping)) .toEqual({ text: "Max Mustermann antwortet", replacements: 1 }); await expect(project.publishProjectCopies(created.run, created.directory)).rejects.toThrow("bereits vorhanden"); expect((await project.listRuns(source, support))[0].run.copyPath).toBe(copyPath); }); it("behält die Kategorieauswahl pro Projekt für den nächsten Lauf", async () => { const root = await fs.mkdtemp(join(tmpdir(), "velum-entity-types-test-")); roots.push(root); const source = join(root, "Quelle"); const other = join(root, "Anderes Projekt"); const support = join(root, "Raycast"); await fs.mkdir(source); await fs.mkdir(other); expect(await project.loadProjectEntityTypes(source, support)).toBeNull(); await project.saveProjectEntityTypes(`${source}/`, ["PERSON", "ORG", "PERSON"], support); expect(await project.loadProjectEntityTypes(source, support)).toEqual(["PERSON", "ORG"]); expect(await project.loadProjectEntityTypes(other, support)).toBeNull(); }); it("überspringt temporäre und falsch benannte Office-Dateien vor der API-Analyse", async () => { const root = await fs.mkdtemp(join(tmpdir(), "velum-office-scan-test-")); roots.push(root); await fs.writeFile(join(root, "Notiz.txt"), "Nur ein künstlicher Testtext"); await fs.writeFile(join(root, "Bericht.xlsx"), "Kein Excel-Archiv"); await fs.writeFile(join(root, "~$Entwurf.xlsx"), "Sperrdatei"); await fs.writeFile(join(root, "._Tabelle.xlsx"), "AppleDouble"); await fs.writeFile(join(root, "Gültig.xlsx"), Buffer.from([0x50, 0x4b, 0x03, 0x04, 0x00])); const scan = await project.scanProjectFiles(root); expect(scan.files.map((file) => file.relPath)).toEqual(["Gültig.xlsx", "Notiz.txt"]); expect(scan.skipped).toEqual(expect.arrayContaining([ { path: "Bericht.xlsx", reason: expect.stringContaining("Office-ZIP-Archiv") }, { path: "~$Entwurf.xlsx", reason: "Temporäre Office-/macOS-Datei" }, { path: "._Tabelle.xlsx", reason: "Temporäre Office-/macOS-Datei" }, ])); }); it("stellt aktuelle In-Place-Dateien aus dem Projektordner und frühere Stände aus dem Snapshot bereit", async () => { const root = await fs.mkdtemp(join(tmpdir(), "velum-current-source-test-")); roots.push(root); const source = join(root, "Projekt"); const support = join(root, "Raycast"); await fs.mkdir(source); const created = await project.createRun(source, support); created.run.inPlace = true; created.run.files = [{ source: "Notiz.txt", output: "Notiz.txt", kind: "text" }]; await fs.writeFile(join(created.directory, "files", "Notiz.txt"), "PERSON_01"); await fs.writeFile(join(source, "Notiz.txt"), "PERSON_01"); await fs.writeFile(join(source, "Antwort.txt"), "PERSON_01 antwortet"); const current = await project.scanRestorableCopyFiles(created.run, created.directory); expect(current.root).toBe(source); expect(current.files.map((file) => file.relPath)).toEqual(["Antwort.txt", "Notiz.txt"]); const historical = await project.scanRestorableCopyFiles(created.run, created.directory, false); expect(historical.root).toBe(join(created.directory, "files")); expect(historical.files.map((file) => file.relPath)).toEqual(["Notiz.txt"]); }); it("ersetzt nur bestätigte Vorkommen und stellt Dateinamen wieder her", () => { const hits = [ { id: "a", start: 0, end: 3, placeholder: "PERSON_01" }, { id: "b", start: 8, end: 11, placeholder: "PERSON_01" }, ]; expect(project.replaceSpans("Max und Max", hits, new Set(["a"]))).toBe("PERSON_01 und Max"); expect(project.restorePath("PERSON_01_Bericht.txt", { PERSON_01: { original: "Max Mustermann", type: "PERSON" }, })).toBe("Max Mustermann_Bericht.txt"); expect(project.restorePath("Archiv/BerichtPERSON_01.docx", { PERSON_01: { original: "Max Mustermann", type: "PERSON" }, })).toBe("Archiv/BerichtMax Mustermann.docx"); expect(project.restorePath("archiv/bericht_person_01.docx", { PERSON_01: { original: "Max Mustermann", type: "PERSON" }, })).toBe("archiv/bericht_max mustermann.docx"); expect(project.restoreTextWithCount("person_01 und Person_01", { PERSON_01: { original: "Max Mustermann", type: "PERSON" }, })).toEqual({ text: "max mustermann und Max Mustermann", replacements: 2 }); expect(project.restoreText("PERSON_01 / person_01 / Person_01", { PERSON_01: { original: "Max Mustermann", type: "PERSON" }, })).toBe("Max Mustermann / max mustermann / Max Mustermann"); expect(project.restoreText("KUNDE_01 / kunde_01 / Kunde_01", { KUNDE_01: { original: "ACME GMBH", type: "KUNDE" }, })).toBe("ACME GMBH / acme gmbh / Acme Gmbh"); expect(project.restoreText("person_01 / PERSON_01", { person_01: { original: "max mustermann", type: "PERSON" }, })).toBe("max mustermann / MAX MUSTERMANN"); expect(project.restoreText("BerichtPERSON_01", { PERSON_01: { original: "Max Mustermann", type: "PERSON" }, })).toBe("BerichtPERSON_01"); expect(project.restoreText("Fachkonzept `PROJEKT_01_Fachkonzept_V03.docx` widerspricht Kommentar PERSON_61 (ID 1088)", { PROJEKT_01: { original: "Beispielprojekt", type: "PROJEKT" }, PERSON_61: { original: "Mara Muster", type: "PERSON" }, }, undefined, true)).toBe("Fachkonzept `Beispielprojekt_Fachkonzept_V03.docx` widerspricht Kommentar Mara Muster (ID 1088)"); expect(project.restoreText("PERSON_0101 und PERSON_01", { PERSON_01: { original: "Max Mustermann", type: "PERSON" }, }, undefined, true)).toBe("PERSON_0101 und Max Mustermann"); expect(project.restorePath("PERSON_0101.txt", { PERSON_01: { original: "Falsch", type: "PERSON" }, PERSON_0101: { original: "Richtig", type: "PERSON" }, })).toBe("Richtig.txt"); expect(project.restoreText("Person_01 / PERSON_01", { PERSON_01: { original: "Eins", type: "PERSON" }, person_01: { original: "Zwei", type: "PERSON" }, })).toBe("Person_01 / Eins"); expect(() => project.safeRelativePath("../geheim.txt")).toThrow(); }); it("stellt alte Personen-Verweise in Text und Dateiname mit dem Hauptbegriff wieder her", () => { const mapping = mappingForRestoration({ PERSON_01: { type: "PERSON", original: "Franz Fuchs" }, PERSON_02: { type: "PERSON", original: "Hr. Fuchs", merged_into: "PERSON_01" }, }); expect(project.restoreText("PERSON_02 und PERSON_01", mapping)).toBe("Franz Fuchs und Franz Fuchs"); expect(project.restorePath("PERSON_02_Entwurf.md", mapping)).toBe("Franz Fuchs_Entwurf.md"); }); it("verwendet ältere Zuordnungen nur bei übereinstimmender Bedeutung und meldet übrig gebliebene Markdown-Platzhalter", () => { const current = { PERSON_01: { original: "Alex Beispiel", type: "PERSON" } }; const older = [ { PERSON_61: { original: "Mara Muster", type: "PERSON" }, PROJEKT_01: { original: "Projekt Eins", type: "PROJEKT" } }, { PERSON_61: { original: "Mara Muster", type: "PERSON" }, PROJEKT_01: { original: "Projekt Zwei", type: "PROJEKT" } }, ]; const mapping = project.withUnambiguousHistoricalMappings(current, older); expect(mapping.PERSON_61?.original).toBe("Mara Muster"); expect(mapping.PROJEKT_01).toBeUndefined(); const restored = project.restoreText("PERSON_61 und PROJEKT_01", mapping, undefined, true); expect(restored).toBe("Mara Muster und PROJEKT_01"); expect(project.unresolvedPlaceholderIds(restored, [current, ...older])).toEqual(["PROJEKT_01"]); expect(project.isMarkdownFile("Notizen.MD")).toBe(true); }); it("löst eine wiederverwendete ID nur über eine identische gespeicherte Datei auf", async () => { const root = await fs.mkdtemp(join(tmpdir(), "velum-exact-snapshot-")); roots.push(root); const first = await project.createRun(join(root, "Quelle"), root); const second = await project.createRun(join(root, "Quelle"), root); const relPath = "Fragenliste.md"; first.run.files = [{ source: relPath, output: relPath, kind: "text" }]; second.run.files = [{ source: relPath, output: relPath, kind: "text" }]; first.run.mapping = { PERSON_60: { original: "Alex Beispiel", type: "PERSON" }, PERSON_61: { original: "Hr. Beispiel", type: "PERSON", merged_into: "PERSON_60" }, }; second.run.mapping = { PERSON_61: { original: "Mara Muster", type: "PERSON" } }; const source = Buffer.from("Kommentar PERSON_61"); await fs.writeFile(join(first.directory, "files", relPath), source); await fs.writeFile(join(second.directory, "files", relPath), "anderer Inhalt"); const exact = await project.mappingForMatchingSnapshots(relPath, project.digest(source), [first, second]); expect(exact.PERSON_61?.original).toBe("Alex Beispiel"); const unknown = await project.mappingForMatchingSnapshots(relPath, project.digest(Buffer.from("neu")), [first, second]); expect(unknown.PERSON_61).toBeUndefined(); }); it("ergänzt einen fehlenden Platzhalter in einem neuen Projektstand ohne alte Daten zu ändern", async () => { const root = await fs.mkdtemp(join(tmpdir(), "velum-manual-mapping-")); roots.push(root); const source = join(root, "Quelle"); await fs.mkdir(source); const saved = await project.createRun(source, root); saved.run.mapping = { PERSON_01: { original: "Alex Beispiel", type: "PERSON" } }; saved.run.files = [{ source: "Notiz.md", output: "Notiz.md", kind: "text" }]; await fs.writeFile(join(saved.directory, "files", "Notiz.md"), "PERSON_61"); await project.saveRun(saved.directory, saved.run); const updated = await project.addProjectMappingEntry(source, { placeholder: "PERSON_61", original: "Mara Muster", type: "PERSON" }, root); expect(updated.mapping.PERSON_61?.original).toBe("Mara Muster"); expect((await project.listRuns(source, root))[0].run.id).toBe(updated.id); expect(await fs.readFile(join(saved.directory, "files", "Notiz.md"), "utf8")).toBe("PERSON_61"); expect((await project.listRuns(source, root))[1].run.mapping.PERSON_61).toBeUndefined(); expect(await fs.readFile(join(project.projectDataRoot(source, root), "runs", updated.id, "files", "Notiz.md"), "utf8")) .toBe("PERSON_61"); expect(project.prepareManualMappingEntry(updated.mapping, [], { original: "Weiterer Begriff", type: "PERSON" }).placeholder).toBe("PERSON_62"); }); it("speichert eine Gruppierung als neuen Projektstand und lässt frühere Dateien unverändert", async () => { const root = await fs.mkdtemp(join(tmpdir(), "velum-group-mapping-")); roots.push(root); const source = join(root, "Quelle"); await fs.mkdir(source); const saved = await project.createRun(source, root); saved.run.mapping = { PERSON_01: { original: "Franz Fuchs", type: "PERSON" }, PERSON_02: { original: "Hr. Fuchs", type: "PERSON" }, }; saved.run.files = [{ source: "Notiz.md", output: "Notiz.md", kind: "text" }]; await fs.writeFile(join(saved.directory, "files", "Notiz.md"), "PERSON_02"); await project.saveRun(saved.directory, saved.run); const updated = await project.updateProjectMapping(source, (mapping) => mergeMappingGroups(mapping, "PERSON_02", "PERSON_01"), root); expect(updated.mapping.PERSON_02.merged_into).toBe("PERSON_01"); expect((await project.listRuns(source, root))[1].run.mapping.PERSON_02.merged_into).toBeUndefined(); expect(await fs.readFile(join(project.projectDataRoot(source, root), "runs", updated.id, "files", "Notiz.md"), "utf8")) .toBe("PERSON_02"); expect(project.restoreText("PERSON_02", mappingForRestoration(updated.mapping))).toBe("Franz Fuchs"); }); it("verhindert eine neue projektweite Zuordnung für eine früher anders belegte ID", () => { expect(() => project.prepareManualMappingEntry({}, [ { PERSON_61: { original: "Alex Beispiel", type: "PERSON" } }, ], { placeholder: "PERSON_61", original: "Mara Muster", type: "PERSON" })) .toThrow("andere Bedeutung"); }); it("übernimmt API-Trefferpositionen auch nach einem Emoji korrekt", () => { const text = "😀 Max"; const spans = project.toUtf16Offsets(text, [{ id: "max", start: 2, end: 5, placeholder: "PERSON_01" }]); expect(project.replaceSpans(text, spans, new Set(["max"]))).toBe("😀 PERSON_01"); }); it("ersetzt ausgewählte Projektdateien vor Ort und sichert die Originale außerhalb", async () => { const root = await fs.mkdtemp(join(tmpdir(), "velum-in-place-test-")); roots.push(root); const source = join(root, "Quelle"); const support = join(root, "Raycast"); await fs.mkdir(source); await fs.writeFile(join(source, "Bericht.txt"), "Max Mustermann"); await fs.chmod(join(source, "Bericht.txt"), 0o640); await fs.writeFile(join(source, "Unberührt.txt"), "bleibt"); const created = await project.createRun(source, support); await fs.writeFile(join(created.directory, "files", "PERSON_01.txt"), "PERSON_01"); const files: RunFile[] = [{ source: "Bericht.txt", output: "PERSON_01.txt", kind: "text" }]; await project.replaceProjectFilesInPlace(source, created.directory, files); expect(await fs.readFile(join(source, "PERSON_01.txt"), "utf8")).toBe("PERSON_01"); expect((await fs.stat(join(source, "PERSON_01.txt"))).mode & 0o777).toBe(0o640); expect(await fs.readFile(join(created.directory, "backup", "Bericht.txt"), "utf8")).toBe("Max Mustermann"); expect((await fs.stat(join(created.directory, "backup", "Bericht.txt"))).mode & 0o777).toBe(0o600); expect(await fs.readFile(join(source, "Unberührt.txt"), "utf8")).toBe("bleibt"); await expect(fs.access(join(source, "Bericht.txt"))).rejects.toMatchObject({ code: "ENOENT" }); }); it("verhindert Namenskollisionen vor dem Ersetzen", async () => { const root = await fs.mkdtemp(join(tmpdir(), "velum-in-place-collision-test-")); roots.push(root); const source = join(root, "Quelle"); const support = join(root, "Raycast"); await fs.mkdir(source); await fs.writeFile(join(source, "Bericht.txt"), "Original"); await fs.writeFile(join(source, "PERSON_01.txt"), "Andere Datei"); const created = await project.createRun(source, support); await fs.writeFile(join(created.directory, "files", "PERSON_01.txt"), "Pseudonym"); await expect(project.replaceProjectFilesInPlace(source, created.directory, [{ source: "Bericht.txt", output: "PERSON_01.txt", kind: "text" }])).rejects.toThrow("Zielname bereits vorhanden"); expect(await fs.readFile(join(source, "Bericht.txt"), "utf8")).toBe("Original"); expect(await fs.readFile(join(source, "PERSON_01.txt"), "utf8")).toBe("Andere Datei"); }); it("erkennt unveränderte Ausgaben für einen erneuten Lauf desselben Ordners", async () => { const root = await fs.mkdtemp(join(tmpdir(), "velum-repeat-test-")); roots.push(root); const source = join(root, "Quelle"); const support = join(root, "Raycast"); await fs.mkdir(source); const created = await project.createRun(source, support); const filename = "PERSON_01.txt"; await fs.writeFile(join(created.directory, "files", filename), "PERSON_01"); const current = join(source, filename); await fs.writeFile(current, "PERSON_01"); const hash = project.digest(await fs.readFile(current)); expect(await project.matchesPreviousOutput(created.directory, filename, hash)).toBe(true); await fs.writeFile(current, "PERSON_01 und neuer Text"); expect(await project.matchesPreviousOutput(created.directory, filename, project.digest(await fs.readFile(current)))).toBe(false); }); });