// tests/kb-update/test-kb-headers.test.mjs // Unit tests for scripts/kb-update/lib/kb-headers.mjs — parses the optional // **Source:** / **Primary source:** authority header from a KB reference file. // Coverage is 0% today (no file declares one yet); the parser is wired into // build-registry so authority_source can populate as files adopt the header. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { parseSourceHeader } from '../../scripts/kb-update/lib/kb-headers.mjs'; test('parseSourceHeader — **Source:** header', () => { const content = '# Title\n\n**Source:** https://learn.microsoft.com/azure/foundry/concepts/x\n**Status:** GA\n'; assert.equal( parseSourceHeader(content), 'https://learn.microsoft.com/azure/foundry/concepts/x' ); }); test('parseSourceHeader — **Primary source:** alias', () => { const content = '# Title\n\n**Primary source:** https://learn.microsoft.com/azure/ai-services/y\n'; assert.equal( parseSourceHeader(content), 'https://learn.microsoft.com/azure/ai-services/y' ); }); test('parseSourceHeader — case-insensitive label', () => { const content = '**source:** https://learn.microsoft.com/azure/z\n'; assert.equal(parseSourceHeader(content), 'https://learn.microsoft.com/azure/z'); }); test('parseSourceHeader — no header returns null', () => { const content = '# Title\n\n**Last updated:** 2026-04\n**Status:** GA\n**Category:** RAG\n'; assert.equal(parseSourceHeader(content), null); }); test('parseSourceHeader — empty/garbage input returns null', () => { assert.equal(parseSourceHeader(''), null); assert.equal(parseSourceHeader(null), null); assert.equal(parseSourceHeader(undefined), null); }); test('parseSourceHeader — only scans the top of the file (header region)', () => { // A Source-like line buried far below the header region must NOT be picked up. const buried = '# Title\n' + 'x'.repeat(800) + '\n**Source:** https://learn.microsoft.com/late\n'; assert.equal(parseSourceHeader(buried), null); }); test('parseSourceHeader — trims trailing whitespace from URL', () => { const content = '**Source:** https://learn.microsoft.com/azure/trim \n'; assert.equal(parseSourceHeader(content), 'https://learn.microsoft.com/azure/trim'); });