code-mode-keyless-smoke.e2e.ts 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. A dummy key satisfies adapter boot, but no prompt means
  13. * no model call; the with-key proof lives in `code-mode.e2e.ts`.
  14. */
  15. const binScript = fileURLToPath(new URL('../../../packages/ui/stdio-agent/src/bin.ts', import.meta.url))
  16. const configPath = fileURLToPath(new URL('../code-mode.cordis.yml', import.meta.url))
  17. const tsxLoader = fileURLToPath(import.meta.resolve('tsx'))
  18. // Dev/test run UNBUILT: resolve `@deepseek-ai/dsh-*` through the root tsconfig
  19. // `paths` map; tsx searches UP from cwd, and we spawn from a temp dir outside
  20. // the repo, so point it at the repo tsconfig.
  21. const repoTsconfig = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url))
  22. // Under parallel e2e load, cold tsx/Loader startup can exceed a tight deadline;
  23. // 30s still detects a wedged child.
  24. const PROCESS_TIMEOUT_MS = 30_000
  25. // Leave enough room for the process-owned timeout to report captured output
  26. // before Vitest aborts the test itself.
  27. const TEST_TIMEOUT_MS = PROCESS_TIMEOUT_MS + 15_000
  28. let child: ChildProcessWithoutNullStreams | undefined
  29. let workdir: string | undefined
  30. afterEach(async () => {
  31. if (child !== undefined && child.exitCode === null) child.kill('SIGKILL')
  32. child = undefined
  33. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  34. workdir = undefined
  35. })
  36. async function bootAndEof(): Promise<{ stdout: string; code: number }> {
  37. workdir = await mkdtemp(join(tmpdir(), 'code-mode-smoke-'))
  38. const cwd = workdir
  39. return new Promise((resolve, reject) => {
  40. const proc = spawn(
  41. process.execPath,
  42. // --expose-internals: the included cordis.yml loads the HMR plugin (mirrors demo:code-mode).
  43. ['--expose-internals', '--import', tsxLoader, binScript, configPath],
  44. {
  45. cwd,
  46. env: {
  47. ...process.env,
  48. TSX_TSCONFIG_PATH: repoTsconfig,
  49. // A dummy key so llm-deepseek's apply() (key-PRESENT check only) boots.
  50. // No prompt is sent, so the adapter never streams — no network call.
  51. DEEPSEEK_API_KEY: 'keyless-smoke-no-call',
  52. },
  53. stdio: ['pipe', 'pipe', 'pipe'],
  54. },
  55. )
  56. child = proc
  57. let stdout = ''
  58. let stderr = ''
  59. proc.stdout.setEncoding('utf8')
  60. proc.stdout.on('data', (chunk: string) => { stdout += chunk })
  61. proc.stderr.setEncoding('utf8')
  62. proc.stderr.on('data', (chunk: string) => { stderr += chunk })
  63. const timer = setTimeout(() => {
  64. proc.kill('SIGKILL')
  65. reject(new Error(`code-mode overlay did not exit within ${PROCESS_TIMEOUT_MS / 1_000}s. stdout:\n${stdout}\nstderr:\n${stderr}`))
  66. }, PROCESS_TIMEOUT_MS)
  67. proc.on('exit', (code) => {
  68. clearTimeout(timer)
  69. if (code === 0) resolve({ stdout, code })
  70. else reject(new Error(`code-mode overlay exited ${code}. stderr:\n${stderr}`))
  71. })
  72. proc.on('error', (err) => { clearTimeout(timer); reject(err) })
  73. // No prompt — just EOF, so the stdio UI exits without ever running a turn.
  74. proc.stdin.end()
  75. })
  76. }
  77. describe('code-mode overlay keyless smoke (real code-mode.cordis.yml via the Loader)', () => {
  78. it('boots the Code Mode plugin tree, prints its banner, and exits cleanly on EOF', async () => {
  79. const { stdout, code } = await bootAndEof()
  80. expect(code).toBe(0)
  81. expect(stdout).toContain('code-mode agent ready.')
  82. }, TEST_TIMEOUT_MS)
  83. })