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

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