import test from 'node:test'; import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import os from 'node:os'; import path from 'node:path'; const cli = path.join(import.meta.dirname, '..', 'src', 'cli.ts'); const SCHEMAS = { 'requirement.schema.json': { $id: 'https://baseline-harness.dev/spec/schemas/requirement.schema.json', type: 'object', required: ['id', 'title', 'level', 'domain', 'capability', 'lifecycle'], properties: { id: { type: 'string', pattern: '^req-[a-z0-9-]+$' }, title: { type: 'string' }, level: { enum: ['MUST', 'MUST NOT', 'SHOULD', 'SHOULD NOT', 'MAY'] }, domain: { type: 'string' }, capability: { type: 'string' }, lifecycle: { type: 'object' }, parameters: { type: 'array' }, }, }, 'parameter.schema.json': { $id: 'https://baseline-harness.dev/spec/schemas/parameter.schema.json', type: 'object', required: ['id', 'type'], properties: { id: { type: 'string', pattern: '^param-[a-z0-9-]+$' } }, }, 'profile.schema.json': { $id: 'https://baseline-harness.dev/spec/schemas/profile.schema.json', type: 'object', required: ['id', 'requirements'], properties: { id: { type: 'string', pattern: '^prof-[a-z0-9-]+$' } }, }, 'project-exception.schema.json': { $id: 'https://baseline-harness.dev/spec/schemas/project-exception.schema.json', type: 'object', required: ['id', 'requirement', 'scope', 'reason', 'decision_ref', 'review_condition'], properties: { id: { type: 'string', pattern: '^ex-[a-z0-9-]+$' }, requirement: { type: 'string', pattern: '^req-[a-z0-9-]+$' }, scope: { type: 'object' }, reason: { type: 'string' }, decision_ref: { type: 'string', minLength: 3 }, review_condition: { type: 'string' }, }, }, 'project-manifest.schema.json': { $id: 'https://baseline-harness.dev/spec/schemas/project-manifest.schema.json', type: 'object', required: ['project', 'spec_version', 'profiles'], properties: { project: { type: 'string' }, spec_version: { type: 'string', pattern: '^\\d+\\.\\d+\\.\\d+(-[0-9A-Za-z.-]+)?$' }, profiles: { type: 'array', items: { type: 'string' } }, parameter_overrides: { type: 'array' }, exceptions: { type: 'array', items: { $ref: 'https://baseline-harness.dev/spec/schemas/project-exception.schema.json' }, }, }, }, }; const EXCEPTION = ' - id: ex-fixture-budget\n requirement: req-alpha\n scope:\n kind: path\n paths: [src/]\n description: Область исключения для тестов манифеста.\n reason: Параметр недоступен в тестовой фикстуре без исключения.\n decision_ref: D-099\n review_condition: Пересмотр при появлении параметра в фикстуре.\n'; const BASE_FILES = { 'requirements/req-alpha.yaml': 'id: req-alpha\ntitle: Альфа\ndomain: dom-build\ncapability: cap-verify\nlevel: MUST\nlifecycle:\n status: proposed\nparameters:\n - id: param-alpha-budget\n type: integer\n', 'requirements/req-beta.yaml': 'id: req-beta\ntitle: Бета\ndomain: dom-docs\ncapability: cap-write\nlevel: SHOULD\nlifecycle:\n status: proposed\n', 'profiles/prof-fixture.yaml': 'id: prof-fixture\nrequirements:\n - requirement: req-alpha\n', 'parameters/param-registry-mode.yaml': 'id: param-registry-mode\ntype: enum\n', 'manifest.yaml': `project: fixture\nspec_version: 0.1.0\nprofiles: [prof-fixture]\nparameter_overrides:\n - parameter: param-registry-mode\n value: json\nexceptions:\n${EXCEPTION}`, }; function fixture(t, overrides = {}) { const root = mkdtempSync(path.join(os.tmpdir(), 'bhl-spec-')); const files = { ...BASE_FILES, ...overrides }; mkdirSync(path.join(root, 'schemas'), { recursive: true }); for (const [name, schema] of Object.entries(SCHEMAS)) { writeFileSync(path.join(root, 'schemas', name), JSON.stringify(schema), 'utf8'); } for (const [name, contents] of Object.entries(files)) { const target = path.join(root, name); mkdirSync(path.dirname(target), { recursive: true }); writeFileSync(target, contents, 'utf8'); } t.after(() => rmSync(root, { recursive: true, force: true })); return root; } function runCli(...args) { const proc = spawnSync(process.execPath, [cli, ...args], { encoding: 'utf8' }); return { code: proc.status, envelope: JSON.parse(proc.stdout) }; } function codes(result) { return result.envelope.data.errors.map((item) => item.code); } test('spec list: сортированный список и фильтр по возможности', (t) => { const root = fixture(t); const all = runCli('spec', 'list', '--spec-root', root); assert.equal(all.code, 0); assert.deepEqual( all.envelope.data.requirements.map((item) => [ item.id, item.level, item.lifecycle, item.capability, ]), [ ['req-alpha', 'MUST', 'proposed', 'cap-verify'], ['req-beta', 'SHOULD', 'proposed', 'cap-write'], ], ); const filtered = runCli('spec', 'list', '--spec-root', root, '--capability', 'cap-write'); assert.deepEqual( filtered.envelope.data.requirements.map((item) => item.id), ['req-beta'], ); }); test('spec show: полный документ и NOT_FOUND для неизвестного ID', (t) => { const root = fixture(t); const shown = runCli('spec', 'show', 'req-alpha', '--spec-root', root); assert.equal(shown.code, 0); assert.equal(shown.envelope.data.requirement.title, 'Альфа'); const ghost = runCli('spec', 'show', 'req-ghost', '--spec-root', root); assert.equal(ghost.code, 1); assert.equal(ghost.envelope.error.code, 'NOT_FOUND'); }); test('manifest validate: валидный манифест проходит', (t) => { const root = fixture(t); const result = runCli( 'manifest', 'validate', path.join(root, 'manifest.yaml'), '--spec-root', root, ); assert.equal(result.code, 0); assert.deepEqual(result.envelope.data.errors, []); }); test('manifest validate: висячие ссылки и неутверждённое исключение падают', (t) => { const ghostProfile = fixture(t, { 'manifest.yaml': BASE_FILES['manifest.yaml'].replace('prof-fixture]', 'prof-ghost]'), }); assert.ok( codes( runCli( 'manifest', 'validate', path.join(ghostProfile, 'manifest.yaml'), '--spec-root', ghostProfile, ), ).includes('unknown-profile'), ); const ghostParam = fixture(t, { 'manifest.yaml': BASE_FILES['manifest.yaml'].replace('param-registry-mode', 'param-ghost'), }); assert.ok( codes( runCli( 'manifest', 'validate', path.join(ghostParam, 'manifest.yaml'), '--spec-root', ghostParam, ), ).includes('unknown-parameter'), ); const ghostReq = fixture(t, { 'manifest.yaml': BASE_FILES['manifest.yaml'].replace('req-alpha\n', 'req-ghost\n'), }); assert.ok( codes( runCli('manifest', 'validate', path.join(ghostReq, 'manifest.yaml'), '--spec-root', ghostReq), ).includes('unknown-requirement'), ); const unapproved = fixture(t, { 'manifest.yaml': BASE_FILES['manifest.yaml'].replace('decision_ref: D-099', 'decision_ref: ""'), }); assert.ok( codes( runCli( 'manifest', 'validate', path.join(unapproved, 'manifest.yaml'), '--spec-root', unapproved, ), ).includes('schema-error'), ); }); test('использование команд spec и manifest', () => { assert.equal(runCli('spec').code, 2); assert.equal(runCli('spec', 'show').code, 2); assert.equal(runCli('manifest').code, 2); assert.equal(runCli('manifest', 'validate').code, 2); });