test(ai-review): 補對話三分類、誤報平行裁決與 appendExclusions 測試

This commit is contained in:
Jeffery
2026-06-23 14:26:06 +08:00
parent 98c60541c4
commit 33c5cf8201
2 changed files with 135 additions and 67 deletions
+70 -1
View File
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { loadOldFindings, loadExclusions, applyExclusions, filterFalsePositivesWithAI } from './findings.js';
import { loadOldFindings, loadExclusions, applyExclusions, filterFalsePositivesWithAI, appendExclusions } from './findings.js';
import { EXCLUSIONS_PATH, FINDINGS_PATH } from './config.js';
describe('findings exclusions', () => {
@@ -41,6 +41,41 @@ describe('findings exclusions', () => {
assert.equal(exclusions[0].title, 'fetch_package_versions jq overhead');
});
it('appends new exclusion entries and dedupes by file + original text', () => {
const fullPath = path.join(workspace, EXCLUSIONS_PATH);
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
fs.writeFileSync(fullPath, JSON.stringify([
{ location: 'app/a.js:1', original_finding: '既有誤報' },
], null, 2));
const merged = appendExclusions(workspace, [
{ location: 'app/a.js:9', original_finding: '既有誤報', reason: '行號不同但同檔同原文 → 視為重複' },
{ location: 'app/b.js:5', original_finding: '新誤報', reason: '誤報' },
]);
const onDisk = JSON.parse(fs.readFileSync(fullPath, 'utf8'));
assert.equal(onDisk.length, 2); // 1 既有 + 1 新增(重複者略過)
assert.deepEqual(onDisk.map(e => e.location), ['app/a.js:1', 'app/b.js:5']);
assert.equal(merged.length, 2);
});
it('writes appended exclusions to both workspace and mirror dir', () => {
const repoRoot = path.join(workspace, 'repo');
fs.mkdirSync(repoRoot, { recursive: true });
appendExclusions(workspace, [{ location: 'app/x.js:3', original_finding: '誤報X', reason: 'r' }], repoRoot);
const ws = JSON.parse(fs.readFileSync(path.join(workspace, EXCLUSIONS_PATH), 'utf8'));
const mirror = JSON.parse(fs.readFileSync(path.join(repoRoot, EXCLUSIONS_PATH), 'utf8'));
assert.equal(ws[0].location, 'app/x.js:3');
assert.deepEqual(mirror, ws);
});
it('returns null and writes nothing when there are no new entries', () => {
assert.equal(appendExclusions(workspace, []), null);
assert.ok(!fs.existsSync(path.join(workspace, EXCLUSIONS_PATH)));
});
it('repairs exclusions wrapper format to a top-level array', () => {
const fullPath = path.join(workspace, EXCLUSIONS_PATH);
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
@@ -142,6 +177,40 @@ describe('findings exclusions', () => {
assert.ok(capturedUserContent.includes('"suggestion":"update tests"'));
});
it('judges each finding with a parallel defender sub-agent and drops only false positives', async () => {
const findings = [
{ level: 'critical', role: 'Assassin', location: 'a.js:1', problem: 'p1', suggestion: 's1' },
{ level: 'warning', role: 'Mage', location: 'b.js:2', problem: 'p2', suggestion: 's2' },
{ level: 'info', role: 'Bard', location: 'c.js:3', problem: 'p3', suggestion: 's3' },
];
const seenPrompts = [];
const chatFn = async (systemPrompt, userContent) => {
seenPrompts.push(systemPrompt);
const loc = JSON.parse(userContent).location;
return { verdict: loc === 'b.js:2' ? 'false_positive' : 'confirmed' };
};
const result = await filterFalsePositivesWithAI(findings, [], chatFn);
assert.deepEqual(result.map(f => f.location), ['a.js:1', 'c.js:3']); // b.js 誤報被剔除
assert.equal(seenPrompts.length, 3); // 每條 finding 各一個 sub-agent
assert.ok(seenPrompts.every(p => p.includes('Paladin'))); // 套用防守方角色
});
it('keeps a finding when its defender sub-agent call fails (conservative)', async () => {
const findings = [
{ level: 'critical', role: 'Assassin', location: 'a.js:1', problem: 'p', suggestion: 's' },
{ level: 'warning', role: 'Mage', location: 'b.js:2', problem: 'p', suggestion: 's' },
];
const chatFn = async (_s, userContent) => {
if (JSON.parse(userContent).location === 'a.js:1') throw new Error('LLM down');
return { verdict: 'false_positive' };
};
const result = await filterFalsePositivesWithAI(findings, [], chatFn);
assert.deepEqual(result.map(f => f.location), ['a.js:1']); // a 失敗→保守保留;b 誤報→剔除
});
it('logs exclusions file metadata and repo state when loading exclusions', () => {
const fullPath = path.join(workspace, EXCLUSIONS_PATH);
fs.mkdirSync(path.dirname(fullPath), { recursive: true });