#!/usr/bin/env node import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import { verifyRegistry } from './gate-verify.mjs'; const mode = process.argv[2]; const root = process.cwd(); const source = JSON.parse(await readFile(path.join(root, 'gates/gates.manifest.json'), 'utf8')); const expectedGateIds = source.gates.map((gate) => gate.id); if (expectedGateIds.length === 0) { process.stderr.write('gate population control requires a non-empty anchored inventory\n'); process.exit(2); } async function rejectedForEveryGate(mutate, diagnostic) { for (const gateId of expectedGateIds) { const manifest = structuredClone(source); const gate = manifest.gates.find((candidate) => candidate.id === gateId); mutate(gate); const directory = await mkdtemp(path.join(os.tmpdir(), 'gate-population-control-')); const manifestPath = path.join(directory, 'manifest.json'); try { await writeFile(manifestPath, `${JSON.stringify(manifest)}\n`); const result = await verifyRegistry({ root, manifest: manifestPath, skipHistory: true, structureOnly: true, fixtureProfile: false, }); if (!result.failures.some((failure) => diagnostic(failure, gateId))) return false; } finally { await rm(directory, { recursive: true, force: true }); } } return true; } let rejected; if (mode === 'evidence-subject') { rejected = await rejectedForEveryGate( (gate) => { gate.evidenceSubject = 'different-gate-subject'; }, (failure, gateId) => failure.includes(`gate ${gateId}: evidence subject`) && failure.includes('does not match gate id'), ); } else if (mode === 'type-strict') { rejected = await rejectedForEveryGate( (gate) => { gate.cases[0].actual.exitCode = '0'; }, (failure, gateId) => failure.includes( `${gateId}/${source.gates.find((gate) => gate.id === gateId).cases[0].id}.actual.exitCode`, ) && failure.includes('expected an integer'), ); } else { process.stderr.write(`unknown gate population control ${String(mode)}\n`); process.exit(2); } if (!rejected) { process.stdout.write(`${mode} population control did not reject every registered gate\n`); process.exit(0); } process.stderr.write(`${mode} population control rejected every registered gate\n`); process.exit(1);