const { test } = require('node:test'), assert = require('node:assert/strict'), fs = require('node:fs/promises'), os = require('node:os'), path = require('node:path'); test('ZCode bridge uses fixed permissions, isolated profile and filtered environment; secrets are not command arguments', async (t) => { const { ZcodeRuntime } = require('../../src/modules/assistant/zcode-runtime.cjs'), dir = await fs.mkdtemp(path.join(os.tmpdir(), 'zcode-driver-')), runtime = new ZcodeRuntime({ stateDir: dir }); t.after(() => fs.rm(dir, { recursive: true, force: true })); process.env.PRIVATE_TEST_KEY = 'must-not-reach-zcode'; t.after(() => delete process.env.PRIVATE_TEST_KEY); const config = { runtimePath: path.resolve('tests/fixtures/zcode-fixture.cjs'), model: 'fixture', apiBase: 'https://models.example/v1', apiKey: 'NEVER-IN-ARGV', timeoutSeconds: 15, allowActions: false, }; assert.equal((await runtime.probe(config.runtimePath)).available, true); const result = JSON.parse( await runtime.chat({ config, text: 'hello $() `shell`', history: [], context: { servers: [] }, signal: new AbortController().signal, }), ); assert.equal(result.observed.privateEnv, undefined); assert.equal(result.observed.args[result.observed.args.indexOf('--mode') + 1], 'plan'); assert.ok(result.observed.args.includes('--disallowed-tools')); assert.doesNotMatch(JSON.stringify(result.observed.args), /NEVER-IN-ARGV/); assert.equal(result.observed.config.plugins.enabled, false); assert.equal(result.observed.config.features.mcp, false); assert.equal(result.observed.config.features.subagent, false); assert.match(result.observed.cwd, /zcode-driver/); assert.deepEqual(await fs.readdir(path.join(dir, 'runs')), []); }); test('ZCode cancellation and excessive output terminate the invocation and clean private profiles', async (t) => { const { ZcodeRuntime } = require('../../src/modules/assistant/zcode-runtime.cjs'), dir = await fs.mkdtemp(path.join(os.tmpdir(), 'zcode-abort-')), runtime = new ZcodeRuntime({ stateDir: dir }); t.after(() => fs.rm(dir, { recursive: true, force: true })); const config = { runtimePath: path.resolve('tests/fixtures/zcode-fixture.cjs'), model: 'fixture', apiBase: 'https://models.example/v1', apiKey: 'secret', timeoutSeconds: 15, }; const controller = new AbortController(), pending = runtime.chat({ config, text: 'FIXTURE_HANG', history: [], context: {}, signal: controller.signal, }); setTimeout(() => controller.abort(Object.assign(new Error('cancelled'), { status: 499 })), 350); await assert.rejects(pending, (e) => e.status === 499); assert.deepEqual(await fs.readdir(path.join(dir, 'runs')), []); await assert.rejects( runtime.chat({ config, text: 'FIXTURE_OVERFLOW', history: [], context: {}, signal: new AbortController().signal, }), (e) => e.status === 502, ); assert.deepEqual(await fs.readdir(path.join(dir, 'runs')), []); }); test('cancelling during runtime discovery interrupts the probe and cleans its profile', async (t) => { const { ZcodeRuntime } = require('../../src/modules/assistant/zcode-runtime.cjs'), dir = await fs.mkdtemp(path.join(os.tmpdir(), 'zcode-probe-abort-')), entry = path.join(dir, 'slow-probe.cjs'), runtime = new ZcodeRuntime({ stateDir: dir }); t.after(() => fs.rm(dir, { recursive: true, force: true })); await fs.writeFile(entry, 'setInterval(()=>{},1000);'); const controller = new AbortController(); const pending = runtime.chat({ config: { runtimePath: entry, timeoutSeconds: 15 }, text: 'hello', history: [], context: {}, signal: controller.signal, }); setTimeout(() => controller.abort(Object.assign(new Error('cancelled'), { status: 499 })), 500); await assert.rejects(pending, (e) => e.status === 499); assert.deepEqual(await fs.readdir(path.join(dir, 'runs')), []); });