args.spec.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 shorthand, 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', fromDefaultProfile: 'web', patches: [], args: [] })
  52. })
  53. it.each(['web', 'headless', 'sdk', 'sdk-minimal', 'acp', 'tui', 'custom', 'run', 'help'])('expands %s without looking up profiles', (profile) => {
  54. for (const args of [
  55. [], ['task', 'words'], ['--help'], ['-h'], ['web'],
  56. ['--patch', 'a.yml', '--patch', 'b.yml'],
  57. ['--from-default-profile', 'web', '--help'],
  58. ['--dump-config'], ['--dump-default-config'],
  59. ['--patch', 'a.yml', '--resume', 'id', '--patch', 'late.yml'],
  60. ['--', '--help'], ['--', '--', 'task'],
  61. ['plugin'], ['--', 'plugin'],
  62. ]) {
  63. expect(parse([profile, ...args])).toEqual(parse(['--profile', profile, ...args]))
  64. }
  65. })
  66. it('reserves leading plugin for management and forwards later command names', () => {
  67. expect(parse(['--profile', 'plugin'])).toMatchObject({ mode: 'profile', profile: 'plugin' })
  68. expect(parse(['--profile', 'x', 'plugin', 'add', 'y']))
  69. .toMatchObject({ mode: 'profile', profile: 'x', args: ['plugin', 'add', 'y'] })
  70. expect(parse(['headless', 'web'])).toMatchObject({ profile: 'headless', args: ['web'] })
  71. expect(exitCode(['--patch', 'a.yml', 'tui'])).toBe(1)
  72. expect(exitCode(['--', 'tui'])).toBe(1)
  73. })
  74. it.each([
  75. [''], ['desktop'], ['Desktop'], ['DESKTOP'],
  76. ['custom', '--patch='], ['custom', '--from-default-profile='],
  77. ['custom', '--dump-config', '--dump-default-config'],
  78. ['custom', '--dump-default-config', '--patch', 'a.yml'],
  79. ['custom', '--dump-config', 'task'],
  80. ])('rejects invalid shorthand %j', (...argv: string[]) => {
  81. expect(exitCode(argv)).toBe(1)
  82. })
  83. it.each(['-V', '--version'])('prints the launcher version for shorthand %s', (flag) => {
  84. expect(exitCode(['custom', flag])).toBe(0)
  85. expect(parse(['custom', 'task', flag])).toMatchObject({ args: ['task', flag] })
  86. })
  87. it.each([
  88. ['web', '--profile', 'tui'],
  89. ['--profile', 'web', '--profile', 'tui'],
  90. ['--profile=web', '--profile=web'],
  91. ['plugin', '--profile', 'web', '--profile', 'tui', 'add', 'x'],
  92. ])('rejects repeated profile selection %j', (...argv: string[]) => {
  93. const stderr = vi.spyOn(process.stderr, 'write').mockReturnValue(true)
  94. expect(exitCode(argv)).toBe(1)
  95. expect(stderr.mock.calls.map(([chunk]) => String(chunk)).join('')).toContain('select a profile only once')
  96. })
  97. it('forwards late profile options to the application', () => {
  98. expect(parse(['web', 'task', '--profile', 'tui']))
  99. .toMatchObject({ profile: 'web', args: ['task', '--profile', 'tui'] })
  100. })
  101. it('routes the plugin pnpm forwarder', () => {
  102. expect(parse(['plugin', '--profile', 'tui', 'add', 'turtle-ui']))
  103. .toEqual({ mode: 'plugin', profile: 'tui', args: ['add', 'turtle-ui'] })
  104. expect(parse(['plugin', '--profile', 'tui', 'remove', 'turtle-ui']))
  105. .toEqual({ mode: 'plugin', profile: 'tui', args: ['remove', 'turtle-ui'] })
  106. expect(parse(['plugin', '--profile', 'tui', 'why', '@deepseek-ai/cordis']))
  107. .toEqual({ mode: 'plugin', profile: 'tui', args: ['why', '@deepseek-ai/cordis'] })
  108. // Unknown pnpm flags forward verbatim.
  109. expect(parse(['plugin', '--profile', 'tui', 'add', '--save-dev', 'x']))
  110. .toEqual({ mode: 'plugin', profile: 'tui', args: ['add', '--save-dev', 'x'] })
  111. })
  112. it('routes profile and web config dumps', () => {
  113. expect(parse(['--profile', 'web', '--dump-config']))
  114. .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: false, patches: [] })
  115. expect(parse(['--profile', 'web', '--dump-default-config']))
  116. .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: true, patches: [] })
  117. expect(parse(['--profile', 'rescue', '--from-default-profile', 'web', '--dump-config']))
  118. .toEqual({
  119. mode: 'dump-config',
  120. profile: 'rescue',
  121. fromDefaultProfile: 'web',
  122. defaultOnly: false,
  123. patches: [],
  124. })
  125. expect(parse(['--profile', 'tui', '--dump-config', '--patch', 'x.yml']))
  126. .toEqual({ mode: 'dump-config', profile: 'tui', defaultOnly: false, patches: ['x.yml'] })
  127. expect(parse(['web', '--dump-config']))
  128. .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: false, patches: [] })
  129. expect(parse(['web', '--dump-default-config']))
  130. .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: true, patches: [] })
  131. })
  132. it('rejects missing profile, removed flags, and contradictory inputs', () => {
  133. expect(exitCode([])).toBe(1)
  134. expect(exitCode(['--config', 'c.yml'])).toBe(1) // removed
  135. expect(exitCode(['-p', 'task'])).toBe(1) // removed
  136. expect(exitCode(['--profile', ''])).toBe(1)
  137. expect(exitCode(['--profile', 'x', '--from-default-profile='])).toBe(1)
  138. expect(exitCode(['--profile', 'x', '--from-default-profile'])).toBe(1)
  139. expect(exitCode(['--profile', 'x', '--patch='])).toBe(1)
  140. expect(exitCode(['--dump-config'])).toBe(1)
  141. expect(exitCode(['--profile', 'x', '--dump-config', '--dump-default-config'])).toBe(1)
  142. expect(exitCode(['--profile', 'x', '--dump-default-config', '--patch', 'p.yml'])).toBe(1)
  143. expect(exitCode(['--profile', 'x', '--dump-config', 'task'])).toBe(1)
  144. expect(exitCode(['--bogus'])).toBe(1)
  145. expect(exitCode(['web', '--dump-config', '--dump-default-config'])).toBe(1)
  146. expect(exitCode(['web', '--dump-default-config', '--patch', 'w.yml'])).toBe(1)
  147. expect(exitCode(['web', '--patch='])).toBe(1)
  148. // A dump never runs app command-line providers, so it cannot show what
  149. // those flags would decide; printing a tree that differs from the same
  150. // invocation's boot would mislead.
  151. expect(exitCode(['web', '--dump-config', '--port', '8080'])).toBe(1)
  152. expect(exitCode(['--profile', 'web', '--dump-config', '-h'])).toBe(1)
  153. expect(exitCode(['plugin', 'add', 'x'])).toBe(1) // --profile required
  154. expect(exitCode(['plugin', '--profile', 'tui'])).toBe(1) // nothing to forward
  155. expect(exitCode(['plugin', '--profile', ''])).toBe(1)
  156. expect(exitCode(['--profile', 'desktop'])).toBe(1)
  157. expect(exitCode(['--profile', 'Desktop'])).toBe(1)
  158. expect(exitCode(['--profile', 'DESKTOP'])).toBe(1)
  159. expect(exitCode(['--profile', 'desktop', '--dump-config'])).toBe(1)
  160. expect(exitCode(['plugin', '--profile', 'desktop', 'add', 'x'])).toBe(1)
  161. expect(exitCode(['plugin', '--profile', 'Desktop', 'add', 'x'])).toBe(1)
  162. expect(exitCode(['--from-default-profile', 'web', 'plugin', '--profile', 'x', 'add', 'y'])).toBe(1)
  163. })
  164. it('keeps its own help for an invocation with no app to hand it to', () => {
  165. const stdout = vi.spyOn(process.stdout, 'write').mockReturnValue(true)
  166. expect(exitCode(['--help'])).toBe(0)
  167. expect(stdout.mock.calls.map(([chunk]) => String(chunk)).join('')).not.toContain('help [command]')
  168. expect(exitCode(['-h'])).toBe(0)
  169. expect(exitCode(['--version'])).toBe(0)
  170. })
  171. })