built-bin.e2e.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { fileURLToPath } from 'node:url'
  5. import { execa } from 'execa'
  6. import { afterEach, beforeEach, describe, expect, it } from 'vitest'
  7. /**
  8. * Published-entry smoke for the `dsh` bin: run the built `lib/bin.js` under
  9. * plain Node (no tsx) with PIPED stdio and assert the TUI refuses to boot.
  10. * `dsh` is the sole terminal front door; the TUI owns no non-TTY fallback, so a
  11. * piped launch must exit nonzero with a stderr pointer at the one-shot `-p`
  12. * mode. The guard fires inside `runTui` BEFORE the Loader resolves the config
  13. * tree — a compose-time throw inside the tree is logged per-entry, not
  14. * rethrown, so without this guard a piped launch would settle into an idle
  15. * UI-less process. The bin resolves its workspace deps through the repo's
  16. * node_modules, so no external consumer is assembled; missing-config fail-loud
  17. * and full-boot coverage for the shared dsh-app-boot glue live in cli-demo's
  18. * built-bin suite, and interactive TTY behavior is PTY-covered by
  19. * apps/cli/tests. Skips before the bin is built.
  20. */
  21. const repoRoot = fileURLToPath(new URL('../../../', import.meta.url))
  22. const dshBin = join(repoRoot, 'apps/cli/lib/bin.js')
  23. /**
  24. * Run the built bin with PIPED stdio (stdin closed at EOF); resolve with output
  25. * + exit code. `env` isolates the Harness home for surfaces that read it.
  26. */
  27. async function runBuiltBin(
  28. args: readonly string[] = [],
  29. env: Record<string, string> = {},
  30. ): Promise<{ stdout: string; code: number; stderr: string }> {
  31. const result = await execa(process.execPath, [dshBin, ...args], {
  32. input: '',
  33. timeout: 25_000,
  34. killSignal: 'SIGKILL',
  35. reject: false,
  36. env,
  37. })
  38. if (result.timedOut) {
  39. throw new Error(`dsh built bin did not exit within 25s. stdout:\n${result.stdout}\nstderr:\n${result.stderr}`)
  40. }
  41. return { stdout: result.stdout, code: result.exitCode ?? -1, stderr: result.stderr }
  42. }
  43. describe.skipIf(!existsSync(dshBin))('dsh BUILT bin (node lib/bin.js, no tsx)', () => {
  44. it('refuses pipes LOUD (non-zero exit + stderr) before booting the Loader', async () => {
  45. const { stdout, code, stderr } = await runBuiltBin()
  46. expect(code).not.toBe(0)
  47. expect(stderr).toContain('requires stdin and stdout to be interactive TTYs')
  48. expect(stderr).toContain('dsh -p')
  49. // The refusal happens before any plugin mounts: stdout stays silent.
  50. expect(stdout).toBe('')
  51. }, 30_000)
  52. describe('dsh --dump-config', () => {
  53. let home: string
  54. beforeEach(() => { home = mkdtempSync(join(tmpdir(), 'dsh-dump-bin-')) })
  55. afterEach(() => { rmSync(home, { recursive: true, force: true }) })
  56. it('prints the shipped TUI composition without booting or needing a TTY', async () => {
  57. const { stdout, code, stderr } = await runBuiltBin(['--dump-default-config'], { DSH_HOME: home })
  58. expect(code).toBe(0)
  59. expect(stderr).toBe('')
  60. // Base rows composed with the TUI overlay's surface values, `!!js`
  61. // expressions verbatim (unevaluated), and TUI-only inserted rows present.
  62. expect(stdout).toContain("name: '@deepseek-ai/dsh-agent-loop'")
  63. expect(stdout).toContain('model: deepseek-v4-pro')
  64. expect(stdout).toContain('cwd: !!js process.cwd()')
  65. expect(stdout).toContain("name: '@deepseek-ai/dsh-tui'")
  66. // Provenance comment separators name each section's source file.
  67. expect(stdout).toContain('# == base.cordis.yml')
  68. expect(stdout).toContain('# == base.cordis.yml, patched by tui.cordis.yml')
  69. expect(stdout).toContain('# == tui.cordis.yml')
  70. }, 30_000)
  71. it('layers the personal overlay in --dump-config and reports an unmatched patch on stderr', async () => {
  72. writeFileSync(join(home, 'config.yaml'), [
  73. '- id: agent-loop',
  74. ' config:',
  75. ' agents:',
  76. ' - id: main',
  77. ' provider: custom-provider',
  78. ' model: custom-model',
  79. '- id: only-on-web',
  80. ' config:',
  81. ' value: 1',
  82. '',
  83. ].join('\n'))
  84. const { stdout, code, stderr } = await runBuiltBin(['--dump-config'], { DSH_HOME: home })
  85. expect(code).toBe(0)
  86. expect(stdout).toContain('provider: custom-provider')
  87. expect(stdout).not.toContain('model: deepseek-v4-pro')
  88. // The personal layer appears in the patched row's provenance and the
  89. // skipped-patch warning carries its label.
  90. expect(stdout).toContain(`patched by tui.cordis.yml, ${join(home, 'config.yaml')}`)
  91. expect(stderr).toContain('patch: entry "only-on-web" not found')
  92. // The shipped view ignores the personal overlay entirely.
  93. const shipped = await runBuiltBin(['--dump-default-config'], { DSH_HOME: home })
  94. expect(shipped.stdout).not.toContain('custom-provider')
  95. expect(shipped.stdout).toContain('model: deepseek-v4-pro')
  96. }, 30_000)
  97. it('composes the web overlay for `dsh web --dump-config`', async () => {
  98. const { stdout, code } = await runBuiltBin(['web', '--dump-config'], { DSH_HOME: home })
  99. expect(code).toBe(0)
  100. expect(stdout).toContain("name: '@deepseek-ai/dsh-host-webserver'")
  101. expect(stdout).not.toContain("name: '@deepseek-ai/dsh-tui'")
  102. }, 30_000)
  103. })
  104. })