- решения владельца: D-039 принято (SemVer), D-044 (BLOCKED — седьмой статус), D-045 (у param-coverage-threshold нет глобального default), D-046 (уточнение D-040: обезличенные производные выводы разрешены) - chg-018 STRENGTHENED + migration: значение задаёт профиль/манифест - C1: bhl audit --spec-version (fallback на lock.json кеша, иначе ошибка) - C2: UNKNOWN-находки валидны по схеме evidence (review-note, locator, дата) - C3: treeFingerprint игнорирует .pi/.artifacts/.cache/.tmp - C4: req-single-quality-command — детерминированная проверка - L1: порог длины id вынесен в именованную константу MAX_ID_LENGTH - пакет: 48/48 тестов, корень зелёный
235 lines
8.9 KiB
JavaScript
235 lines
8.9 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { spawnSync } from 'node:child_process';
|
|
import { mkdirSync, mkdtempSync, cpSync, readFileSync, 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 specRoot = path.join(import.meta.dirname, '..', '..', '..', 'examples', 'synthetic-spec');
|
|
const GENERATED_AT = '2026-01-01';
|
|
|
|
function makeDir() {
|
|
const dir = mkdtempSync(path.join(os.tmpdir(), 'bhl-audit-'));
|
|
return dir;
|
|
}
|
|
|
|
function writeProject(dir, files) {
|
|
for (const [name, content] of Object.entries(files)) {
|
|
const target = path.join(dir, name);
|
|
mkdirSync(path.dirname(target), { recursive: true });
|
|
writeFileSync(target, content, 'utf8');
|
|
}
|
|
}
|
|
|
|
function runAudit(t, projectFiles) {
|
|
const dir = makeDir();
|
|
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
|
writeProject(dir, projectFiles);
|
|
const out = path.join(dir, 'out', 'report.json');
|
|
const proc = spawnSync(
|
|
process.execPath,
|
|
[
|
|
cli,
|
|
'audit',
|
|
dir,
|
|
'--out',
|
|
out,
|
|
'--spec-root',
|
|
specRoot,
|
|
'--preset',
|
|
'preset-synth-all',
|
|
'--spec-version',
|
|
'0.1.0',
|
|
'--generated-at',
|
|
GENERATED_AT,
|
|
],
|
|
{ encoding: 'utf8' },
|
|
);
|
|
return { dir, out, code: proc.status, stdout: JSON.parse(proc.stdout) };
|
|
}
|
|
|
|
const GOOD_PROJECT = {
|
|
'package.json': JSON.stringify({ name: 'fixture', scripts: { quality: 'node check.mjs' } }),
|
|
'package-lock.json': JSON.stringify({ lockfileVersion: 3 }),
|
|
'.github/workflows/ci.yml': 'name: ci\n',
|
|
};
|
|
|
|
test('audit: детерминированный отчёт проходит report validate и повторяется байт-в-байт', (t) => {
|
|
const first = runAudit(t, GOOD_PROJECT);
|
|
assert.equal(first.code, 0);
|
|
assert.equal(first.stdout.data.out, first.out);
|
|
const report1 = readFileSync(first.out, 'utf8');
|
|
const reportJson = JSON.parse(report1);
|
|
assert.equal(reportJson.generated_at, GENERATED_AT);
|
|
assert.ok(['directory', 'repository'].includes(reportJson.subject.kind));
|
|
assert.match(reportJson.subject.fingerprint, /^sha256:[a-f0-9]{64}$/);
|
|
assert.deepEqual(reportJson.summary, {
|
|
PASS: 3,
|
|
FAIL: 0,
|
|
PARTIAL: 0,
|
|
EXEMPT: 0,
|
|
NOT_APPLICABLE: 0,
|
|
UNKNOWN: 0,
|
|
BLOCKED: 0,
|
|
});
|
|
const validate = spawnSync(
|
|
process.execPath,
|
|
[cli, 'report', 'validate', first.out, '--spec-root', specRoot],
|
|
{ encoding: 'utf8' },
|
|
);
|
|
assert.equal(validate.status, 0, validate.stdout);
|
|
const second = runAudit(t, GOOD_PROJECT);
|
|
assert.equal(readFileSync(second.out, 'utf8'), report1, 'повторный аудит идентичен');
|
|
});
|
|
|
|
test('audit: отсутствующие артефакты дают FAIL без обвала отчёта', (t) => {
|
|
const run = runAudit(t, { 'README.md': 'проект без проверок\n' });
|
|
assert.equal(run.code, 0);
|
|
const report = JSON.parse(readFileSync(run.out, 'utf8'));
|
|
assert.equal(report.summary.FAIL, 3);
|
|
assert.equal(report.summary.PASS, 0);
|
|
for (const finding of report.findings) {
|
|
assert.equal(finding.status, 'FAIL');
|
|
assert.ok(finding.rationale.length > 10);
|
|
}
|
|
const validate = spawnSync(
|
|
process.execPath,
|
|
[cli, 'report', 'validate', run.out, '--spec-root', specRoot],
|
|
{
|
|
encoding: 'utf8',
|
|
},
|
|
);
|
|
assert.equal(validate.status, 0, validate.stdout);
|
|
});
|
|
|
|
test('audit: usage-ошибки и нечитаемый spec-root дают понятные коды', (t) => {
|
|
const dir = makeDir();
|
|
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
|
const noOut = spawnSync(process.execPath, [cli, 'audit', dir], { encoding: 'utf8' });
|
|
assert.equal(noOut.status, 2);
|
|
assert.equal(JSON.parse(noOut.stdout).error.code, 'USAGE');
|
|
const badSpec = spawnSync(
|
|
process.execPath,
|
|
[cli, 'audit', dir, '--out', path.join(dir, 'r.json'), '--spec-root', path.join(dir, 'нет')],
|
|
{ encoding: 'utf8' },
|
|
);
|
|
assert.equal(badSpec.status, 1);
|
|
assert.equal(JSON.parse(badSpec.stdout).error.code, 'VALIDATION_FAILED');
|
|
});
|
|
|
|
function makeFixtureSpec(t) {
|
|
const dir = makeDir();
|
|
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
|
const spec = path.join(dir, 'spec');
|
|
cpSync(specRoot, spec, { recursive: true });
|
|
return { dir, spec };
|
|
}
|
|
|
|
function auditRun(spec, projectDir, out, extraArgs) {
|
|
return spawnSync(
|
|
process.execPath,
|
|
[
|
|
cli,
|
|
'audit',
|
|
projectDir,
|
|
'--out',
|
|
out,
|
|
'--spec-root',
|
|
spec,
|
|
'--generated-at',
|
|
GENERATED_AT,
|
|
...extraArgs,
|
|
],
|
|
{ encoding: 'utf8' },
|
|
);
|
|
}
|
|
|
|
test('audit: версия спецификации из флага, из lock.json или ошибка (C1/BHL-043)', (t) => {
|
|
const { dir, spec } = makeFixtureSpec(t);
|
|
const project = path.join(dir, 'project');
|
|
writeProject(project, GOOD_PROJECT);
|
|
const noVersion = auditRun(spec, project, path.join(dir, 'r1.json'), []);
|
|
assert.equal(noVersion.status, 1);
|
|
assert.equal(JSON.parse(noVersion.stdout).error.code, 'VALIDATION_FAILED');
|
|
assert.match(noVersion.stdout, /spec-version-unknown/);
|
|
writeProject(dir, { 'lock.json': JSON.stringify({ version: '9.9.9' }) });
|
|
const fromLock = auditRun(spec, project, path.join(dir, 'r2.json'), []);
|
|
assert.equal(fromLock.status, 0, fromLock.stdout);
|
|
const report = JSON.parse(readFileSync(path.join(dir, 'r2.json'), 'utf8'));
|
|
assert.equal(report.spec_version, '9.9.9');
|
|
const flagged = auditRun(spec, project, path.join(dir, 'r3.json'), ['--spec-version', '8.8.8']);
|
|
assert.equal(flagged.status, 0);
|
|
assert.equal(JSON.parse(readFileSync(path.join(dir, 'r3.json'), 'utf8')).spec_version, '8.8.8');
|
|
});
|
|
|
|
test('audit: UNKNOWN-находки валидны по схеме отчёта (C2/BHL-043)', (t) => {
|
|
const { dir, spec } = makeFixtureSpec(t);
|
|
const alpha = readFileSync(path.join(spec, 'requirements', 'req-synth-alpha.yaml'), 'utf8');
|
|
writeProject(spec, {
|
|
'requirements/req-plain-no-check.yaml': alpha.replace('req-synth-alpha', 'req-plain-no-check'),
|
|
'profiles/prof-plain.yaml':
|
|
'id: prof-plain\ntitle: Только без проверки\ndescription: Профиль фикстуры с требованием без детерминированной проверки.\nrequirements:\n - requirement: req-plain-no-check\n',
|
|
});
|
|
const project = path.join(dir, 'project');
|
|
writeProject(project, GOOD_PROJECT);
|
|
const out = path.join(dir, 'r.json');
|
|
const run = auditRun(spec, project, out, ['--spec-version', '0.1.0', '--profile', 'prof-plain']);
|
|
assert.equal(run.status, 0, run.stdout);
|
|
const report = JSON.parse(readFileSync(out, 'utf8'));
|
|
assert.equal(report.findings[0].status, 'UNKNOWN');
|
|
const validate = spawnSync(
|
|
process.execPath,
|
|
[cli, 'report', 'validate', out, '--spec-root', spec],
|
|
{
|
|
encoding: 'utf8',
|
|
},
|
|
);
|
|
assert.equal(validate.status, 0, validate.stdout);
|
|
});
|
|
|
|
test('audit: отпечаток не зависит от изменчивого локального состояния .pi/.artifacts (C3/BHL-043)', (t) => {
|
|
const { dir, spec } = makeFixtureSpec(t);
|
|
const project = path.join(dir, 'project');
|
|
writeProject(project, GOOD_PROJECT);
|
|
const first = auditRun(spec, project, path.join(dir, 'r1.json'), ['--spec-version', '0.1.0']);
|
|
assert.equal(first.status, 0, first.stdout);
|
|
writeProject(project, {
|
|
'.pi/transcript.jsonl': '{"turn":1}\n',
|
|
'.artifacts/tmp.bin': 'volatile',
|
|
});
|
|
const second = auditRun(spec, project, path.join(dir, 'r2.json'), ['--spec-version', '0.1.0']);
|
|
assert.equal(second.status, 0, second.stdout);
|
|
assert.equal(
|
|
readFileSync(path.join(dir, 'r2.json'), 'utf8'),
|
|
readFileSync(path.join(dir, 'r1.json'), 'utf8'),
|
|
);
|
|
});
|
|
|
|
test('audit: req-single-quality-command проверяется детерминированно (C4/BHL-043)', (t) => {
|
|
const { dir, spec } = makeFixtureSpec(t);
|
|
const realReq = path.join(
|
|
import.meta.dirname,
|
|
'..',
|
|
'..',
|
|
'..',
|
|
'spec',
|
|
'requirements',
|
|
'req-single-quality-command.yaml',
|
|
);
|
|
cpSync(realReq, path.join(spec, 'requirements', 'req-single-quality-command.yaml'));
|
|
writeProject(spec, {
|
|
'profiles/prof-real.yaml':
|
|
'id: prof-real\ntitle: Реальное требование\ndescription: Профиль фикстуры с реальным требованием единой команды качества.\nrequirements:\n - requirement: req-single-quality-command\n',
|
|
});
|
|
const project = path.join(dir, 'project');
|
|
writeProject(project, GOOD_PROJECT);
|
|
const out = path.join(dir, 'r.json');
|
|
const run = auditRun(spec, project, out, ['--spec-version', '0.1.0', '--profile', 'prof-real']);
|
|
assert.equal(run.status, 0, run.stdout);
|
|
const report = JSON.parse(readFileSync(out, 'utf8'));
|
|
const finding = report.findings.find((f) => f.requirement === 'req-single-quality-command');
|
|
assert.equal(finding.method, 'deterministic');
|
|
assert.equal(finding.status, 'PASS');
|
|
});
|