/** * Unit tests for buildScanSummary() — the shared categorization logic * consumed by both the terminal scan renderer and the browser. * * What we verify: * 1. byVerdict counts every block/review regardless of source (matches terminal) * 2. Sections group strictly by source (default / each shield / user / cloud) * 3. User-defined rules with block verdict live in "Your Rules" section, * but still count in byVerdict.blocked * 4. Rule findings are deduplicated by (project + command preview) * 5. Per-agent breakdown only shows agents that had activity * 6. Loop cost is (count - threshold) × per-iter for each loop */ import { describe, it, expect } from 'vitest'; import { buildScanSummary, agentDisplayName, agentBadgeText, agentColorName, type AgentScanInput, } from '../scan-summary'; import { isNode9SelfOutput, type ScanResult } from '../cli/commands/scan'; describe('agent display helpers', () => { // Regression: before these, badge/label sites inlined // `agent === 'gemini' ? … : 'codex' ? … : Claude`, so antigravity and // copilot findings rendered as "[Claude]" — misattributing the source // agent in the security report. it('gives antigravity and copilot distinct names (not Claude)', () => { expect(agentDisplayName('antigravity')).toBe('Antigravity'); expect(agentDisplayName('copilot')).toBe('GitHub Copilot'); expect(agentBadgeText('antigravity', 0)).toBe('[Agy]'); expect(agentBadgeText('copilot', 0)).toBe('[Copilot]'); }); it('does not fall through copilot/antigravity to the Claude colour', () => { expect(agentColorName('copilot')).toBe('green'); expect(agentColorName('antigravity')).toBe('yellow'); // claude / unknown still default to cyan expect(agentColorName('claude')).toBe('cyan'); expect(agentColorName('something-new')).toBe('cyan'); }); it('preserves the existing claude/gemini/codex/shell rendering', () => { expect(agentDisplayName('gemini')).toBe('Gemini CLI'); expect(agentDisplayName('codex')).toBe('Codex'); expect(agentDisplayName('claude')).toBe('Claude Code'); expect(agentColorName('gemini')).toBe('blue'); expect(agentColorName('codex')).toBe('magenta'); expect(agentColorName('shell')).toBe('yellow'); }); it('pads the badge to a fixed column width for aligned scan rows', () => { expect(agentBadgeText('copilot')).toBe('[Copilot] '); // width 10 expect(agentBadgeText('gemini')).toHaveLength(10); expect(agentBadgeText('antigravity')).toHaveLength(10); }); it('falls back to Claude for an unknown agent id (never throws)', () => { expect(agentDisplayName('zzz')).toBe('Claude Code'); expect(agentBadgeText('zzz', 0)).toBe('[Claude]'); }); }); function finding(opts: { ruleName: string; verdict: 'block' | 'review'; sourceType: 'default' | 'shield' | 'user'; shieldName: string; // eslint-disable-next-line @typescript-eslint/no-explicit-any input?: any; project?: string; agent?: 'claude' | 'gemini' | 'codex'; }) { return { source: { sourceType: opts.sourceType, shieldName: opts.shieldName, shieldLabel: opts.shieldName, rule: { name: opts.ruleName, verdict: opts.verdict, reason: opts.ruleName + ' reason' }, }, toolName: 'bash', input: opts.input ?? { command: 'echo test' }, timestamp: '2026-04-01T00:00:00Z', project: opts.project ?? '~/p', sessionId: 'sess1', agent: opts.agent ?? ('claude' as const), // eslint-disable-next-line @typescript-eslint/no-explicit-any } as any; } function emptyScan(overrides: Partial = {}): ScanResult { return { filesScanned: 0, sessions: 0, totalToolCalls: 0, bashCalls: 0, findings: [], dlpFindings: [], canaryFindings: [], loopFindings: [], totalCostUSD: 0, firstDate: null, lastDate: null, sessionsWithEarlySecrets: 0, perSession: [], ...overrides, }; } function claudeAgent(scan: ScanResult): AgentScanInput { return { id: 'claude', label: 'Claude', icon: '🤖', scan }; } describe('buildScanSummary', () => { it('returns zeroed shape for empty input', () => { const s = buildScanSummary([claudeAgent(emptyScan())]); expect(s.stats.sessions).toBe(0); expect(s.byVerdict).toEqual({ blocked: 0, supervised: 0, leaks: 0, canaries: 0, loops: 0, loggedOnly: 0, }); expect(s.canaries).toEqual([]); expect(s.sections).toEqual([]); expect(s.byAgent).toEqual([]); expect(s.leaks).toEqual([]); expect(s.loops).toEqual([]); expect(s.loopWastedUSD).toBe(0); }); it('counts byVerdict across all sources (terminal parity)', () => { // User's block rule (block-force-push) + shield block (block-eval-remote) // should both count toward byVerdict.blocked. const scan = emptyScan({ sessions: 1, findings: [ finding({ ruleName: 'block-force-push', verdict: 'block', sourceType: 'user', shieldName: 'custom', }), finding({ ruleName: 'shield:bash-safe:block-eval-remote', verdict: 'block', sourceType: 'shield', shieldName: 'bash-safe', }), finding({ ruleName: 'review-git-push', verdict: 'review', sourceType: 'user', shieldName: 'custom', }), ], }); const s = buildScanSummary([claudeAgent(scan)]); expect(s.byVerdict.blocked).toBe(2); expect(s.byVerdict.supervised).toBe(1); }); it('groups sections by source, stripping shield: prefix from rule names', () => { const scan = emptyScan({ sessions: 1, findings: [ finding({ ruleName: 'shield:bash-safe:block-eval-remote', verdict: 'block', sourceType: 'shield', shieldName: 'bash-safe', }), finding({ ruleName: 'review-git-push', verdict: 'review', sourceType: 'user', shieldName: 'custom', }), finding({ ruleName: 'review-git-destructive', verdict: 'review', sourceType: 'default', shieldName: 'default', }), ], }); const s = buildScanSummary([claudeAgent(scan)]); const ids = s.sections.map((x) => x.id).sort(); expect(ids).toEqual(['default', 'shield:bash-safe', 'user']); const shieldSection = s.sections.find((x) => x.id === 'shield:bash-safe')!; expect(shieldSection.rules[0].name).toBe('block-eval-remote'); // prefix stripped expect(shieldSection.shieldKey).toBe('bash-safe'); expect(shieldSection.blockedCount).toBe(1); expect(shieldSection.reviewCount).toBe(0); const userSection = s.sections.find((x) => x.id === 'user')!; expect(userSection.rules[0].name).toBe('review-git-push'); expect(userSection.blockedCount).toBe(0); expect(userSection.reviewCount).toBe(1); }); it("places user's block-verdict rule in Your Rules, but still counts it in byVerdict.blocked", () => { // This is the exact discrepancy we set out to fix: // block-force-push is a USER rule with BLOCK verdict. Old browser // excluded it from "Blocked" because source=user; terminal included it. const scan = emptyScan({ sessions: 1, findings: [ finding({ ruleName: 'block-force-push', verdict: 'block', sourceType: 'user', shieldName: 'custom', }), ], }); const s = buildScanSummary([claudeAgent(scan)]); expect(s.byVerdict.blocked).toBe(1); // counted in top stats const userSection = s.sections.find((x) => x.id === 'user')!; expect(userSection.rules[0].name).toBe('block-force-push'); // also in Your Rules section expect(userSection.blockedCount).toBe(1); }); it('deduplicates findings within a rule group by (project, command preview)', () => { const scan = emptyScan({ findings: [ finding({ ruleName: 'review-git-push', verdict: 'review', sourceType: 'user', shieldName: 'custom', input: { command: 'git push origin dev' }, project: '~/p', }), finding({ ruleName: 'review-git-push', verdict: 'review', sourceType: 'user', shieldName: 'custom', input: { command: 'git push origin dev' }, // same cmd + same project project: '~/p', }), finding({ ruleName: 'review-git-push', verdict: 'review', sourceType: 'user', shieldName: 'custom', input: { command: 'git push origin main' }, // different cmd project: '~/p', }), ], }); const s = buildScanSummary([claudeAgent(scan)]); const userSection = s.sections.find((x) => x.id === 'user')!; expect(userSection.rules[0].findings.length).toBe(2); // deduped: 3 in → 2 out }); it('computes per-agent breakdown only for agents with activity', () => { const claudeScan = emptyScan({ sessions: 5, totalCostUSD: 1.23, findings: [ finding({ ruleName: 'review-git-push', verdict: 'review', sourceType: 'user', shieldName: 'custom', agent: 'claude', }), ], }); const geminiScan = emptyScan(); // empty const codexScan = emptyScan({ sessions: 1 }); const s = buildScanSummary([ { id: 'claude', label: 'Claude', icon: '🤖', scan: claudeScan }, { id: 'gemini', label: 'Gemini', icon: '♊', scan: geminiScan }, { id: 'codex', label: 'Codex', icon: '🔮', scan: codexScan }, ]); const ids = s.byAgent.map((a) => a.id); expect(ids).toEqual(['claude', 'codex']); // gemini filtered out (no activity) const claude = s.byAgent.find((a) => a.id === 'claude')!; expect(claude.sessions).toBe(5); expect(claude.findings).toBe(1); expect(claude.costUSD).toBeCloseTo(1.23); }); it('estimates loop waste as (count - threshold) × per-iter', () => { const scan = emptyScan({ loopFindings: [ { toolName: 'edit', commandPreview: 'edit /a', count: 10, // 10 - 3 = 7 wasted timestamp: '', project: '~/p', sessionId: 's', agent: 'claude', }, { toolName: 'edit', commandPreview: 'edit /b', count: 5, // 5 - 3 = 2 wasted timestamp: '', project: '~/p', sessionId: 's', agent: 'claude', }, ], }); scan.perSession = [{ sessionId: 's', costUSD: 200, toolCalls: 100 }]; const s = buildScanSummary([claudeAgent(scan)]); // 9 wasted iterations, priced at that session's own rate ($2.00/call). // The flat $0.006 this replaced would have called the same work $0.054. expect(s.loopWaste.pricedIterations).toBe(9); expect(s.loopWastedUSD).toBeCloseTo(9 * 2); }); it('reports waste it cannot price as unpriced, never as free', () => { const scan = emptyScan({ loopFindings: [ { toolName: 'edit', commandPreview: 'edit /a', count: 10, timestamp: '', project: '~/p', sessionId: 's', agent: 'claude', }, ], }); // No perSession row: Antigravity and Copilot transcripts carry no token // data, so this is the real shape for those agents — and $0.00 next to 7 // wasted iterations reads as "nothing happened". const s = buildScanSummary([claudeAgent(scan)]); expect(s.loopWastedUSD).toBe(0); expect(s.loopWaste.pricedIterations).toBe(0); expect(s.loopWaste.unpricedIterations).toBe(7); }); it('sorts sections: most blocked first, then by total findings', () => { const scan = emptyScan({ findings: [ // user section: 0 blocked, 3 review finding({ ruleName: 'a', verdict: 'review', sourceType: 'user', shieldName: 'custom', }), finding({ ruleName: 'b', verdict: 'review', sourceType: 'user', shieldName: 'custom', input: { command: 'diff b' }, }), finding({ ruleName: 'c', verdict: 'review', sourceType: 'user', shieldName: 'custom', input: { command: 'diff c' }, }), // shield section: 1 blocked finding({ ruleName: 'shield:bash-safe:x', verdict: 'block', sourceType: 'shield', shieldName: 'bash-safe', }), ], }); const s = buildScanSummary([claudeAgent(scan)]); // Shield has blocked=1; user has blocked=0 → shield first expect(s.sections[0].id).toBe('shield:bash-safe'); expect(s.sections[1].id).toBe('user'); }); }); // ── DLP self-output filter & fixture-token demotion ───────────────────────── describe('isNode9SelfOutput', () => { it('detects scanArgs output struct (patternName + redactedSample)', () => { const text = `token: ghp_abc scan: { patternName: 'GitHub Token', redactedSample: 'ghp_***abc' }`; expect(isNode9SelfOutput(text)).toBe(true); }); it('detects live security alert text', () => { // Two markers needed; combine alert + redactedSample. const text = `NODE9 SECURITY ALERT: secret detected redactedSample: 'ghp_***xyz'`; expect(isNode9SelfOutput(text)).toBe(true); }); it('detects severity + patternName combo', () => { const text = `{ patternName: 'AWS Access Key ID', severity: 'block' }`; expect(isNode9SelfOutput(text)).toBe(true); }); it('does not flag a single coincidental marker', () => { // patternName alone is not enough — could appear in user docs. const text = `the patternName: 'thing' was discussed in the meeting`; expect(isNode9SelfOutput(text)).toBe(false); }); it('does not flag normal tool output', () => { expect(isNode9SelfOutput('Here is your token: ghp_realsecret123')).toBe(false); }); }); // ── detectLoops classifies kind by time span ──────────────────────────────── import { detectLoops } from '../cli/commands/scan'; describe('detectLoops kind classification', () => { function call(toolName: string, command: string, isoTs: string) { return { toolName, input: { command }, timestamp: isoTs }; } it('flags bursty repeats as kind="loop"', () => { // 5 calls within 60 seconds — clearly stuck behavior. const calls = [ call('Bash', 'ls -la', '2026-04-01T00:00:00Z'), call('Bash', 'ls -la', '2026-04-01T00:00:15Z'), call('Bash', 'ls -la', '2026-04-01T00:00:30Z'), call('Bash', 'ls -la', '2026-04-01T00:00:45Z'), call('Bash', 'ls -la', '2026-04-01T00:01:00Z'), ]; const found = detectLoops(calls, '~/p', 'sess1', 'claude'); expect(found).toHaveLength(1); expect(found[0].kind).toBe('loop'); expect(found[0].count).toBe(5); }); it('flags long-spanning repeats as kind="long-iteration"', () => { // 5 edits over 2 hours — sustained deep work, not stuck. const calls = [ call('Edit', 'src/feature.ts', '2026-04-01T10:00:00Z'), call('Edit', 'src/feature.ts', '2026-04-01T10:30:00Z'), call('Edit', 'src/feature.ts', '2026-04-01T11:00:00Z'), call('Edit', 'src/feature.ts', '2026-04-01T11:30:00Z'), call('Edit', 'src/feature.ts', '2026-04-01T12:00:00Z'), ]; const found = detectLoops(calls, '~/p', 'sess1', 'claude'); expect(found).toHaveLength(1); expect(found[0].kind).toBe('long-iteration'); }); it('boundary: 10 minutes exactly is long-iteration (≥)', () => { const calls = [ call('Bash', 'cmd', '2026-04-01T00:00:00Z'), call('Bash', 'cmd', '2026-04-01T00:05:00Z'), call('Bash', 'cmd', '2026-04-01T00:10:00Z'), ]; const found = detectLoops(calls, '~/p', 'sess1', 'claude'); expect(found[0].kind).toBe('long-iteration'); }); }); // ── Loop vs long-iteration classification ─────────────────────────────────── describe('loopWastedUSD excludes long-iteration findings', () => { it('counts only kind="loop" toward waste; ignores long-iteration', () => { const scan = emptyScan({ sessions: 1, loopFindings: [ // Real loop: 10 iterations, kind 'loop' — counts as waste { toolName: 'Bash', commandPreview: 'ls -la', count: 10, timestamp: '2026-04-01T00:00:00Z', project: '~/p', sessionId: 'sess1', agent: 'claude', kind: 'loop', }, // Long iteration: 100 iterations, kind 'long-iteration' — NO waste { toolName: 'Edit', commandPreview: 'src/feature.ts', count: 100, timestamp: '2026-04-01T00:00:00Z', project: '~/p', sessionId: 'sess1', agent: 'claude', kind: 'long-iteration', }, ], }); scan.perSession = [{ sessionId: 'sess1', costUSD: 100, toolCalls: 100 }]; const s = buildScanSummary([claudeAgent(scan)]); // Only 7 wasted iters (10 loop - 3 threshold) at $1.00/call. The 97 // long-iteration iterations above threshold contribute ZERO: that is // sustained work on one target, not a stuck loop. expect(s.loopWaste.pricedIterations).toBe(7); expect(s.loopWastedUSD).toBeCloseTo(7, 4); }); it('treats missing kind as a real loop (backwards compat)', () => { const scan = emptyScan({ sessions: 1, loopFindings: [ { toolName: 'Bash', commandPreview: 'ls -la', count: 10, timestamp: '2026-04-01T00:00:00Z', project: '~/p', sessionId: 'sess1', agent: 'claude', // no `kind` field — legacy data }, ], }); scan.perSession = [{ sessionId: 'sess1', costUSD: 100, toolCalls: 100 }]; const s = buildScanSummary([claudeAgent(scan)]); expect(s.loopWaste.pricedIterations).toBe(7); expect(s.loopWastedUSD).toBeCloseTo(7, 4); }); }); // looksLikeFixtureToken was REMOVED (see doc/roadmap/active/tool-result-dlp-design.md). // It ran on maskSecret's OUTPUT, whose asterisks are themselves "6+ repeated // characters", so it silently dropped every credential of 14 characters or more // found in a tool result: 75 of 75 on real history. This test could not catch // that because it pinned hand-written samples ('AKIA****5XYZ', 4 asterisks) that // the real masker never produces; maskSecret emits up to 12. The repo rule it // broke is its own: tests must use the inputs the REAL caller produces. The job // it attempted is done one layer down on the raw value by DLP_STOPWORDS.