args.spec.ts 6.0 KB

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