- bhl fetch/verify: lock.json (sha256 байтов, без LF-нормализации), несовпадение суммы прерывает использование артефакта - missing-file/unexpected-file/missing-cache — явные диагностики - офлайн validate зафиксированной версии после загрузки (тест) - validate.ts: Ajv strict-отказ и битый JSON — диагностики вместо crash - пакет: 36/36 тестов
209 lines
7.6 KiB
JavaScript
209 lines
7.6 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { spawnSync } from 'node:child_process';
|
|
import {
|
|
appendFileSync,
|
|
mkdirSync,
|
|
mkdtempSync,
|
|
readFileSync,
|
|
rmSync,
|
|
unlinkSync,
|
|
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',
|
|
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',
|
|
required: ['id', 'domain', 'title', 'description'],
|
|
properties: {
|
|
id: { type: 'string', pattern: '^cap-[a-z0-9-]+$' },
|
|
domain: { type: 'string' },
|
|
title: { type: 'string' },
|
|
description: { type: 'string' },
|
|
},
|
|
},
|
|
'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: ['integer', 'string', 'boolean', 'enum'] },
|
|
},
|
|
},
|
|
'requirement.schema.json': {
|
|
$id: 'https://baseline-harness.dev/spec/schemas/requirement.schema.json',
|
|
type: 'object',
|
|
required: ['id', 'domain', 'capability', 'level', 'statement'],
|
|
properties: {
|
|
id: { type: 'string', pattern: '^req-[a-z0-9-]+$' },
|
|
domain: { type: 'string' },
|
|
capability: { type: 'string' },
|
|
statement: { type: 'string' },
|
|
level: { enum: ['MUST', 'MUST NOT', 'SHOULD', 'SHOULD NOT', 'MAY'] },
|
|
},
|
|
},
|
|
'antipattern.schema.json': {
|
|
$id: 'https://baseline-harness.dev/spec/schemas/antipattern.schema.json',
|
|
type: 'object',
|
|
required: ['id', 'level'],
|
|
properties: {
|
|
id: { type: 'string', pattern: '^ant-[a-z0-9-]+$' },
|
|
level: { enum: ['SHOULD NOT', 'MUST NOT'] },
|
|
},
|
|
},
|
|
'normative-change.schema.json': {
|
|
$id: 'https://baseline-harness.dev/spec/schemas/normative-change.schema.json',
|
|
type: 'object',
|
|
properties: { id: { type: 'string' } },
|
|
},
|
|
};
|
|
|
|
const RELEASE_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\n',
|
|
'parameters/.gitkeep': '',
|
|
'antipatterns/.gitkeep': '',
|
|
'CHANGELOG.yaml': 'changes: []\n',
|
|
};
|
|
|
|
function tempDir(prefix) {
|
|
return mkdtempSync(path.join(os.tmpdir(), prefix));
|
|
}
|
|
|
|
function makeRelease(t) {
|
|
const source = tempDir('bhl-release-');
|
|
mkdirSync(path.join(source, 'schemas'), { recursive: true });
|
|
for (const [name, schema] of Object.entries(SCHEMAS)) {
|
|
writeFileSync(path.join(source, 'schemas', name), JSON.stringify(schema), 'utf8');
|
|
}
|
|
for (const [name, contents] of Object.entries(RELEASE_FILES)) {
|
|
const target = path.join(source, name);
|
|
mkdirSync(path.dirname(target), { recursive: true });
|
|
writeFileSync(target, contents, 'utf8');
|
|
}
|
|
const cache = tempDir('bhl-cache-');
|
|
t.after(() => {
|
|
rmSync(source, { recursive: true, force: true });
|
|
rmSync(cache, { recursive: true, force: true });
|
|
});
|
|
return { source, cache };
|
|
}
|
|
|
|
function runCli(...args) {
|
|
const proc = spawnSync(process.execPath, [cli, ...args], { encoding: 'utf8' });
|
|
return { code: proc.status, envelope: JSON.parse(proc.stdout) };
|
|
}
|
|
|
|
function readLockText(cache, version) {
|
|
return readFileSync(path.join(cache, version, 'lock.json'), 'utf8');
|
|
}
|
|
|
|
function fetchAndVerify(t) {
|
|
const { source, cache } = makeRelease(t);
|
|
const fetched = runCli('fetch', source, '--version', '0.1.0', '--cache', cache);
|
|
assert.equal(fetched.code, 0);
|
|
const verified = runCli('verify', '--version', '0.1.0', '--cache', cache);
|
|
return { source, cache, fetched, verified };
|
|
}
|
|
|
|
function codes(result) {
|
|
return result.envelope.data.errors.map((item) => item.code);
|
|
}
|
|
|
|
test('fetch кладёт релиз в кеш с lock из SHA-256 фактических байтов', (t) => {
|
|
const { cache, fetched, verified } = fetchAndVerify(t);
|
|
assert.equal(verified.code, 0);
|
|
assert.equal(fetched.envelope.data.files, 12);
|
|
const lock = JSON.parse(readLockText(cache, '0.1.0'));
|
|
assert.equal(lock.algorithm, 'sha256');
|
|
assert.equal(lock.files.length, 12);
|
|
assert.deepEqual(
|
|
lock.files.map((entry) => entry.path),
|
|
[...lock.files.map((entry) => entry.path)].sort((a, b) => a.localeCompare(b)),
|
|
);
|
|
assert.ok(lock.files.some((entry) => entry.path === 'CHANGELOG.yaml'));
|
|
for (const entry of lock.files) {
|
|
assert.match(entry.digest, /^[a-f0-9]{64}$/);
|
|
}
|
|
});
|
|
|
|
test('битый кеш прерывает использование артефакта с явной диагностикой', (t) => {
|
|
const { cache } = fetchAndVerify(t);
|
|
appendFileSync(
|
|
path.join(cache, '0.1.0', 'spec', 'requirements', 'req-alpha.yaml'),
|
|
'x\n',
|
|
'utf8',
|
|
);
|
|
const verified = runCli('verify', '--version', '0.1.0', '--cache', cache);
|
|
assert.equal(verified.code, 1);
|
|
assert.ok(codes(verified).includes('checksum-mismatch'));
|
|
});
|
|
|
|
test('удалённый или лишний файл в кеше обнаруживается', (t) => {
|
|
const { cache } = fetchAndVerify(t);
|
|
unlinkSync(path.join(cache, '0.1.0', 'spec', 'CHANGELOG.yaml'));
|
|
assert.ok(
|
|
codes(runCli('verify', '--version', '0.1.0', '--cache', cache)).includes('missing-file'),
|
|
);
|
|
const extra = fetchAndVerify(t);
|
|
writeFileSync(path.join(extra.cache, '0.1.0', 'spec', 'EXTRA.md'), 'лишний\n', 'utf8');
|
|
assert.ok(
|
|
codes(runCli('verify', '--version', '0.1.0', '--cache', extra.cache)).includes(
|
|
'unexpected-file',
|
|
),
|
|
);
|
|
});
|
|
|
|
test('офлайн-повтор: verify и validate зафиксированной версии работают без источника', (t) => {
|
|
const { source, cache } = fetchAndVerify(t);
|
|
rmSync(source, { recursive: true, force: true });
|
|
const verified = runCli('verify', '--version', '0.1.0', '--cache', cache);
|
|
assert.equal(verified.code, 0);
|
|
const validated = runCli('validate', path.join(cache, '0.1.0', 'spec'));
|
|
assert.equal(validated.code, 0);
|
|
assert.deepEqual(validated.envelope.data.errors, []);
|
|
});
|
|
|
|
test('отсутствующий кеш даёт missing-cache', (t) => {
|
|
const { cache } = makeRelease(t);
|
|
const verified = runCli('verify', '--version', '0.9.9', '--cache', cache);
|
|
assert.equal(verified.code, 1);
|
|
assert.ok(codes(verified).includes('missing-cache'));
|
|
});
|
|
|
|
test('fetch детерминирован: повторный fetch даёт идентичный lock', (t) => {
|
|
const { source, cache } = makeRelease(t);
|
|
const first = runCli('fetch', source, '--version', '0.1.0', '--cache', cache);
|
|
const lockFirst = readLockText(cache, '0.1.0');
|
|
const second = runCli('fetch', source, '--version', '0.1.0', '--cache', cache);
|
|
assert.equal(first.code, 0);
|
|
assert.equal(second.code, 0);
|
|
assert.equal(readLockText(cache, '0.1.0'), lockFirst);
|
|
});
|
|
|
|
test('использование fetch и verify', () => {
|
|
assert.equal(runCli('fetch', '/tmp/x').code, 2);
|
|
assert.equal(runCli('verify').code, 2);
|
|
assert.equal(runCli('verify', '--version', '1.0.0').code, 2);
|
|
});
|