|
| 1 | +import * as commander from 'commander' |
| 2 | +import { addConnectCommandToProgram } from '../connect' |
| 3 | + |
| 4 | +type Opts = Record<string, unknown> |
| 5 | + |
| 6 | +// Build the real production command tree via addConnectCommandToProgram, |
| 7 | +// then replace the named subcommand's action with a spy so we can assert on |
| 8 | +// the options the handler would have received. This exercises the actual |
| 9 | +// preAction hook wired up in connect.ts — if that hook is removed or broken, |
| 10 | +// these tests fail. |
| 11 | +function exerciseSubcommand(subcommandName: string) { |
| 12 | + const program = new commander.Command() |
| 13 | + addConnectCommandToProgram(program) |
| 14 | + const connectCmd = program.commands.find((c) => c.name() === 'connect')! |
| 15 | + const sub = connectCmd.commands.find((c) => c.name() === subcommandName)! |
| 16 | + let captured: Opts = {} |
| 17 | + // commander's action callback receives any positional arguments first, then |
| 18 | + // the options object, then the Command instance — so options is always the |
| 19 | + // second-to-last argument regardless of the subcommand's positional shape. |
| 20 | + sub.action((...allArgs: unknown[]) => { |
| 21 | + captured = allArgs[allArgs.length - 2] as Opts |
| 22 | + }) |
| 23 | + return (args: string[]): Opts => { |
| 24 | + program.parse(['node', 'cli', ...args]) |
| 25 | + return captured |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +describe('addConnectCommandToProgram: shared option propagation', () => { |
| 30 | + it('forwards leading flags through to the subcommand handler', () => { |
| 31 | + const parse = exerciseSubcommand('parse') |
| 32 | + expect(parse(['connect', '-v', '-c', 'cfg', 'parse'])).toMatchObject({ |
| 33 | + verbose: true, |
| 34 | + config: 'cfg', |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + it('still honours trailing flags', () => { |
| 39 | + const parse = exerciseSubcommand('parse') |
| 40 | + expect(parse(['connect', 'parse', '-v', '-c', 'cfg'])).toMatchObject({ |
| 41 | + verbose: true, |
| 42 | + config: 'cfg', |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + it.each([ |
| 47 | + ['leading', ['connect', '-v', 'parse']], |
| 48 | + ['trailing', ['connect', 'parse', '-v']], |
| 49 | + ])('--verbose works in %s position', (_label, args) => { |
| 50 | + expect(exerciseSubcommand('parse')(args).verbose).toBe(true) |
| 51 | + }) |
| 52 | + |
| 53 | + it.each([ |
| 54 | + ['leading', ['connect', '-t', 'TKN', 'publish']], |
| 55 | + ['trailing', ['connect', 'publish', '-t', 'TKN']], |
| 56 | + ])('--token works in %s position', (_label, args) => { |
| 57 | + expect(exerciseSubcommand('publish')(args).token).toBe('TKN') |
| 58 | + }) |
| 59 | + |
| 60 | + it.each([ |
| 61 | + ['leading', ['connect', '-c', 'cfg.json', 'publish']], |
| 62 | + ['trailing', ['connect', 'publish', '-c', 'cfg.json']], |
| 63 | + ])('--config works in %s position', (_label, args) => { |
| 64 | + expect(exerciseSubcommand('publish')(args).config).toBe('cfg.json') |
| 65 | + }) |
| 66 | + |
| 67 | + it.each([ |
| 68 | + ['leading', ['connect', '--api-url', 'https://api.test', 'preview']], |
| 69 | + ['trailing', ['connect', 'preview', '--api-url', 'https://api.test']], |
| 70 | + ])('--api-url works in %s position', (_label, args) => { |
| 71 | + expect(exerciseSubcommand('preview')(args).apiUrl).toBe('https://api.test') |
| 72 | + }) |
| 73 | + |
| 74 | + it.each([ |
| 75 | + ['leading', ['connect', '--skip-update-check', 'parse']], |
| 76 | + ['trailing', ['connect', 'parse', '--skip-update-check']], |
| 77 | + ])('--skip-update-check works in %s position', (_label, args) => { |
| 78 | + expect(exerciseSubcommand('parse')(args).skipUpdateCheck).toBe(true) |
| 79 | + }) |
| 80 | + |
| 81 | + it.each([ |
| 82 | + ['leading', ['connect', '--dry-run', 'publish']], |
| 83 | + ['trailing', ['connect', 'publish', '--dry-run']], |
| 84 | + ])('--dry-run works in %s position', (_label, args) => { |
| 85 | + expect(exerciseSubcommand('publish')(args).dryRun).toBe(true) |
| 86 | + }) |
| 87 | + |
| 88 | + it('the same flag at both levels resolves rightmost-wins', () => { |
| 89 | + const parse = exerciseSubcommand('publish') |
| 90 | + expect(parse(['connect', '-t', 'PARENT', 'publish', '-t', 'CHILD']).token).toBe('CHILD') |
| 91 | + }) |
| 92 | + |
| 93 | + it('subcommand-only flags (e.g. --force on publish) still reach the handler', () => { |
| 94 | + const parse = exerciseSubcommand('publish') |
| 95 | + expect(parse(['connect', '-v', 'publish', '--force'])).toMatchObject({ |
| 96 | + verbose: true, |
| 97 | + force: true, |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + it('with no shared flag supplied, options stays at subcommand defaults', () => { |
| 102 | + const parse = exerciseSubcommand('parse') |
| 103 | + const opts = parse(['connect', 'parse']) |
| 104 | + expect(opts.verbose).toBeUndefined() |
| 105 | + expect(opts.token).toBeUndefined() |
| 106 | + expect(opts.config).toBeUndefined() |
| 107 | + }) |
| 108 | + |
| 109 | + it('multiple shared flags written in mixed positions all reach the handler', () => { |
| 110 | + const parse = exerciseSubcommand('publish') |
| 111 | + expect(parse(['connect', '-v', 'publish', '-t', 'TKN'])).toMatchObject({ |
| 112 | + verbose: true, |
| 113 | + token: 'TKN', |
| 114 | + }) |
| 115 | + }) |
| 116 | +}) |
0 commit comments