feat: review text hits and release false-positive mappings

This commit is contained in:
muena
2026-10-19 06:44:21 +02:00
parent ebf78128c4
commit 03a793ad7c
20 changed files with 536 additions and 210 deletions

View File

@@ -4,16 +4,27 @@ 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: {}, getPreferenceValues: () => ({}),
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 } = await import("../src/text-context");
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 () => { await Promise.all(roots.splice(0).map((root) => fs.rm(root, { recursive: true, force: true }))); });
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({
@@ -44,6 +55,69 @@ it("übernimmt eine Projekt-Ausnahme auch bei Textbefehlen", () => {
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("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);