|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | +import { EventEmitter } from 'events'; |
| 3 | +import { spawn } from 'child_process'; |
| 4 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 5 | + |
| 6 | +// CRITICAL: Mock BEFORE imports to ensure proper mock chain |
| 7 | +vi.mock('child_process', () => ({ |
| 8 | + spawn: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +import { |
| 12 | + registerResources, |
| 13 | + getAvailableResources, |
| 14 | + supportsResources, |
| 15 | + RESOURCE_URIS, |
| 16 | +} from '../resources.js'; |
| 17 | +import { createMockExecutor } from '../../utils/test-common.js'; |
| 18 | + |
| 19 | +class MockChildProcess extends EventEmitter { |
| 20 | + stdout = new EventEmitter(); |
| 21 | + stderr = new EventEmitter(); |
| 22 | + pid = 12345; |
| 23 | +} |
| 24 | + |
| 25 | +describe('resources', () => { |
| 26 | + let mockProcess: MockChildProcess; |
| 27 | + let mockServer: McpServer; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + vi.clearAllMocks(); |
| 31 | + mockProcess = new MockChildProcess(); |
| 32 | + vi.mocked(spawn).mockReturnValue(mockProcess as any); |
| 33 | + |
| 34 | + // Create a mock MCP server |
| 35 | + mockServer = { |
| 36 | + resource: vi.fn(), |
| 37 | + } as unknown as McpServer; |
| 38 | + }); |
| 39 | + |
| 40 | + describe('Constants and Exports', () => { |
| 41 | + it('should export correct RESOURCE_URIS', () => { |
| 42 | + expect(RESOURCE_URIS.SIMULATORS).toBe('mcp://xcodebuild/simulators'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should export supportsResources function', () => { |
| 46 | + expect(typeof supportsResources).toBe('function'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should export registerResources function', () => { |
| 50 | + expect(typeof registerResources).toBe('function'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should export getAvailableResources function', () => { |
| 54 | + expect(typeof getAvailableResources).toBe('function'); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('supportsResources', () => { |
| 59 | + it('should return true for resource support', () => { |
| 60 | + expect(supportsResources()).toBe(true); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('getAvailableResources', () => { |
| 65 | + it('should return array of available resource URIs', () => { |
| 66 | + const resources = getAvailableResources(); |
| 67 | + expect(Array.isArray(resources)).toBe(true); |
| 68 | + expect(resources).toContain('mcp://xcodebuild/simulators'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should return non-empty array', () => { |
| 72 | + const resources = getAvailableResources(); |
| 73 | + expect(resources.length).toBeGreaterThan(0); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('registerResources', () => { |
| 78 | + it('should register simulators resource with correct parameters', () => { |
| 79 | + registerResources(mockServer); |
| 80 | + |
| 81 | + expect(mockServer.resource).toHaveBeenCalledWith( |
| 82 | + 'mcp://xcodebuild/simulators', |
| 83 | + 'Available iOS simulators with their UUIDs and states', |
| 84 | + { mimeType: 'application/json' }, |
| 85 | + expect.any(Function), |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should call server.resource once for each resource', () => { |
| 90 | + registerResources(mockServer); |
| 91 | + |
| 92 | + expect(mockServer.resource).toHaveBeenCalledTimes(1); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('Simulators Resource Handler', () => { |
| 97 | + let resourceHandler: () => Promise<{ contents: Array<{ type: 'text'; text: string }> }>; |
| 98 | + |
| 99 | + beforeEach(() => { |
| 100 | + registerResources(mockServer); |
| 101 | + // Extract the handler function from the mock call |
| 102 | + const calls = vi.mocked(mockServer.resource).mock.calls; |
| 103 | + resourceHandler = calls[0][3]; // Fourth parameter is the handler |
| 104 | + }); |
| 105 | + |
| 106 | + it('should handle successful simulator data retrieval', async () => { |
| 107 | + // Mock successful command execution |
| 108 | + setTimeout(() => { |
| 109 | + mockProcess.stdout.emit( |
| 110 | + 'data', |
| 111 | + JSON.stringify({ |
| 112 | + devices: { |
| 113 | + 'iOS 17.0': [ |
| 114 | + { |
| 115 | + name: 'iPhone 15 Pro', |
| 116 | + udid: 'ABC123-DEF456-GHI789', |
| 117 | + state: 'Shutdown', |
| 118 | + isAvailable: true, |
| 119 | + }, |
| 120 | + ], |
| 121 | + }, |
| 122 | + }), |
| 123 | + ); |
| 124 | + mockProcess.emit('close', 0); |
| 125 | + }, 0); |
| 126 | + |
| 127 | + const result = await resourceHandler(); |
| 128 | + |
| 129 | + expect(result.contents).toHaveLength(1); |
| 130 | + expect(result.contents[0].type).toBe('text'); |
| 131 | + expect(result.contents[0].text).toContain('Available iOS Simulators:'); |
| 132 | + expect(result.contents[0].text).toContain('iPhone 15 Pro'); |
| 133 | + expect(result.contents[0].text).toContain('ABC123-DEF456-GHI789'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should handle command execution failure', async () => { |
| 137 | + // Mock command failure |
| 138 | + setTimeout(() => { |
| 139 | + mockProcess.stderr.emit('data', 'Command failed'); |
| 140 | + mockProcess.emit('close', 1); |
| 141 | + }, 0); |
| 142 | + |
| 143 | + const result = await resourceHandler(); |
| 144 | + |
| 145 | + expect(result.contents).toHaveLength(1); |
| 146 | + expect(result.contents[0].type).toBe('text'); |
| 147 | + expect(result.contents[0].text).toContain('Error retrieving simulator data'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should handle JSON parsing errors', async () => { |
| 151 | + // Mock invalid JSON response |
| 152 | + setTimeout(() => { |
| 153 | + mockProcess.stdout.emit('data', 'invalid json'); |
| 154 | + mockProcess.emit('close', 0); |
| 155 | + }, 0); |
| 156 | + |
| 157 | + const result = await resourceHandler(); |
| 158 | + |
| 159 | + expect(result.contents).toHaveLength(1); |
| 160 | + expect(result.contents[0].type).toBe('text'); |
| 161 | + expect(result.contents[0].text).toBe('invalid json'); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should handle spawn errors', async () => { |
| 165 | + // Mock spawn error |
| 166 | + setTimeout(() => { |
| 167 | + mockProcess.emit('error', new Error('spawn xcrun ENOENT')); |
| 168 | + }, 0); |
| 169 | + |
| 170 | + const result = await resourceHandler(); |
| 171 | + |
| 172 | + expect(result.contents).toHaveLength(1); |
| 173 | + expect(result.contents[0].type).toBe('text'); |
| 174 | + expect(result.contents[0].text).toContain('Error retrieving simulator data'); |
| 175 | + expect(result.contents[0].text).toContain('spawn xcrun ENOENT'); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should handle empty simulator data', async () => { |
| 179 | + // Mock empty simulator response |
| 180 | + setTimeout(() => { |
| 181 | + mockProcess.stdout.emit('data', JSON.stringify({ devices: {} })); |
| 182 | + mockProcess.emit('close', 0); |
| 183 | + }, 0); |
| 184 | + |
| 185 | + const result = await resourceHandler(); |
| 186 | + |
| 187 | + expect(result.contents).toHaveLength(1); |
| 188 | + expect(result.contents[0].type).toBe('text'); |
| 189 | + expect(result.contents[0].text).toContain('Available iOS Simulators:'); |
| 190 | + }); |
| 191 | + |
| 192 | + it('should handle booted simulators correctly', async () => { |
| 193 | + // Mock simulator with booted state |
| 194 | + setTimeout(() => { |
| 195 | + mockProcess.stdout.emit( |
| 196 | + 'data', |
| 197 | + JSON.stringify({ |
| 198 | + devices: { |
| 199 | + 'iOS 17.0': [ |
| 200 | + { |
| 201 | + name: 'iPhone 15 Pro', |
| 202 | + udid: 'ABC123-DEF456-GHI789', |
| 203 | + state: 'Booted', |
| 204 | + isAvailable: true, |
| 205 | + }, |
| 206 | + ], |
| 207 | + }, |
| 208 | + }), |
| 209 | + ); |
| 210 | + mockProcess.emit('close', 0); |
| 211 | + }, 0); |
| 212 | + |
| 213 | + const result = await resourceHandler(); |
| 214 | + |
| 215 | + expect(result.contents[0].text).toContain('[Booted]'); |
| 216 | + }); |
| 217 | + |
| 218 | + it('should filter out unavailable simulators', async () => { |
| 219 | + // Mock mix of available and unavailable simulators |
| 220 | + setTimeout(() => { |
| 221 | + mockProcess.stdout.emit( |
| 222 | + 'data', |
| 223 | + JSON.stringify({ |
| 224 | + devices: { |
| 225 | + 'iOS 17.0': [ |
| 226 | + { |
| 227 | + name: 'iPhone 15 Pro', |
| 228 | + udid: 'ABC123-DEF456-GHI789', |
| 229 | + state: 'Shutdown', |
| 230 | + isAvailable: true, |
| 231 | + }, |
| 232 | + { |
| 233 | + name: 'iPhone 14', |
| 234 | + udid: 'XYZ789-UVW456-RST123', |
| 235 | + state: 'Shutdown', |
| 236 | + isAvailable: false, |
| 237 | + }, |
| 238 | + ], |
| 239 | + }, |
| 240 | + }), |
| 241 | + ); |
| 242 | + mockProcess.emit('close', 0); |
| 243 | + }, 0); |
| 244 | + |
| 245 | + const result = await resourceHandler(); |
| 246 | + |
| 247 | + expect(result.contents[0].text).toContain('iPhone 15 Pro'); |
| 248 | + expect(result.contents[0].text).not.toContain('iPhone 14'); |
| 249 | + }); |
| 250 | + |
| 251 | + it('should include next steps guidance', async () => { |
| 252 | + // Mock successful response |
| 253 | + setTimeout(() => { |
| 254 | + mockProcess.stdout.emit( |
| 255 | + 'data', |
| 256 | + JSON.stringify({ |
| 257 | + devices: { |
| 258 | + 'iOS 17.0': [ |
| 259 | + { |
| 260 | + name: 'iPhone 15 Pro', |
| 261 | + udid: 'ABC123-DEF456-GHI789', |
| 262 | + state: 'Shutdown', |
| 263 | + isAvailable: true, |
| 264 | + }, |
| 265 | + ], |
| 266 | + }, |
| 267 | + }), |
| 268 | + ); |
| 269 | + mockProcess.emit('close', 0); |
| 270 | + }, 0); |
| 271 | + |
| 272 | + const result = await resourceHandler(); |
| 273 | + |
| 274 | + expect(result.contents[0].text).toContain('Next Steps:'); |
| 275 | + expect(result.contents[0].text).toContain('boot_sim'); |
| 276 | + expect(result.contents[0].text).toContain('open_sim'); |
| 277 | + expect(result.contents[0].text).toContain('build_ios_sim_id_proj'); |
| 278 | + expect(result.contents[0].text).toContain('get_sim_app_path_id_proj'); |
| 279 | + }); |
| 280 | + }); |
| 281 | +}); |
0 commit comments