import { expect, it } from "bun:test"; import { projectTextAnalyzer } from "../src/lib/project-text-mapping"; import type { PlaceholderMapping } from "../src/types"; it("keeps more than 5000 project identities without sending historical NER mappings", async () => { const previous: PlaceholderMapping = {}; for (let i = 1; i <= 6000; i++) previous[`PERSON_${i}`] = { original: `Person ${i}`, type: "PERSON" }; const analyze = projectTextAnalyzer(previous, async (text, mapping) => { expect(Object.keys(mapping)).toHaveLength(0); return { pseudonymized_text: "PERSON_01", selected_entity_types: ["PERSON"], entity_count: 1, mapping: { PERSON_01: { original: text, type: "PERSON" } }, spans: [{ start: 0, end: [...text].length, original: text, placeholder: "PERSON_01", type: "PERSON", score: 1 }] }; }); const first = await analyze("Anna Berger", previous, ["PERSON"], {}); expect(first.spans[0].placeholder).toBe("PERSON_6001"); const repeat = await analyze("Anna Berger", first.mapping, ["PERSON"], {}); expect(repeat.spans[0].placeholder).toBe("PERSON_6001"); const known = await analyze("Person 4321", repeat.mapping, ["PERSON"], {}); expect(known.spans[0].placeholder).toBe("PERSON_4321"); expect(Object.keys(known.mapping)).toHaveLength(6001); expect(Object.keys(previous)).toHaveLength(6000); }); it("preserves custom terms and complete manual alias dependencies", async () => { const previous: PlaceholderMapping = { PERSON_90: { original: "Anna Berger", type: "PERSON" }, PERSON_91: { original: "Anna", type: "PERSON", merged_into: "PERSON_90" }, BEGRIFF_07: { original: "/CLIENT/ABC", type: "BEGRIFF" }, BEGRIFF_08: { original: "Other project", type: "BEGRIFF" }, }; const analyze = projectTextAnalyzer(previous, async (text, mapping, types) => { expect(Object.keys(mapping).sort()).toEqual(["BEGRIFF_07", "PERSON_90", "PERSON_91"]); return { pseudonymized_text: text, selected_entity_types: types, entity_count: 3, mapping, spans: [{ start: 0, end: 4, original: "Anna", placeholder: "PERSON_90", type: "PERSON", score: 1 }] }; }); const result = await analyze("Anna nutzt /CLIENT/ABC", previous, ["PERSON", "BEGRIFF"], {}); expect(result.pseudonymized_text).toBe("PERSON_90 nutzt /CLIENT/ABC"); expect(result.mapping.PERSON_91.merged_into).toBe("PERSON_90"); }); it("leaves literal placeholders alone and rebuilds Unicode output from positions", async () => { const previous = { PERSON_12: { original: "Berta", type: "PERSON" } }; const text = "😀 PERSON_01 Anna"; const analyze = projectTextAnalyzer(previous, async () => ({ pseudonymized_text: "😀 PERSON_01 PERSON_01", selected_entity_types: ["PERSON"], entity_count: 1, mapping: { PERSON_01: { original: "Anna", type: "PERSON" } }, spans: [{ start: 12, end: 16, original: "Anna", placeholder: "PERSON_01", type: "PERSON", score: 1 }], })); const result = await analyze(text, previous, ["PERSON"], {}); expect(result.pseudonymized_text).toBe("😀 PERSON_01 PERSON_13"); }); it("does not retry authentication errors or keep results from a failed API call", async () => { let calls = 0; const analyze = projectTextAnalyzer({}, async () => { calls++; throw Error("HTTP 403"); }); await expect(analyze("Anna", {}, ["PERSON"], {})).rejects.toThrow("403"); expect(calls).toBe(1); }); it("retains mappings added by another phase between text requests", async () => { const analyze = projectTextAnalyzer({}, async (text) => ({ pseudonymized_text: "PERSON_01", selected_entity_types: ["PERSON"], entity_count: 1, mapping: { PERSON_01: { original: text, type: "PERSON" } }, spans: [{ start: 0, end: text.length, original: text, placeholder: "PERSON_01", type: "PERSON", score: 1 }], })); const first = await analyze("Anna", {}, ["PERSON"], {}); const withDocument = { ...first.mapping, PERSON_02: { original: "Berta", type: "PERSON" } }; const next = await analyze("Clara", withDocument, ["PERSON"], {}); expect(next.mapping.PERSON_02.original).toBe("Berta"); expect(next.spans[0].placeholder).toBe("PERSON_03"); }); it("passes known multiword terms across whitespace and line boundaries", async () => { const previous = { BEGRIFF_01: { original: "Private Term", type: "BEGRIFF" } }; const analyze = projectTextAnalyzer(previous, async (text, mapping) => { expect(mapping.BEGRIFF_01.original).toBe("Private Term"); return { pseudonymized_text: text, mapping, spans: [], entity_count: 1, selected_entity_types: ["BEGRIFF"] }; }); await analyze("Private\nTerm", previous, ["BEGRIFF"], {}); });