diff --git a/src/cli/__tests__/cli.test.ts b/src/cli/__tests__/cli.test.ts new file mode 100644 index 000000000..0a48e336e --- /dev/null +++ b/src/cli/__tests__/cli.test.ts @@ -0,0 +1,70 @@ +import { createProgram } from '../cli.js'; +import { PACKAGE_VERSION } from '../constants.js'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; + +describe('CLI version flag', () => { + let mockExit: ReturnType; + let mockStdoutWrite: ReturnType; + let stdoutOutput: string; + + beforeEach(() => { + stdoutOutput = ''; + mockExit = vi.spyOn(process, 'exit').mockImplementation(() => { + throw new Error('process.exit called'); + }); + mockStdoutWrite = vi.spyOn(process.stdout, 'write').mockImplementation((chunk: string | Uint8Array) => { + stdoutOutput += chunk.toString(); + return true; + }); + }); + + afterEach(() => { + mockExit.mockRestore(); + mockStdoutWrite.mockRestore(); + }); + + it('should output version with --version flag', async () => { + const program = createProgram(); + program.configureOutput({ + writeOut: (str: string) => { + stdoutOutput += str; + }, + }); + + try { + await program.parseAsync(['node', 'agentcore', '--version']); + } catch { + // Expected: process.exit is called + } + + expect(stdoutOutput).toContain(PACKAGE_VERSION); + expect(mockExit).toHaveBeenCalledWith(0); + }); + + it('should output version with -V flag', async () => { + const program = createProgram(); + program.configureOutput({ + writeOut: (str: string) => { + stdoutOutput += str; + }, + }); + + try { + await program.parseAsync(['node', 'agentcore', '-V']); + } catch { + // Expected: process.exit is called + } + + expect(stdoutOutput).toContain(PACKAGE_VERSION); + expect(mockExit).toHaveBeenCalledWith(0); + }); + + it('should have version option configured correctly', () => { + const program = createProgram(); + const versionOption = program.options.find(opt => opt.long === '--version'); + + expect(versionOption).toBeDefined(); + expect(versionOption?.short).toBe('-V'); + expect(versionOption?.description).toBe('Output the current version'); + }); +}); diff --git a/src/cli/cli.ts b/src/cli/cli.ts index 4d992ad78..720187c6e 100644 --- a/src/cli/cli.ts +++ b/src/cli/cli.ts @@ -95,7 +95,7 @@ export function createProgram(): Command { program .name('agentcore') .description(COMMAND_DESCRIPTIONS.program) - .version(PACKAGE_VERSION) + .version(PACKAGE_VERSION, '-V, --version', 'Output the current version') .showHelpAfterError() .showSuggestionAfterError();