echo.e2e.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 its `cordis.yml` (the cordis Loader, `unwrapExports`, the whole
  10. * plugin tree), pipe a script of stdin lines, and assert the rendered stdout.
  11. *
  12. * This is the guard the per-file unit suite structurally cannot be: it drives
  13. * the extracted `@deepseek-ai/dsh-ui-stdio` plugin AND the example-local
  14. * `mock-llm.ts` / `echo-tool.ts` through their REAL load path, so a broken
  15. * plugin export shape (a stray `export default` that `unwrapExports` would
  16. * collapse, dropping `inject`) fails here even though hand-mounted unit tests
  17. * stay green (see docs/postmortem/0001). It needs no API key — the `mock-echo`
  18. * adapter never touches the network — so it runs in the default e2e gate.
  19. *
  20. * Both branches of mock-llm.ts are exercised: an `echo …` line (the tool
  21. * round-trip → `ECHO: …`) and a plain line (the direct canned reply).
  22. */
  23. const startScript = fileURLToPath(new URL('../start.ts', import.meta.url))
  24. const tsxLoader = fileURLToPath(import.meta.resolve('tsx'))
  25. // Dev/test run UNBUILT: `@deepseek-ai/dsh-*` imports resolve through the root
  26. // tsconfig `paths` map, which tsx finds by searching UP from cwd. We spawn from
  27. // a temp cwd OUTSIDE the repo, so point tsx at the repo tsconfig explicitly
  28. // (repo root is four levels up from examples/echo-agent/tests).
  29. const repoTsconfig = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url))
  30. let child: ChildProcessWithoutNullStreams | undefined
  31. let workdir: string | undefined
  32. afterEach(async () => {
  33. if (child !== undefined && child.exitCode === null) child.kill('SIGKILL')
  34. child = undefined
  35. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  36. workdir = undefined
  37. })
  38. /**
  39. * Boot echo-agent, write `lines` to its stdin, close stdin, and resolve with
  40. * the full stdout once the process exits (the stdio UI exits on EOF after the
  41. * agent settles). Rejects on a non-zero exit or a 10s timeout.
  42. */
  43. async function runEcho(lines: string[]): Promise<{ stdout: string; code: number }> {
  44. workdir = await mkdtemp(join(tmpdir(), 'echo-smoke-'))
  45. const cwd = workdir
  46. return new Promise((resolve, reject) => {
  47. const proc = spawn(
  48. process.execPath,
  49. // --expose-internals: the example's cordis.yml loads the HMR plugin, which
  50. // requires it (mirrors the `demo:echo` script). The whole point is to boot
  51. // the example EXACTLY as it really runs, through the Loader.
  52. ['--expose-internals', '--import', tsxLoader, startScript],
  53. { cwd, env: { ...process.env, TSX_TSCONFIG_PATH: repoTsconfig }, stdio: ['pipe', 'pipe', 'pipe'] },
  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(`echo-agent 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(`echo-agent exited ${code}. stderr:\n${stderr}`))
  70. })
  71. proc.on('error', (err) => { clearTimeout(timer); reject(err) })
  72. // Feed the script, then EOF so the stdio UI exits after the agent settles.
  73. for (const line of lines) proc.stdin.write(`${line}\n`)
  74. proc.stdin.end()
  75. })
  76. }
  77. describe('echo-agent keyless smoke (real cordis.yml via the Loader)', () => {
  78. it('boots, prints its welcome banner, and exits cleanly on stdin EOF', async () => {
  79. const { stdout, code } = await runEcho([])
  80. expect(code).toBe(0)
  81. expect(stdout).toContain('echo-agent ready.')
  82. }, 15_000)
  83. it('runs the echo tool round-trip for an "echo …" line', async () => {
  84. const { stdout } = await runEcho(['echo hello world'])
  85. // mock-llm.ts emits a tool-call for the echo tool; echo-tool.ts uppercases.
  86. expect(stdout).toContain('[tool call] echo')
  87. expect(stdout).toContain('[tool result] ECHO: HELLO WORLD')
  88. }, 15_000)
  89. it('streams a direct canned reply for a non-echo line', async () => {
  90. const { stdout } = await runEcho(['just chatting'])
  91. // The direct-response branch of mock-llm.ts quotes the input back.
  92. expect(stdout).toContain('just chatting')
  93. expect(stdout).not.toContain('[tool call]')
  94. }, 15_000)
  95. })