import test from 'node:test'; import assert from 'node:assert/strict'; import { mkdtempSync, readFileSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { startBroker } from '../src/runtime.mjs'; import { Client } from '../src/client.mjs'; import { startTimeOf } from '../../seat/src/proc.mjs'; import { initProject } from '../../project/src/index.mjs'; import { fixture } from './spec-fixtures.mjs'; test('Broker keeps the sandbox client import closure: only the listed imports, no lazy project import', () => { const source = readFileSync(new URL('../src/broker.mjs', import.meta.url), 'utf8'); assert.deepEqual([...source.matchAll(/^import .*;$/gm)].map(m => m[0]), [ "import { randomUUID, randomBytes } from 'node:crypto';", // Row 65 (#1558): bindLaunch checks a run's transcript path is absolute. "import { isAbsolute } from 'node:path';", // Row 65: the plug-in name pattern a harness takes. kinds.mjs is in RUNNER_FILES and imports nothing. "import { NAME as HARNESS_NAME } from '../../plugins/src/kinds.mjs';", "import { loadRequirements } from '../../tasks/src/requirements.mjs';", // Row 137 (#1631): land-work's checks, in RUNNER_FILES beside the broker. They import nothing. "import { MERGE, LandingRefusal, readLanding } from './landing.mjs';", // Row 141 (#1635): the business ID_PATTERN for instance names. The vocabulary is already in // RUNNER_FILES and imports nothing. Row 155 (#1649) reads THREAD_DEFINITION and END_PROTECTED // from it too. "import { ID_PATTERN, THREAD_DEFINITION, END_PROTECTED } from '../../business/src/vocabulary.mjs';", ]); assert.doesNotMatch(source, /\bimport\s*\(/); assert.doesNotMatch(readFileSync(new URL('../src/landing.mjs', import.meta.url), 'utf8'), /\bimport\b/); assert.doesNotMatch(readFileSync(new URL('../../business/src/vocabulary.mjs', import.meta.url), 'utf8'), /\bimport\b/); assert.doesNotMatch(readFileSync(new URL('../../plugins/src/kinds.mjs', import.meta.url), 'utf8'), /\bimport\b/); }); test('without a trusted project adapter all three async paths refuse project-not-configured', async t => { const f = await fixture(t, { adapter: false }); for (const [verb, args] of [ ['spec.read', { project: 'stack' }], ['record.write', { project: 'stack', path: 'docs/a.md', record: '# A' }], ['decision.raise', { project: 'stack', spec_path: 'spec/north-star.md', spec_commit: f.commit }], ]) await assert.rejects(f.call(f.coder, verb, args), { code: 'project-not-configured' }); assert.equal(f.store.get('SELECT count(*) n FROM decisions').n, 0); }); test('real startBroker injects the project adapter for socket reads and bound-author record writes', async t => { const root = mkdtempSync(join(tmpdir(), 'project-runtime-')); let runtime; t.after(async () => { await runtime?.close(); rmSync(root, { recursive: true, force: true }); }); // Row 65: the bound run's harness is switched on. const business = { id: 'demo', human: 'jason', plugins: { harness: ['pi'] }, projects: { stack: { root: join(root, 'code') } }, arbiters: { technical: 'coder', delivery: 'coder' }, roles: { coder: { definition: 'coder', authority: { withinRole: ['record.write'], crossRole: [] }, } } }; const initialized = await initProject({ business, project: 'stack', dataRoot: root }); runtime = await startBroker({ dataRoot: root, businesses: { demo: business }, launches: [{ business: 'demo', role: 'coder', run: 'runtime-run', harness: 'pi', pid: process.pid, startTime: startTimeOf(process.pid), }] }); const client = new Client({ path: runtime.path, cap: runtime.launches[0].cap }); await client.call('role.claim'); const spec = await client.call('spec.read', { project: 'stack' }); assert.equal(spec.commit, initialized.commit); assert.equal(spec.confirmed, true); const record = await client.call('record.write', { project: 'stack', path: 'docs/runtime.md', record: '# Runtime\n' }); assert.match(record.commit, /^[0-9a-f]{40}$/); assert.equal(readFileSync(join(initialized.repo, record.path), 'utf8'), '# Runtime\n'); });