echo.e2e.ts 5.2 KB

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