import { describe, it, beforeEach, afterEach } from 'node:test'; 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 } from './findings.js'; import { EXCLUSIONS_PATH, FINDINGS_PATH } from './config.js'; describe('findings exclusions', () => { let workspace; let logs; let originalLog; beforeEach(() => { workspace = fs.mkdtempSync(path.join(os.tmpdir(), 'findings-test-')); logs = []; originalLog = console.log; console.log = (...args) => { logs.push(args.join(' ')); }; }); afterEach(() => { console.log = originalLog; fs.rmSync(workspace, { recursive: true, force: true }); }); it('loads excluded_findings wrapper format', () => { const fullPath = path.join(workspace, EXCLUSIONS_PATH); fs.mkdirSync(path.dirname(fullPath), { recursive: true }); fs.writeFileSync(fullPath, JSON.stringify({ excluded_findings: [ { location: 'entrypoint.sh:180', title: 'fetch_package_versions jq overhead' }, ], }, null, 2)); const exclusions = loadExclusions(workspace); assert.equal(exclusions.length, 1); assert.equal(exclusions[0].location, 'entrypoint.sh:180'); assert.equal(exclusions[0].title, 'fetch_package_versions jq overhead'); }); it('applies exclusions loaded from wrapper format', () => { const findings = [ { location: 'entrypoint.sh:180', role: 'Maya', suggestion: 'keep' }, { location: 'README.md:12', role: 'Maya', suggestion: 'keep' }, ]; const exclusions = [ { location: 'entrypoint.sh:180', title: 'fetch_package_versions jq overhead' }, ]; const filtered = applyExclusions(findings, exclusions); assert.equal(filtered.length, 1); assert.equal(filtered[0].location, 'README.md:12'); }); 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 }); fs.writeFileSync(fullPath, JSON.stringify([ { location: 'entrypoint.sh:180', suggestion: 'ignore' }, { location: 'README.md:12', suggestion: 'ignore' }, ], null, 2)); const repoState = { branch: 'feat/test', shortSha: 'abc1234', commitTime: '2026-05-15T09:29:49.817Z', repoDir: path.join(workspace, 'repo'), }; const exclusions = loadExclusions(workspace, repoState); assert.equal(exclusions.length, 2); assert.ok(logs.some(line => line.includes(`讀取排除問題檔案: ${fullPath}`))); assert.ok(logs.some(line => line.includes('來源分支狀態: branch=feat/test commit=abc1234'))); assert.ok(logs.some(line => line.includes('raw=2 normalized=2'))); assert.ok(logs.some(line => line.includes(`path=${path.relative(workspace, fullPath)}`))); }); it('logs findings file metadata when loading old findings', () => { const fullPath = path.join(workspace, FINDINGS_PATH); fs.mkdirSync(path.dirname(fullPath), { recursive: true }); fs.writeFileSync(fullPath, JSON.stringify([ { level: 'info', role: 'Maya', location: 'README.md:12', suggestion: 'keep' }, ], null, 2)); const findings = loadOldFindings(workspace); assert.equal(findings.length, 1); assert.equal(findings[0].is_new, false); assert.ok(logs.some(line => line.includes(`讀取舊 findings 檔案: ${fullPath}`))); assert.ok(logs.some(line => line.includes('舊 findings 檔案資訊: bytes='))); assert.ok(logs.some(line => line.includes(`path=${path.relative(workspace, fullPath)}`))); }); });