136 lines
5.7 KiB
JavaScript
136 lines
5.7 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';
|
|
|
|
// 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 — 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);
|
|
});
|