test(code-runtime): complete sandboxed process coverage and catalogs

This commit is contained in:
Tianyi Cui committed 2026-09-12 15:03:01 +08:00
1 parent 8e19ec4962
commit fc5cb0f649
31 files changed
+361 -169

No files matched your search

@@ -17,13 +17,13 @@ it('boots an unbuilt source closure outside the workspace and exchanges tool rep
const source = `import {Socket} from 'node:net';import {runNodeMain} from ${JSON.stringify(pathToFileURL(join(directory, 'process.ts')).href)};await runNodeMain(new Socket({fd:7,readable:true,writable:true}),100000,process);`
const child = spawn(process.execPath, ['--input-type=module', '--eval', source], {
env: { PLACEHOLDER_SECRET: 'fixture-only' },
stdio: ['ignore', 'pipe', 'pipe', 'ignore', 'ignore', 'ignore', 'ignore', 'pipe'],
stdio: ['ignore', 'pipe', 'pipe', 'ignore', 'ignore', 'ignore', 'ignore', 'overlapped'],
})
let stderr = ''
child.stderr?.on('data', (chunk: Buffer) => { stderr += chunk.toString('utf8') })
const finished = new Promise<void>((resolve) => { child.once('close', () => { resolve() }) })
const completed = Promise.withResolvers<unknown>()
child.once('error', error => completed.reject(error))
child.once('error', (error) => { completed.reject(error) })
child.once('exit', (code) => { if (code !== 0) completed.reject(new Error(`child exit ${code}: ${stderr}`)) })
const control = Array.from(child.stdio)[7]
if (!(control instanceof Duplex)) { child.kill(); await finished; throw new Error('missing child control channel') }
@@ -34,15 +34,15 @@ it('boots an unbuilt source closure outside the workspace and exchanges tool rep
code: 'const answer = await tools.echo({ n: 21 }); return { answer, env: { ...process.env } }',
namespaces: [{ global: 'tools', names: ['echo'] }],
maxOutputBytes: 10_000,
} }).catch(error => completed.reject(error))
} }).catch((error: unknown) => { completed.reject(error) })
} else if (message.type === 'call') {
expect(decodeCodeJsonWire(message.args)).toEqual({ n: 21 })
void channel.send({ type: 'reply', id: message.id, ok: true, value: encodeCodeJsonWire(42) }).catch(error => completed.reject(error))
void channel.send({ type: 'reply', id: message.id, ok: true, value: encodeCodeJsonWire(42) }).catch((error: unknown) => { completed.reject(error) })
} else if (message.type === 'done') {
if (message.error !== undefined) completed.reject(new Error(JSON.stringify(message.error)))
else completed.resolve(decodeCodeJsonWire(message.value))
}
}, error => completed.reject(error))
}, (error) => { completed.reject(error) })
onTestFinished(async () => { channel.close(); child.kill(); await finished })
expect(await completed.promise).toEqual({ answer: 42, env: {} })
await finished