feat: каркас bhl и валидатор спецификации (BHL-020, BHL-011)
packages/bhl: @baseline-harness/bhl 0.0.1 — детерминированный CLI без LLM/оркестрации/сервера/БД; JSON-конверт и коды выхода 0/1/2/3/4 задокументированы и закреплены тестами. validate: Ajv 2020-12 по схемам <root>/schemas, семантическая сверка ID, ссылок, RFC-2119 слов и циклов depends-on. Местный строгий gate (prettier, eslint --max-warnings 0, tsc strict) зелёный: 10 тестов.
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
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 = {
|
||||
'domain.schema.json': {
|
||||
$id: 'https://baseline-harness.dev/spec/schemas/domain.schema.json',
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
required: ['id', 'title', 'description'],
|
||||
properties: {
|
||||
id: { type: 'string', pattern: '^dom-[a-z0-9-]+$' },
|
||||
title: { type: 'string' },
|
||||
description: { type: 'string' },
|
||||
},
|
||||
},
|
||||
'capability.schema.json': {
|
||||
$id: 'https://baseline-harness.dev/spec/schemas/capability.schema.json',
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
required: ['id', 'domain', 'title', 'description'],
|
||||
properties: {
|
||||
id: { type: 'string', pattern: '^cap-[a-z0-9-]+$' },
|
||||
domain: { type: 'string', pattern: '^dom-[a-z0-9-]+$' },
|
||||
title: { type: 'string' },
|
||||
description: { type: 'string' },
|
||||
},
|
||||
},
|
||||
'requirement.schema.json': {
|
||||
$id: 'https://baseline-harness.dev/spec/schemas/requirement.schema.json',
|
||||
type: 'object',
|
||||
required: ['id', 'domain', 'capability', 'statement', 'level'],
|
||||
properties: {
|
||||
id: { type: 'string', pattern: '^req-[a-z0-9-]+$' },
|
||||
domain: { type: 'string', pattern: '^dom-[a-z0-9-]+$' },
|
||||
capability: { type: 'string', pattern: '^cap-[a-z0-9-]+$' },
|
||||
statement: { type: 'string' },
|
||||
level: { enum: ['MUST', 'MUST NOT', 'SHOULD', 'SHOULD NOT', 'MAY'] },
|
||||
},
|
||||
},
|
||||
'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-]+$' },
|
||||
type: { enum: ['boolean', 'integer', 'number', 'string', 'enum'] },
|
||||
},
|
||||
},
|
||||
'normative-change.schema.json': {
|
||||
$id: 'https://baseline-harness.dev/spec/schemas/normative-change.schema.json',
|
||||
type: 'object',
|
||||
required: ['id', 'release', 'date', 'type', 'requirements', 'description'],
|
||||
properties: {
|
||||
id: { type: 'string', pattern: '^chg-[a-z0-9-]+$' },
|
||||
release: { type: 'string' },
|
||||
date: { type: 'string' },
|
||||
type: { enum: ['ADDED', 'STRENGTHENED', 'RELAXED', 'CLARIFIED', 'DEPRECATED', 'REMOVED'] },
|
||||
requirements: { type: 'array', items: { type: 'string' } },
|
||||
description: { type: 'string' },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const BASE_FILES = {
|
||||
'domains/dom-build.yaml':
|
||||
'id: dom-build\ntitle: Сборка\ndescription: Требования к сборке проекта.\n',
|
||||
'capabilities/cap-verify.yaml':
|
||||
'id: cap-verify\ndomain: dom-build\ntitle: Проверка\ndescription: Требования к проверкам.\n',
|
||||
'requirements/req-alpha.yaml':
|
||||
'id: req-alpha\ndomain: dom-build\ncapability: cap-verify\nstatement: Проект MUST проходить проверку.\nlevel: MUST\nrelations:\n - target: req-beta\n kind: depends-on\n',
|
||||
'requirements/req-beta.yaml':
|
||||
'id: req-beta\ndomain: dom-build\ncapability: cap-verify\nstatement: Проект SHOULD документировать шаги.\nlevel: SHOULD\n',
|
||||
'parameters/.gitkeep': '',
|
||||
'CHANGELOG.yaml': 'changes: []\n',
|
||||
};
|
||||
|
||||
function fixture(t, overrides = {}) {
|
||||
const root = mkdtempSync(path.join(os.tmpdir(), 'bhl-validate-'));
|
||||
const files = { ...BASE_FILES, ...overrides };
|
||||
delete files['parameters/.gitkeep'];
|
||||
mkdirSync(path.join(root, 'schemas'), { recursive: true });
|
||||
for (const name of Object.keys(SCHEMAS)) {
|
||||
writeFileSync(path.join(root, 'schemas', name), JSON.stringify(SCHEMAS[name]), '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');
|
||||
}
|
||||
mkdirSync(path.join(root, 'parameters'), { recursive: true });
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
return root;
|
||||
}
|
||||
|
||||
function runValidate(root) {
|
||||
const proc = spawnSync(process.execPath, [cli, 'validate', root], { encoding: 'utf8' });
|
||||
return { code: proc.status, envelope: JSON.parse(proc.stdout) };
|
||||
}
|
||||
|
||||
function codes(result) {
|
||||
return result.envelope.data.errors.map((item) => item.code);
|
||||
}
|
||||
|
||||
test('валидная спецификация: код 0 и пустые диагностики', (t) => {
|
||||
const result = runValidate(fixture(t));
|
||||
assert.equal(result.code, 0);
|
||||
assert.equal(result.envelope.ok, true);
|
||||
assert.deepEqual(result.envelope.data.errors, []);
|
||||
assert.equal(result.envelope.data.files, 5);
|
||||
});
|
||||
|
||||
test('дубликат ID и ссылка на несуществующее требование', (t) => {
|
||||
const dup = fixture(t, {
|
||||
'requirements/req-gamma.yaml':
|
||||
'id: req-alpha\ndomain: dom-build\ncapability: cap-verify\nstatement: Gamma MUST существовать.\nlevel: MUST\n',
|
||||
});
|
||||
assert.ok(codes(runValidate(dup)).includes('duplicate-id'));
|
||||
const dangling = fixture(t, {
|
||||
'requirements/req-beta.yaml':
|
||||
'id: req-beta\ndomain: dom-build\ncapability: cap-verify\nstatement: Beta MUST существовать.\nlevel: MUST\nrelations:\n - target: req-ghost\n kind: depends-on\n',
|
||||
});
|
||||
assert.ok(codes(runValidate(dangling)).includes('unknown-requirement'));
|
||||
});
|
||||
|
||||
test('отсутствующие домен и возможность, несовпадение домена', (t) => {
|
||||
const missingDomain = fixture(t, {
|
||||
'capabilities/cap-verify.yaml':
|
||||
'id: cap-verify\ndomain: dom-absent\ntitle: Проверка\ndescription: Требования к проверкам.\n',
|
||||
});
|
||||
assert.ok(codes(runValidate(missingDomain)).includes('missing-domain'));
|
||||
const missingCapability = fixture(t, {
|
||||
'requirements/req-beta.yaml':
|
||||
'id: req-beta\ndomain: dom-build\ncapability: cap-ghost\nstatement: Beta MUST существовать.\nlevel: MUST\n',
|
||||
});
|
||||
assert.ok(codes(runValidate(missingCapability)).includes('missing-capability'));
|
||||
const mismatch = fixture(t, {
|
||||
'domains/dom-other.yaml': 'id: dom-other\ntitle: Другой\ndescription: Другой домен.\n',
|
||||
'requirements/req-beta.yaml':
|
||||
'id: req-beta\ndomain: dom-other\ncapability: cap-verify\nstatement: Beta MUST существовать.\nlevel: MUST\n',
|
||||
});
|
||||
assert.ok(codes(runValidate(mismatch)).includes('domain-mismatch'));
|
||||
});
|
||||
|
||||
test('нормативное ключевое слово должно соответствовать level', (t) => {
|
||||
const result = runValidate(
|
||||
fixture(t, {
|
||||
'requirements/req-beta.yaml':
|
||||
'id: req-beta\ndomain: dom-build\ncapability: cap-verify\nstatement: Проект желает документировать шаги.\nlevel: SHOULD\n',
|
||||
}),
|
||||
);
|
||||
assert.equal(result.code, 1);
|
||||
assert.ok(codes(result).includes('keyword-mismatch'));
|
||||
});
|
||||
|
||||
test('цикл depends-on обнаруживается', (t) => {
|
||||
const result = runValidate(
|
||||
fixture(t, {
|
||||
'requirements/req-beta.yaml':
|
||||
'id: req-beta\ndomain: dom-build\ncapability: cap-verify\nstatement: Beta MUST существовать.\nlevel: MUST\nrelations:\n - target: req-alpha\n kind: depends-on\n',
|
||||
}),
|
||||
);
|
||||
assert.equal(result.code, 1);
|
||||
assert.ok(codes(result).includes('dependency-cycle'));
|
||||
});
|
||||
|
||||
test('схема отклоняет неверный level, журнал ссылается на известное требование', (t) => {
|
||||
const badLevel = fixture(t, {
|
||||
'requirements/req-beta.yaml':
|
||||
'id: req-beta\ndomain: dom-build\ncapability: cap-verify\nstatement: Beta MUST существовать.\nlevel: READY\n',
|
||||
});
|
||||
assert.ok(codes(runValidate(badLevel)).includes('schema-error'));
|
||||
const changelog = fixture(t, {
|
||||
'CHANGELOG.yaml':
|
||||
'changes:\n - id: chg-first\n release: 0.1.0\n date: 2026-11-28\n type: ADDED\n requirements: [req-ghost]\n description: Требование добавлено в спецификацию.\n',
|
||||
});
|
||||
assert.ok(codes(runValidate(changelog)).includes('unknown-requirement'));
|
||||
});
|
||||
|
||||
test('обязательная структура корня: schemas и директории сущностей', (t) => {
|
||||
const noRootArg = spawnSync(process.execPath, [cli, 'validate'], { encoding: 'utf8' });
|
||||
assert.equal(noRootArg.status, 2);
|
||||
assert.equal(JSON.parse(noRootArg.stdout).error.code, 'USAGE');
|
||||
const root = fixture(t);
|
||||
rmSync(path.join(root, 'requirements'), { recursive: true, force: true });
|
||||
const result = runValidate(root);
|
||||
assert.equal(result.code, 1);
|
||||
assert.ok(codes(result).includes('missing-directory'));
|
||||
});
|
||||
Reference in New Issue
Block a user