built-bin.e2e.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { spawn } from 'node:child_process'
  2. import { existsSync } from 'node:fs'
  3. import { join } from 'node:path'
  4. import { fileURLToPath } from 'node:url'
  5. import { describe, expect, it } from 'vitest'
  6. /**
  7. * Published-entry smoke for the `dsh` bin: run the built `lib/bin.js` under
  8. * plain Node (no tsx) with PIPED stdio and assert the TUI refuses to boot.
  9. * `dsh` is the sole terminal front door; the TUI owns no non-TTY fallback, so a
  10. * piped launch must exit nonzero with a stderr pointer at the one-shot `-p`
  11. * mode. The guard fires inside `runTui` BEFORE the Loader resolves the config
  12. * tree — a compose-time throw inside the tree is logged per-entry, not
  13. * rethrown, so without this guard a piped launch would settle into an idle
  14. * UI-less process. The bin resolves its workspace deps through the repo's
  15. * node_modules, so no external consumer is assembled; missing-config fail-loud
  16. * and full-boot coverage for the shared dsh-app-boot glue live in cli-demo's
  17. * built-bin suite, and interactive TTY behavior is PTY-covered by
  18. * examples/tui-agent. Skips before the bin is built.
  19. */
  20. const repoRoot = fileURLToPath(new URL('../../../', import.meta.url))
  21. const dshBin = join(repoRoot, 'apps/cli/lib/bin.js')
  22. /** Run the built bin with PIPED stdio; resolve with output + exit code. */
  23. function runBuiltBin(): Promise<{ stdout: string; code: number; stderr: string }> {
  24. return new Promise((resolve, reject) => {
  25. const child = spawn(process.execPath, [dshBin], { stdio: ['pipe', 'pipe', 'pipe'] })
  26. let stdout = ''
  27. let stderr = ''
  28. child.stdout.setEncoding('utf8')
  29. child.stdout.on('data', (c: string) => { stdout += c })
  30. child.stderr.setEncoding('utf8')
  31. child.stderr.on('data', (c: string) => { stderr += c })
  32. const timer = setTimeout(() => {
  33. child.kill('SIGKILL')
  34. reject(new Error(`dsh built bin did not exit within 25s. stdout:\n${stdout}\nstderr:\n${stderr}`))
  35. }, 25_000)
  36. // Resolve on `close` (all stdio drained), not `exit`, so captured output is complete.
  37. child.on('close', (code) => { clearTimeout(timer); resolve({ stdout, code: code ?? -1, stderr }) })
  38. child.on('error', (err) => { clearTimeout(timer); reject(err) })
  39. child.stdin.end()
  40. })
  41. }
  42. describe.skipIf(!existsSync(dshBin))('dsh BUILT bin (node lib/bin.js, no tsx)', () => {
  43. it('refuses pipes LOUD (non-zero exit + stderr) before booting the Loader', async () => {
  44. const { stdout, code, stderr } = await runBuiltBin()
  45. expect(code).not.toBe(0)
  46. expect(stderr).toContain('requires stdin and stdout to be interactive TTYs')
  47. expect(stderr).toContain('dsh -p')
  48. // The refusal happens before any plugin mounts: stdout stays silent.
  49. expect(stdout).toBe('')
  50. }, 30_000)
  51. })