| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- import { afterEach, describe, expect, it, vi } from 'vitest'
- import { parseDshArgs } from '../src/args.ts'
- const parse = (argv: string[]) => parseDshArgs(argv, '1.2.3')
- /** Capture the process exit code while muting Commander's output. */
- function exitCode(argv: string[]): number {
- const exit = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit') })
- vi.spyOn(process.stdout, 'write').mockReturnValue(true)
- vi.spyOn(process.stderr, 'write').mockReturnValue(true)
- try {
- parse(argv)
- throw new Error(`expected ${JSON.stringify(argv)} to exit`)
- } catch {
- return exit.mock.calls.at(-1)?.[0] as number
- } finally {
- vi.restoreAllMocks()
- }
- }
- afterEach(() => { vi.restoreAllMocks() })
- describe('parseDshArgs', () => {
- it('routes profile boots and shorthand, handing the rest to the app', () => {
- expect(parse(['--profile', 'tui'])).toEqual({ mode: 'profile', profile: 'tui', patches: [], args: [] })
- expect(parse(['--profile', 'tui', '--patch', 'a.yml', '--patch', 'b.yml']))
- .toEqual({ mode: 'profile', profile: 'tui', patches: ['a.yml', 'b.yml'], args: [] })
- expect(parse(['--profile', 'rescue', '--from-default-profile', 'web']))
- .toEqual({ mode: 'profile', profile: 'rescue', fromDefaultProfile: 'web', patches: [], args: [] })
- expect(parse(['web'])).toEqual({ mode: 'profile', profile: 'web', patches: [], args: [] })
- expect(parse(['web', '--patch', 'web.yml']))
- .toEqual({ mode: 'profile', profile: 'web', patches: ['web.yml'], args: [] })
- })
- it('ends the launcher flags at the first token it does not own', () => {
- // App flags, including its -h, and positionals reach the app verbatim.
- expect(parse(['--profile', 'tui', '--resume', 'abc']))
- .toEqual({ mode: 'profile', profile: 'tui', patches: [], args: ['--resume', 'abc'] })
- expect(parse(['--profile', 'web', '-h']))
- .toEqual({ mode: 'profile', profile: 'web', patches: [], args: ['-h'] })
- expect(parse(['web', '--host', '127.0.0.1', '--port', '8080', '--no-open', '--future-web-flag']))
- .toEqual({ mode: 'profile', profile: 'web', patches: [], args: ['--host', '127.0.0.1', '--port', '8080', '--no-open', '--future-web-flag'] })
- expect(parse(['--profile', 'headless', 'run', 'the', 'tests']))
- .toEqual({ mode: 'profile', profile: 'headless', patches: [], args: ['run', 'the', 'tests'] })
- // Launcher flags placed after that boundary belong to the app too.
- expect(parse(['--profile', 'tui', '--patch', 'a.yml', '--resume', 'b', '--patch', 'late.yml']))
- .toEqual({ mode: 'profile', profile: 'tui', patches: ['a.yml'], args: ['--resume', 'b', '--patch', 'late.yml'] })
- expect(parse(['--profile', 'rescue', '--resume', 'abc', '--from-default-profile', 'web']))
- .toEqual({
- mode: 'profile',
- profile: 'rescue',
- patches: [],
- args: ['--resume', 'abc', '--from-default-profile', 'web'],
- })
- expect(parse(['web', '--from-default-profile', 'web']))
- .toEqual({ mode: 'profile', profile: 'web', fromDefaultProfile: 'web', patches: [], args: [] })
- })
- it.each(['web', 'headless', 'sdk', 'sdk-minimal', 'acp', 'tui', 'custom', 'run', 'help'])('expands %s without looking up profiles', (profile) => {
- for (const args of [
- [], ['task', 'words'], ['--help'], ['-h'], ['web'],
- ['--patch', 'a.yml', '--patch', 'b.yml'],
- ['--from-default-profile', 'web', '--help'],
- ['--dump-config'], ['--dump-default-config'],
- ['--patch', 'a.yml', '--resume', 'id', '--patch', 'late.yml'],
- ['--', '--help'], ['--', '--', 'task'],
- ['plugin'], ['--', 'plugin'],
- ]) {
- expect(parse([profile, ...args])).toEqual(parse(['--profile', profile, ...args]))
- }
- })
- it('reserves leading plugin for management and forwards later command names', () => {
- expect(parse(['--profile', 'plugin'])).toMatchObject({ mode: 'profile', profile: 'plugin' })
- expect(parse(['--profile', 'x', 'plugin', 'add', 'y']))
- .toMatchObject({ mode: 'profile', profile: 'x', args: ['plugin', 'add', 'y'] })
- expect(parse(['headless', 'web'])).toMatchObject({ profile: 'headless', args: ['web'] })
- expect(exitCode(['--patch', 'a.yml', 'tui'])).toBe(1)
- expect(exitCode(['--', 'tui'])).toBe(1)
- })
- it.each([
- [''], ['desktop'], ['Desktop'], ['DESKTOP'],
- ['custom', '--patch='], ['custom', '--from-default-profile='],
- ['custom', '--dump-config', '--dump-default-config'],
- ['custom', '--dump-default-config', '--patch', 'a.yml'],
- ['custom', '--dump-config', 'task'],
- ])('rejects invalid shorthand %j', (...argv: string[]) => {
- expect(exitCode(argv)).toBe(1)
- })
- it.each(['-V', '--version'])('prints the launcher version for shorthand %s', (flag) => {
- expect(exitCode(['custom', flag])).toBe(0)
- expect(parse(['custom', 'task', flag])).toMatchObject({ args: ['task', flag] })
- })
- it.each([
- ['web', '--profile', 'tui'],
- ['--profile', 'web', '--profile', 'tui'],
- ['--profile=web', '--profile=web'],
- ['plugin', '--profile', 'web', '--profile', 'tui', 'add', 'x'],
- ])('rejects repeated profile selection %j', (...argv: string[]) => {
- const stderr = vi.spyOn(process.stderr, 'write').mockReturnValue(true)
- expect(exitCode(argv)).toBe(1)
- expect(stderr.mock.calls.map(([chunk]) => String(chunk)).join('')).toContain('select a profile only once')
- })
- it('forwards late profile options to the application', () => {
- expect(parse(['web', 'task', '--profile', 'tui']))
- .toMatchObject({ profile: 'web', args: ['task', '--profile', 'tui'] })
- })
- it('routes the plugin pnpm forwarder', () => {
- expect(parse(['plugin', '--profile', 'tui', 'add', 'turtle-ui']))
- .toEqual({ mode: 'plugin', profile: 'tui', args: ['add', 'turtle-ui'] })
- expect(parse(['plugin', '--profile', 'tui', 'remove', 'turtle-ui']))
- .toEqual({ mode: 'plugin', profile: 'tui', args: ['remove', 'turtle-ui'] })
- expect(parse(['plugin', '--profile', 'tui', 'why', '@deepseek-ai/cordis']))
- .toEqual({ mode: 'plugin', profile: 'tui', args: ['why', '@deepseek-ai/cordis'] })
- // Unknown pnpm flags forward verbatim.
- expect(parse(['plugin', '--profile', 'tui', 'add', '--save-dev', 'x']))
- .toEqual({ mode: 'plugin', profile: 'tui', args: ['add', '--save-dev', 'x'] })
- })
- it('routes profile and web config dumps', () => {
- expect(parse(['--profile', 'web', '--dump-config']))
- .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: false, patches: [] })
- expect(parse(['--profile', 'web', '--dump-default-config']))
- .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: true, patches: [] })
- expect(parse(['--profile', 'rescue', '--from-default-profile', 'web', '--dump-config']))
- .toEqual({
- mode: 'dump-config',
- profile: 'rescue',
- fromDefaultProfile: 'web',
- defaultOnly: false,
- patches: [],
- })
- expect(parse(['--profile', 'tui', '--dump-config', '--patch', 'x.yml']))
- .toEqual({ mode: 'dump-config', profile: 'tui', defaultOnly: false, patches: ['x.yml'] })
- expect(parse(['web', '--dump-config']))
- .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: false, patches: [] })
- expect(parse(['web', '--dump-default-config']))
- .toEqual({ mode: 'dump-config', profile: 'web', defaultOnly: true, patches: [] })
- })
- it('rejects missing profile, removed flags, and contradictory inputs', () => {
- expect(exitCode([])).toBe(1)
- expect(exitCode(['--config', 'c.yml'])).toBe(1) // removed
- expect(exitCode(['-p', 'task'])).toBe(1) // removed
- expect(exitCode(['--profile', ''])).toBe(1)
- expect(exitCode(['--profile', 'x', '--from-default-profile='])).toBe(1)
- expect(exitCode(['--profile', 'x', '--from-default-profile'])).toBe(1)
- expect(exitCode(['--profile', 'x', '--patch='])).toBe(1)
- expect(exitCode(['--dump-config'])).toBe(1)
- expect(exitCode(['--profile', 'x', '--dump-config', '--dump-default-config'])).toBe(1)
- expect(exitCode(['--profile', 'x', '--dump-default-config', '--patch', 'p.yml'])).toBe(1)
- expect(exitCode(['--profile', 'x', '--dump-config', 'task'])).toBe(1)
- expect(exitCode(['--bogus'])).toBe(1)
- expect(exitCode(['web', '--dump-config', '--dump-default-config'])).toBe(1)
- expect(exitCode(['web', '--dump-default-config', '--patch', 'w.yml'])).toBe(1)
- expect(exitCode(['web', '--patch='])).toBe(1)
- // A dump never runs app command-line providers, so it cannot show what
- // those flags would decide; printing a tree that differs from the same
- // invocation's boot would mislead.
- expect(exitCode(['web', '--dump-config', '--port', '8080'])).toBe(1)
- expect(exitCode(['--profile', 'web', '--dump-config', '-h'])).toBe(1)
- expect(exitCode(['plugin', 'add', 'x'])).toBe(1) // --profile required
- expect(exitCode(['plugin', '--profile', 'tui'])).toBe(1) // nothing to forward
- expect(exitCode(['plugin', '--profile', ''])).toBe(1)
- expect(exitCode(['--profile', 'desktop'])).toBe(1)
- expect(exitCode(['--profile', 'Desktop'])).toBe(1)
- expect(exitCode(['--profile', 'DESKTOP'])).toBe(1)
- expect(exitCode(['--profile', 'desktop', '--dump-config'])).toBe(1)
- expect(exitCode(['plugin', '--profile', 'desktop', 'add', 'x'])).toBe(1)
- expect(exitCode(['plugin', '--profile', 'Desktop', 'add', 'x'])).toBe(1)
- expect(exitCode(['--from-default-profile', 'web', 'plugin', '--profile', 'x', 'add', 'y'])).toBe(1)
- })
- it('keeps its own help for an invocation with no app to hand it to', () => {
- const stdout = vi.spyOn(process.stdout, 'write').mockReturnValue(true)
- expect(exitCode(['--help'])).toBe(0)
- expect(stdout.mock.calls.map(([chunk]) => String(chunk)).join('')).not.toContain('help [command]')
- expect(exitCode(['-h'])).toBe(0)
- expect(exitCode(['--version'])).toBe(0)
- })
- })
|