args.spec.ts 7.4 KB

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