forked from r.muellner/velum-raycast
feat: choose project or session for text commands
This commit is contained in:
94
tests/text-context.test.ts
Normal file
94
tests/text-context.test.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
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";
|
||||
|
||||
mock.module("@raycast/api", () => ({
|
||||
environment: { supportPath: tmpdir() },
|
||||
Form: {}, Icon: {}, Toast: { Style: {} }, showToast: async () => undefined,
|
||||
LocalStorage: {}, getPreferenceValues: () => ({}),
|
||||
}));
|
||||
const { applyProjectWhitelist, pseudonymizeInContext } = await import("../src/text-context");
|
||||
const project = await import("../src/lib/velum-project");
|
||||
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 }))); });
|
||||
|
||||
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("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();
|
||||
});
|
||||
Reference in New Issue
Block a user