echo.e2e.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. let child: ChildProcessWithoutNullStreams | undefined
  39. let workdir: string | undefined
  40. afterEach(async () => {
  41. if (child !== undefined && child.exitCode === null) child.kill('SIGKILL')
  42. child = undefined
  43. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  44. workdir = undefined
  45. })
  46. /**
  47. * Boot echo-agent, write `lines` to its stdin, close stdin, and resolve with
  48. * the full stdout once the process exits (the stdio UI exits on EOF after the
  49. * agent settles). Rejects on a non-zero exit or a 10s timeout.
  50. */
  51. async function runEcho(lines: string[]): Promise<{ stdout: string; code: number }> {
  52. workdir = await mkdtemp(join(tmpdir(), 'echo-smoke-'))
  53. const cwd = workdir
  54. return new Promise((resolve, reject) => {
  55. const proc = spawn(
  56. process.execPath,
  57. // --expose-internals: the example's cordis.yml loads the HMR plugin, which
  58. // requires it (mirrors the `demo:echo` script). The whole point is to boot
  59. // the example EXACTLY as it really runs, through the bin + Loader.
  60. ['--expose-internals', '--import', tsxLoader, binScript, configPath],
  61. {
  62. cwd,
  63. env: {
  64. ...process.env,
  65. TSX_TSCONFIG_PATH: repoTsconfig,
  66. DSH_HOME: join(cwd, '.dsh'),
  67. DSH_AGENTS_HOME: join(cwd, '.agents'),
  68. },
  69. stdio: ['pipe', 'pipe', 'pipe'],
  70. },
  71. )
  72. child = proc
  73. let stdout = ''
  74. let stderr = ''
  75. proc.stdout.setEncoding('utf8')
  76. proc.stdout.on('data', (chunk: string) => { stdout += chunk })
  77. proc.stderr.setEncoding('utf8')
  78. proc.stderr.on('data', (chunk: string) => { stderr += chunk })
  79. const timer = setTimeout(() => {
  80. proc.kill('SIGKILL')
  81. reject(new Error(`echo-agent did not exit within 10s. stdout:\n${stdout}\nstderr:\n${stderr}`))
  82. }, 10_000)
  83. proc.on('exit', (code) => {
  84. clearTimeout(timer)
  85. if (code === 0) resolve({ stdout, code })
  86. else reject(new Error(`echo-agent exited ${code}. stderr:\n${stderr}`))
  87. })
  88. proc.on('error', (err) => { clearTimeout(timer); reject(err) })
  89. // Feed the script, then EOF so the stdio UI exits after the agent settles.
  90. for (const line of lines) proc.stdin.write(`${line}\n`)
  91. proc.stdin.end()
  92. })
  93. }
  94. describe('echo-agent keyless smoke (real cordis.yml via the Loader)', () => {
  95. it('boots, prints its welcome banner, and exits cleanly on stdin EOF', async () => {
  96. const { stdout, code } = await runEcho([])
  97. expect(code).toBe(0)
  98. expect(stdout).toContain('echo-agent ready.')
  99. }, 15_000)
  100. it('runs the echo tool round-trip for an "echo …" line', async () => {
  101. const { stdout } = await runEcho(['echo hello world'])
  102. // mock-llm.ts emits a tool-call for the echo tool; echo-tool.ts uppercases.
  103. expect(stdout).toContain('[tool call] echo')
  104. expect(stdout).toContain('[tool result] ECHO: HELLO WORLD')
  105. }, 15_000)
  106. it('streams a direct canned reply for a non-echo line', async () => {
  107. const { stdout } = await runEcho(['just chatting'])
  108. // The direct-response branch of mock-llm.ts quotes the input back.
  109. expect(stdout).toContain('just chatting')
  110. expect(stdout).not.toContain('[tool call]')
  111. }, 15_000)
  112. })