|
| 1 | +/** |
| 2 | + * @fileoverview Tests for CLI version flag functionality |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 6 | +import { VERSION, getVersion, getVersionInfo, parseVersion } from '../version.js'; |
| 7 | +import { handleCliArgs } from '../index.js'; |
| 8 | + |
| 9 | +describe('CLI Version Module', () => { |
| 10 | + describe('Version exports', () => { |
| 11 | + it('should export VERSION constant matching expected format', () => { |
| 12 | + expect(VERSION).toMatch(/^\d+\.\d+\.\d+(-.*)?$/); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should return version from getVersion function', () => { |
| 16 | + expect(getVersion()).toBe(VERSION); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should return version info from getVersionInfo', () => { |
| 20 | + const info = getVersionInfo(); |
| 21 | + expect(info.version).toBe(VERSION); |
| 22 | + |
| 23 | + const parsed = parseVersion(VERSION); |
| 24 | + if (parsed) { |
| 25 | + expect(info.major).toBe(parsed.major); |
| 26 | + expect(info.minor).toBe(parsed.minor); |
| 27 | + expect(info.patch).toBe(parsed.patch); |
| 28 | + } |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('CLI argument handling', () => { |
| 33 | + let consoleLogSpy: ReturnType<typeof vi.spyOn>; |
| 34 | + let logOutput: string[]; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + logOutput = []; |
| 38 | + // Mock console.log to capture output |
| 39 | + consoleLogSpy = vi.spyOn(console, 'log').mockImplementation((...args) => { |
| 40 | + logOutput.push(args.join(' ')); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + consoleLogSpy.mockRestore(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should display version with --version flag', () => { |
| 49 | + const result = handleCliArgs(['--version']); |
| 50 | + |
| 51 | + expect(result).toBe(true); |
| 52 | + expect(logOutput).toContain(`deepsource-mcp-server version ${VERSION}`); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should display version with -v flag', () => { |
| 56 | + const result = handleCliArgs(['-v']); |
| 57 | + |
| 58 | + expect(result).toBe(true); |
| 59 | + expect(logOutput).toContain(`deepsource-mcp-server version ${VERSION}`); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should display help with --help flag', () => { |
| 63 | + const result = handleCliArgs(['--help']); |
| 64 | + |
| 65 | + expect(result).toBe(true); |
| 66 | + expect(logOutput.join('\n')).toContain(`DeepSource MCP Server v${VERSION}`); |
| 67 | + expect(logOutput.join('\n')).toContain('Usage: deepsource-mcp-server [options]'); |
| 68 | + expect(logOutput.join('\n')).toContain('Options:'); |
| 69 | + expect(logOutput.join('\n')).toContain('-v, --version Display version information'); |
| 70 | + expect(logOutput.join('\n')).toContain('-h, --help Display this help message'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should display help with -h flag', () => { |
| 74 | + const result = handleCliArgs(['-h']); |
| 75 | + |
| 76 | + expect(result).toBe(true); |
| 77 | + expect(logOutput.join('\n')).toContain(`DeepSource MCP Server v${VERSION}`); |
| 78 | + expect(logOutput.join('\n')).toContain('Usage: deepsource-mcp-server [options]'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should handle --version flag at any position', () => { |
| 82 | + const result = handleCliArgs(['some', 'args', '--version']); |
| 83 | + |
| 84 | + expect(result).toBe(true); |
| 85 | + expect(logOutput).toContain(`deepsource-mcp-server version ${VERSION}`); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return false for non-CLI arguments', () => { |
| 89 | + const result = handleCliArgs(['some', 'other', 'args']); |
| 90 | + |
| 91 | + expect(result).toBe(false); |
| 92 | + expect(logOutput).toEqual([]); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should handle empty arguments', () => { |
| 96 | + const result = handleCliArgs([]); |
| 97 | + |
| 98 | + expect(result).toBe(false); |
| 99 | + expect(logOutput).toEqual([]); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should work without DEEPSOURCE_API_KEY for version flag', () => { |
| 103 | + // Remove DEEPSOURCE_API_KEY if present |
| 104 | + const originalApiKey = process.env.DEEPSOURCE_API_KEY; |
| 105 | + delete process.env.DEEPSOURCE_API_KEY; |
| 106 | + |
| 107 | + const result = handleCliArgs(['--version']); |
| 108 | + |
| 109 | + expect(result).toBe(true); |
| 110 | + expect(logOutput).toContain(`deepsource-mcp-server version ${VERSION}`); |
| 111 | + |
| 112 | + // Restore API key if it was set |
| 113 | + if (originalApiKey) { |
| 114 | + process.env.DEEPSOURCE_API_KEY = originalApiKey; |
| 115 | + } |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments