import test from 'node:test'; import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; import { createHash } from 'node:crypto'; import { mkdtempSync, readFileSync, rmSync } 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'); function tmp(t) { const dir = mkdtempSync(path.join(os.tmpdir(), 'bhl-release-')); t.after(() => rmSync(dir, { recursive: true, force: true })); return dir; } function runRelease(t, version) { const out = tmp(t); const proc = spawnSync( process.execPath, [cli, 'release', specRoot, '--version', version, '--out', out], { encoding: 'utf8' }, ); return { out, code: proc.status, stdout: JSON.parse(proc.stdout) }; } function sha256(file) { return createHash('sha256').update(readFileSync(file)).digest('hex'); } test('release: tarball детерминирован, manifest сверяется с извлечёнными байтами', (t) => { const first = runRelease(t, '0.1.0'); assert.equal(first.code, 0); assert.deepEqual(first.stdout.data.files, [ 'bhs-spec-0.1.0.tar', 'bhs-spec-0.1.0.manifest.json', 'bhs-spec-0.1.0.tar.sha256', ]); const tarball = path.join(first.out, 'bhs-spec-0.1.0.tar'); const manifest = JSON.parse( readFileSync(path.join(first.out, 'bhs-spec-0.1.0.manifest.json'), 'utf8'), ); assert.equal(manifest.version, '0.1.0'); assert.ok(manifest.files.some((entry) => entry.path === 'CHANGELOG.yaml')); const checksumLine = readFileSync(path.join(first.out, 'bhs-spec-0.1.0.tar.sha256'), 'utf8'); assert.equal(checksumLine.split(' ')[0], sha256(tarball), 'сумма tarball совпадает'); const extract = tmp(t); const untar = spawnSync('tar', ['-xf', tarball, '-C', extract], { encoding: 'utf8' }); assert.equal(untar.status, 0, untar.stderr); for (const entry of manifest.files) { const file = path.join(extract, 'bhs-spec-0.1.0', entry.path); assert.equal(sha256(file), entry.sha256, `байты ${entry.path} совпадают с manifest`); } const second = runRelease(t, '0.1.0'); assert.equal( readFileSync(path.join(second.out, 'bhs-spec-0.1.0.tar'), 'utf8'), readFileSync(tarball, 'utf8'), 'повторная сборка идентична байт-в-байт', ); }); test('release: не-semver и лишние аргументы отклоняются', (t) => { const bad = runRelease(t, '0.1'); assert.equal(bad.code, 1); assert.equal(bad.stdout.error.code, 'VALIDATION_FAILED'); const dir = tmp(t); const usage = spawnSync(process.execPath, [cli, 'release', specRoot, '--version', '1.0.0'], { encoding: 'utf8', }); assert.equal(usage.status, 2); assert.equal(JSON.parse(usage.stdout).error.code, 'USAGE'); rmSync(dir, { recursive: true, force: true }); });