args.spec.ts 4.5 KB

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