args.spec.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { afterEach, describe, expect, it, vi } from 'vitest'
  2. import { parseDshArgs } from '../src/args.ts'
  3. const parse = (argv: string[], experimentalEnv = false) => parseDshArgs(argv, '1.2.3', experimentalEnv)
  4. /**
  5. * `parseDshArgs` calls `process.exit` for `--help`/`--version`/errors and lets
  6. * Commander print to the real streams; capture the exit code and mute output.
  7. */
  8. function exitCode(argv: string[], experimentalEnv = false): number {
  9. const exit = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit') })
  10. vi.spyOn(process.stdout, 'write').mockReturnValue(true)
  11. vi.spyOn(process.stderr, 'write').mockReturnValue(true)
  12. try {
  13. parse(argv, experimentalEnv)
  14. throw new Error(`expected ${JSON.stringify(argv)} to exit`)
  15. } catch {
  16. return exit.mock.calls.at(-1)?.[0] as number
  17. } finally {
  18. vi.restoreAllMocks()
  19. }
  20. }
  21. afterEach(() => { vi.restoreAllMocks() })
  22. describe('parseDshArgs', () => {
  23. it('routes each mode by its shape: default TUI, -p headless, experimental and web subcommands', () => {
  24. expect(parse([])).toEqual({ mode: 'tui' })
  25. expect(parse(['--config', 'custom.yml'])).toEqual({ mode: 'tui', config: 'custom.yml' })
  26. expect(parse(['--config-replace', 'tree.yml'])).toEqual({ mode: 'tui', configReplace: 'tree.yml' })
  27. expect(parse(['--resume', 'sess', '--config', 'app.yml'])).toEqual({ mode: 'tui', config: 'app.yml', resume: 'sess' })
  28. expect(parse(['-p', 'do the thing'])).toEqual({ mode: 'headless', prompt: 'do the thing' })
  29. // Experimental subcommands run under the per-invocation flag or the env opt-in.
  30. expect(parse(['meta', '--experimental'])).toEqual({ mode: 'meta' })
  31. expect(parse(['meta'], true)).toEqual({ mode: 'meta' })
  32. // Bare `web` carries no host/port: the shipped Web overlay owns the default.
  33. expect(parse(['web'])).toEqual({ mode: 'web', dev: false })
  34. expect(parse(['web', '--config', 'web.yml'])).toEqual({ mode: 'web', dev: false, config: 'web.yml' })
  35. // Host/port are unvalidated pass-throughs (the webserver schema gates them
  36. // at boot); the adapter only coerces the port string to a number.
  37. expect(parse(['web', '--host', '0.0.0.0', '--port', '8080', '--dev', '--workspace-root', '/w']))
  38. .toEqual({ mode: 'web', host: '0.0.0.0', port: 8080, dev: true, workspaceRoot: '/w' })
  39. // Guided fresh-session entries carry nothing: bare mode discriminant only.
  40. expect(parse(['upgrade', '--experimental'])).toEqual({ mode: 'upgrade' })
  41. expect(parse(['upgrade'], true)).toEqual({ mode: 'upgrade' })
  42. // --trusted-host is variadic and repeatable; authorities pass through unvalidated.
  43. expect(parse(['web', '--trusted-host', 'harness.internal:3080', 'lab.internal', '--trusted-host', '10.0.0.9']))
  44. .toEqual({ mode: 'web', dev: false, trustedHosts: ['harness.internal:3080', 'lab.internal', '10.0.0.9'] })
  45. })
  46. it('routes the dump flags per surface: composed with the user layer, or shipped only', () => {
  47. expect(parse(['--dump-config'])).toEqual({ mode: 'dump-config', surface: 'tui', defaultOnly: false })
  48. expect(parse(['--dump-config', '--config', 'c.yml']))
  49. .toEqual({ mode: 'dump-config', surface: 'tui', defaultOnly: false, config: 'c.yml' })
  50. expect(parse(['--dump-default-config'])).toEqual({ mode: 'dump-config', surface: 'tui', defaultOnly: true })
  51. expect(parse(['web', '--dump-config'])).toEqual({ mode: 'dump-config', surface: 'web', defaultOnly: false })
  52. expect(parse(['web', '--dump-config', '--config', 'w.yml']))
  53. .toEqual({ mode: 'dump-config', surface: 'web', defaultOnly: false, config: 'w.yml' })
  54. expect(parse(['web', '--dump-default-config'])).toEqual({ mode: 'dump-config', surface: 'web', defaultOnly: true })
  55. // The two dump flags contradict each other; boot-only flags alongside a
  56. // dump would be silently ignored; the shipped tree takes no user overlay.
  57. expect(exitCode(['--dump-config', '--dump-default-config'])).toBe(1)
  58. expect(exitCode(['--dump-default-config', '--config', 'c.yml'])).toBe(1)
  59. expect(exitCode(['--dump-config', '--resume', 's'])).toBe(1)
  60. expect(exitCode(['--dump-config', '-p', 'task'])).toBe(1)
  61. expect(exitCode(['--dump-config', '--config-replace', 'tree.yml'])).toBe(1)
  62. expect(exitCode(['web', '--dump-config', '--dump-default-config'])).toBe(1)
  63. expect(exitCode(['web', '--dump-default-config', '--config', 'w.yml'])).toBe(1)
  64. // A leaked dump flag on a subcommand that has none is a mistyped invocation.
  65. expect(exitCode(['meta', '--experimental', '--dump-config'])).toBe(1)
  66. expect(exitCode(['upgrade', '--experimental', '--dump-config'])).toBe(1)
  67. })
  68. it('exits nonzero instead of silently starting fresh or dropping inputs', () => {
  69. // Empty resume/prompt would be swallowed downstream; --prompt mixed with
  70. // TUI inputs must not lose them. (Bad host/port are gated by the webserver
  71. // schema at boot, not here.)
  72. expect(exitCode(['--resume='])).toBe(1)
  73. expect(exitCode(['-p', ''])).toBe(1)
  74. expect(exitCode(['-p', 'x', '--config', 'c.yml'])).toBe(1)
  75. expect(exitCode(['-p', 'x', '--config-replace', 'tree.yml'])).toBe(1)
  76. expect(exitCode(['--config', 'c.yml', '--config-replace', 'tree.yml'])).toBe(1)
  77. expect(exitCode(['-p', 'x', '--resume', 's'])).toBe(1)
  78. expect(exitCode(['--bogus'])).toBe(1)
  79. expect(exitCode(['bogus-positional'])).toBe(1)
  80. // A default-surface flag on either side of `web` leaks into program.opts()
  81. // but the web subcommand shares none of them: reject rather than serve.
  82. expect(exitCode(['web', '-p', 'task'])).toBe(1)
  83. expect(exitCode(['web', '--resume', 's'])).toBe(1)
  84. expect(exitCode(['--config', 'c.yml', 'web'])).toBe(1)
  85. expect(exitCode(['--config-replace', 'tree.yml', 'web'])).toBe(1)
  86. // Same rule for each subcommand that shares no option with the default
  87. // surface, so a leaked flag is a typo, not something to ignore.
  88. // `meta` fixes its own config tree and always starts fresh,
  89. // so every default-surface option is rejected.
  90. expect(exitCode(['meta', '--experimental', '--resume', 's'])).toBe(1)
  91. expect(exitCode(['meta', '--experimental', '--config', 'c.yml'])).toBe(1)
  92. expect(exitCode(['meta', '--experimental', '--config-replace', 'tree.yml'])).toBe(1)
  93. expect(exitCode(['meta', '--experimental', '-p', 'task'])).toBe(1)
  94. // `upgrade` takes no options beyond the gate: any leaked default-surface
  95. // flag is a mistyped invocation, not a silently-dropped input.
  96. expect(exitCode(['upgrade', '--experimental', '--resume', 's'])).toBe(1)
  97. expect(exitCode(['upgrade', '--experimental', '--config', 'c.yml'])).toBe(1)
  98. expect(exitCode(['-p', 'task', 'upgrade', '--experimental'])).toBe(1)
  99. // The pre-release command names have no compatibility aliases.
  100. expect(exitCode(['experimental-meta'])).toBe(1)
  101. expect(exitCode(['experimental-upgrade'])).toBe(1)
  102. })
  103. it('gates experimental subcommands behind --experimental or the env opt-in', () => {
  104. // Bare `meta`/`upgrade` without either opt-in must fail loud, not run.
  105. expect(exitCode(['meta'])).toBe(1)
  106. expect(exitCode(['upgrade'])).toBe(1)
  107. // A leaked default-surface flag stays a typo even when the gate is passed
  108. // by the environment alone.
  109. expect(exitCode(['meta', '--resume', 's'], true)).toBe(1)
  110. // The flag and the env opt-in may coexist.
  111. expect(parse(['meta', '--experimental'], true)).toEqual({ mode: 'meta' })
  112. expect(parse(['upgrade', '--experimental'], true)).toEqual({ mode: 'upgrade' })
  113. })
  114. it('exits 0 for --help (disclosing web) and --version', () => {
  115. expect(exitCode(['--help'])).toBe(0)
  116. expect(exitCode(['--version'])).toBe(0)
  117. })
  118. })