|
| 1 | +import { describe, test, expect, beforeEach, afterEach } from 'bun:test' |
| 2 | +import { Command } from 'commander' |
| 3 | + |
| 4 | +describe('CLI Argument Parsing', () => { |
| 5 | + let originalArgv: string[] |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + originalArgv = process.argv |
| 9 | + }) |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + process.argv = originalArgv |
| 13 | + }) |
| 14 | + |
| 15 | + function parseTestArgs(args: string[]) { |
| 16 | + process.argv = ['node', 'codecane', ...args] |
| 17 | + |
| 18 | + const program = new Command() |
| 19 | + program |
| 20 | + .name('codecane') |
| 21 | + .version('1.0.0', '-v, --version', 'Print the CLI version') |
| 22 | + .option('--agent <agent-id>', 'Specify which agent to use') |
| 23 | + .option('--clear-logs', 'Remove any existing CLI log files') |
| 24 | + .argument('[prompt...]', 'Initial prompt to send') |
| 25 | + .allowExcessArguments(true) |
| 26 | + .exitOverride() // Prevent process.exit in tests |
| 27 | + |
| 28 | + try { |
| 29 | + program.parse(process.argv) |
| 30 | + } catch (error) { |
| 31 | + // Commander throws on --help, --version in exitOverride mode |
| 32 | + if (error instanceof Error && error.message.includes('(outputHelp)')) { |
| 33 | + return { help: true } |
| 34 | + } |
| 35 | + if (error instanceof Error && (error.message.includes('(version)') || error.message.includes('1.0.0'))) { |
| 36 | + return { version: true } |
| 37 | + } |
| 38 | + throw error |
| 39 | + } |
| 40 | + |
| 41 | + const options = program.opts() |
| 42 | + const promptArgs = program.args |
| 43 | + |
| 44 | + return { |
| 45 | + agent: options.agent, |
| 46 | + clearLogs: options.clearLogs || false, |
| 47 | + initialPrompt: promptArgs.length > 0 ? promptArgs.join(' ') : null, |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + test('parses --agent flag correctly', () => { |
| 52 | + const result = parseTestArgs(['--agent', 'file-picker', 'find all TypeScript files']) |
| 53 | + expect(result.agent).toBe('file-picker') |
| 54 | + expect(result.initialPrompt).toBe('find all TypeScript files') |
| 55 | + }) |
| 56 | + |
| 57 | + test('parses --agent with full agent ID', () => { |
| 58 | + const result = parseTestArgs(['--agent', 'codebuff/base-lite@1.0.0', 'hello']) |
| 59 | + expect(result.agent).toBe('codebuff/base-lite@1.0.0') |
| 60 | + expect(result.initialPrompt).toBe('hello') |
| 61 | + }) |
| 62 | + |
| 63 | + test('works without --agent flag (defaults to base)', () => { |
| 64 | + const result = parseTestArgs(['create a new component']) |
| 65 | + expect(result.agent).toBeUndefined() |
| 66 | + expect(result.initialPrompt).toBe('create a new component') |
| 67 | + }) |
| 68 | + |
| 69 | + test('parses --clear-logs flag', () => { |
| 70 | + const result = parseTestArgs(['--clear-logs', 'hello']) |
| 71 | + expect(result.clearLogs).toBe(true) |
| 72 | + expect(result.initialPrompt).toBe('hello') |
| 73 | + }) |
| 74 | + |
| 75 | + test('handles multiple flags together', () => { |
| 76 | + const result = parseTestArgs(['--agent', 'reviewer', '--clear-logs', 'review my code']) |
| 77 | + expect(result.agent).toBe('reviewer') |
| 78 | + expect(result.clearLogs).toBe(true) |
| 79 | + expect(result.initialPrompt).toBe('review my code') |
| 80 | + }) |
| 81 | + |
| 82 | + test('handles prompt with no flags', () => { |
| 83 | + const result = parseTestArgs(['this is a test prompt']) |
| 84 | + expect(result.agent).toBeUndefined() |
| 85 | + expect(result.clearLogs).toBe(false) |
| 86 | + expect(result.initialPrompt).toBe('this is a test prompt') |
| 87 | + }) |
| 88 | + |
| 89 | + test('handles empty arguments', () => { |
| 90 | + const result = parseTestArgs([]) |
| 91 | + expect(result.agent).toBeUndefined() |
| 92 | + expect(result.clearLogs).toBe(false) |
| 93 | + expect(result.initialPrompt).toBeNull() |
| 94 | + }) |
| 95 | + |
| 96 | + test('handles multi-word prompt', () => { |
| 97 | + const result = parseTestArgs(['--agent', 'base', 'fix the bug in auth.ts file']) |
| 98 | + expect(result.agent).toBe('base') |
| 99 | + expect(result.initialPrompt).toBe('fix the bug in auth.ts file') |
| 100 | + }) |
| 101 | + |
| 102 | + test('handles --help flag', () => { |
| 103 | + const result = parseTestArgs(['--help']) |
| 104 | + expect(result.help).toBe(true) |
| 105 | + }) |
| 106 | + |
| 107 | + test('handles -h flag', () => { |
| 108 | + const result = parseTestArgs(['-h']) |
| 109 | + expect(result.help).toBe(true) |
| 110 | + }) |
| 111 | + |
| 112 | + test('handles --version flag', () => { |
| 113 | + const result = parseTestArgs(['--version']) |
| 114 | + expect(result.version).toBe(true) |
| 115 | + }) |
| 116 | + |
| 117 | + test('handles -v flag', () => { |
| 118 | + const result = parseTestArgs(['-v']) |
| 119 | + expect(result.version).toBe(true) |
| 120 | + }) |
| 121 | +}) |
0 commit comments