code-mode-keyless-smoke.e2e.ts 3.8 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. * Keyless Loader-path smoke for the Code Mode overlay: boot the real example through the
  9. * `@deepseek-ai/dsh-stdio-agent` bin against `code-mode.cordis.yml` (the cordis Loader,
  10. * `unwrapExports`, the include patches over ./cordis.yml, the worker-thread code runtime, and
  11. * the registry in `mode: code`), then close stdin with no prompt and assert the Code Mode
  12. * banner + a clean exit.
  13. */
  14. const binScript = fileURLToPath(new URL('../../../packages/ui/stdio-agent/src/bin.ts', import.meta.url))
  15. const configPath = fileURLToPath(new URL('../code-mode.cordis.yml', import.meta.url))
  16. const tsxLoader = fileURLToPath(import.meta.resolve('tsx'))
  17. // Dev/test run UNBUILT: resolve `@deepseek-ai/dsh-*` through the root tsconfig
  18. // `paths` map; tsx searches UP from cwd, and we spawn from a temp dir outside
  19. // the repo, so point it at the repo tsconfig.
  20. const repoTsconfig = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url))
  21. // The real-API workflow runs up to 14 e2e files at once.
  22. const PROCESS_TIMEOUT_MS = 30_000
  23. // Leave enough room for the process-owned timeout to report captured output
  24. // before Vitest aborts the test itself.
  25. const TEST_TIMEOUT_MS = PROCESS_TIMEOUT_MS + 15_000
  26. let child: ChildProcessWithoutNullStreams | undefined
  27. let workdir: string | undefined
  28. afterEach(async () => {
  29. if (child !== undefined && child.exitCode === null) child.kill('SIGKILL')
  30. child = undefined
  31. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  32. workdir = undefined
  33. })
  34. async function bootAndEof(): Promise<{ stdout: string; code: number }> {
  35. workdir = await mkdtemp(join(tmpdir(), 'code-mode-smoke-'))
  36. const cwd = workdir
  37. return new Promise((resolve, reject) => {
  38. const proc = spawn(
  39. process.execPath,
  40. // --expose-internals: the included cordis.yml loads the HMR plugin (mirrors demo:code-mode).
  41. ['--expose-internals', '--import', tsxLoader, binScript, configPath],
  42. {
  43. cwd,
  44. env: {
  45. ...process.env,
  46. TSX_TSCONFIG_PATH: repoTsconfig,
  47. // A dummy key so llm-deepseek's apply() (key-PRESENT check only) boots.
  48. // No prompt is sent, so the adapter never streams — no network call.
  49. DEEPSEEK_API_KEY: 'keyless-smoke-no-call',
  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(`code-mode overlay 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(`code-mode overlay 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('code-mode overlay keyless smoke (real code-mode.cordis.yml via the Loader)', () => {
  76. it('boots the Code Mode 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('code-mode agent ready.')
  80. }, TEST_TIMEOUT_MS)
  81. })