// tests/kb-update/test-dedup-dato.test.mjs // TDD for Enhet 3b — the dual-**Dato:** dedup on the 5 mlops-genaiops files that carry BOTH a // bold **Dato:** (creation) AND a bold **Last updated:** (modification). Ground truth 2026-07-16 // (re-measured against the committed corpus + the git first-commit "Created-map"): // 4 files carry a DISTINCT creation date (**Dato:** 2026-02-04, authored BEFORE the git // first-commit 2026-04-08 → a genuine creation date) → relabel **Dato:** → **Created:**, // value byte-preserved (the same norsk→english label-rename class as Enhet 1/2). // 1 file (mlops-security-access-control) carries IDENTICAL dates (**Dato:**=**Last updated:**= // 2026-06-19, both AFTER the git first-commit → NOT a genuine creation date, a metadata-pass // artifact) → drop the redundant **Dato:**; relabeling it would assert a FALSE creation date. // // This is never a derive-a-value backfill and never an unproven delete: the drop primitive throws // unless a bold proof field with the IDENTICAL value exists. One new pure primitive // (dropRedundantBoldField) + the reused relabelHeaderDialect (CREATED_MAP) are pinned here, plus // a corpus-state guard (the gap-discipline lock) over the 5 committed files. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import { dropRedundantBoldField } from '../../scripts/kb-update/lib/transform.mjs'; import { relabelHeaderDialect } from '../../scripts/kb-update/relabel-dialect.mjs'; import { MANIFEST, CREATED_MAP, applyOp } from '../../scripts/kb-update/dedup-dato.mjs'; const __dirname = dirname(fileURLToPath(import.meta.url)); const PLUGIN_ROOT = join(__dirname, '..', '..'); const DIR = 'skills/ms-ai-engineering/references/mlops-genaiops'; // A dual-**Dato:** fixture: bold Category, bold **Dato:** (creation) + bold **Last updated:** // (modification) with DISTINCT values, then a body that mentions "Dato:" in prose (never touched). const FIXTURE = [ '# Feedback Loops and Continuous Improvement', '', '**Category:** MLOps & GenAIOps', '**Dato:** 2026-02-04', '**Last updated:** 2026-06-24', '**Status:** Established Practice', '', '## Innhold', '', 'A body line mentioning **Dato:** [YYYY-MM-DD] in a template — must be left alone.', '', ].join('\n'); // mlops-security shape: IDENTICAL **Dato:** and **Last updated:** → the drop case. const FIXTURE_IDENTICAL = [ '# Security and Access Control in MLOps', '', '**Category:** MLOps & GenAIOps', '**Last updated:** 2026-06-19', '**Dato:** 2026-06-19', '**Status:** Established Practice', '', '---', '', ].join('\n'); // ── dropRedundantBoldField ─────────────────────────────────────────────────── test('dropRedundantBoldField: drops **Dato:** when a bold **Last updated:** with the same value proves it redundant', () => { const out = dropRedundantBoldField(FIXTURE_IDENTICAL, 'Dato', 'Last updated'); const header = out.split('\n---')[0]; assert.ok(/\*\*Last updated:\*\* 2026-06-19/.test(header), 'proof field kept'); assert.ok(!/\*\*Dato:\*\*/.test(header), 'redundant **Dato:** removed'); assert.equal(out.split('\n').length, FIXTURE_IDENTICAL.split('\n').length - 1, 'exactly one line removed'); }); test('dropRedundantBoldField: throws when the drop and proof values differ (not redundant, human decision)', () => { assert.throws(() => dropRedundantBoldField(FIXTURE, 'Dato', 'Last updated'), /not redundant|≠/); }); test('dropRedundantBoldField: throws when no proof field exists to prove redundancy', () => { const noProof = FIXTURE_IDENTICAL.replace('**Last updated:** 2026-06-19\n', ''); assert.throws(() => dropRedundantBoldField(noProof, 'Dato', 'Last updated'), /no bold/i); }); test('dropRedundantBoldField: no-op when the drop field is absent (idempotent)', () => { const once = dropRedundantBoldField(FIXTURE_IDENTICAL, 'Dato', 'Last updated'); assert.equal(dropRedundantBoldField(once, 'Dato', 'Last updated'), once); }); test('dropRedundantBoldField: never touches a **Dato:** in the body (below the header region)', () => { // FIXTURE has a distinct-value dual header (would throw) so build an identical-value variant // that ALSO carries a body **Dato:** template line — only the header one may be removed. const withBody = FIXTURE_IDENTICAL.replace('---', '## Innhold\n\n**Dato:** [YYYY-MM-DD] template\n\n---'); const out = dropRedundantBoldField(withBody, 'Dato', 'Last updated'); assert.ok(out.includes('**Dato:** [YYYY-MM-DD] template'), 'body template **Dato:** untouched'); assert.ok(!/\*\*Dato:\*\*/.test(out.split('## Innhold')[0]), 'header **Dato:** removed'); }); test('dropRedundantBoldField: blank labels throw', () => { assert.throws(() => dropRedundantBoldField(FIXTURE_IDENTICAL, '', 'Last updated'), /required/); assert.throws(() => dropRedundantBoldField(FIXTURE_IDENTICAL, 'Dato', ''), /required/); }); // ── relabel **Dato:** → **Created:** (reused relabelHeaderDialect + CREATED_MAP) ────────────── test('CREATED_MAP: relabels **Dato:** → **Created:**, value byte-preserved, one line, body identical', () => { const { content: out, applied } = relabelHeaderDialect(FIXTURE, CREATED_MAP); assert.equal(applied.length, 1); assert.ok(/\*\*Created:\*\* 2026-02-04/.test(out), 'Created line present with preserved value'); assert.ok(!/\*\*Dato:\*\*/.test(out.split('## Innhold')[0]), 'header **Dato:** gone'); const bodyOf = (c) => c.slice(c.indexOf('## Innhold')); assert.equal(bodyOf(out), bodyOf(FIXTURE), 'body byte-identical (template **Dato:** untouched)'); assert.equal(out.split('\n').length, FIXTURE.split('\n').length, 'line count unchanged'); }); test('CREATED_MAP: idempotent — a file with no **Dato:** in header is unchanged', () => { const { content: once } = relabelHeaderDialect(FIXTURE, CREATED_MAP); const { content: twice, applied } = relabelHeaderDialect(once, CREATED_MAP); assert.equal(twice, once); assert.equal(applied.length, 0); }); // ── applyOp dispatch ───────────────────────────────────────────────────────── test('applyOp: relabel dispatches to Created relabel', () => { assert.ok(/\*\*Created:\*\* 2026-02-04/.test(applyOp(FIXTURE, 'relabel'))); }); test('applyOp: drop dispatches to redundant-bold drop', () => { const out = applyOp(FIXTURE_IDENTICAL, 'drop'); assert.ok(!/\*\*Dato:\*\*/.test(out.split('\n---')[0])); }); test('applyOp: unknown action throws', () => { assert.throws(() => applyOp(FIXTURE, 'nonsense'), /unknown action/i); }); // ── MANIFEST ───────────────────────────────────────────────────────────────── test('MANIFEST: exactly the 5 dual-**Dato:** mlops-genaiops files, 4 relabel + 1 drop', () => { assert.equal(MANIFEST.length, 5); assert.equal(new Set(MANIFEST.map((m) => m.rel)).size, 5, 'no duplicate paths'); const relabels = MANIFEST.filter((m) => m.action === 'relabel'); const drops = MANIFEST.filter((m) => m.action === 'drop'); assert.equal(relabels.length, 4); assert.equal(drops.length, 1); assert.equal(drops[0].rel, `${DIR}/mlops-security-access-control.md`, 'the identical-date file is the drop'); for (const r of relabels) { assert.match(r.created, /^\d{4}-\d{2}-\d{2}$/, `${r.rel}: frozen created date must be YYYY-MM-DD`); } const expected = [ `${DIR}/feedback-loops-continuous-improvement.md`, `${DIR}/genaiops-llm-specific-practices.md`, `${DIR}/model-deployment-strategies-azure.md`, `${DIR}/prompt-flow-production-deployment.md`, `${DIR}/mlops-security-access-control.md`, ]; for (const p of expected) assert.ok(MANIFEST.some((m) => m.rel === p), `missing ${p}`); }); // ── Corpus-state guard (gap-discipline lock over the committed files) ───────── test('corpus guard: the 4 distinct files carry **Created:** with the frozen date and no **Dato:** in the header', () => { for (const m of MANIFEST.filter((x) => x.action === 'relabel')) { const content = readFileSync(join(PLUGIN_ROOT, m.rel), 'utf8'); const header = content.split(/\n(?:---|## )/)[0]; assert.match(header, new RegExp(`\\*\\*Created:\\*\\* ${m.created}`), `${m.rel}: missing **Created:** ${m.created}`); assert.ok(!/\*\*Dato:\*\*/.test(header), `${m.rel}: header still carries **Dato:**`); assert.match(header, /\*\*Last updated:\*\*/, `${m.rel}: **Last updated:** must survive`); } }); test('corpus guard: mlops-security-access-control has no **Dato:** and keeps **Last updated:** 2026-06-19', () => { const rel = `${DIR}/mlops-security-access-control.md`; const content = readFileSync(join(PLUGIN_ROOT, rel), 'utf8'); const header = content.split(/\n(?:---|## )/)[0]; assert.ok(!/\*\*Dato:\*\*/.test(header), 'redundant **Dato:** must be gone'); assert.ok(!/\*\*Created:\*\*/.test(header), 'no fabricated **Created:** (drop, not relabel)'); assert.match(header, /\*\*Last updated:\*\* 2026-06-19/, '**Last updated:** 2026-06-19 must survive'); });