import test from 'node:test'; import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; import { readFileSync, readdirSync } from 'node:fs'; import path from 'node:path'; const repoRoot = path.dirname(import.meta.dirname); const cli = path.join(repoRoot, 'packages', 'bhl', 'src', 'cli.ts'); const specRoot = path.join(repoRoot, 'examples', 'synthetic-spec'); function runCli(...args) { const proc = spawnSync(process.execPath, [cli, ...args], { encoding: 'utf8', cwd: repoRoot }); return { code: proc.status, stdout: proc.stdout, envelope: JSON.parse(proc.stdout) }; } test('синтетическая спецификация проходит bhl validate без диагностик', () => { const result = runCli('validate', specRoot); assert.equal(result.code, 0); assert.deepEqual(result.envelope.data.errors, []); assert.equal(result.envelope.data.files, 8); }); test('пресет разворачивается в Effective Specification со всеми связями', () => { const result = runCli('profile', 'resolve', specRoot, '--preset', 'preset-synth-all'); assert.equal(result.code, 0); const { profiles, effective } = result.envelope.data; assert.deepEqual(profiles, ['prof-synth-light', 'prof-synth-strict']); assert.deepEqual( effective.map((item) => [item.requirement, item.level, item.parameters]), [ ['req-synth-alpha', 'SHOULD', [{ parameter: 'param-synth-budget', value: 4, source: 'prof-synth-light' }]], ['req-synth-beta', 'SHOULD', [{ parameter: 'param-synth-mode', value: 'plain', source: 'prof-synth-strict' }]], ['req-synth-gamma', 'MUST NOT', []], ], ); const gamma = effective.find((item) => item.requirement === 'req-synth-gamma'); assert.deepEqual(gamma.sources, ['prof-synth-strict']); }); test('разрешение пресета не зависит от порядка явных профилей', () => { const first = runCli('profile', 'resolve', specRoot, '--profile', 'prof-synth-light', '--profile', 'prof-synth-strict'); const second = runCli('profile', 'resolve', specRoot, '--profile', 'prof-synth-strict', '--profile', 'prof-synth-light'); assert.equal(first.code, 0); assert.equal(second.stdout, first.stdout); }); test('копия схем в примере синхронна с spec/schemas', () => { const canonical = new Set(readdirSync(path.join(repoRoot, 'spec', 'schemas'))); const copy = new Set(readdirSync(path.join(specRoot, 'schemas'))); assert.deepEqual([...copy].sort(), [...canonical].sort()); for (const name of canonical) { assert.equal( readFileSync(path.join(specRoot, 'schemas', name), 'utf8'), readFileSync(path.join(repoRoot, 'spec', 'schemas', name), 'utf8'), `расхождение копии схемы ${name}`, ); } }); test('пример явно отмечен как инфраструктурный, а не BHS 0.1.0', () => { const readme = readFileSync(path.join(specRoot, 'README.md'), 'utf8'); assert.ok(readme.includes('не является'), 'отметка о не-стандартности'); assert.ok(readme.includes('BHS 0.1.0'), 'ссылка на содержательную редакцию'); }); test('spec list и spec show работают на синтетической спецификации', () => { const list = runCli('spec', 'list', '--spec-root', specRoot); assert.equal(list.code, 0); assert.deepEqual( list.envelope.data.requirements.map((item) => item.id), ['req-synth-alpha', 'req-synth-beta', 'req-synth-gamma'], ); const byDomain = runCli('spec', 'list', '--spec-root', specRoot, '--domain', 'dom-synth-build'); assert.equal(byDomain.envelope.data.requirements.length, 3); const shown = runCli('spec', 'show', 'req-synth-alpha', '--spec-root', specRoot); assert.equal(shown.code, 0); assert.equal(shown.envelope.data.requirement.level, 'MUST'); const ghost = runCli('spec', 'show', 'req-ghost', '--spec-root', specRoot); assert.equal(ghost.code, 1); assert.equal(ghost.envelope.error.code, 'NOT_FOUND'); });