Files
velum-raycast/tests/project-analysis.test.ts

143 lines
4.1 KiB
TypeScript

import { expect, it } from "bun:test";
import {
analyzeProjectBatch,
analyzeProjectTexts,
splitTextForAnalysis,
} 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",
);
});
it("analysiert eine große Textdatei abschnittsweise und ordnet Grenztreffer an der Originalposition zu", async () => {
const prefix = `😀${"x ".repeat(4999)}Max `;
const source = `${prefix}Mustermann ${"text ".repeat(3000)}`;
const requests: number[] = [];
const analyzed = await analyzeProjectTexts(
[{ key: "groß.txt", text: source }],
{},
["PERSON"],
{},
async (text, mapping) => {
requests.push(text.length);
const matches = [...text.matchAll(/Max Mustermann/g)].map((match) => {
const start = [...text.slice(0, match.index)].length;
return {
start,
end: start + 14,
type: "PERSON",
original: "Max Mustermann",
placeholder: "PERSON_01",
score: 1,
};
});
return {
pseudonymized_text: text,
selected_entity_types: ["PERSON"],
mapping: matches.length
? {
...mapping,
PERSON_01: { original: "Max Mustermann", type: "PERSON" },
}
: mapping,
entity_count: matches.length ? 1 : 0,
spans: matches,
};
},
() => undefined,
);
expect(requests.length).toBeGreaterThan(1);
expect(Math.max(...requests)).toBeLessThan(24_000);
expect(analyzed.spansByFile.get("groß.txt")).toEqual([
{
start: prefix.length - 4,
end: prefix.length + 10,
type: "PERSON",
original: "Max Mustermann",
placeholder: "PERSON_01",
score: 1,
},
]);
});
it("verweigert das stumme Zerschneiden eines überlangen Worts", () => {
expect(() => splitTextForAnalysis("x".repeat(100), 20, 5)).toThrow(
"ohne Trennstelle",
);
});