process-main.spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 messages: Record<string, unknown>[] = []
  22. const peer = new JsonChannel(host, 4096, (raw) => {
  23. const message = raw as Record<string, unknown>
  24. messages.push(message)
  25. 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 } })
  26. if (message.type === 'call') void peer.send({ type: 'reply', id: message.id, ok: true, value: encodeCodeJsonWire(42) })
  27. }, () => {})
  28. onTestFinished(() => { peer.close() })
  29. await runNodeMain(child, 4096, state)
  30. expect(state.env).toEqual({})
  31. expect(state.exitCode).toBeUndefined()
  32. expect(decodeCodeJsonWire(messages.find(message => message.type === 'done')?.value)).toBe(42)
  33. })
  34. it.each([0, -1, 1.5, 4294967296])('rejects an invalid bootstrap frame limit %i', async (limit) => {
  35. const { child } = endpoints()
  36. await expect(runNodeMain(child, limit, processState())).rejects.toThrow('invalid control message limit')
  37. })
  38. it('rejects an unexpected first control frame', async () => {
  39. const { child, host } = endpoints()
  40. const peer = new JsonChannel(host, 4096, () => { void peer.send({ type: 'reply' }) }, () => {})
  41. onTestFinished(() => { peer.close() })
  42. await expect(runNodeMain(child, 4096, processState())).rejects.toThrow('expected program boot')
  43. })
  44. it('records an I/O failure before the boot frame', async () => {
  45. const { child, host } = endpoints()
  46. const state = processState()
  47. const peer = new JsonChannel(host, 4096, () => { peer.close() }, () => {})
  48. await expect(runNodeMain(child, 4096, state)).rejects.toBeInstanceOf(Error)
  49. expect(state.exitCode).toBe(1)
  50. })
  51. it('contains program writes that exceed queued control output', async () => {
  52. const { child, host } = endpoints()
  53. const state = processState()
  54. const peer = new JsonChannel(host, 1024, (raw) => {
  55. 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 } })
  56. }, () => {})
  57. onTestFinished(() => { peer.close() })
  58. await runNodeMain(child, 1024, state)
  59. expect(state.exitCode).toBe(1)
  60. })