- встроенные проверки: команда качества, lockfile, CI-конфиг по Effective Spec - subject: отпечаток дерева SHA-256 + git revision/dirty (git из PATH — узкое исключение sonarjs/no-os-command-from-path в eslint.config.mjs) - evidence с fingerprint байтов; FAIL доказателен (отсутствие — наблюдение) - отчёт проходит report validate; повторный аудит идентичен (тесты) - требования без проверки → UNKNOWN/semantic/low; пакет 41/41
118 lines
4.0 KiB
JavaScript
118 lines
4.0 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { spawnSync } from 'node:child_process';
|
|
import { mkdirSync, mkdtempSync, 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',
|
|
'--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');
|
|
});
|