args.spec.ts 5.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 profile boots, one-shot tasks, and the web alias', () => {
  21. expect(parse(['--profile', 'tui'])).toEqual({ mode: 'profile', profile: 'tui', patches: [] })
  22. expect(parse(['--profile', 'headless', 'run', 'the', 'tests']))
  23. .toEqual({ mode: 'profile', profile: 'headless', patches: [], task: 'run the tests' })
  24. expect(parse(['--profile', 'tui', '--patch', 'a.yml', '--patch', 'b.yml']))
  25. .toEqual({ mode: 'profile', profile: 'tui', patches: ['a.yml', 'b.yml'] })
  26. expect(parse(['web'])).toEqual({ mode: 'web', dev: false, patches: [] })
  27. expect(parse(['web', '--patch', 'web.yml'])).toEqual({ mode: 'web', dev: false, patches: ['web.yml'] })
  28. expect(parse(['web', '--host', '0.0.0.0', '--port', '8080', '--dev', '--workspace-root', '/w']))
  29. .toEqual({ mode: 'web', host: '0.0.0.0', port: 8080, dev: true, workspaceRoot: '/w', patches: [] })
  30. expect(parse(['web', '--trusted-host', 'harness.internal:3080', 'lab.internal', '--trusted-host', '10.0.0.9']))
  31. .toEqual({ mode: 'web', dev: false, patches: [], trustedHosts: ['harness.internal:3080', 'lab.internal', '10.0.0.9'] })
  32. })
  33. it('routes the plugin pnpm forwarder', () => {
  34. expect(parse(['plugin', '--profile', 'tui', 'add', 'turtle-ui']))
  35. .toEqual({ mode: 'plugin', profile: 'tui', args: ['add', 'turtle-ui'] })
  36. expect(parse(['plugin', '--profile', 'tui', 'remove', 'turtle-ui']))
  37. .toEqual({ mode: 'plugin', profile: 'tui', args: ['remove', 'turtle-ui'] })
  38. expect(parse(['plugin', '--profile', 'tui', 'why', 'cordis']))
  39. .toEqual({ mode: 'plugin', profile: 'tui', args: ['why', 'cordis'] })
  40. // Unknown pnpm flags forward verbatim.
  41. expect(parse(['plugin', '--profile', 'tui', 'add', '--save-dev', 'x']))
  42. .toEqual({ mode: 'plugin', profile: 'tui', args: ['add', '--save-dev', 'x'] })
  43. })
  44. it('routes profile and web config dumps', () => {
  45. expect(parse(['--profile', 'web', '--dump-config']))
  46. .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: false, patches: [] })
  47. expect(parse(['--profile', 'web', '--dump-default-config']))
  48. .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: true, patches: [] })
  49. expect(parse(['--profile', 'tui', '--dump-config', '--patch', 'x.yml']))
  50. .toEqual({ mode: 'dump-config', profile: 'tui', defaultOnly: false, patches: ['x.yml'] })
  51. expect(parse(['web', '--dump-config']))
  52. .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: false, patches: [] })
  53. expect(parse(['web', '--dump-default-config']))
  54. .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: true, patches: [] })
  55. })
  56. it('rejects missing profile, removed flags, and contradictory inputs', () => {
  57. expect(exitCode([])).toBe(1)
  58. expect(exitCode(['tui'])).toBe(1) // a bare word is a task without --profile
  59. expect(exitCode(['--config', 'c.yml'])).toBe(1) // removed
  60. expect(exitCode(['-p', 'task'])).toBe(1) // removed
  61. expect(exitCode(['--profile', ''])).toBe(1)
  62. expect(exitCode(['--profile', 'x', '--patch='])).toBe(1)
  63. expect(exitCode(['--dump-config'])).toBe(1)
  64. expect(exitCode(['--profile', 'x', '--dump-config', '--dump-default-config'])).toBe(1)
  65. expect(exitCode(['--profile', 'x', '--dump-default-config', '--patch', 'p.yml'])).toBe(1)
  66. expect(exitCode(['--profile', 'x', '--dump-config', 'task'])).toBe(1)
  67. expect(exitCode(['--bogus'])).toBe(1)
  68. expect(exitCode(['--profile', 'x', 'web'])).toBe(1)
  69. expect(exitCode(['web', '--dump-config', '--dump-default-config'])).toBe(1)
  70. expect(exitCode(['web', '--dump-default-config', '--patch', 'w.yml'])).toBe(1)
  71. expect(exitCode(['web', '--patch='])).toBe(1)
  72. // Boot-free dumps derive no flag patches; silently dropping the flags
  73. // would print a tree that differs from the same invocation's boot.
  74. expect(exitCode(['web', '--dump-config', '--port', '8080'])).toBe(1)
  75. expect(exitCode(['web', '--dump-config', '--dev'])).toBe(1)
  76. // A non-numeric port fails at the flag, not deep in the webserver schema.
  77. expect(exitCode(['web', '--port', 'abc'])).toBe(1)
  78. expect(exitCode(['plugin', 'add', 'x'])).toBe(1) // --profile required
  79. expect(exitCode(['plugin', '--profile', 'tui'])).toBe(1) // nothing to forward
  80. expect(exitCode(['plugin', '--profile', ''])).toBe(1)
  81. expect(exitCode(['--profile', 'x', 'plugin', 'add', 'y'])).toBe(1)
  82. })
  83. it('exits 0 for help and version', () => {
  84. expect(exitCode(['--help'])).toBe(0)
  85. expect(exitCode(['--version'])).toBe(0)
  86. })
  87. })