import { expect, it } from "bun:test"; import { analyzeProjectBatch } from "../src/lib/project-analysis"; import { makeTextBatches, spansBySegment } from "../src/lib/project-review"; import type { PlaceholderMapping, PseudonymizeResponse } from "../src/types"; function response( text: string, mapping: PlaceholderMapping, ): PseudonymizeResponse { const original = text.includes("Anna") ? "Anna" : "Berta"; const placeholder = original === "Anna" ? "PERSON_01" : "PERSON_02"; return { pseudonymized_text: text.replace(original, placeholder), selected_entity_types: ["PERSON"], mapping: { ...mapping, [placeholder]: { original, type: "PERSON" } }, entity_count: 1, spans: [ { start: text.indexOf(original), end: text.indexOf(original) + original.length, original, placeholder, type: "PERSON", score: 1, }, ], }; } it("zerlegt einen fehlgeschlagenen Analyseblock und erhält Dateigrenzen und Zuordnungen", async () => { const [batch] = makeTextBatches([ { key: "a.md", text: "Anna" }, { key: "b.md", text: "Berta" }, ]); const calls: string[] = []; const analyzed = await analyzeProjectBatch( batch, {}, ["PERSON"], {}, async (text, mapping) => { calls.push(text); if (text.includes("Anna") && text.includes("Berta")) throw new Error("Velum API request failed: 500 Internal Server Error"); return response(text, mapping); }, "Textdateien analysieren", ); expect(calls).toHaveLength(3); expect(Object.keys(analyzed.mapping)).toEqual(["PERSON_01", "PERSON_02"]); expect( analyzed.results.flatMap(({ batch: part, result }) => [ ...spansBySegment(part, result.spans).keys(), ]), ).toEqual(["a.md", "b.md"]); }); it("nennt nach der Eingrenzung die fehlerhafte Datei", async () => { const [batch] = makeTextBatches([ { key: "a.md", text: "Anna" }, { key: "b.md", text: "Berta" }, ]); expect( analyzeProjectBatch( batch, {}, ["PERSON"], {}, async (text, mapping) => { if (text.includes("Berta")) throw new Error( "Velum API request failed: 500 Internal Server Error", ); return response(text, mapping); }, "Textdateien analysieren", ), ).rejects.toThrow( "Textdateien analysieren: b.md: Velum API request failed: 500", ); });