|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { createCLI } from './cli.js'; |
| 3 | +import * as exampleModule from './tools/example.js'; |
| 4 | +import * as fetchExampleModule from './tools/fetch-example.js'; |
| 5 | + |
| 6 | +vi.mock('./tools/example.js'); |
| 7 | +vi.mock('./tools/fetch-example.js'); |
| 8 | + |
| 9 | +describe('CLI', () => { |
| 10 | + let consoleLogSpy: ReturnType<typeof vi.spyOn>; |
| 11 | + let consoleErrorSpy: ReturnType<typeof vi.spyOn>; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + vi.clearAllMocks(); |
| 15 | + consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 16 | + consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should create CLI program', () => { |
| 20 | + const program = createCLI(); |
| 21 | + expect(program.name()).toBe('mcp-template'); |
| 22 | + expect(program.description()).toContain('MCP template'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should handle example command', async () => { |
| 26 | + vi.spyOn(exampleModule, 'exampleTool').mockResolvedValue({ |
| 27 | + content: [ |
| 28 | + { |
| 29 | + type: 'text', |
| 30 | + text: 'Echo: Hello World', |
| 31 | + }, |
| 32 | + ], |
| 33 | + }); |
| 34 | + |
| 35 | + const program = createCLI(); |
| 36 | + await program.parseAsync(['node', 'cli', 'example', 'Hello World']); |
| 37 | + |
| 38 | + expect(exampleModule.exampleTool).toHaveBeenCalledWith({ |
| 39 | + message: 'Hello World', |
| 40 | + uppercase: false, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should handle example command with uppercase option', async () => { |
| 45 | + vi.spyOn(exampleModule, 'exampleTool').mockResolvedValue({ |
| 46 | + content: [ |
| 47 | + { |
| 48 | + type: 'text', |
| 49 | + text: 'Echo: HELLO WORLD', |
| 50 | + }, |
| 51 | + ], |
| 52 | + }); |
| 53 | + |
| 54 | + const program = createCLI(); |
| 55 | + await program.parseAsync(['node', 'cli', 'example', 'Hello World', '--uppercase']); |
| 56 | + |
| 57 | + expect(exampleModule.exampleTool).toHaveBeenCalledWith({ |
| 58 | + message: 'Hello World', |
| 59 | + uppercase: true, |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should handle errors gracefully', async () => { |
| 64 | + const mockProcessExit = vi.spyOn(process, 'exit').mockImplementation(() => { |
| 65 | + throw new Error('Process exit called'); |
| 66 | + }); |
| 67 | + |
| 68 | + vi.spyOn(exampleModule, 'exampleTool').mockRejectedValue(new Error('Test error')); |
| 69 | + |
| 70 | + const program = createCLI(); |
| 71 | + |
| 72 | + try { |
| 73 | + await program.parseAsync(['node', 'cli', 'example', 'Hello World']); |
| 74 | + } catch (error) { |
| 75 | + // Expected to throw due to process.exit mock |
| 76 | + } |
| 77 | + |
| 78 | + expect(mockProcessExit).toHaveBeenCalledWith(1); |
| 79 | + mockProcessExit.mockRestore(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should handle fetch-example command', async () => { |
| 83 | + vi.spyOn(fetchExampleModule, 'fetchExampleTool').mockResolvedValue({ |
| 84 | + content: [ |
| 85 | + { |
| 86 | + type: 'text', |
| 87 | + text: '# Fetch Example Results\n\nURL: https://httpbin.org/json\nStatus: 200 OK', |
| 88 | + }, |
| 89 | + ], |
| 90 | + isError: false, |
| 91 | + }); |
| 92 | + |
| 93 | + const program = createCLI(); |
| 94 | + await program.parseAsync(['node', 'cli', 'fetch-example', 'https://httpbin.org/json']); |
| 95 | + |
| 96 | + expect(fetchExampleModule.fetchExampleTool).toHaveBeenCalledWith({ |
| 97 | + url: 'https://httpbin.org/json', |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should handle fetch-example command with options', async () => { |
| 102 | + vi.spyOn(fetchExampleModule, 'fetchExampleTool').mockResolvedValue({ |
| 103 | + content: [ |
| 104 | + { |
| 105 | + type: 'text', |
| 106 | + text: '# Fetch Example Results\n\nURL: https://httpbin.org/json\nBackend: cache-memory', |
| 107 | + }, |
| 108 | + ], |
| 109 | + isError: false, |
| 110 | + }); |
| 111 | + |
| 112 | + const program = createCLI(); |
| 113 | + await program.parseAsync([ |
| 114 | + 'node', |
| 115 | + 'cli', |
| 116 | + 'fetch-example', |
| 117 | + 'https://httpbin.org/json', |
| 118 | + '--backend', |
| 119 | + 'cache-memory', |
| 120 | + '--no-cache', |
| 121 | + '--user-agent', |
| 122 | + 'Test-Agent/1.0', |
| 123 | + ]); |
| 124 | + |
| 125 | + expect(fetchExampleModule.fetchExampleTool).toHaveBeenCalledWith({ |
| 126 | + url: 'https://httpbin.org/json', |
| 127 | + backend: 'cache-memory', |
| 128 | + no_cache: true, |
| 129 | + user_agent: 'Test-Agent/1.0', |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should handle configure-fetch command', async () => { |
| 134 | + vi.spyOn(fetchExampleModule, 'configureFetchTool').mockResolvedValue({ |
| 135 | + content: [ |
| 136 | + { |
| 137 | + type: 'text', |
| 138 | + text: '# Fetch Configuration Updated\n\nBackend: cache-disk\nCache TTL: 60000ms', |
| 139 | + }, |
| 140 | + ], |
| 141 | + isError: false, |
| 142 | + }); |
| 143 | + |
| 144 | + const program = createCLI(); |
| 145 | + await program.parseAsync([ |
| 146 | + 'node', |
| 147 | + 'cli', |
| 148 | + 'configure-fetch', |
| 149 | + '--backend', |
| 150 | + 'cache-disk', |
| 151 | + '--cache-ttl', |
| 152 | + '60000', |
| 153 | + '--clear-cache', |
| 154 | + ]); |
| 155 | + |
| 156 | + expect(fetchExampleModule.configureFetchTool).toHaveBeenCalledWith({ |
| 157 | + backend: 'cache-disk', |
| 158 | + cache_ttl: 60000, |
| 159 | + clear_cache: true, |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should handle fetch-example errors', async () => { |
| 164 | + const mockProcessExit = vi.spyOn(process, 'exit').mockImplementation(() => { |
| 165 | + throw new Error('Process exit called'); |
| 166 | + }); |
| 167 | + |
| 168 | + vi.spyOn(fetchExampleModule, 'fetchExampleTool').mockResolvedValue({ |
| 169 | + content: [ |
| 170 | + { |
| 171 | + type: 'text', |
| 172 | + text: 'Network error occurred', |
| 173 | + }, |
| 174 | + ], |
| 175 | + isError: true, |
| 176 | + }); |
| 177 | + |
| 178 | + const program = createCLI(); |
| 179 | + |
| 180 | + try { |
| 181 | + await program.parseAsync(['node', 'cli', 'fetch-example', 'https://invalid-url']); |
| 182 | + } catch (error) { |
| 183 | + // Expected to throw due to process.exit mock |
| 184 | + } |
| 185 | + |
| 186 | + expect(mockProcessExit).toHaveBeenCalledWith(1); |
| 187 | + mockProcessExit.mockRestore(); |
| 188 | + }); |
| 189 | +}); |
0 commit comments