185 lines
9.6 KiB
TypeScript
185 lines
9.6 KiB
TypeScript
import { afterEach, 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 { PlaceholderMapping, PseudonymizeResponse } from "../src/types";
|
|
|
|
const storage = new Map<string, string>();
|
|
mock.module("@raycast/api", () => ({
|
|
environment: { supportPath: tmpdir() },
|
|
Form: {}, Icon: {}, Toast: { Style: {} }, showToast: async () => undefined,
|
|
LocalStorage: {
|
|
getItem: async (key: string) => storage.get(key),
|
|
setItem: async (key: string, value: string) => { storage.set(key, value); },
|
|
removeItem: async (key: string) => { storage.delete(key); },
|
|
},
|
|
getPreferenceValues: () => ({ velumBaseUrl: "", authentikTokenUrl: "", clientId: "",
|
|
serviceAccountUsername: "", serviceAccountPassword: "", maxSessions: "20" }),
|
|
}));
|
|
const { applyProjectWhitelist, pseudonymizeInContext, prepareTextInContext, commitPreparedText } = await import("../src/text-context");
|
|
const project = await import("../src/lib/velum-project");
|
|
const sessions = await import("../src/sessions");
|
|
const { normalizePseudonymizeResponse } = await import("../src/velum");
|
|
const roots: string[] = [];
|
|
afterEach(async () => {
|
|
storage.clear();
|
|
await Promise.all(roots.splice(0).map((root) => fs.rm(root, { recursive: true, force: true })));
|
|
});
|
|
|
|
it("behält Verweise beim Umstellen auf KI-Platzhalter konsistent", () => {
|
|
const result = normalizePseudonymizeResponse({
|
|
pseudonymized_text: "PERSON_02", selected_entity_types: ["PERSON"], spans: [], entity_count: 1,
|
|
mapping: {
|
|
PERSON_01: { type: "PERSON", original: "Franz Fuchs" },
|
|
PERSON_02: { type: "PERSON", original: "Hr. Fuchs", merged_into: "PERSON_01" },
|
|
},
|
|
});
|
|
expect(result.mapping["<PERSON_02>"].merged_into).toBe("<PERSON_01>");
|
|
expect(result.pseudonymized_text).toBe("<PERSON_02>");
|
|
});
|
|
|
|
it("übernimmt eine Projekt-Ausnahme auch bei Textbefehlen", () => {
|
|
const result = applyProjectWhitelist("Max und Montag", {
|
|
pseudonymized_text: "PERSON_01 und ORT_01", selected_entity_types: ["PERSON", "ORT"],
|
|
spans: [
|
|
{ start: 0, end: 3, type: "PERSON", score: 1, placeholder: "PERSON_01", original: "Max" },
|
|
{ start: 8, end: 14, type: "ORT", score: 1, placeholder: "ORT_01", original: "Montag" },
|
|
], entity_count: 2,
|
|
mapping: {
|
|
PERSON_01: { type: "PERSON", original: "Max" },
|
|
ORT_01: { type: "ORT", original: "Montag" },
|
|
},
|
|
}, {}, [{ type: "ORT", original: "Montag" }]);
|
|
expect(result.pseudonymized_text).toBe("PERSON_01 und Montag");
|
|
expect(result.mapping.ORT_01).toBeUndefined();
|
|
expect(result.entity_count).toBe(1);
|
|
});
|
|
|
|
it("speichert vor der Trefferbestätigung nichts und übernimmt nur ausgewählte Treffer", async () => {
|
|
const root = await fs.mkdtemp(join(tmpdir(), "velum-text-review-"));
|
|
roots.push(root);
|
|
const folder = join(root, "Projekt");
|
|
await fs.mkdir(folder);
|
|
const request = async (_text: string, mapping: PlaceholderMapping): Promise<PseudonymizeResponse> => ({
|
|
pseudonymized_text: "PERSON_01 und ORT_01", selected_entity_types: ["PERSON", "ORT"],
|
|
spans: [
|
|
{ start: 0, end: 3, type: "PERSON", score: 1, placeholder: "PERSON_01", original: "Max" },
|
|
{ start: 8, end: 14, type: "ORT", score: 1, placeholder: "ORT_01", original: "Montag" },
|
|
], entity_count: 2,
|
|
mapping: { ...mapping, PERSON_01: { type: "PERSON", original: "Max" },
|
|
ORT_01: { type: "ORT", original: "Montag" } },
|
|
});
|
|
const prepared = await prepareTextInContext({ sessionId: "__new_project__", newProjectPath: [folder] },
|
|
"Max und Montag", ["PERSON", "ORT"], request);
|
|
expect(await project.listRuns(folder)).toHaveLength(0);
|
|
expect(await project.loadProjectWhitelist(folder)).toEqual([]);
|
|
const { result } = await commitPreparedText(prepared, new Set([0]), true);
|
|
expect(result.pseudonymized_text).toBe("<PERSON_01> und Montag");
|
|
expect(result.mapping["<ORT_01>"]).toBeUndefined();
|
|
expect((await project.listRuns(folder))[0].run.mapping.ORT_01).toBeUndefined();
|
|
expect(await project.loadProjectWhitelist(folder)).toEqual([{ type: "ORT", original: "Montag" }]);
|
|
const next = await prepareTextInContext({ sessionId: `project:${folder}` },
|
|
"Max und Montag", ["PERSON", "ORT"], request);
|
|
expect(next.raw.spans.map((span) => span.original)).toEqual(["Max"]);
|
|
});
|
|
|
|
it("merkt abgelehnte Sitzungstreffer und schlägt sie im nächsten Lauf nicht erneut vor", async () => {
|
|
const request = async (_text: string, mapping: PlaceholderMapping): Promise<PseudonymizeResponse> => ({
|
|
pseudonymized_text: "PERSON_01", selected_entity_types: ["PERSON"], entity_count: 1,
|
|
mapping: { ...mapping, PERSON_01: { type: "PERSON", original: "Fachbegriff" } },
|
|
spans: [{ start: 0, end: 11, type: "PERSON", score: 0.8,
|
|
placeholder: "PERSON_01", original: "Fachbegriff" }],
|
|
});
|
|
const prepared = await prepareTextInContext({ sessionId: "__new_session__" },
|
|
"Fachbegriff", ["PERSON"], request);
|
|
expect(await sessions.listSessions()).toHaveLength(0);
|
|
const { result, session } = await commitPreparedText(prepared, new Set());
|
|
expect(result.pseudonymized_text).toBe("Fachbegriff");
|
|
expect(session.mapping).toEqual({});
|
|
expect((await sessions.getSession(session.id))?.excludedTerms).toEqual([
|
|
{ type: "PERSON", original: "Fachbegriff" },
|
|
]);
|
|
const next = await prepareTextInContext({ sessionId: session.id }, "Fachbegriff", ["PERSON"], request);
|
|
expect(next.raw.spans).toEqual([]);
|
|
});
|
|
|
|
it("entfernt eine False-Positive-Gruppe aus der Sitzung und merkt alle Varianten", async () => {
|
|
const session = await sessions.createSession("Test");
|
|
await sessions.updateSessionMapping(session.id, {
|
|
"<PERSON_01>": { type: "PERSON", original: "Fachbegriff" },
|
|
"<PERSON_02>": { type: "PERSON", original: "Fachbegriff kurz", merged_into: "<PERSON_01>" },
|
|
"<ORG_01>": { type: "ORG", original: "Beispiel GmbH" },
|
|
});
|
|
const revised = await sessions.releaseSessionMappingGroup(session.id, "<PERSON_01>");
|
|
expect(Object.keys(revised.mapping)).toEqual(["<ORG_01>"]);
|
|
expect(revised.excludedTerms).toEqual([
|
|
{ type: "PERSON", original: "Fachbegriff" },
|
|
{ type: "PERSON", original: "Fachbegriff kurz" },
|
|
]);
|
|
});
|
|
|
|
it("bewahrt das Anlagedatum einer Sitzung beim Umklammern und Zusammenfassen", async () => {
|
|
const session = await sessions.createSession("Zeitfolge");
|
|
const first = await sessions.updateSessionMapping(session.id, {
|
|
PERSON_01: { type: "PERSON", original: "Beispiel Eins" },
|
|
});
|
|
const created = first.mappingCreatedAt?.PERSON_01;
|
|
expect(created).toBeTruthy();
|
|
const next = await sessions.updateSessionMapping(session.id, {
|
|
"<PERSON_01>": { type: "PERSON", original: "Beispiel Eins" },
|
|
"<PERSON_02>": { type: "PERSON", original: "Beispiel Zwei", merged_into: "<PERSON_01>" },
|
|
});
|
|
expect(next.mappingCreatedAt?.["<PERSON_01>"]).toBe(created);
|
|
expect(next.mappingCreatedAt?.["<PERSON_02>"]).toBeTruthy();
|
|
expect(next.mappingCreatedAt?.PERSON_01).toBeUndefined();
|
|
});
|
|
|
|
it("verlangt eine ausdrückliche Kontextwahl und legt ein Textprojekt nur einmal an", async () => {
|
|
const root = await fs.mkdtemp(join(tmpdir(), "velum-text-context-"));
|
|
roots.push(root);
|
|
const folder = join(root, "Projekt");
|
|
await fs.mkdir(folder);
|
|
const request = async (_text: string, mapping: PlaceholderMapping): Promise<PseudonymizeResponse> => ({
|
|
pseudonymized_text: "PERSON_01", mapping: {
|
|
...mapping, PERSON_01: { type: "PERSON", original: "Alex Beispiel" },
|
|
}, selected_entity_types: ["PERSON"], spans: [], entity_count: 1,
|
|
});
|
|
await expect(pseudonymizeInContext({ sessionId: "__choose_context__" }, "Alex Beispiel", ["PERSON"], false, request))
|
|
.rejects.toThrow("Bitte ein Projekt");
|
|
const created = await pseudonymizeInContext({ sessionId: "__new_project__", newProjectPath: [folder] },
|
|
"Alex Beispiel", ["PERSON"], false, request);
|
|
expect(created.session.name).toBe("Projekt Projekt");
|
|
expect((await project.listRuns(folder)).length).toBe(1);
|
|
await expect(pseudonymizeInContext({ sessionId: "__new_project__", newProjectPath: [folder] },
|
|
"Alex Beispiel", ["PERSON"], false, request)).rejects.toThrow("bereits ein Projekt");
|
|
});
|
|
|
|
it("verwendet die bestehende Projekttabelle und bewahrt ihre Verweise für spätere Läufe", async () => {
|
|
const root = await fs.mkdtemp(join(tmpdir(), "velum-text-reuse-"));
|
|
roots.push(root);
|
|
const folder = join(root, "Quelle");
|
|
await fs.mkdir(folder);
|
|
const saved = await project.createRun(folder);
|
|
saved.run.mapping = {
|
|
PERSON_01: { type: "PERSON", original: "Franz Fuchs" },
|
|
PERSON_02: { type: "PERSON", original: "Hr. Fuchs", merged_into: "PERSON_01" },
|
|
};
|
|
await project.saveRun(saved.directory, saved.run);
|
|
const request = async (_text: string, mapping: PlaceholderMapping): Promise<PseudonymizeResponse> => {
|
|
expect(mapping.PERSON_02.merged_into).toBe("PERSON_01");
|
|
return { pseudonymized_text: "PERSON_02 und PERSON_03", mapping: {
|
|
...mapping, PERSON_03: { type: "PERSON", original: "Mara Muster" },
|
|
}, selected_entity_types: ["PERSON"], spans: [], entity_count: 2 };
|
|
};
|
|
const { result, session } = await pseudonymizeInContext({ sessionId: `project:${folder}` },
|
|
"Hr. Fuchs und Mara Muster", ["PERSON"], true, request);
|
|
expect(result.pseudonymized_text).toBe("<PERSON_02> und <PERSON_03>");
|
|
expect(session.mapping["<PERSON_02>"].original).toBe("Franz Fuchs");
|
|
const runs = await project.listRuns(folder);
|
|
expect(runs).toHaveLength(2);
|
|
expect(runs[0].run.mapping.PERSON_02.original).toBe("Hr. Fuchs");
|
|
expect(runs[0].run.mapping.PERSON_02.merged_into).toBe("PERSON_01");
|
|
expect(runs[0].run.mapping.PERSON_03.original).toBe("Mara Muster");
|
|
expect(runs[1].run.mapping.PERSON_03).toBeUndefined();
|
|
});
|