// tests/kb-update/test-audit-corpus-headers.test.mjs // Spor 1 Step 1 — the read-only corpus-header audit that pins the migration's // empirical ground truth: which files carry a stale (non-date) **Verified:**, // which have a body-duplicate or pipe-delimited shape, which lack the English // **Last updated:** / **Status:** base fields, and which header dialect each uses. // // auditHeaders is a pure function with an injectable reader (validate-kb-file.mjs // pattern), so these tests never touch disk — synthetic fixtures only. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { auditHeaders } from '../../scripts/kb-update/audit-corpus-headers.mjs'; // Stale verified: value is "MCP ", not a clean date. Norwegian dialect, // no English **Last updated:** (the 45-file shape). const V_NONDATE = '# T\n\n**Kategori:** governance\n**Status:** GA\n**Sist oppdatert:** 2026-06\n' + '**Verified:** MCP 2026-06\n\n---\n\n## A\n\ntekst\n'; // Clean date verified: must NOT be flagged stale. const V_DATE = '# T\n\n**Category:** governance\n**Status:** GA\n**Last updated:** 2026-06\n' + '**Verified:** 2026-06\n\n---\n\n## A\n\ntekst\n'; // Body-duplicate: a second **Verified:** below the first `---` but still inside // the 500-byte header window (the mlops-fundamentals-overview.md:10 shape). const V_BODYDUP = '# T\n\n**Status:** GA\n**Last updated:** 2026-06\n**Verified:** MCP 2026-06\n\n---\n\n' + '## A\n\n**Verified:** MCP 2026-06\n\ntekst\n'; // Pipe-delimited meta row (the ai-center-of-excellence-setup.md:4 shape). const V_PIPE = '# T\n\n**Sist oppdatert:** 2026-06-17 | **Verified:** MCP 2026-06-17\n\n---\n\n' + '## A\n\ntekst\n'; // PLAIN (non-bold) Verified pipe-tail on the **Last updated:** line — the 87-file RX-KB1 // shape. Invisible to the bold-only RE_VERIFIED (verified stays null), which is exactly the // M4 blindness; the audit must surface it via a SEPARATE plainVerified signal. const V_PLAIN_PIPE = '# T\n\n**Last updated:** 2026-06-19 | Verified: MCP 2026-06-19\n\n---\n\n' + '## A\n\ntekst\n'; // Base-field gap: no **Status:** at all. const NO_STATUS = '# T\n\n**Category:** x\n**Last updated:** 2026-06\n\n---\n\n## A\n\ntekst\n'; // Fully conformant, no Verified — the quiet majority. const CLEAN = '# T\n\n**Category:** x\n**Status:** GA\n**Last updated:** 2026-06\n\n---\n\n## A\n\ntekst\n'; function reader(map) { return (p) => { if (!(p in map)) throw new Error(`unexpected read: ${p}`); return map[p]; }; } test('auditHeaders — non-date **Verified:** MCP is flagged stale (isDate:false)', () => { const r = auditHeaders(['a.md'], reader({ 'a.md': V_NONDATE })); const f = r.files['a.md']; assert.ok(f.verified, 'verified header detected'); assert.equal(f.verified.isDate, false); assert.equal(f.verified.value, 'MCP 2026-06'); assert.equal(r.aggregate.verifiedNonDate, 1); assert.equal(r.aggregate.verifiedDate, 0); }); test('auditHeaders — clean date **Verified:** is NOT flagged stale', () => { const r = auditHeaders(['b.md'], reader({ 'b.md': V_DATE })); const f = r.files['b.md']; assert.ok(f.verified); assert.equal(f.verified.isDate, true); assert.equal(r.aggregate.verifiedNonDate, 0); assert.equal(r.aggregate.verifiedDate, 1); }); test('auditHeaders — body-duplicate within 500B is flagged separately from the header hit', () => { const r = auditHeaders(['c.md'], reader({ 'c.md': V_BODYDUP })); const f = r.files['c.md']; assert.ok(f.verified); assert.equal(f.verified.duplicateWithinHeaderRegion, true); assert.equal(r.aggregate.verifiedDuplicate, 1); // the single-occurrence fixtures must not trip the duplicate flag const r2 = auditHeaders(['a.md'], reader({ 'a.md': V_NONDATE })); assert.equal(r2.files['a.md'].verified.duplicateWithinHeaderRegion, false); }); test('auditHeaders — pipe-delimited **Verified:** row is flagged pipe, value stops at the separator', () => { const r = auditHeaders(['d.md'], reader({ 'd.md': V_PIPE })); const f = r.files['d.md']; assert.ok(f.verified); assert.equal(f.verified.pipe, true); assert.equal(f.verified.value, 'MCP 2026-06-17'); assert.equal(f.verified.isDate, false); assert.equal(r.aggregate.verifiedPipe, 1); assert.equal(f.dialect, 'pipe'); }); test('auditHeaders — Norwegian **Sist oppdatert:** does not satisfy the English Last-updated check', () => { const r = auditHeaders(['a.md'], reader({ 'a.md': V_NONDATE })); const f = r.files['a.md']; assert.equal(f.lastUpdatedEnglish, false); assert.equal(r.aggregate.missingEnglishLastUpdated, 1); assert.equal(f.dialect, 'kategori'); }); test('auditHeaders — missing **Status:** is flagged', () => { const r = auditHeaders(['e.md'], reader({ 'e.md': NO_STATUS })); const f = r.files['e.md']; assert.equal(f.status, false); assert.equal(r.aggregate.missingStatus, 1); assert.equal(f.dialect, 'category'); }); test('auditHeaders — conformant file raises no flags; aggregate counts add up over a batch', () => { const map = { 'a.md': V_NONDATE, 'b.md': V_DATE, 'c.md': V_BODYDUP, 'd.md': V_PIPE, 'e.md': NO_STATUS, 'f.md': CLEAN, }; const r = auditHeaders(Object.keys(map), reader(map)); assert.equal(r.aggregate.files, 6); const clean = r.files['f.md']; assert.equal(clean.verified, null); assert.equal(clean.title, true); assert.equal(clean.status, true); assert.equal(clean.lastUpdatedEnglish, true); // a/c/d carry a non-date verified; b is the only clean date assert.equal(r.aggregate.verifiedPresent, 4); assert.equal(r.aggregate.verifiedNonDate, 3); assert.equal(r.aggregate.verifiedDate, 1); // e has no Status; d's pipe row carries no Status either assert.equal(r.aggregate.missingStatus, 2); // a (Sist oppdatert) and d (pipe row, no English Last updated) lack the English field assert.equal(r.aggregate.missingEnglishLastUpdated, 2); assert.equal(r.aggregate.missingTitle, 0); }); test('auditHeaders — plain (non-bold) Verified pipe-tail is surfaced, though bold RE_VERIFIED is blind to it', () => { const r = auditHeaders(['p.md'], reader({ 'p.md': V_PLAIN_PIPE })); const f = r.files['p.md']; assert.equal(f.verified, null, 'bold-only RE_VERIFIED does not see the plain tail (M4)'); assert.equal(f.plainVerified, true, 'the plain tail IS surfaced by the new signal'); assert.equal(r.aggregate.plainVerifiedPipe, 1); }); test('auditHeaders — a bold **Verified:** is NOT counted as plainVerified', () => { // both the header-bold and pipe-bold fixtures must leave plainVerified false const r = auditHeaders(['a.md', 'd.md'], reader({ 'a.md': V_NONDATE, 'd.md': V_PIPE })); assert.equal(r.files['a.md'].plainVerified, false); assert.equal(r.files['d.md'].plainVerified, false); assert.equal(r.aggregate.plainVerifiedPipe, 0); }); test('auditHeaders — a clean file with no Verified at all has plainVerified false', () => { const r = auditHeaders(['f.md'], reader({ 'f.md': CLEAN })); assert.equal(r.files['f.md'].plainVerified, false); assert.equal(r.aggregate.plainVerifiedPipe, 0); }); test('auditHeaders — an unreadable file is reported, not thrown', () => { const r = auditHeaders(['x.md'], () => { throw new Error('ENOENT'); }); assert.equal(r.files['x.md'].error !== undefined, true); assert.equal(r.aggregate.files, 1); assert.equal(r.aggregate.readErrors, 1); }); // ── RX-KB1b: footer date-conflict + label-whitelist ──────────────────────── // A file that claims one **Last updated:** date in the header and a DIFFERENT date // under a norsk "Sist oppdatert" footer contradicts itself. Only the last-updated- // EQUIVALENT label ("Sist oppdatert") is conflict-bearing; "Verifisert"/"Sist // verifisert" record an independent axis (when content was checked vs source) and are // whitelisted. Detection is read-only and flag-to-human — the audit never mutates. // header 2026-06-24, italic footer "Sist oppdatert: 2026-02" — the // stakeholder-communication-ai-decisions.md shape (genuine self-contradiction). const FOOTER_CONFLICT = '# T\n\n**Category:** x\n**Status:** GA\n**Last updated:** 2026-06-24\n\n---\n\n' + '## A\n\ntekst\n\n*Sist oppdatert: 2026-02 av Cosmo Skyberg (AI Architect Plugin)*\n'; // header and footer agree on the month — no conflict. const FOOTER_MATCH = '# T\n\n**Category:** x\n**Status:** GA\n**Last updated:** 2026-02-03\n\n---\n\n' + '## A\n\ntekst\n\n**Sist oppdatert:** 2026-02-03\n'; // footer carries a DIFFERENT date but under the whitelisted "Sist verifisert" label — // an independent provenance axis, NOT a conflict. const FOOTER_VERIFISERT_INDEP = '# T\n\n**Category:** x\n**Status:** GA\n**Last updated:** 2026-06-24\n\n---\n\n' + '## A\n\ntekst\n\n**Sist verifisert:** 2026-02-04\n'; // placeholder footer ([Dato]) is template scaffolding, not a real date — never a conflict. const FOOTER_PLACEHOLDER = '# T\n\n**Category:** x\n**Status:** GA\n**Last updated:** 2026-06-24\n\n---\n\n' + '## A\n\n## Versjon og godkjenning\n- **Sist oppdatert:** [Dato]\n'; test('auditHeaders — footer "Sist oppdatert" date ≠ header Last updated is flagged as a conflict', () => { const r = auditHeaders(['a.md'], reader({ 'a.md': FOOTER_CONFLICT })); const f = r.files['a.md']; assert.ok(f.footerConflict, 'conflict object present'); assert.equal(f.footerConflict.headerLastUpdated, '2026-06-24'); assert.deepEqual(f.footerConflict.footerDates, ['2026-02']); assert.equal(r.aggregate.footerConflicts, 1); }); test('auditHeaders — footer "Sist oppdatert" matching the header month raises no conflict', () => { const r = auditHeaders(['b.md'], reader({ 'b.md': FOOTER_MATCH })); assert.equal(r.files['b.md'].footerConflict, null); assert.equal(r.aggregate.footerConflicts, 0); }); test('auditHeaders — a differing "Sist verifisert" footer is whitelisted, not a conflict', () => { const r = auditHeaders(['c.md'], reader({ 'c.md': FOOTER_VERIFISERT_INDEP })); assert.equal(r.files['c.md'].footerConflict, null); assert.equal(r.aggregate.footerConflicts, 0); }); test('auditHeaders — a placeholder "[Dato]" footer never counts as a conflict', () => { const r = auditHeaders(['d.md'], reader({ 'd.md': FOOTER_PLACEHOLDER })); assert.equal(r.files['d.md'].footerConflict, null); assert.equal(r.aggregate.footerConflicts, 0); }); test('auditHeaders — footer conflict needs BOTH a header date and a norsk footer date', () => { // V_NONDATE has a norsk "Sist oppdatert" but no English header Last updated → no conflict. // V_DATE has an English header date but no norsk footer → no conflict. const r = auditHeaders(['a.md', 'b.md'], reader({ 'a.md': V_NONDATE, 'b.md': V_DATE })); assert.equal(r.files['a.md'].footerConflict, null); assert.equal(r.files['b.md'].footerConflict, null); assert.equal(r.aggregate.footerConflicts, 0); }); // ── RX-HDR (dissolved → permanent audit signal): R7-unstampable detection ─── // RX-HDR was replanned as a 10-file header-slanking unit. Premiss-verification against // ground truth (simulating the real R7 stamper insertVerifiedFields over all 327 non- // advisor files, 2026-07-17) collapsed the population to 1: only headers so byte-dense // that BOTH stamps (**Verified:** + **Verified by:**) cannot fit inside the 500-byte // window are truly "passing-men-ustempelbar". Field-reordering alone cannot fix such a // file (its meta lines carry fat prose tails — a VALUE-slim is required, out of RX-HDR's // scope), so the unit dissolved into R7's existing "header-slanking kreves" flag path. // This signal makes the population permanent so it can never silently grow: the audit // runs the exact stamper R7 uses (representative stamp date + judge-v3.1) and flags any // file it would reject. [[gap-discipline-must-close]] — the lint signal IS the mechanism. // A header so dense that inserting Verified + Verified by lands past 500B — the // gpt5-gpt41-pricing-models.md shape (a ~175B **Status:** prose tail). `---` sits at // byte 500, so the two stamps (~53B) cannot fit however the meta lines are ordered. const UNSTAMPABLE = '# T\n\n**Last updated:** 2026-06\n**Status:** ' + 'GA '.repeat(140).trim() + '\n**Category:** x\n**Type:** reference\n\n---\n\n## A\n\ntekst\n'; test('auditHeaders — a header too dense for R7 to stamp within 500B is flagged unstampable', () => { const r = auditHeaders(['u.md'], reader({ 'u.md': UNSTAMPABLE })); assert.equal(r.files['u.md'].stampable, false, 'dense header is not R7-stampable'); assert.equal(r.aggregate.unstampable, 1); }); test('auditHeaders — a conformant header is stampable and not counted unstampable', () => { const r = auditHeaders(['f.md'], reader({ 'f.md': CLEAN })); assert.equal(r.files['f.md'].stampable, true, 'clean header is R7-stampable'); assert.equal(r.aggregate.unstampable, 0); });