keyless-smoke.e2e.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 examples/coding-agent: boot the REAL example
  9. * through the `@deepseek-ai/dsh-stdio-agent` bin against its `cordis.yml` (the
  10. * cordis Loader, `unwrapExports`, the full plugin tree incl. the
  11. * `@deepseek-ai/dsh-agent-core` bundle and the app's in-package readline UI
  12. * module), then close stdin with no prompt and assert the
  13. * ready banner + a clean exit.
  14. *
  15. * No prompt is ever sent, so the model is NEVER called — this is why it runs
  16. * without a real key. coding-agent's `cordis.yml` loads `llm-deepseek`, whose
  17. * `apply()` only requires a key to be PRESENT (it does not validate it and only
  18. * uses it when a stream actually starts), so a dummy key lets the tree boot
  19. * while the absence of any prompt guarantees no network call. The value is the
  20. * real-Loader-path guard that the composed tree boots (see postmortem 0001;
  21. * the app carries no `inject`, so its export SHAPE is pinned by the stdio-agent
  22. * unit suite's unwrap assertion, not by a crash here),
  23. * complementing coding-agent's with-key e2e suites which prove the real
  24. * product.
  25. */
  26. // TODO(loader-smoke-harness): extract the shared spawn/tempdir/timeout/EOF
  27. // harness used here, code-mode-keyless-smoke, and cordis-agent's keyless smoke.
  28. // The dsh-stdio-agent bin (the demo:repl entry) and this example's cordis.yml.
  29. // The bin resolves its config-path arg from CWD; the test spawns from a temp
  30. // cwd, so we pass the example config's ABSOLUTE path.
  31. const binScript = fileURLToPath(new URL('../../../packages/ui/stdio-agent/src/bin.ts', import.meta.url))
  32. const configPath = fileURLToPath(new URL('../cordis.yml', import.meta.url))
  33. const tsxLoader = fileURLToPath(import.meta.resolve('tsx'))
  34. // Dev/test run UNBUILT: resolve `@deepseek-ai/dsh-*` through the root tsconfig
  35. // `paths` map; tsx searches UP from cwd, and we spawn from a temp dir outside
  36. // the repo, so point it at the repo tsconfig (root is four levels up).
  37. const repoTsconfig = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url))
  38. // The real-API workflow runs up to 14 e2e files at once. Cold tsx/Loader
  39. // startup can therefore outlive a tight smoke-test deadline before the child
  40. // emits any output; 30s still detects a wedged process without confusing
  41. // bounded CI contention with a lifecycle failure.
  42. const PROCESS_TIMEOUT_MS = 30_000
  43. // Leave enough room for the process-owned timeout to report captured output
  44. // before Vitest aborts the test itself.
  45. const TEST_TIMEOUT_MS = PROCESS_TIMEOUT_MS + 15_000
  46. let child: ChildProcessWithoutNullStreams | undefined
  47. let workdir: string | undefined
  48. afterEach(async () => {
  49. if (child !== undefined && child.exitCode === null) child.kill('SIGKILL')
  50. child = undefined
  51. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  52. workdir = undefined
  53. })
  54. async function bootAndEof(): Promise<{ stdout: string; code: number }> {
  55. workdir = await mkdtemp(join(tmpdir(), 'coding-smoke-'))
  56. const cwd = workdir
  57. return new Promise((resolve, reject) => {
  58. const proc = spawn(
  59. process.execPath,
  60. // --expose-internals: cordis.yml loads the HMR plugin (mirrors demo:repl).
  61. ['--expose-internals', '--import', tsxLoader, binScript, configPath],
  62. {
  63. cwd,
  64. env: {
  65. ...process.env,
  66. TSX_TSCONFIG_PATH: repoTsconfig,
  67. // A dummy key so llm-deepseek's apply() (key-PRESENT check only) boots.
  68. // No prompt is sent, so the adapter never streams — no network call.
  69. DEEPSEEK_API_KEY: 'keyless-smoke-no-call',
  70. DSH_HOME: join(cwd, '.dsh'),
  71. DSH_AGENTS_HOME: join(cwd, '.agents'),
  72. },
  73. stdio: ['pipe', 'pipe', 'pipe'],
  74. },
  75. )
  76. child = proc
  77. let stdout = ''
  78. let stderr = ''
  79. proc.stdout.setEncoding('utf8')
  80. proc.stdout.on('data', (chunk: string) => { stdout += chunk })
  81. proc.stderr.setEncoding('utf8')
  82. proc.stderr.on('data', (chunk: string) => { stderr += chunk })
  83. const timer = setTimeout(() => {
  84. proc.kill('SIGKILL')
  85. reject(new Error(`coding-agent did not exit within ${PROCESS_TIMEOUT_MS / 1_000}s. stdout:\n${stdout}\nstderr:\n${stderr}`))
  86. }, PROCESS_TIMEOUT_MS)
  87. proc.on('exit', (code) => {
  88. clearTimeout(timer)
  89. if (code === 0) resolve({ stdout, code })
  90. else reject(new Error(`coding-agent exited ${code}. stderr:\n${stderr}`))
  91. })
  92. proc.on('error', (err) => { clearTimeout(timer); reject(err) })
  93. // No prompt — just EOF, so the stdio UI exits without ever running a turn.
  94. proc.stdin.end()
  95. })
  96. }
  97. describe('coding-agent keyless smoke (real cordis.yml via the Loader)', () => {
  98. it('boots the full plugin tree, prints its banner, and exits cleanly on EOF', async () => {
  99. const { stdout, code } = await bootAndEof()
  100. expect(code).toBe(0)
  101. expect(stdout).toContain('agent REPL ready.')
  102. }, TEST_TIMEOUT_MS)
  103. })