process-main.spec.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { Duplex, PassThrough } from 'node:stream'
  2. import { expect, it, onTestFinished } from 'vitest'
  3. import { JsonChannel } from '../src/channel.ts'
  4. import { runNodeMain } from '../src/process.ts'
  5. import type { ProgramProcess } from '../src/process.ts'
  6. import { decodeCodeJsonWire, encodeCodeJsonWire } from '../src/json-wire.ts'
  7. function endpoints() {
  8. const first = new PassThrough()
  9. const second = new PassThrough()
  10. const child = Duplex.from({ readable: first, writable: second })
  11. const host = Duplex.from({ readable: second, writable: first })
  12. child.on('error', () => {})
  13. host.on('error', () => {})
  14. onTestFinished(() => { child.destroy(); host.destroy() })
  15. return { child, host }
  16. }
  17. function processState(): ProgramProcess { return { env: { FIXTURE_SECRET: 'test' }, stdout: { write: () => true }, stderr: { write: () => true }, exitCode: undefined } }
  18. it('clears process environment, dispatches a binding reply and flushes the terminal frame', async () => {
  19. const { child, host } = endpoints()
  20. const state = processState()
  21. const nativeEnvironment = state.env
  22. nativeEnvironment.SystemRoot = 'C:\\Windows'
  23. nativeEnvironment.PATH = '/native/bin'
  24. const messages: Record<string, unknown>[] = []
  25. const peer = new JsonChannel(host, 4096, (raw) => {
  26. const message = raw as Record<string, unknown>
  27. messages.push(message)
  28. if (message.type === 'ready') void peer.send({ type: 'boot', data: { code: 'console.log("ready"); return await tools.echo({});', namespaces: [{ global: 'tools', names: ['echo'] }], maxOutputBytes: 1024 } })
  29. if (message.type === 'call') void peer.send({ type: 'reply', id: message.id, ok: true, value: encodeCodeJsonWire(42) })
  30. }, () => {})
  31. onTestFinished(() => { peer.close() })
  32. await runNodeMain(child, 4096, state)
  33. expect(state.env).toEqual({})
  34. expect(state.env).not.toBe(nativeEnvironment)
  35. expect(Object.getPrototypeOf(state.env)).toBeNull()
  36. expect(nativeEnvironment).toEqual({ SystemRoot: 'C:\\Windows', PATH: '/native/bin' })
  37. expect(state.exitCode).toBeUndefined()
  38. expect(decodeCodeJsonWire(messages.find(message => message.type === 'done')?.value)).toBe(42)
  39. })
  40. it.each([0, -1, 1.5, 4294967296])('rejects an invalid bootstrap frame limit %i', async (limit) => {
  41. const { child } = endpoints()
  42. await expect(runNodeMain(child, limit, processState())).rejects.toThrow('invalid control message limit')
  43. })
  44. it('rejects an unexpected first control frame', async () => {
  45. const { child, host } = endpoints()
  46. const peer = new JsonChannel(host, 4096, () => { void peer.send({ type: 'reply' }) }, () => {})
  47. onTestFinished(() => { peer.close() })
  48. await expect(runNodeMain(child, 4096, processState())).rejects.toThrow('expected program boot')
  49. })
  50. it('records an I/O failure before the boot frame', async () => {
  51. const { child, host } = endpoints()
  52. const state = processState()
  53. const peer = new JsonChannel(host, 4096, () => { peer.close() }, () => {})
  54. await expect(runNodeMain(child, 4096, state)).rejects.toBeInstanceOf(Error)
  55. expect(state.exitCode).toBe(1)
  56. })
  57. it('contains program writes that exceed queued control output', async () => {
  58. const { child, host } = endpoints()
  59. const state = processState()
  60. const peer = new JsonChannel(host, 1024, (raw) => {
  61. if ((raw as { type: string }).type === 'ready') void peer.send({ type: 'boot', data: { code: 'for(let i=0;i<30;i++) console.log("x".repeat(100));', namespaces: [], maxOutputBytes: 8000 } })
  62. }, () => {})
  63. onTestFinished(() => { peer.close() })
  64. await runNodeMain(child, 1024, state)
  65. expect(state.exitCode).toBe(1)
  66. })