fix: analyze large project text files in bounded chunks

This commit is contained in:
muena
2026-10-19 09:43:36 +02:00
parent e783c67af0
commit 1442ed4740
3 changed files with 255 additions and 18 deletions

View File

@@ -1,5 +1,9 @@
import { expect, it } from "bun:test";
import { analyzeProjectBatch } from "../src/lib/project-analysis";
import {
analyzeProjectBatch,
analyzeProjectTexts,
splitTextForAnalysis,
} from "../src/lib/project-analysis";
import { makeTextBatches, spansBySegment } from "../src/lib/project-review";
import type { PlaceholderMapping, PseudonymizeResponse } from "../src/types";
@@ -79,3 +83,60 @@ it("nennt nach der Eingrenzung die fehlerhafte Datei", async () => {
"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",
);
});