echo.e2e.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/echo-agent: boot the REAL example
  9. * through the `@deepseek-ai/dsh-stdio-agent` bin against this example's
  10. * `cordis.yml` (the cordis Loader, `unwrapExports`, the whole plugin tree),
  11. * pipe a script of stdin lines, and assert the rendered stdout.
  12. *
  13. * This is the guard the per-file unit suite structurally cannot be: it drives
  14. * the `@deepseek-ai/dsh-stdio-agent` app plugin, the `@deepseek-ai/dsh-agent-core`
  15. * bundle it loads, the app's in-package readline UI module, AND the
  16. * example-local `mock-llm.ts` / `echo-tool.ts` through their REAL load path
  17. * (see docs/postmortem/0001). The app itself carries no `inject`, so a stray
  18. * `export default` would boot rather than crash here — the export SHAPE is
  19. * pinned by the explicit unwrap assertion in the stdio-agent unit suite; this
  20. * smoke proves the composed tree actually runs. It needs no API key — the
  21. * `mock-echo` adapter never touches the network — so it runs in the default e2e
  22. * gate.
  23. *
  24. * Both branches of mock-llm.ts are exercised: an `echo …` line (the tool
  25. * round-trip → `ECHO: …`) and a plain line (the direct canned reply).
  26. */
  27. // The dsh-stdio-agent bin (the demo:echo entry) and this example's cordis.yml.
  28. // The bin resolves its config-path arg from CWD; the test spawns from a temp
  29. // cwd, so we pass the example config's ABSOLUTE path.
  30. const binScript = fileURLToPath(new URL('../../../packages/ui/stdio-agent/src/bin.ts', import.meta.url))
  31. const configPath = fileURLToPath(new URL('../cordis.yml', import.meta.url))
  32. const tsxLoader = fileURLToPath(import.meta.resolve('tsx'))
  33. // Dev/test run UNBUILT: `@deepseek-ai/dsh-*` imports resolve through the root
  34. // tsconfig `paths` map, which tsx finds by searching UP from cwd. We spawn from
  35. // a temp cwd OUTSIDE the repo, so point tsx at the repo tsconfig explicitly
  36. // (repo root is four levels up from examples/echo-agent/tests).
  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. /**
  55. * Boot echo-agent, write `lines` to its stdin, close stdin, and resolve with
  56. * the full stdout once the process exits (the stdio UI exits on EOF after the
  57. * agent settles). Rejects on a non-zero exit or the process deadline.
  58. */
  59. async function runEcho(lines: string[]): Promise<{ stdout: string; code: number }> {
  60. workdir = await mkdtemp(join(tmpdir(), 'echo-smoke-'))
  61. const cwd = workdir
  62. return new Promise((resolve, reject) => {
  63. const proc = spawn(
  64. process.execPath,
  65. // --expose-internals: the example's cordis.yml loads the HMR plugin, which
  66. // requires it (mirrors the `demo:echo` script). The whole point is to boot
  67. // the example EXACTLY as it really runs, through the bin + Loader.
  68. ['--expose-internals', '--import', tsxLoader, binScript, configPath],
  69. {
  70. cwd,
  71. env: {
  72. ...process.env,
  73. TSX_TSCONFIG_PATH: repoTsconfig,
  74. DSH_HOME: join(cwd, '.dsh'),
  75. DSH_AGENTS_HOME: join(cwd, '.agents'),
  76. },
  77. stdio: ['pipe', 'pipe', 'pipe'],
  78. },
  79. )
  80. child = proc
  81. let stdout = ''
  82. let stderr = ''
  83. proc.stdout.setEncoding('utf8')
  84. proc.stdout.on('data', (chunk: string) => { stdout += chunk })
  85. proc.stderr.setEncoding('utf8')
  86. proc.stderr.on('data', (chunk: string) => { stderr += chunk })
  87. const timer = setTimeout(() => {
  88. proc.kill('SIGKILL')
  89. reject(new Error(`echo-agent did not exit within ${PROCESS_TIMEOUT_MS / 1_000}s. stdout:\n${stdout}\nstderr:\n${stderr}`))
  90. }, PROCESS_TIMEOUT_MS)
  91. proc.on('exit', (code) => {
  92. clearTimeout(timer)
  93. if (code === 0) resolve({ stdout, code })
  94. else reject(new Error(`echo-agent exited ${code}. stderr:\n${stderr}`))
  95. })
  96. proc.on('error', (err) => { clearTimeout(timer); reject(err) })
  97. // Feed the script, then EOF so the stdio UI exits after the agent settles.
  98. for (const line of lines) proc.stdin.write(`${line}\n`)
  99. proc.stdin.end()
  100. })
  101. }
  102. describe('echo-agent keyless smoke (real cordis.yml via the Loader)', () => {
  103. it('boots, prints its welcome banner, and exits cleanly on stdin EOF', async () => {
  104. const { stdout, code } = await runEcho([])
  105. expect(code).toBe(0)
  106. expect(stdout).toContain('echo-agent ready.')
  107. }, TEST_TIMEOUT_MS)
  108. it('runs the echo tool round-trip for an "echo …" line', async () => {
  109. const { stdout } = await runEcho(['echo hello world'])
  110. // mock-llm.ts emits a tool-call for the echo tool; echo-tool.ts uppercases.
  111. expect(stdout).toContain('[tool call] echo')
  112. expect(stdout).toContain('[tool result] ECHO: HELLO WORLD')
  113. }, TEST_TIMEOUT_MS)
  114. it('streams a direct canned reply for a non-echo line', async () => {
  115. const { stdout } = await runEcho(['just chatting'])
  116. // The direct-response branch of mock-llm.ts quotes the input back.
  117. expect(stdout).toContain('just chatting')
  118. expect(stdout).not.toContain('[tool call]')
  119. }, TEST_TIMEOUT_MS)
  120. })