1
0

process.spec.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { spawn } from 'node:child_process'
  2. import { copyFile, mkdtemp, rm } from 'node:fs/promises'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { pathToFileURL } from 'node:url'
  6. import { Duplex } from 'node:stream'
  7. import { expect, it, onTestFinished } from 'vitest'
  8. import { JsonChannel } from '../src/channel.ts'
  9. import { decodeCodeJsonWire, encodeCodeJsonWire } from '../src/json-wire.ts'
  10. it('boots an unbuilt source closure outside the workspace and exchanges tool replies', async () => {
  11. const directory = await mkdtemp(join(tmpdir(), 'dsh-node-source-'))
  12. onTestFinished(async () => { await rm(directory, { recursive: true, force: true }) })
  13. for (const file of ['process.ts', 'bootstrap.ts', 'channel.ts', 'json-wire.ts', 'output-json.ts', 'protocol.ts']) {
  14. await copyFile(new URL(`../src/${file}`, import.meta.url), join(directory, file))
  15. }
  16. 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);`
  17. const child = spawn(process.execPath, ['--input-type=module', '--eval', source], {
  18. env: { PLACEHOLDER_SECRET: 'fixture-only' },
  19. stdio: ['ignore', 'pipe', 'pipe', 'ignore', 'ignore', 'ignore', 'ignore', 'overlapped'],
  20. })
  21. let stderr = ''
  22. child.stderr?.on('data', (chunk: Buffer) => { stderr += chunk.toString('utf8') })
  23. const finished = new Promise<void>((resolve) => { child.once('close', () => { resolve() }) })
  24. const completed = Promise.withResolvers<unknown>()
  25. child.once('error', (error) => { completed.reject(error) })
  26. child.once('exit', (code) => { if (code !== 0) completed.reject(new Error(`child exit ${code}: ${stderr}`)) })
  27. const control = Array.from(child.stdio)[7]
  28. if (!(control instanceof Duplex)) { child.kill(); await finished; throw new Error('missing child control channel') }
  29. const channel = new JsonChannel(control, 100_000, (raw) => {
  30. const message = raw as { type: string; id?: number; args?: unknown; value?: unknown; error?: unknown }
  31. if (message.type === 'ready') {
  32. void channel.send({ type: 'boot', data: {
  33. code: 'const answer = await tools.echo({ n: 21 }); return { answer, env: { ...process.env } }',
  34. namespaces: [{ global: 'tools', names: ['echo'] }],
  35. maxOutputBytes: 10_000,
  36. } }).catch((error: unknown) => { completed.reject(error) })
  37. } else if (message.type === 'call') {
  38. expect(decodeCodeJsonWire(message.args)).toEqual({ n: 21 })
  39. void channel.send({ type: 'reply', id: message.id, ok: true, value: encodeCodeJsonWire(42) }).catch((error: unknown) => { completed.reject(error) })
  40. } else if (message.type === 'done') {
  41. if (message.error !== undefined) completed.reject(new Error(JSON.stringify(message.error)))
  42. else completed.resolve(decodeCodeJsonWire(message.value))
  43. }
  44. }, (error) => { completed.reject(error) })
  45. onTestFinished(async () => { channel.close(); child.kill(); await finished })
  46. expect(await completed.promise).toEqual({ answer: 42, env: {} })
  47. await finished
  48. })