import test from 'node:test'; import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'; import os from 'node:os'; import path from 'node:path'; const repoRoot = path.dirname(import.meta.dirname); const cli = path.join(repoRoot, 'packages', 'bhl', 'src', 'cli.ts'); const committed = path.join(repoRoot, 'docs', 'spec'); function renderTo(outDir) { const proc = spawnSync(process.execPath, [cli, 'spec', 'render', '--spec-root', 'spec', '--out', outDir], { encoding: 'utf8', cwd: repoRoot, }); assert.equal(proc.status, 0, proc.stdout); return JSON.parse(proc.stdout).data.files; } function collectFiles(dir, prefix = '') { const result = new Map(); for (const entry of readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) { const rel = prefix === '' ? entry.name : `${prefix}/${entry.name}`; if (entry.isDirectory()) { for (const [name, content] of collectFiles(path.join(dir, entry.name), rel)) { result.set(name, content); } } else { result.set(rel, readFileSync(path.join(dir, entry.name), 'utf8')); } } return result; } function tempDir() { return mkdtempSync(path.join(os.tmpdir(), 'bhl-docs-')); } test('сгенерированные страницы свежий: повторная генерация не создаёт изменений', (t) => { const first = tempDir(); const second = tempDir(); t.after(() => { rmSync(first, { recursive: true, force: true }); rmSync(second, { recursive: true, force: true }); }); const files = renderTo(first); renderTo(second); assert.deepEqual(collectFiles(second), collectFiles(first), 'повторная генерация идемпотентна'); assert.deepEqual(collectFiles(committed), collectFiles(first), 'страницы в Git совпадают со свежей генерацией'); assert.ok(files.includes('index.md')); }); test('gate отвергает устаревшие сгенерированные страницы', (t) => { const fresh = tempDir(); const stale = tempDir(); t.after(() => { rmSync(fresh, { recursive: true, force: true }); rmSync(stale, { recursive: true, force: true }); }); renderTo(fresh); renderTo(stale); const target = path.join(stale, 'index.md'); writeFileSync(target, `${readFileSync(target, 'utf8')}\nручная правка\n`, 'utf8'); assert.notDeepEqual(collectFiles(stale), collectFiles(fresh), 'расхождение обнаружено'); }); test('каждая сгенерированная страница помечена как не редактируемая вручную', () => { for (const [name, content] of collectFiles(committed)) { assert.ok(content.includes('Не редактировать вручную'), name); } }); test('docs/spec существует и содержит обзор спецификации', () => { const index = readFileSync(path.join(committed, 'index.md'), 'utf8'); assert.ok(index.includes('dom-verification-evidence'), 'домен в обзоре'); assert.ok(index.includes('req-green-not-proof'), 'таблица требований'); mkdirSync(committed, { recursive: true }); });