okr/tests/okf-check.test.mjs
2026-06-26 15:12:48 +02:00

137 lines
5.1 KiB
JavaScript

// okf-check.test.mjs
// Tester OKF-mekanismen (Step 6): okf-index genererer index.md per nivaa i ekte
// OKF-form, og okf-check validerer at hver konsept-fil baerer `type:`.
// okf-index kjoeres mot en TEMP-KOPI av okf-realistic (cpSync) saa de committede
// fixturene (lest read-only av okf-retrieval.test.mjs, m/ en bevisst dangling-link)
// forblir uroert. okf-check kjoeres som subprosess for aa fange exit-koden (kontrakt).
// Zero npm deps. Plassert i tests/ (samme moenster som org-profile-write.test.mjs).
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import {
mkdtempSync, cpSync, writeFileSync, readFileSync, existsSync, readdirSync, rmSync,
} from 'node:fs';
import { tmpdir } from 'node:os';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { generateIndexes } from '../scripts/okf-index.mjs';
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
const CHECK = join(ROOT, 'scripts', 'okf-check.mjs');
const REALISTIC = join(ROOT, 'tests', 'fixtures', 'okf-realistic');
function tmpRoot() {
return mkdtempSync(join(tmpdir(), 'okf-'));
}
// Kjoer okf-check som subprosess; fang non-zero exit (execFileSync kaster da).
function runCheck(root) {
try {
const stdout = execFileSync('node', [CHECK, root], { encoding: 'utf8' });
return { status: 0, stdout };
} catch (e) {
return { status: e.status ?? 1, stdout: `${e.stdout || ''}${e.stderr || ''}` };
}
}
// Alle kataloger under root (inkl. root selv), rekursivt.
function allDirs(root) {
const out = [root];
for (const e of readdirSync(root, { withFileTypes: true })) {
if (e.isDirectory()) out.push(...allDirs(join(root, e.name)));
}
return out;
}
// --- okf-index ---
test('okf-index: genererer index.md per nivaa (hver katalog) i temp-kopi', () => {
const dir = tmpRoot();
try {
cpSync(REALISTIC, dir, { recursive: true });
generateIndexes(dir);
for (const d of allDirs(dir)) {
assert.ok(existsSync(join(d, 'index.md')), `index.md skal finnes i ${d}`);
}
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test('okf-index: entry-linjer matcher OKF-form `* [t](l) - d`, uten frontmatter', () => {
const dir = tmpRoot();
try {
cpSync(REALISTIC, dir, { recursive: true });
generateIndexes(dir);
const sk = readFileSync(join(dir, 'strategisk-kontekst', 'index.md'), 'utf8');
assert.ok(!sk.startsWith('---'), 'index.md skal IKKE ha frontmatter');
const entries = sk.split('\n').filter((l) => l.startsWith('* '));
assert.ok(entries.length >= 3, 'strategisk-kontekst skal liste sine 3 konsept-filer');
for (const l of entries) {
assert.match(l, /^\* \[[^\]]+\]\([^)]+\) - .+$/, `OKF-form: ${l}`);
}
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test('okf-index: rot-index baerer okf_version, undernivaa gjoer ikke', () => {
const dir = tmpRoot();
try {
cpSync(REALISTIC, dir, { recursive: true });
generateIndexes(dir);
const root = readFileSync(join(dir, 'index.md'), 'utf8');
assert.match(root, /^okf_version:\s*\S+/m, 'rot-index skal ha okf_version');
const sub = readFileSync(join(dir, 'strategisk-kontekst', 'index.md'), 'utf8');
assert.ok(!/^okf_version:/m.test(sub), 'undernivaa skal IKKE ha okf_version');
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test('okf-index: konsept-tittel/beskrivelse hentes fra frontmatter', () => {
const dir = tmpRoot();
try {
cpSync(REALISTIC, dir, { recursive: true });
generateIndexes(dir);
const sk = readFileSync(join(dir, 'strategisk-kontekst', 'index.md'), 'utf8');
assert.match(
sk,
/\* \[Tildelingsbrev 2026\]\(tildelingsbrev-2026\.md\) - Departementets/,
'entry skal bruke frontmatter-title + -description + lenke til konsept-fil',
);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
// --- okf-check ---
test('okf-check: gyldig bundle (alle konsept har type:) -> exit 0, «0 filer uten type:»', () => {
const { status, stdout } = runCheck(REALISTIC);
assert.equal(status, 0, 'gyldig bundle skal gi exit 0');
assert.match(stdout, /0 filer uten type:/, 'skal rapportere null manglende type');
});
test('okf-check: konsept-fil uten type: -> exit != 0 + teller > 0 + navngir filen', () => {
const dir = tmpRoot();
try {
cpSync(REALISTIC, dir, { recursive: true });
writeFileSync(
join(dir, 'strategisk-kontekst', 'mangler-type.md'),
'---\ntitle: Uten type\ndescription: En konsept-fil uten paakrevd type.\n---\n# Uten type\n',
);
const { status, stdout } = runCheck(dir);
assert.notEqual(status, 0, 'type-loes fil skal gi exit != 0');
assert.match(stdout, /[1-9]\d* filer uten type:/, 'teller skal vaere > 0');
assert.match(stdout, /mangler-type\.md/, 'skal navngi den feilende filen');
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test('okf-check: rapporterer okf_version fra rot-index', () => {
const { stdout } = runCheck(REALISTIC);
assert.match(stdout, /okf_version:\s*kb-layout-2026-06/, 'skal ekko okf_version for menneskelig sammenligning');
});