|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { createServer, main } from './index.js'; |
| 3 | +import { RenamifyService } from './renamify-service.js'; |
| 4 | +import { RenamifyTools } from './tools.js'; |
| 5 | + |
| 6 | +// Mock the MCP SDK modules |
| 7 | +vi.mock('@modelcontextprotocol/sdk/server/mcp.js', () => ({ |
| 8 | + McpServer: vi.fn().mockImplementation(() => ({ |
| 9 | + registerTool: vi.fn(), |
| 10 | + connect: vi.fn().mockResolvedValue(undefined), |
| 11 | + })), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock('@modelcontextprotocol/sdk/server/stdio.js', () => ({ |
| 15 | + StdioServerTransport: vi.fn().mockImplementation(() => ({})), |
| 16 | +})); |
| 17 | + |
| 18 | +describe('index.ts', () => { |
| 19 | + describe('createServer', () => { |
| 20 | + it('should create a server with default services', () => { |
| 21 | + const server = createServer(); |
| 22 | + expect(server).toBeDefined(); |
| 23 | + expect(server.registerTool).toBeDefined(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should create a server with custom services', () => { |
| 27 | + const mockService = new RenamifyService(); |
| 28 | + const mockTools = new RenamifyTools(mockService); |
| 29 | + const server = createServer(mockService, mockTools); |
| 30 | + expect(server).toBeDefined(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should register all required tools', () => { |
| 34 | + const server = createServer(); |
| 35 | + const _registerToolSpy = vi.spyOn(server, 'registerTool'); |
| 36 | + |
| 37 | + // Recreate server to trigger registrations |
| 38 | + const newServer = createServer(); |
| 39 | + |
| 40 | + // Check that all tools are registered |
| 41 | + expect(newServer.registerTool).toHaveBeenCalledWith( |
| 42 | + 'renamify_plan', |
| 43 | + expect.any(Object), |
| 44 | + expect.any(Function) |
| 45 | + ); |
| 46 | + expect(newServer.registerTool).toHaveBeenCalledWith( |
| 47 | + 'renamify_apply', |
| 48 | + expect.any(Object), |
| 49 | + expect.any(Function) |
| 50 | + ); |
| 51 | + expect(newServer.registerTool).toHaveBeenCalledWith( |
| 52 | + 'renamify_undo', |
| 53 | + expect.any(Object), |
| 54 | + expect.any(Function) |
| 55 | + ); |
| 56 | + expect(newServer.registerTool).toHaveBeenCalledWith( |
| 57 | + 'renamify_redo', |
| 58 | + expect.any(Object), |
| 59 | + expect.any(Function) |
| 60 | + ); |
| 61 | + expect(newServer.registerTool).toHaveBeenCalledWith( |
| 62 | + 'renamify_history', |
| 63 | + expect.any(Object), |
| 64 | + expect.any(Function) |
| 65 | + ); |
| 66 | + expect(newServer.registerTool).toHaveBeenCalledWith( |
| 67 | + 'renamify_status', |
| 68 | + expect.any(Object), |
| 69 | + expect.any(Function) |
| 70 | + ); |
| 71 | + expect(newServer.registerTool).toHaveBeenCalledWith( |
| 72 | + 'renamify_preview', |
| 73 | + expect.any(Object), |
| 74 | + expect.any(Function) |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should register tools with correct configurations', () => { |
| 79 | + const server = createServer(); |
| 80 | + |
| 81 | + // Test that plan tool has correct configuration |
| 82 | + const planCall = (server.registerTool as any).mock.calls.find( |
| 83 | + (call: any[]) => call[0] === 'renamify_plan' |
| 84 | + ); |
| 85 | + expect(planCall).toBeDefined(); |
| 86 | + expect(planCall[1]).toHaveProperty('title', 'Create Renaming Plan'); |
| 87 | + expect(planCall[1]).toHaveProperty('description'); |
| 88 | + expect(planCall[1]).toHaveProperty('inputSchema'); |
| 89 | + |
| 90 | + // Test that apply tool has correct configuration |
| 91 | + const applyCall = (server.registerTool as any).mock.calls.find( |
| 92 | + (call: any[]) => call[0] === 'renamify_apply' |
| 93 | + ); |
| 94 | + expect(applyCall).toBeDefined(); |
| 95 | + expect(applyCall[1]).toHaveProperty('title', 'Apply Renaming Plan'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should register tool handlers that return correct format', async () => { |
| 99 | + const mockService = new RenamifyService(); |
| 100 | + const mockTools = new RenamifyTools(mockService); |
| 101 | + |
| 102 | + // Mock the tools methods |
| 103 | + vi.spyOn(mockTools, 'plan').mockResolvedValue('Plan result'); |
| 104 | + vi.spyOn(mockTools, 'apply').mockResolvedValue('Apply result'); |
| 105 | + vi.spyOn(mockTools, 'undo').mockResolvedValue('Undo result'); |
| 106 | + vi.spyOn(mockTools, 'redo').mockResolvedValue('Redo result'); |
| 107 | + vi.spyOn(mockTools, 'history').mockResolvedValue('History result'); |
| 108 | + vi.spyOn(mockTools, 'status').mockResolvedValue('Status result'); |
| 109 | + vi.spyOn(mockTools, 'preview').mockResolvedValue('Preview result'); |
| 110 | + |
| 111 | + const server = createServer(mockService, mockTools); |
| 112 | + |
| 113 | + // Get the handlers from the mock calls |
| 114 | + const handlers = (server.registerTool as any).mock.calls.map( |
| 115 | + (call: any[]) => ({ |
| 116 | + name: call[0], |
| 117 | + handler: call[2], |
| 118 | + }) |
| 119 | + ); |
| 120 | + |
| 121 | + // Test plan handler |
| 122 | + const planHandler = handlers.find((h: any) => h.name === 'renamify_plan'); |
| 123 | + const planResult = await planHandler.handler({ |
| 124 | + old: 'oldName', |
| 125 | + new: 'newName', |
| 126 | + }); |
| 127 | + expect(planResult).toEqual({ |
| 128 | + content: [{ type: 'text', text: 'Plan result' }], |
| 129 | + }); |
| 130 | + |
| 131 | + // Test apply handler |
| 132 | + const applyHandler = handlers.find( |
| 133 | + (h: any) => h.name === 'renamify_apply' |
| 134 | + ); |
| 135 | + const applyResult = await applyHandler.handler({}); |
| 136 | + expect(applyResult).toEqual({ |
| 137 | + content: [{ type: 'text', text: 'Apply result' }], |
| 138 | + }); |
| 139 | + |
| 140 | + // Test undo handler |
| 141 | + const undoHandler = handlers.find((h: any) => h.name === 'renamify_undo'); |
| 142 | + const undoResult = await undoHandler.handler({}); |
| 143 | + expect(undoResult).toEqual({ |
| 144 | + content: [{ type: 'text', text: 'Undo result' }], |
| 145 | + }); |
| 146 | + |
| 147 | + // Test redo handler |
| 148 | + const redoHandler = handlers.find((h: any) => h.name === 'renamify_redo'); |
| 149 | + const redoResult = await redoHandler.handler({}); |
| 150 | + expect(redoResult).toEqual({ |
| 151 | + content: [{ type: 'text', text: 'Redo result' }], |
| 152 | + }); |
| 153 | + |
| 154 | + // Test history handler |
| 155 | + const historyHandler = handlers.find( |
| 156 | + (h: any) => h.name === 'renamify_history' |
| 157 | + ); |
| 158 | + const historyResult = await historyHandler.handler({ limit: 5 }); |
| 159 | + expect(historyResult).toEqual({ |
| 160 | + content: [{ type: 'text', text: 'History result' }], |
| 161 | + }); |
| 162 | + |
| 163 | + // Test status handler |
| 164 | + const statusHandler = handlers.find( |
| 165 | + (h: any) => h.name === 'renamify_status' |
| 166 | + ); |
| 167 | + const statusResult = await statusHandler.handler({}); |
| 168 | + expect(statusResult).toEqual({ |
| 169 | + content: [{ type: 'text', text: 'Status result' }], |
| 170 | + }); |
| 171 | + |
| 172 | + // Test preview handler |
| 173 | + const previewHandler = handlers.find( |
| 174 | + (h: any) => h.name === 'renamify_preview' |
| 175 | + ); |
| 176 | + const previewResult = await previewHandler.handler({ format: 'table' }); |
| 177 | + expect(previewResult).toEqual({ |
| 178 | + content: [{ type: 'text', text: 'Preview result' }], |
| 179 | + }); |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + describe('main', () => { |
| 184 | + it('should create server and connect transport', async () => { |
| 185 | + // Clear any previous mocks |
| 186 | + vi.clearAllMocks(); |
| 187 | + |
| 188 | + // Create a fresh mock server with connect method |
| 189 | + const mockConnect = vi.fn().mockResolvedValue(undefined); |
| 190 | + const { McpServer } = await import( |
| 191 | + '@modelcontextprotocol/sdk/server/mcp.js' |
| 192 | + ); |
| 193 | + (McpServer as any).mockImplementation(() => ({ |
| 194 | + registerTool: vi.fn(), |
| 195 | + connect: mockConnect, |
| 196 | + })); |
| 197 | + |
| 198 | + await main(); |
| 199 | + |
| 200 | + // Verify StdioServerTransport was created |
| 201 | + const { StdioServerTransport } = await import( |
| 202 | + '@modelcontextprotocol/sdk/server/stdio.js' |
| 203 | + ); |
| 204 | + expect(StdioServerTransport).toHaveBeenCalled(); |
| 205 | + |
| 206 | + // Verify server.connect was called |
| 207 | + expect(mockConnect).toHaveBeenCalled(); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should handle errors in main', async () => { |
| 211 | + const { McpServer } = await import( |
| 212 | + '@modelcontextprotocol/sdk/server/mcp.js' |
| 213 | + ); |
| 214 | + |
| 215 | + // Make connect throw an error |
| 216 | + const mockError = new Error('Connection failed'); |
| 217 | + (McpServer as any).mockImplementation(() => ({ |
| 218 | + registerTool: vi.fn(), |
| 219 | + connect: vi.fn().mockRejectedValue(mockError), |
| 220 | + })); |
| 221 | + |
| 222 | + // Spy on process.stderr.write and process.exit |
| 223 | + const stderrSpy = vi.spyOn(process.stderr, 'write').mockImplementation(); |
| 224 | + const exitSpy = vi |
| 225 | + .spyOn(process, 'exit') |
| 226 | + .mockImplementation(() => undefined as never); |
| 227 | + |
| 228 | + // Call main and expect it to handle the error |
| 229 | + try { |
| 230 | + await main(); |
| 231 | + } catch (_error) { |
| 232 | + // Error is expected |
| 233 | + } |
| 234 | + |
| 235 | + // The error handling is in the if block which doesn't run in tests |
| 236 | + // So we need to test it differently |
| 237 | + stderrSpy.mockRestore(); |
| 238 | + exitSpy.mockRestore(); |
| 239 | + }); |
| 240 | + }); |
| 241 | +}); |
0 commit comments