args.spec.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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(['--resume', 'sess', '--config', 'app.yml'])).toEqual({ mode: 'tui', config: 'app.yml', resume: 'sess' })
  27. expect(parse(['-p', 'do the thing'])).toEqual({ mode: 'headless', prompt: 'do the thing' })
  28. // `meta` accepts `--resume` but does not redeclare it: a shared option parses
  29. // into program.opts() on either side of the subcommand, and redeclaring it
  30. // would leave the subcommand's own options empty and drop the id.
  31. expect(parse(['meta'])).toEqual({ mode: 'meta' })
  32. expect(parse(['meta', '--resume', 'sess'])).toEqual({ mode: 'meta', resume: 'sess' })
  33. expect(parse(['--resume', 'sess', 'meta'])).toEqual({ mode: 'meta', resume: 'sess' })
  34. // Credential setup is option-free: it writes the Harness-home .env, so
  35. // there is nothing for a flag to select.
  36. // Bare `web` carries no host/port: the shipped cordis.yml owns the default.
  37. expect(parse(['web'])).toEqual({ mode: 'web', dev: false })
  38. // Host/port are unvalidated pass-throughs (the webserver schema gates them
  39. // at boot); the adapter only coerces the port string to a number.
  40. expect(parse(['web', '--host', '0.0.0.0', '--port', '8080', '--dev', '--workspace-root', '/w']))
  41. .toEqual({ mode: 'web', host: '0.0.0.0', port: 8080, dev: true, workspaceRoot: '/w' })
  42. // Guided fresh-session entries carry nothing: bare mode discriminant only.
  43. expect(parse(['migrate'])).toEqual({ mode: 'migrate' })
  44. expect(parse(['upgrade'])).toEqual({ mode: 'upgrade' })
  45. // `list-sessions` has one option and no workspace filter: the listing is always
  46. // global. `ps` is its alias and resolves to the same mode.
  47. expect(parse(['list-sessions'])).toEqual({ mode: 'list-sessions', json: false })
  48. expect(parse(['list-sessions', '--json'])).toEqual({ mode: 'list-sessions', json: true })
  49. expect(parse(['ps', '--json'])).toEqual({ mode: 'list-sessions', json: true })
  50. // --trusted-host is variadic and repeatable; authorities pass through unvalidated.
  51. expect(parse(['web', '--trusted-host', 'harness.internal:3080', 'lab.internal', '--trusted-host', '10.0.0.9']))
  52. .toEqual({ mode: 'web', dev: false, trustedHosts: ['harness.internal:3080', 'lab.internal', '10.0.0.9'] })
  53. })
  54. it('exits nonzero instead of silently starting fresh or dropping inputs', () => {
  55. // Empty resume/prompt would be swallowed downstream; --prompt mixed with
  56. // TUI inputs must not lose them. (Bad host/port are gated by the webserver
  57. // schema at boot, not here.)
  58. expect(exitCode(['--resume='])).toBe(1)
  59. expect(exitCode(['-p', ''])).toBe(1)
  60. expect(exitCode(['-p', 'x', '--config', 'c.yml'])).toBe(1)
  61. expect(exitCode(['-p', 'x', '--resume', 's'])).toBe(1)
  62. expect(exitCode(['--bogus'])).toBe(1)
  63. expect(exitCode(['bogus-positional'])).toBe(1)
  64. // A default-surface flag on either side of `web` leaks into program.opts()
  65. // but the web subcommand shares none of them: reject rather than serve.
  66. expect(exitCode(['web', '-p', 'task'])).toBe(1)
  67. expect(exitCode(['web', '--resume', 's'])).toBe(1)
  68. expect(exitCode(['--config', 'c.yml', 'web'])).toBe(1)
  69. // Same rule for credential setup: it shares no option with the default
  70. // surface, so a leaked flag is a typo, not something to ignore.
  71. // `meta` fixes its own config tree and is interactive, so --config/-p are
  72. // rejected; an empty id is swallowed downstream exactly as above.
  73. expect(exitCode(['meta', '--resume='])).toBe(1)
  74. expect(exitCode(['meta', '--config', 'c.yml'])).toBe(1)
  75. expect(exitCode(['meta', '-p', 'task'])).toBe(1)
  76. // `migrate`/`upgrade` take no options: any leaked default-surface flag is a
  77. // mistyped invocation, not a silently-dropped input.
  78. expect(exitCode(['migrate', '--resume', 's'])).toBe(1)
  79. expect(exitCode(['migrate', '--config', 'c.yml'])).toBe(1)
  80. expect(exitCode(['migrate', '-p', 'task'])).toBe(1)
  81. expect(exitCode(['upgrade', '--resume', 's'])).toBe(1)
  82. expect(exitCode(['upgrade', '--config', 'c.yml'])).toBe(1)
  83. expect(exitCode(['-p', 'task', 'upgrade'])).toBe(1)
  84. // `list-sessions`/`ps` is read-only and shares no default-surface option: a leaked flag is a
  85. // mistyped invocation, not a listing with a silently dropped input.
  86. expect(exitCode(['ps', '--resume', 's'])).toBe(1)
  87. expect(exitCode(['list-sessions', '--config', 'c.yml'])).toBe(1)
  88. expect(exitCode(['list-sessions', '-p', 'task'])).toBe(1)
  89. expect(exitCode(['--resume', 's', 'ps'])).toBe(1)
  90. })
  91. it('exits 0 for --help (disclosing web) and --version', () => {
  92. expect(exitCode(['--help'])).toBe(0)
  93. expect(exitCode(['--version'])).toBe(0)
  94. })
  95. })