| 1 | import { test, before } from 'node:test' |
| 2 | import assert from 'node:assert/strict' |
| 3 | import { mkdtempSync, rmSync } from 'node:fs' |
| 4 | import { join, dirname } from 'node:path' |
| 5 | import { fileURLToPath, pathToFileURL } from 'node:url' |
| 6 | import { build } from 'esbuild' |
| 7 | |
| 8 | const here = dirname(fileURLToPath(import.meta.url)) |
| 9 | let terminalAdapter |
| 10 | let Context |
| 11 | let ErrorCode |
| 12 | |
| 13 | before(async () => { |
| 14 | const dir = mkdtempSync(join(here, '..', 'node_modules', '.cw-terminal-')) |
| 15 | const bundle = async (entry, name) => { |
| 16 | const outfile = join(dir, name) |
| 17 | await build({ entryPoints: [join(here, entry)], outfile, bundle: true, platform: 'node', format: 'esm', packages: 'external', absWorkingDir: join(here, '..'), logLevel: 'silent' }) |
| 18 | return import(pathToFileURL(outfile).href) |
| 19 | } |
| 20 | ;({ terminalAdapter } = await bundle('../src/dsh/terminal.ts', 'terminal.mjs')) |
| 21 | ;({ ErrorCode } = await bundle('../src/protocol.ts', 'protocol.mjs')) |
| 22 | ;({ Context } = await import('@deepseek-ai/cordis')) |
| 23 | process.on('exit', () => rmSync(dir, { recursive: true, force: true })) |
| 24 | }) |
| 25 | |
| 26 | const settle = () => new Promise((resolve) => setTimeout(resolve, 0)) |
| 27 | |
| 28 | async function harness() { |
| 29 | const calls = [] |
| 30 | const host = { |
| 31 | tier: 'plugin', |
| 32 | rpc: { request: async () => { throw new Error('terminal never calls the core directly') }, notify() {} }, |
| 33 | ownerOf: () => undefined, |
| 34 | liveInvocation: () => undefined, |
| 35 | broker: async (ctx, service, method, input) => { calls.push([service, method, input]); return { ok: method } }, |
| 36 | log() {}, |
| 37 | } |
| 38 | const ctx = new Context() |
| 39 | ctx.plugin(terminalAdapter.define(host)) |
| 40 | await settle() |
| 41 | return { ctx, calls } |
| 42 | } |
| 43 | |
| 44 | test('terminal sends every method through the terminal broker with a typed payload and nothing else', async () => { |
| 45 | const { ctx, calls } = await harness() |
| 46 | assert.deepEqual(await ctx.terminal.list(), { ok: 'list' }) |
| 47 | await ctx.terminal.run('make') |
| 48 | await ctx.terminal.run('make', { session: 'build', timeoutSecs: 30, cwd: '/', sandbox_permissions: 'danger' }) |
| 49 | await ctx.terminal.send('build', 'y\n', { waitMs: 0 }) |
| 50 | await ctx.terminal.wait('build') |
| 51 | await ctx.terminal.wait('build', { timeoutSecs: 600 }) |
| 52 | await ctx.terminal.cancel('build') |
| 53 | await ctx.terminal.reset('build') |
| 54 | assert.deepEqual(calls, [ |
| 55 | ['terminal', 'list', {}], |
| 56 | ['terminal', 'run', { command: 'make' }], |
| 57 | ['terminal', 'run', { command: 'make', session: 'build', timeout_secs: 30 }], |
| 58 | ['terminal', 'send', { session: 'build', text: 'y\n', wait_ms: 0 }], |
| 59 | ['terminal', 'wait', { session: 'build' }], |
| 60 | ['terminal', 'wait', { session: 'build', timeout_secs: 600 }], |
| 61 | ['terminal', 'cancel', { session: 'build' }], |
| 62 | ['terminal', 'reset', { session: 'build' }], |
| 63 | ]) |
| 64 | }) |
| 65 | |
| 66 | test('terminal refuses malformed arguments locally, before any broker call', async () => { |
| 67 | const { ctx, calls } = await harness() |
| 68 | const refused = (act) => assert.throws(act, (error) => error.code === ErrorCode.InvalidParams) |
| 69 | for (const command of [undefined, '', 7, ['ls']]) refused(() => ctx.terminal.run(command)) |
| 70 | for (const name of [undefined, '', 7, '../term-1', 'dsh:b:x', 'a b', 'n'.repeat(65)]) { |
| 71 | refused(() => ctx.terminal.run('true', { session: name === undefined ? null : name })) |
| 72 | refused(() => ctx.terminal.send(name, 'x')) |
| 73 | refused(() => ctx.terminal.wait(name)) |
| 74 | refused(() => ctx.terminal.cancel(name)) |
| 75 | refused(() => ctx.terminal.reset(name)) |
| 76 | } |
| 77 | refused(() => ctx.terminal.send('build', '')) |
| 78 | for (const secs of [0, 601, 1.5, '30']) { |
| 79 | refused(() => ctx.terminal.run('true', { timeoutSecs: secs })) |
| 80 | refused(() => ctx.terminal.wait('build', { timeoutSecs: secs })) |
| 81 | } |
| 82 | refused(() => ctx.terminal.send('build', 'x', { waitMs: 60_001 })) |
| 83 | for (const absent of ['spawn', 'kill', 'resize', 'read', 'exec']) assert.equal(typeof ctx.terminal[absent], 'undefined') |
| 84 | assert.deepEqual(calls, []) |
| 85 | }) |
| 86 |