echo.e2e.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 through the
  9. * `@deepseek-ai/dsh-stdio-agent` bin against this example's `cordis.yml` (the cordis Loader,
  10. * `unwrapExports`, the whole plugin tree), pipe a script of stdin lines, and assert the
  11. * rendered stdout. The mock adapter is network-free, making this the complete
  12. * smoke; inputs cover both the echo-tool round trip and direct-reply branch.
  13. */
  14. // The temp-cwd child needs absolute bin and config paths.
  15. const binScript = fileURLToPath(new URL('../../../packages/ui/stdio-agent/src/bin.ts', import.meta.url))
  16. const configPath = fileURLToPath(new URL('../cordis.yml', import.meta.url))
  17. const tsxLoader = fileURLToPath(import.meta.resolve('tsx'))
  18. // The temp cwd is outside the repo, so point tsx at the root config that resolves
  19. // unbuilt workspace packages through `paths`.
  20. const repoTsconfig = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url))
  21. // Under parallel e2e load, cold tsx/Loader startup can exceed a tight deadline;
  22. // 30s still detects a wedged child.
  23. const PROCESS_TIMEOUT_MS = 30_000
  24. // Leave enough room for the process-owned timeout to report captured output
  25. // before Vitest aborts the test itself.
  26. const TEST_TIMEOUT_MS = PROCESS_TIMEOUT_MS + 15_000
  27. let child: ChildProcessWithoutNullStreams | undefined
  28. let workdir: string | undefined
  29. afterEach(async () => {
  30. if (child !== undefined && child.exitCode === null) child.kill('SIGKILL')
  31. child = undefined
  32. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  33. workdir = undefined
  34. })
  35. /**
  36. * Boot echo-agent, write `lines` to its stdin, close stdin, and resolve with
  37. * the full stdout once the process exits (the stdio UI exits on EOF after the
  38. * agent settles). Rejects on a non-zero exit or the process deadline.
  39. */
  40. async function runEcho(lines: string[]): Promise<{ stdout: string; code: number }> {
  41. workdir = await mkdtemp(join(tmpdir(), 'echo-smoke-'))
  42. const cwd = workdir
  43. return new Promise((resolve, reject) => {
  44. const proc = spawn(
  45. process.execPath,
  46. // --expose-internals: the example's cordis.yml loads the HMR plugin, which requires it
  47. // (mirrors the `demo:echo` script).
  48. ['--expose-internals', '--import', tsxLoader, binScript, configPath],
  49. {
  50. cwd,
  51. env: {
  52. ...process.env,
  53. TSX_TSCONFIG_PATH: repoTsconfig,
  54. DSH_HOME: join(cwd, '.dsh'),
  55. DSH_AGENTS_HOME: join(cwd, '.agents'),
  56. },
  57. stdio: ['pipe', 'pipe', 'pipe'],
  58. },
  59. )
  60. child = proc
  61. let stdout = ''
  62. let stderr = ''
  63. proc.stdout.setEncoding('utf8')
  64. proc.stdout.on('data', (chunk: string) => { stdout += chunk })
  65. proc.stderr.setEncoding('utf8')
  66. proc.stderr.on('data', (chunk: string) => { stderr += chunk })
  67. const timer = setTimeout(() => {
  68. proc.kill('SIGKILL')
  69. reject(new Error(`echo-agent did not exit within ${PROCESS_TIMEOUT_MS / 1_000}s. stdout:\n${stdout}\nstderr:\n${stderr}`))
  70. }, PROCESS_TIMEOUT_MS)
  71. proc.on('exit', (code) => {
  72. clearTimeout(timer)
  73. if (code === 0) resolve({ stdout, code })
  74. else reject(new Error(`echo-agent exited ${code}. stderr:\n${stderr}`))
  75. })
  76. proc.on('error', (err) => { clearTimeout(timer); reject(err) })
  77. // Feed the script, then EOF so the stdio UI exits after the agent settles.
  78. for (const line of lines) proc.stdin.write(`${line}\n`)
  79. proc.stdin.end()
  80. })
  81. }
  82. describe('echo-agent keyless smoke (real cordis.yml via the Loader)', () => {
  83. it('boots, prints its welcome banner, and exits cleanly on stdin EOF', async () => {
  84. const { stdout, code } = await runEcho([])
  85. expect(code).toBe(0)
  86. expect(stdout).toContain('echo-agent ready.')
  87. }, TEST_TIMEOUT_MS)
  88. it('runs the echo tool round-trip for an "echo …" line', async () => {
  89. const { stdout } = await runEcho(['echo hello world'])
  90. // mock-llm.ts emits a tool-call for the echo tool; echo-tool.ts uppercases.
  91. expect(stdout).toContain('[tool call] echo')
  92. expect(stdout).toContain('[tool result] ECHO: HELLO WORLD')
  93. }, TEST_TIMEOUT_MS)
  94. it('streams a direct canned reply for a non-echo line', async () => {
  95. const { stdout } = await runEcho(['just chatting'])
  96. // The direct-response branch of mock-llm.ts quotes the input back.
  97. expect(stdout).toContain('just chatting')
  98. expect(stdout).not.toContain('[tool call]')
  99. }, TEST_TIMEOUT_MS)
  100. })