args.spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 and the web alias, handing the rest to the app', () => {
  21. expect(parse(['--profile', 'tui'])).toEqual({ mode: 'profile', profile: 'tui', patches: [], args: [] })
  22. expect(parse(['--profile', 'tui', '--patch', 'a.yml', '--patch', 'b.yml']))
  23. .toEqual({ mode: 'profile', profile: 'tui', patches: ['a.yml', 'b.yml'], args: [] })
  24. expect(parse(['web'])).toEqual({ mode: 'profile', profile: 'web', patches: [], args: [] })
  25. expect(parse(['web', '--patch', 'web.yml']))
  26. .toEqual({ mode: 'profile', profile: 'web', patches: ['web.yml'], args: [] })
  27. })
  28. it('ends the launcher flags at the first token it does not own', () => {
  29. // App flags, including its -h, and positionals reach the app verbatim.
  30. expect(parse(['--profile', 'tui', '--resume', 'abc']))
  31. .toEqual({ mode: 'profile', profile: 'tui', patches: [], args: ['--resume', 'abc'] })
  32. expect(parse(['--profile', 'web', '-h']))
  33. .toEqual({ mode: 'profile', profile: 'web', patches: [], args: ['-h'] })
  34. expect(parse(['web', '--host', '127.0.0.1', '--port', '8080', '--no-open', '--future-web-flag']))
  35. .toEqual({ mode: 'profile', profile: 'web', patches: [], args: ['--host', '127.0.0.1', '--port', '8080', '--no-open', '--future-web-flag'] })
  36. expect(parse(['--profile', 'headless', 'run', 'the', 'tests']))
  37. .toEqual({ mode: 'profile', profile: 'headless', patches: [], args: ['run', 'the', 'tests'] })
  38. // Launcher flags placed after that boundary belong to the app too.
  39. expect(parse(['--profile', 'tui', '--patch', 'a.yml', '--resume', 'b', '--patch', 'late.yml']))
  40. .toEqual({ mode: 'profile', profile: 'tui', patches: ['a.yml'], args: ['--resume', 'b', '--patch', 'late.yml'] })
  41. })
  42. it('routes the plugin pnpm forwarder', () => {
  43. expect(parse(['plugin', '--profile', 'tui', 'add', 'turtle-ui']))
  44. .toEqual({ mode: 'plugin', profile: 'tui', args: ['add', 'turtle-ui'] })
  45. expect(parse(['plugin', '--profile', 'tui', 'remove', 'turtle-ui']))
  46. .toEqual({ mode: 'plugin', profile: 'tui', args: ['remove', 'turtle-ui'] })
  47. expect(parse(['plugin', '--profile', 'tui', 'why', '@deepseek-ai/cordis']))
  48. .toEqual({ mode: 'plugin', profile: 'tui', args: ['why', '@deepseek-ai/cordis'] })
  49. // Unknown pnpm flags forward verbatim.
  50. expect(parse(['plugin', '--profile', 'tui', 'add', '--save-dev', 'x']))
  51. .toEqual({ mode: 'plugin', profile: 'tui', args: ['add', '--save-dev', 'x'] })
  52. })
  53. it('routes profile and web config dumps', () => {
  54. expect(parse(['--profile', 'web', '--dump-config']))
  55. .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: false, patches: [] })
  56. expect(parse(['--profile', 'web', '--dump-default-config']))
  57. .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: true, patches: [] })
  58. expect(parse(['--profile', 'tui', '--dump-config', '--patch', 'x.yml']))
  59. .toEqual({ mode: 'dump-config', profile: 'tui', defaultOnly: false, patches: ['x.yml'] })
  60. expect(parse(['web', '--dump-config']))
  61. .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: false, patches: [] })
  62. expect(parse(['web', '--dump-default-config']))
  63. .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: true, patches: [] })
  64. })
  65. it('rejects missing profile, removed flags, and contradictory inputs', () => {
  66. expect(exitCode([])).toBe(1)
  67. expect(exitCode(['tui'])).toBe(1) // an app argument without --profile has no app to reach
  68. expect(exitCode(['--config', 'c.yml'])).toBe(1) // removed
  69. expect(exitCode(['-p', 'task'])).toBe(1) // removed
  70. expect(exitCode(['run', 'task'])).toBe(1) // app-owned task replaced the launcher subcommand
  71. expect(exitCode(['--profile', ''])).toBe(1)
  72. expect(exitCode(['--profile', 'x', '--patch='])).toBe(1)
  73. expect(exitCode(['--dump-config'])).toBe(1)
  74. expect(exitCode(['--profile', 'x', '--dump-config', '--dump-default-config'])).toBe(1)
  75. expect(exitCode(['--profile', 'x', '--dump-default-config', '--patch', 'p.yml'])).toBe(1)
  76. expect(exitCode(['--profile', 'x', '--dump-config', 'task'])).toBe(1)
  77. expect(exitCode(['--bogus'])).toBe(1)
  78. expect(exitCode(['--profile', 'x', 'web'])).toBe(1)
  79. expect(exitCode(['web', '--dump-config', '--dump-default-config'])).toBe(1)
  80. expect(exitCode(['web', '--dump-default-config', '--patch', 'w.yml'])).toBe(1)
  81. expect(exitCode(['web', '--patch='])).toBe(1)
  82. // A dump never runs app command-line providers, so it cannot show what
  83. // those flags would decide; printing a tree that differs from the same
  84. // invocation's boot would mislead.
  85. expect(exitCode(['web', '--dump-config', '--port', '8080'])).toBe(1)
  86. expect(exitCode(['--profile', 'web', '--dump-config', '-h'])).toBe(1)
  87. expect(exitCode(['plugin', 'add', 'x'])).toBe(1) // --profile required
  88. expect(exitCode(['plugin', '--profile', 'tui'])).toBe(1) // nothing to forward
  89. expect(exitCode(['plugin', '--profile', ''])).toBe(1)
  90. expect(exitCode(['--profile', 'x', 'plugin', 'add', 'y'])).toBe(1)
  91. })
  92. it('keeps its own help for an invocation with no app to hand it to', () => {
  93. expect(exitCode(['--help'])).toBe(0)
  94. expect(exitCode(['-h'])).toBe(0)
  95. expect(exitCode(['--version'])).toBe(0)
  96. })
  97. })