import test from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync, writeFileSync, existsSync } from 'node:fs'; import { join } from 'node:path'; import { spawnSync } from 'node:child_process'; import { main, run } from '../src/cli.mjs'; import { fixture, humanEnv, io, tmp } from './helpers.mjs'; import { writeJson } from '../../business/tests/helpers.mjs'; test('project init/check use declared business repo and never contact the bus', async t => { const root = tmp(t), f = fixture(root); f.doc.projects.stack.repo = join(root, 'records'); writeJson(join(root, 'config/businesses/acme.json'), f.doc); const x = io(humanEnv(f.env)); const deps = { transport: () => assert.fail('local project command contacted bus') }; await main(['project', 'init', 'acme', 'stack'], x, deps); assert.ok(existsSync(join(root, 'records/spec/north-star.md'))); const checked = io(humanEnv(f.env)); assert.equal(await run(['project', 'check', 'acme', 'stack'], checked, deps), 0); assert.match(checked.out.text, /valid|checked/); writeFileSync(join(root, 'records/unknown'), 'bad'); const bad = io(humanEnv(f.env)); assert.equal(await run(['project', 'check', 'acme', 'stack'], bad, deps), 2); assert.match(bad.err.text, /unknown/); }); test('project commands validate args and refuse undeclared projects and agent init', async t => { const root = tmp(t), f = fixture(root); await assert.rejects(main(['project', 'init', 'acme', 'missing'], io(humanEnv(f.env)), {}), /declare/); await assert.rejects(main(['project', 'init', 'acme', 'stack'], io({ ...f.env, CODEX_THREAD_ID: 'fixture-agent' }), {}), /agent/i); for (const args of [[], ['other', 'acme', 'stack'], ['init', 'acme'], ['check', 'acme', 'stack', '--write']]) assert.equal(await run(['project', ...args], io(humanEnv(f.env)), {}), 4); }); test('project dispatcher reaches the human CLI', async t => { const root = tmp(t), f = fixture(root); const result = spawnSync(new URL('../../../scripts/mosaic', import.meta.url).pathname, ['project'], { encoding: 'utf8', env: humanEnv(f.env), }); assert.equal(result.status, 4); assert.match(result.stderr, /mosaic project/); });