ms-ai-architect/tests/kb-update/test-audit-corpus-headers.test.mjs
Kjell Tore Guttormsen 712a143e58 fix(ms-ai-architect): RX-KB1 strip stale plain-Verified pipe-tails (87) + audit-deteksjon [skip-docs]
De 87 referansefilene bar en plain-text `| Verified: <dato>`-hale på **Last updated:**-linjen
i 500B-header-vinduet — usynlig for den bold-only kontrakt-stacken (kb-headers.mjs / audit
RE_VERIFIED), og claimet en verifisering judgen aldri gjorde (samme poison-klasse som de 14
bold **Verified:** MCP Spor 1 fjernet). Uhåndtert springer den også dual-Verified-fellen: R7s
insertVerifiedFields ville stemplet en bold-verdi ved siden av den plain → to motstridende
provenance-claims per fil.

- ny driver strip-stale-verified-pipe.mjs: frosset 87-manifest (18 advisor + 45 eng + 8 gov +
  16 sec), pure verdi-bevarende strip (kun ` | Verified: …`-halen; **Last updated:**-dato
  byte-eksakt), hard per-fil-invariant (linjeantall uendret, body byte-identisk, dato bevart),
  idempotent, atomicWriteSync (RX-OPS2 recovery-kontrakt).
- audit-corpus-headers.mjs: ny plain-Verified-deteksjon (RE_PLAIN_VERIFIED + plainVerifiedPipe)
  — gjør M4-blindheten synlig så en stale plain-hale ikke kan gjenoppstå stille (non-advisor scope).
- 87 filer strippet; plain Verified i vinduet 0/389; live-audit plainVerifiedPipe 0.

Mekanisme: +15 tester (12 strip + 3 audit). Suite 875→890 exit 0. validate-plugin.sh 250/0.

Utsatt → RX-KB1b: footer-dato-avvik + label-whitelist (annen dialekt, flag-to-human).
2026-07-16 20:04:52 +02:00

165 lines
7.2 KiB
JavaScript

// 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 <date>", 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);
});