import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { applyExclusions, deduplicateWithAI, filterFalsePositivesWithAI, toAIPayload } from './findings.js'; describe('findings helpers', () => { it('toAIPayload strips internal fields', () => { const payload = toAIPayload([ { level: 'critical', role: 'Rex', location: 'a.js:1', suggestion: 'fix', is_new: true, extra: 'x' }, ]); assert.deepEqual(payload, [ { level: 'critical', role: 'Rex', location: 'a.js:1', suggestion: 'fix' }, ]); }); it('deduplicateWithAI preserves original findings on quota failure', async () => { const findings = [ { level: 'warning', role: 'Rex', location: 'a.js:1', suggestion: 'fix', is_new: true }, ]; const quotaError = new Error('quota'); quotaError.response = { status: 402 }; const result = await deduplicateWithAI(findings, async () => { throw quotaError; }); assert.deepEqual(result, findings); }); it('deduplicateWithAI remaps AI output back to original findings', async () => { const findings = [ { level: 'warning', role: 'Rex', location: 'a.js:1', suggestion: 'fix' , is_new: true }, { level: 'info', role: 'Maya', location: 'b.js:2', suggestion: 'note', is_new: false }, ]; const result = await deduplicateWithAI(findings, async () => ([ { level: 'warning', role: 'Rex', location: 'a.js:1', suggestion: 'fix' }, ])); assert.equal(result[0].is_new, true); assert.equal(result[0].location, 'a.js:1'); }); it('applyExclusions filters by path and role', () => { const findings = [ { level: 'warning', role: 'Rex', location: 'src/a.js:1', suggestion: 'fix' }, { level: 'info', role: 'Maya', location: 'src/b.js:2', suggestion: 'note' }, ]; const exclusions = [ { location: 'src/a.js', role: 'Rex' }, ]; assert.deepEqual(applyExclusions(findings, exclusions), [ { level: 'info', role: 'Maya', location: 'src/b.js:2', suggestion: 'note' }, ]); }); it('filterFalsePositivesWithAI preserves findings on error', async () => { const findings = [ { level: 'critical', role: 'Aria', location: 'main.js:1', suggestion: 'fix', is_new: true }, ]; const result = await filterFalsePositivesWithAI(findings, [], async () => { throw new Error('api down'); }); assert.deepEqual(result, findings); }); it('filterFalsePositivesWithAI returns AI filtered findings', async () => { const findings = [ { level: 'critical', role: 'Aria', location: 'main.js:1', suggestion: 'fix', is_new: true }, { level: 'warning', role: 'Rex', location: 'git.js:2', suggestion: 'note', is_new: false }, ]; const result = await filterFalsePositivesWithAI(findings, [{ location: 'git.js', suggestion: 'note' }], async () => ([ { level: 'critical', role: 'Aria', location: 'main.js:1', suggestion: 'fix' }, ])); assert.equal(result.length, 1); assert.equal(result[0].location, 'main.js:1'); assert.equal(result[0].is_new, true); }); });