import { describe, expect, it } from "vitest"; import { redactAnalysis } from "./redact"; import { filterEvents } from "./filter"; import { analysisToMarkdown } from "./export"; import { isAnalysis } from "./app/validation"; import { analysisIndex, evidenceEvents } from "./app/indexes"; import type { Analysis, EventItem } from "./types"; function event(id: number, overrides: Partial = {}): EventItem { return { id: String(id), timestamp: "2026-11-20T08:00:00Z", timestamp_source: "event", timestamp_valid: true, provider: "Microsoft-Windows-Security-Auditing", event_id: 4625, level: "information", category: "authentication", summary: `Failed sign-in for user${id % 500}`, channel: "Security", computer: `HOST${id % 20}`, fields: { "EventData.TargetUserName": `user${id % 500}`, "EventData.IpAddress": `192.0.2.${(id % 254) + 1}`, "EventData.WorkstationName": `WS${id % 300}`, "EventData.Status": "0xc000006d", }, ...overrides, }; } describe("redaction performance and coverage", () => { it("redacts thousands of events with many distinct values within budget", () => { // The previous implementation needed ~73 s for 2,000 such events. const events = Array.from({ length: 10_000 }, (_, index) => event(index)); const started = performance.now(); const result = redactAnalysis({ source: "Security.evtx", events, incidents: [] }); expect(performance.now() - started).toBeLessThan(5_000); expect(JSON.stringify(result.analysis)).not.toMatch(/user\d|HOST\d|WS\d|10\.0\./); }); it("uses consistent pseudonyms for the same value across fields and text", () => { const source: Analysis = { source: "a.evtx", incidents: [], events: [event(1), event(2, { computer: "HOST1" })] }; const [first, second] = redactAnalysis(source).analysis.events; expect(first.computer).toBe(second.computer); expect(first.summary).toContain(first.fields?.["EventData.TargetUserName"]); expect(first.fields?.["EventData.TargetUserName"]).not.toBe(second.fields?.["EventData.TargetUserName"]); }); it("masks DNS query names, domain fields and internal FQDNs in free text", () => { const source: Analysis = { source: "System.evtx", incidents: [], events: [ event(1, { provider: "Microsoft-Windows-DNS-Client", event_id: 1014, category: "network", summary: "Name resolution for fileserver.corp.contoso.local timed out", fields: { "EventData.QueryName": "fileserver.corp.contoso.local", "EventData.SubjectDomainName": "CONTOSO", query: "api.example.test", }, }), ], }; const result = redactAnalysis(source); const serialized = JSON.stringify(result.analysis); expect(serialized).not.toContain("contoso"); expect(serialized).not.toContain("CONTOSO"); expect(serialized).not.toContain("api.example.test"); expect(result.summary.domains).toBeGreaterThanOrEqual(3); }); it("keeps two-label file names and reports untouched field names", () => { const source: Analysis = { source: "a.evtx", incidents: [], events: [ event(1, { summary: "ExampleApp.exe crashed in example.dll", fields: { "EventData.ExceptionCode": "0xc0000005" }, }), ], }; const result = redactAnalysis(source); expect(result.analysis.events[0].summary).toContain("ExampleApp.exe"); expect(result.summary.untouchedFieldKeys).toEqual(["EventData.ExceptionCode"]); }); it("redacts campaign titles and reasons", () => { const source: Analysis = { source: "a.evtx", events: [event(1)], incidents: [ { id: "i1", rule_id: "r", start: "2026-11-20T08:00:00Z", end: "2026-11-20T08:00:00Z", title: "t", category: "authentication", severity: "warning", reasons: [], evidence_event_ids: ["1"], original_count: 1, compressed_count: 1, }, ], campaigns: [ { id: "c1", rule_id: "r", title: "Recurring for user1 on HOST1", category: "authentication", severity: "warning", start: "2026-11-20T08:00:00Z", end: "2026-11-20T08:10:00Z", incident_ids: ["i1", "i1"], event_count: 2, reasons: ["seen from 192.0.2.1"], }, ], }; const campaign = redactAnalysis(source).analysis.campaigns?.[0]; expect(JSON.stringify(campaign)).not.toMatch(/user1|HOST1|10\.0\.1\.1/); }); }); describe("attention filter", () => { it("treats Security audit failures as attention even at information level", () => { const failure = event(1, { outcome: "failure" }); const success = event(2, { outcome: "success" }); const visible = filterEvents([failure, success], { search: "", category: "", level: "", provider: "", noiseMode: "attention", }); expect(visible.map((item) => item.id)).toEqual(["1"]); }); }); describe("response contract", () => { const base: Analysis = { source: "a.evtx", events: [event(1), event(2)], incidents: [ { id: "i1", rule_id: "r", start: "2026-11-20T08:00:00Z", end: "2026-11-20T08:00:00Z", title: "a", category: "authentication", severity: "warning", reasons: [], evidence_event_ids: ["1"], original_count: 1, compressed_count: 1, campaign_id: "c1", }, { id: "i2", rule_id: "r", start: "2026-11-20T08:03:00Z", end: "2026-11-20T08:03:00Z", title: "b", category: "authentication", severity: "warning", reasons: [], evidence_event_ids: ["2"], original_count: 1, compressed_count: 1, campaign_id: "c1", }, ], campaigns: [ { id: "c1", rule_id: "r", title: "Recurring: a", category: "authentication", severity: "warning", start: "2026-11-20T08:00:00Z", end: "2026-11-20T08:03:00Z", incident_ids: ["i1", "i2"], event_count: 2, reasons: ["x"], }, ], source_info: { sha256: "a".repeat(64), size_bytes: 69632 }, parse: { parser: "evtx-rs", status: "complete", parsed_events: 2, records_seen: 2, skipped_records: 0, issue_count: 300, truncated: false, duration_ms: 3, issues: [{ code: "invalid_timestamp", message: "m" }], issues_truncated: true, issue_counts: { invalid_timestamp: 300 }, }, }; it("accepts campaigns, source integrity and a truncated issue list", () => { expect(isAnalysis(base)).toBe(true); }); it("rejects campaigns that reference unknown incidents and malformed hashes", () => { expect( isAnalysis({ ...base, campaigns: [{ ...(base.campaigns ?? [])[0], incident_ids: ["i1", "missing"] }] }), ).toBe(false); expect(isAnalysis({ ...base, source_info: { sha256: "xyz", size_bytes: 1 } })).toBe(false); expect( isAnalysis({ ...base, parse: { ...(base.parse as NonNullable), issues_truncated: false } }), ).toBe(false); }); it("indexes evidence in incident order and exports campaign and hash metadata", () => { expect(analysisIndex(base).incidentByEvent.get("2")?.id).toBe("i2"); expect(evidenceEvents(base, base.incidents[0]).map((item) => item.id)).toEqual(["1"]); const markdown = analysisToMarkdown(base, false); expect(markdown).toContain("## Campaigns"); expect(markdown).toContain("a".repeat(64)); expect(markdown).toContain("further issue(s)"); }); });