|
1 | 1 | import { beforeEach, describe, expect, it, vi } from 'vitest';
|
2 | 2 | import { createCLI } from './cli.js';
|
3 | 3 | import * as exampleModule from './tools/example.js';
|
| 4 | +import * as fetchExampleModule from './tools/fetch-example.js'; |
4 | 5 |
|
5 | 6 | vi.mock('./tools/example.js');
|
| 7 | +vi.mock('./tools/fetch-example.js'); |
6 | 8 |
|
7 | 9 | describe('CLI', () => {
|
8 | 10 | let consoleLogSpy: ReturnType<typeof vi.spyOn>;
|
@@ -76,4 +78,112 @@ describe('CLI', () => {
|
76 | 78 | expect(mockProcessExit).toHaveBeenCalledWith(1);
|
77 | 79 | mockProcessExit.mockRestore();
|
78 | 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 | + }); |
79 | 189 | });
|
0 commit comments