args.spec.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. /** Capture the process exit code while muting Commander's output. */
  5. function exitCode(argv: string[]): number {
  6. const exit = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit') })
  7. vi.spyOn(process.stdout, 'write').mockReturnValue(true)
  8. vi.spyOn(process.stderr, 'write').mockReturnValue(true)
  9. try {
  10. parse(argv)
  11. throw new Error(`expected ${JSON.stringify(argv)} to exit`)
  12. } catch {
  13. return exit.mock.calls.at(-1)?.[0] as number
  14. } finally {
  15. vi.restoreAllMocks()
  16. }
  17. }
  18. afterEach(() => { vi.restoreAllMocks() })
  19. describe('parseDshArgs', () => {
  20. it('routes the required raw config, one-shot prompt, and Web command', () => {
  21. expect(parse(['--config', 'custom.yml'])).toEqual({ mode: 'config', config: 'custom.yml' })
  22. expect(parse(['-p', 'do the thing'])).toEqual({ mode: 'headless', prompt: 'do the thing' })
  23. expect(parse(['web'])).toEqual({ mode: 'web', dev: false })
  24. expect(parse(['web', '--config', 'web.yml'])).toEqual({ mode: 'web', dev: false, config: 'web.yml' })
  25. expect(parse(['web', '--host', '0.0.0.0', '--port', '8080', '--dev', '--workspace-root', '/w']))
  26. .toEqual({ mode: 'web', host: '0.0.0.0', port: 8080, dev: true, workspaceRoot: '/w' })
  27. expect(parse(['web', '--trusted-host', 'harness.internal:3080', 'lab.internal', '--trusted-host', '10.0.0.9']))
  28. .toEqual({ mode: 'web', dev: false, trustedHosts: ['harness.internal:3080', 'lab.internal', '10.0.0.9'] })
  29. })
  30. it('routes raw and Web config dumps', () => {
  31. expect(parse(['--config', 'c.yml', '--dump-config']))
  32. .toEqual({ mode: 'dump-config', surface: 'config', defaultOnly: false, config: 'c.yml' })
  33. expect(parse(['--dump-default-config']))
  34. .toEqual({ mode: 'dump-config', surface: 'config', defaultOnly: true })
  35. expect(parse(['web', '--dump-config']))
  36. .toEqual({ mode: 'dump-config', surface: 'web', defaultOnly: false })
  37. expect(parse(['web', '--dump-config', '--config', 'w.yml']))
  38. .toEqual({ mode: 'dump-config', surface: 'web', defaultOnly: false, config: 'w.yml' })
  39. expect(parse(['web', '--dump-default-config']))
  40. .toEqual({ mode: 'dump-config', surface: 'web', defaultOnly: true })
  41. })
  42. it('rejects missing config, removed commands, and contradictory inputs', () => {
  43. expect(exitCode([])).toBe(1)
  44. expect(exitCode(['tui'])).toBe(1)
  45. expect(exitCode(['meta'])).toBe(1)
  46. expect(exitCode(['upgrade'])).toBe(1)
  47. expect(exitCode(['--dump-config'])).toBe(1)
  48. expect(exitCode(['--dump-config', '--dump-default-config', '--config', 'c.yml'])).toBe(1)
  49. expect(exitCode(['--dump-default-config', '--config', 'c.yml'])).toBe(1)
  50. expect(exitCode(['--dump-config', '--config', 'c.yml', '-p', 'task'])).toBe(1)
  51. expect(exitCode(['-p', ''])).toBe(1)
  52. expect(exitCode(['--config='])).toBe(1)
  53. expect(exitCode(['-p', 'x', '--config', 'c.yml'])).toBe(1)
  54. expect(exitCode(['--bogus'])).toBe(1)
  55. expect(exitCode(['bogus-positional'])).toBe(1)
  56. expect(exitCode(['web', '-p', 'task'])).toBe(1)
  57. expect(exitCode(['--config', 'c.yml', 'web'])).toBe(1)
  58. expect(exitCode(['web', '--dump-config', '--dump-default-config'])).toBe(1)
  59. expect(exitCode(['web', '--dump-default-config', '--config', 'w.yml'])).toBe(1)
  60. expect(exitCode(['web', '--config='])).toBe(1)
  61. })
  62. it('exits 0 for help and version', () => {
  63. expect(exitCode(['--help'])).toBe(0)
  64. expect(exitCode(['--version'])).toBe(0)
  65. })
  66. })