args.spec.ts 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { afterEach, describe, expect, it, vi } from 'vitest'
  2. import { parseDshArgs } from '../src/args.ts'
  3. const parse = (argv: string[]) => parseDshArgs(argv, '1.2.3')
  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[]): 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)
  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, meta 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. expect(parse(['meta'])).toEqual({ mode: 'meta' })
  30. // Bare `web` carries no host/port: the shipped Web overlay owns the default.
  31. expect(parse(['web'])).toEqual({ mode: 'web', dev: false })
  32. expect(parse(['web', '--config', 'web.yml'])).toEqual({ mode: 'web', dev: false, config: 'web.yml' })
  33. // Host/port are unvalidated pass-throughs (the webserver schema gates them
  34. // at boot); the adapter only coerces the port string to a number.
  35. expect(parse(['web', '--host', '0.0.0.0', '--port', '8080', '--dev', '--workspace-root', '/w']))
  36. .toEqual({ mode: 'web', host: '0.0.0.0', port: 8080, dev: true, workspaceRoot: '/w' })
  37. // Guided fresh-session entries carry nothing: bare mode discriminant only.
  38. expect(parse(['upgrade'])).toEqual({ mode: 'upgrade' })
  39. // --trusted-host is variadic and repeatable; authorities pass through unvalidated.
  40. expect(parse(['web', '--trusted-host', 'harness.internal:3080', 'lab.internal', '--trusted-host', '10.0.0.9']))
  41. .toEqual({ mode: 'web', dev: false, trustedHosts: ['harness.internal:3080', 'lab.internal', '10.0.0.9'] })
  42. })
  43. it('exits nonzero instead of silently starting fresh or dropping inputs', () => {
  44. // Empty resume/prompt would be swallowed downstream; --prompt mixed with
  45. // TUI inputs must not lose them. (Bad host/port are gated by the webserver
  46. // schema at boot, not here.)
  47. expect(exitCode(['--resume='])).toBe(1)
  48. expect(exitCode(['-p', ''])).toBe(1)
  49. expect(exitCode(['-p', 'x', '--config', 'c.yml'])).toBe(1)
  50. expect(exitCode(['-p', 'x', '--config-replace', 'tree.yml'])).toBe(1)
  51. expect(exitCode(['--config', 'c.yml', '--config-replace', 'tree.yml'])).toBe(1)
  52. expect(exitCode(['-p', 'x', '--resume', 's'])).toBe(1)
  53. expect(exitCode(['--bogus'])).toBe(1)
  54. expect(exitCode(['bogus-positional'])).toBe(1)
  55. // A default-surface flag on either side of `web` leaks into program.opts()
  56. // but the web subcommand shares none of them: reject rather than serve.
  57. expect(exitCode(['web', '-p', 'task'])).toBe(1)
  58. expect(exitCode(['web', '--resume', 's'])).toBe(1)
  59. expect(exitCode(['--config', 'c.yml', 'web'])).toBe(1)
  60. expect(exitCode(['--config-replace', 'tree.yml', 'web'])).toBe(1)
  61. // Same rule for each subcommand that shares no option with the default
  62. // surface, so a leaked flag is a typo, not something to ignore.
  63. // `meta` fixes its own config tree and always starts fresh, so every
  64. // default-surface option is rejected.
  65. expect(exitCode(['meta', '--resume', 's'])).toBe(1)
  66. expect(exitCode(['meta', '--config', 'c.yml'])).toBe(1)
  67. expect(exitCode(['meta', '--config-replace', 'tree.yml'])).toBe(1)
  68. expect(exitCode(['meta', '-p', 'task'])).toBe(1)
  69. // `upgrade` takes no options: any leaked default-surface flag is a
  70. // mistyped invocation, not a silently-dropped input.
  71. expect(exitCode(['upgrade', '--resume', 's'])).toBe(1)
  72. expect(exitCode(['upgrade', '--config', 'c.yml'])).toBe(1)
  73. expect(exitCode(['-p', 'task', 'upgrade'])).toBe(1)
  74. })
  75. it('exits 0 for --help (disclosing web) and --version', () => {
  76. expect(exitCode(['--help'])).toBe(0)
  77. expect(exitCode(['--version'])).toBe(0)
  78. })
  79. })