import { expect, it } from "bun:test"; import { promises as fs } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { createHash } from "node:crypto"; import { analyzeProjectTextFiles } from "../src/lib/project-analysis"; import { projectTextAnalyzer } from "../src/lib/project-text-mapping"; import { hashProjectFile, writeReplacedTextFile } from "../src/lib/project-text-stream"; it("processes 27 MiB with over 50000 occurrences and over 5000 identities without changing unselected bytes", async () => { const root = await fs.mkdtemp(join(tmpdir(), "velum-dense-")); try { const source = join(root, "27MiB.txt"), target = join(root, "masked.txt"); const handle = await fs.open(source, "w"); let bytes = 0, rows = 0; const expected = createHash("sha256"); while (bytes < 27 * 1024 * 1024) { let block = ""; for (let i = 0; i < 500; i++, rows++) { const original = `customer${rows % 10_000}@example.invalid`; const row = `😀 Max Mustermann; ${original}; 08.11.2026; ${"Protokolltext ".repeat(14)}\r\n`; block += row; // Only the first name is selected; every other byte must survive. expected.update(row.replace("Max Mustermann", "PERSON_01")); } bytes += Buffer.byteLength(block); await handle.writeFile(block); } await handle.close(); const hash = await hashProjectFile(source); let requests = 0, maxRequestMapping = 0; const analyze = projectTextAnalyzer({}, async (text, mapping, types) => { requests++; maxRequestMapping = Math.max(maxRequestMapping, Object.keys(mapping).length); const responseMapping: Record = {}; const local = new Map(), counters: Record = {}; const spans = []; let previous = 0, position = 0; for (const match of text.matchAll(/Max Mustermann|customer\d+@example\.invalid|08\.11\.2026/g)) { const type = match[0] === "Max Mustermann" ? "PERSON" : match[0].includes("@") ? "EMAIL" : "DATUM"; let placeholder = local.get(match[0]); if (!placeholder) { counters[type] = (counters[type] ?? 0) + 1; placeholder = `${type}_${String(counters[type]).padStart(2, "0")}`; local.set(match[0], placeholder); responseMapping[placeholder] = { original: match[0], type }; } position += Array.from(text.slice(previous, match.index)).length; previous = match.index; const start = position; spans.push({ start, end: start + match[0].length, original: match[0], type, placeholder, score: 1 }); } return JSON.parse(JSON.stringify({ pseudonymized_text: text, selected_entity_types: types, mapping: responseMapping, spans, entity_count: local.size })); }); const result = await analyzeProjectTextFiles([{ key: "large.txt", path: source }], {}, ["PERSON", "EMAIL", "DATUM"], {}, analyze, () => undefined); const spans = result.spansByFile.get("large.txt")!; if (spans.length !== rows * 3) throw Error(`Lost hits: ${spans.length}/${rows * 3}`); if (Object.keys(result.mapping).length !== 10_002) throw Error("Lost project mapping"); if (maxRequestMapping > 5000) throw Error("API mapping limit exceeded"); const accepted = new Set(); const selected = spans.filter((span) => span.type === "PERSON").map((span, index) => { const id = String(index); accepted.add(id); if (span.placeholder !== "PERSON_01") throw Error("Unstable person placeholder"); return { ...span, id }; }); const output = await writeReplacedTextFile(source, target, selected, accepted, hash); if (output !== expected.digest("hex")) throw Error("Output bytes do not match approved replacements"); if (await hashProjectFile(source) !== hash) throw Error("Original modified"); expect(bytes).toBeGreaterThanOrEqual(27 * 1024 * 1024); expect(spans.length).toBeGreaterThan(50_000); expect(maxRequestMapping).toBeLessThanOrEqual(5000); expect(requests).toBeGreaterThan(1000); } finally { await fs.rm(root, { recursive: true, force: true }); } }, 30_000);