keyless-smoke.e2e.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process'
  2. import { mkdtemp, rm } from 'node:fs/promises'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { fileURLToPath } from 'node:url'
  6. import { afterEach, describe, expect, it } from 'vitest'
  7. /**
  8. * Boots the real example through the stdio bin and `cordis.yml`, covering Loader,
  9. * `unwrapExports`, the full plugin tree, the agent-core bundle, and the readline module.
  10. * A dummy key permits startup; closing stdin before a prompt prevents network calls,
  11. * while with-key suites cover product behavior.
  12. */
  13. // TODO(loader-smoke-harness): share spawn/tempdir/timeout/EOF setup with the other keyless smoke tests.
  14. // The temp-cwd child needs absolute bin and config paths.
  15. const binScript = fileURLToPath(new URL('../../../packages/ui/stdio-agent/src/bin.ts', import.meta.url))
  16. const configPath = fileURLToPath(new URL('../cordis.yml', import.meta.url))
  17. const tsxLoader = fileURLToPath(import.meta.resolve('tsx'))
  18. // The temp cwd cannot discover the root tsconfig used for unbuilt package aliases.
  19. const repoTsconfig = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url))
  20. // Allow cold Loader startup under parallel load while still detecting hangs.
  21. const PROCESS_TIMEOUT_MS = 30_000
  22. // Let the child timeout report captured output before Vitest aborts.
  23. const TEST_TIMEOUT_MS = PROCESS_TIMEOUT_MS + 15_000
  24. let child: ChildProcessWithoutNullStreams | undefined
  25. let workdir: string | undefined
  26. afterEach(async () => {
  27. if (child !== undefined && child.exitCode === null) child.kill('SIGKILL')
  28. child = undefined
  29. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  30. workdir = undefined
  31. })
  32. async function bootAndEof(): Promise<{ stdout: string; code: number }> {
  33. workdir = await mkdtemp(join(tmpdir(), 'coding-smoke-'))
  34. const cwd = workdir
  35. return new Promise((resolve, reject) => {
  36. const proc = spawn(
  37. process.execPath,
  38. // --expose-internals: cordis.yml loads the HMR plugin (mirrors demo:repl).
  39. ['--expose-internals', '--import', tsxLoader, binScript, configPath],
  40. {
  41. cwd,
  42. env: {
  43. ...process.env,
  44. TSX_TSCONFIG_PATH: repoTsconfig,
  45. // A dummy key so llm-deepseek's apply() (key-PRESENT check only) boots.
  46. // No prompt is sent, so the adapter never streams — no network call.
  47. DEEPSEEK_API_KEY: 'keyless-smoke-no-call',
  48. DSH_HOME: join(cwd, '.dsh'),
  49. DSH_AGENTS_HOME: join(cwd, '.agents'),
  50. },
  51. stdio: ['pipe', 'pipe', 'pipe'],
  52. },
  53. )
  54. child = proc
  55. let stdout = ''
  56. let stderr = ''
  57. proc.stdout.setEncoding('utf8')
  58. proc.stdout.on('data', (chunk: string) => { stdout += chunk })
  59. proc.stderr.setEncoding('utf8')
  60. proc.stderr.on('data', (chunk: string) => { stderr += chunk })
  61. const timer = setTimeout(() => {
  62. proc.kill('SIGKILL')
  63. reject(new Error(`coding-agent did not exit within ${PROCESS_TIMEOUT_MS / 1_000}s. stdout:\n${stdout}\nstderr:\n${stderr}`))
  64. }, PROCESS_TIMEOUT_MS)
  65. proc.on('exit', (code) => {
  66. clearTimeout(timer)
  67. if (code === 0) resolve({ stdout, code })
  68. else reject(new Error(`coding-agent exited ${code}. stderr:\n${stderr}`))
  69. })
  70. proc.on('error', (err) => { clearTimeout(timer); reject(err) })
  71. // No prompt — just EOF, so the stdio UI exits without ever running a turn.
  72. proc.stdin.end()
  73. })
  74. }
  75. describe('coding-agent keyless smoke (real cordis.yml via the Loader)', () => {
  76. it('boots the full plugin tree, prints its banner, and exits cleanly on EOF', async () => {
  77. const { stdout, code } = await bootAndEof()
  78. expect(code).toBe(0)
  79. expect(stdout).toContain('agent REPL ready.')
  80. }, TEST_TIMEOUT_MS)
  81. })