import { describe, expect, it } from "vitest"; import { __test } from "../index.ts"; import type { SecretSpan, SpanLabel } from "../src/types.ts"; function label( tag: string, source: SpanLabel["source"] = "generic", ): SpanLabel { return { tag, source, priority: 10 }; } function span( text: string, value: string, overrides: Partial = {}, ): SecretSpan { const start = text.indexOf(value); return { start, end: start + value.length, value, labels: [label(overrides.kind?.toUpperCase() ?? "SECRET")], kind: "generic", confidence: 10, wholeSpan: false, ...overrides, }; } describe("leaks span selector", () => { it("merges labels for identical spans", () => { const text = "token alpha-secret"; const selected = __test.selectSecretSpans(text, [ span(text, "alpha-secret", { labels: [label("A", "dotenv")] }), span(text, "alpha-secret", { labels: [label("B", "builtin")] }), ]); expect(selected).toHaveLength(1); expect(selected[0].labels.map((item) => item.tag)).toEqual(["A", "B"]); }); it("keeps component spans over enclosing exact learned values", () => { const text = "DATABASE_URL=postgres://alice:pw@db/app"; const exact = span(text, "postgres://alice:pw@db/app", { kind: "exact", confidence: 80, }); const component = span(text, "pw", { kind: "structure", confidence: 60 }); expect(__test.selectSecretSpans(text, [exact, component])).toEqual([ component, ]); }); it("keeps exact learned values over generic regexes", () => { const text = "send learned-secret-value"; const exact = span(text, "learned-secret-value", { kind: "exact", confidence: 50, }); const generic = span(text, "learned-secret-value", { kind: "generic", confidence: 90, }); expect(__test.selectSecretSpans(text, [generic, exact])[0].kind).toBe( "exact", ); }); it("keeps the smallest secret-bearing span for ordinary overlaps", () => { const text = "Authorization: Bearer nested-secret-token"; const large = span(text, "Bearer nested-secret-token", { kind: "builtin", confidence: 70, }); const small = span(text, "nested-secret-token", { kind: "builtin", confidence: 70, }); expect(__test.selectSecretSpans(text, [large, small])).toEqual([small]); }); it("keeps private key whole spans over smaller matches", () => { const text = "-----BEGIN PRIVATE KEY-----\nabc123\n-----END PRIVATE KEY-----"; const whole = span(text, text, { kind: "private-key", wholeSpan: true, confidence: 100, }); const small = span(text, "abc123", { kind: "generic", confidence: 90 }); expect(__test.selectSecretSpans(text, [small, whole])).toEqual([whole]); }); it("keeps JWT whole spans over component-looking matches", () => { const text = "aaa.bbb.ccc"; const whole = span(text, text, { kind: "jwt", wholeSpan: true, confidence: 100, }); const small = span(text, "bbb", { kind: "structure", confidence: 90 }); expect(__test.selectSecretSpans(text, [small, whole])).toEqual([whole]); }); it("skips spans inside existing placeholders", () => { const text = "keep __LEAK_TOKEN_abcdef123456__ visible"; const candidate = span(text, "abcdef123456", { kind: "builtin", confidence: 100, }); expect(__test.selectSecretSpans(text, [candidate])).toEqual([]); }); it("replaces adjacent spans without index drift", () => { const left = "adjacent-secret-left"; const right = "adjacent-secret-right"; const text = `${left}${right}`; const engine = new __test.LeaksEngine({ detectors: [() => [span(text, left), span(text, right)]], }); const redacted = engine.redactText(text, "provider"); expect(redacted.count).toBe(2); expect(redacted.text).not.toContain(left); expect(redacted.text).not.toContain(right); expect(engine.restoreText(redacted.text)).toBe(text); }); });