import { test } from 'node:test'; import assert from 'node:assert/strict'; import { validateQueue, ENTRY_CLASSES, ENTRY_STATES } from '../../scripts/kb-eval/lib/g7-queue.mjs'; // A queue entry is the ONLY thing standing between a real defect and silent loss, // so the checks here are deliberately unforgiving about shape and about anchors // that no longer exist in the corpus. const okEntry = () => ({ id: 'idx-99', file: 'skills/x/references/y.md', class: 'multi-locator', status: 'open', raised: '2026-08-03', summary: 'Something the file asserts in two places.', evidence: 'docs/r11-pilot-results.md ยง9.6', anchors: ['the exact text'], }); const stub = (contents) => (p) => { if (!(p in contents)) throw new Error(`ENOENT ${p}`); return contents[p]; }; test('a well-formed open entry whose anchor is present passes', () => { const r = validateQueue([okEntry()], stub({ 'skills/x/references/y.md': 'aaa the exact text bbb' })); assert.equal(r.ok, true); assert.deepEqual(r.findings, []); }); test('vocabularies are closed', () => { assert.deepEqual([...ENTRY_CLASSES].sort(), ['multi-locator', 'replacement']); assert.deepEqual([...ENTRY_STATES].sort(), ['open', 'resolved']); }); test('unknown class is rejected', () => { const e = { ...okEntry(), class: 'deletion' }; const r = validateQueue([e], stub({ 'skills/x/references/y.md': 'the exact text' })); assert.equal(r.ok, false); assert.match(r.findings[0].message, /class/); }); test('unknown status is rejected', () => { const e = { ...okEntry(), status: 'wontfix' }; const r = validateQueue([e], stub({ 'skills/x/references/y.md': 'the exact text' })); assert.equal(r.ok, false); assert.match(r.findings[0].message, /status/); }); test('a missing required field is reported by name', () => { const e = okEntry(); delete e.evidence; const r = validateQueue([e], stub({ 'skills/x/references/y.md': 'the exact text' })); assert.equal(r.ok, false); assert.match(r.findings[0].message, /evidence/); }); test('an open entry MUST carry at least one anchor', () => { const e = { ...okEntry(), anchors: [] }; const r = validateQueue([e], stub({ 'skills/x/references/y.md': 'the exact text' })); assert.equal(r.ok, false); assert.match(r.findings[0].message, /anchor/i); }); // The point of the whole mechanism: a defect must not be able to leave the queue // just because someone edited the file around it. test('an open entry whose anchor no longer occurs is a DRIFT finding, not a pass', () => { const r = validateQueue([okEntry()], stub({ 'skills/x/references/y.md': 'nothing matching here' })); assert.equal(r.ok, false); assert.equal(r.findings[0].kind, 'anchor_drift'); assert.match(r.findings[0].message, /the exact text/); }); test('every anchor is checked, not just the first', () => { const e = { ...okEntry(), anchors: ['present', 'absent'] }; const r = validateQueue([e], stub({ 'skills/x/references/y.md': 'present only' })); assert.equal(r.ok, false); assert.equal(r.findings.length, 1); assert.match(r.findings[0].message, /absent/); }); test('a missing file is reported rather than thrown', () => { const r = validateQueue([okEntry()], stub({})); assert.equal(r.ok, false); assert.equal(r.findings[0].kind, 'file_unreadable'); }); test('a resolved entry MUST record how it was resolved', () => { const e = { ...okEntry(), status: 'resolved' }; const r = validateQueue([e], stub({ 'skills/x/references/y.md': 'whatever' })); assert.equal(r.ok, false); assert.match(r.findings[0].message, /resolution/); }); test('a resolved entry is exempt from anchor checking', () => { const e = { ...okEntry(), status: 'resolved', resolution: 'colon -> period, ratified 2026-08-03' }; const r = validateQueue([e], stub({ 'skills/x/references/y.md': 'anchor is long gone' })); assert.equal(r.ok, true); }); test('duplicate ids are rejected', () => { const r = validateQueue([okEntry(), okEntry()], stub({ 'skills/x/references/y.md': 'the exact text' })); assert.equal(r.ok, false); assert.match(r.findings[0].message, /duplicate/i); }); test('findings from several entries are all reported', () => { const a = { ...okEntry(), id: 'idx-1' }; const b = { ...okEntry(), id: 'idx-2', anchors: ['gone'] }; const r = validateQueue([a, b], stub({ 'skills/x/references/y.md': 'the exact text' })); assert.equal(r.ok, false); assert.equal(r.findings.length, 1); assert.equal(r.findings[0].id, 'idx-2'); }); test('a non-array queue is rejected without throwing', () => { const r = validateQueue({ entries: [] }, stub({})); assert.equal(r.ok, false); assert.match(r.findings[0].message, /array/i); }); test('the real queue file validates against the live corpus', async () => { const { readFileSync } = await import('node:fs'); const queue = JSON.parse(readFileSync('scripts/kb-eval/data/g7-review-queue.json', 'utf8')); const r = validateQueue(queue.entries, (p) => readFileSync(p, 'utf8')); assert.deepEqual(r.findings, [], `G7 queue drifted: ${JSON.stringify(r.findings, null, 2)}`); assert.equal(r.ok, true); });