|
| 1 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 2 | +import * as z from 'zod'; |
| 3 | +import { |
| 4 | + createMockCommandResponse, |
| 5 | + createMockFileSystemExecutor, |
| 6 | +} from '../../../../test-utils/mock-executors.ts'; |
| 7 | +import type { CommandExecutor } from '../../../../utils/execution/index.ts'; |
| 8 | +import { sessionStore } from '../../../../utils/session-store.ts'; |
| 9 | +import { schema, handler, build_run_deviceLogic } from '../build_run_device.ts'; |
| 10 | + |
| 11 | +describe('build_run_device tool', () => { |
| 12 | + beforeEach(() => { |
| 13 | + sessionStore.clear(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('exposes only non-session fields in public schema', () => { |
| 17 | + const schemaObj = z.strictObject(schema); |
| 18 | + |
| 19 | + expect(schemaObj.safeParse({}).success).toBe(true); |
| 20 | + expect(schemaObj.safeParse({ extraArgs: ['-quiet'] }).success).toBe(true); |
| 21 | + expect(schemaObj.safeParse({ env: { FOO: 'bar' } }).success).toBe(true); |
| 22 | + |
| 23 | + expect(schemaObj.safeParse({ scheme: 'App' }).success).toBe(false); |
| 24 | + expect(schemaObj.safeParse({ deviceId: 'device-id' }).success).toBe(false); |
| 25 | + |
| 26 | + const schemaKeys = Object.keys(schema).sort(); |
| 27 | + expect(schemaKeys).toEqual(['env', 'extraArgs']); |
| 28 | + }); |
| 29 | + |
| 30 | + it('requires scheme + deviceId and project/workspace via handler', async () => { |
| 31 | + const missingAll = await handler({}); |
| 32 | + expect(missingAll.isError).toBe(true); |
| 33 | + expect(missingAll.content[0].text).toContain('Provide scheme and deviceId'); |
| 34 | + |
| 35 | + const missingSource = await handler({ scheme: 'MyApp', deviceId: 'DEVICE-UDID' }); |
| 36 | + expect(missingSource.isError).toBe(true); |
| 37 | + expect(missingSource.content[0].text).toContain('Provide a project or workspace'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('builds, installs, and launches successfully', async () => { |
| 41 | + const commands: string[] = []; |
| 42 | + const mockExecutor: CommandExecutor = async (command) => { |
| 43 | + commands.push(command.join(' ')); |
| 44 | + |
| 45 | + if (command.includes('-showBuildSettings')) { |
| 46 | + return createMockCommandResponse({ |
| 47 | + success: true, |
| 48 | + output: 'BUILT_PRODUCTS_DIR = /tmp/build\nFULL_PRODUCT_NAME = MyApp.app\n', |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + if (command[0] === '/bin/sh') { |
| 53 | + return createMockCommandResponse({ success: true, output: 'io.sentry.MyApp' }); |
| 54 | + } |
| 55 | + |
| 56 | + return createMockCommandResponse({ success: true, output: 'OK' }); |
| 57 | + }; |
| 58 | + |
| 59 | + const result = await build_run_deviceLogic( |
| 60 | + { |
| 61 | + projectPath: '/tmp/MyApp.xcodeproj', |
| 62 | + scheme: 'MyApp', |
| 63 | + deviceId: 'DEVICE-UDID', |
| 64 | + }, |
| 65 | + mockExecutor, |
| 66 | + createMockFileSystemExecutor({ |
| 67 | + existsSync: () => true, |
| 68 | + readFile: async () => JSON.stringify({ result: { process: { processIdentifier: 1234 } } }), |
| 69 | + }), |
| 70 | + ); |
| 71 | + |
| 72 | + expect(result.isError).toBe(false); |
| 73 | + expect(result.content[0].text).toContain('device build and run succeeded'); |
| 74 | + expect(result.nextStepParams).toMatchObject({ |
| 75 | + start_device_log_cap: { deviceId: 'DEVICE-UDID', bundleId: 'io.sentry.MyApp' }, |
| 76 | + stop_app_device: { deviceId: 'DEVICE-UDID', processId: 1234 }, |
| 77 | + }); |
| 78 | + |
| 79 | + expect(commands.some((c) => c.includes('xcodebuild') && c.includes('build'))).toBe(true); |
| 80 | + expect(commands.some((c) => c.includes('xcodebuild') && c.includes('-showBuildSettings'))).toBe( |
| 81 | + true, |
| 82 | + ); |
| 83 | + expect(commands.some((c) => c.includes('devicectl') && c.includes('install'))).toBe(true); |
| 84 | + expect(commands.some((c) => c.includes('devicectl') && c.includes('launch'))).toBe(true); |
| 85 | + }); |
| 86 | + |
| 87 | + it('uses generic destination for build-settings lookup', async () => { |
| 88 | + const commandCalls: string[][] = []; |
| 89 | + const mockExecutor: CommandExecutor = async (command) => { |
| 90 | + commandCalls.push(command); |
| 91 | + |
| 92 | + if (command.includes('-showBuildSettings')) { |
| 93 | + return createMockCommandResponse({ |
| 94 | + success: true, |
| 95 | + output: 'BUILT_PRODUCTS_DIR = /tmp/build\nFULL_PRODUCT_NAME = MyWatchApp.app\n', |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + if (command[0] === '/bin/sh') { |
| 100 | + return createMockCommandResponse({ success: true, output: 'io.sentry.MyWatchApp' }); |
| 101 | + } |
| 102 | + |
| 103 | + if (command.includes('launch')) { |
| 104 | + return createMockCommandResponse({ |
| 105 | + success: true, |
| 106 | + output: JSON.stringify({ result: { process: { processIdentifier: 9876 } } }), |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + return createMockCommandResponse({ success: true, output: 'OK' }); |
| 111 | + }; |
| 112 | + |
| 113 | + const result = await build_run_deviceLogic( |
| 114 | + { |
| 115 | + projectPath: '/tmp/MyWatchApp.xcodeproj', |
| 116 | + scheme: 'MyWatchApp', |
| 117 | + platform: 'watchOS', |
| 118 | + deviceId: 'DEVICE-UDID', |
| 119 | + }, |
| 120 | + mockExecutor, |
| 121 | + createMockFileSystemExecutor({ existsSync: () => true }), |
| 122 | + ); |
| 123 | + |
| 124 | + expect(result.isError).toBe(false); |
| 125 | + |
| 126 | + const showBuildSettingsCommand = commandCalls.find((command) => |
| 127 | + command.includes('-showBuildSettings'), |
| 128 | + ); |
| 129 | + expect(showBuildSettingsCommand).toBeDefined(); |
| 130 | + expect(showBuildSettingsCommand).toContain('-destination'); |
| 131 | + |
| 132 | + const destinationIndex = showBuildSettingsCommand!.indexOf('-destination'); |
| 133 | + expect(showBuildSettingsCommand![destinationIndex + 1]).toBe('generic/platform=watchOS'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('includes fallback stop guidance when process id is unavailable', async () => { |
| 137 | + const mockExecutor: CommandExecutor = async (command) => { |
| 138 | + if (command.includes('-showBuildSettings')) { |
| 139 | + return createMockCommandResponse({ |
| 140 | + success: true, |
| 141 | + output: 'BUILT_PRODUCTS_DIR = /tmp/build\nFULL_PRODUCT_NAME = MyApp.app\n', |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + if (command[0] === '/bin/sh') { |
| 146 | + return createMockCommandResponse({ success: true, output: 'io.sentry.MyApp' }); |
| 147 | + } |
| 148 | + |
| 149 | + return createMockCommandResponse({ success: true, output: 'OK' }); |
| 150 | + }; |
| 151 | + |
| 152 | + const result = await build_run_deviceLogic( |
| 153 | + { |
| 154 | + projectPath: '/tmp/MyApp.xcodeproj', |
| 155 | + scheme: 'MyApp', |
| 156 | + deviceId: 'DEVICE-UDID', |
| 157 | + }, |
| 158 | + mockExecutor, |
| 159 | + createMockFileSystemExecutor({ |
| 160 | + existsSync: () => true, |
| 161 | + readFile: async () => 'not-json', |
| 162 | + }), |
| 163 | + ); |
| 164 | + |
| 165 | + expect(result.isError).toBe(false); |
| 166 | + expect(result.content[0].text).toContain('Process ID was unavailable'); |
| 167 | + expect(result.nextStepParams).toMatchObject({ |
| 168 | + start_device_log_cap: { deviceId: 'DEVICE-UDID', bundleId: 'io.sentry.MyApp' }, |
| 169 | + }); |
| 170 | + expect(result.nextStepParams?.stop_app_device).toBeUndefined(); |
| 171 | + }); |
| 172 | + |
| 173 | + it('returns an error when app-path lookup fails after successful build', async () => { |
| 174 | + const mockExecutor: CommandExecutor = async (command) => { |
| 175 | + if (command.includes('-showBuildSettings')) { |
| 176 | + return createMockCommandResponse({ success: false, error: 'no build settings' }); |
| 177 | + } |
| 178 | + return createMockCommandResponse({ success: true, output: 'OK' }); |
| 179 | + }; |
| 180 | + |
| 181 | + const result = await build_run_deviceLogic( |
| 182 | + { |
| 183 | + projectPath: '/tmp/MyApp.xcodeproj', |
| 184 | + scheme: 'MyApp', |
| 185 | + deviceId: 'DEVICE-UDID', |
| 186 | + }, |
| 187 | + mockExecutor, |
| 188 | + createMockFileSystemExecutor({ existsSync: () => true }), |
| 189 | + ); |
| 190 | + |
| 191 | + expect(result.isError).toBe(true); |
| 192 | + expect(result.content[0].text).toContain('failed to get app path'); |
| 193 | + }); |
| 194 | + |
| 195 | + it('returns an error when install fails', async () => { |
| 196 | + const mockExecutor: CommandExecutor = async (command) => { |
| 197 | + if (command.includes('-showBuildSettings')) { |
| 198 | + return createMockCommandResponse({ |
| 199 | + success: true, |
| 200 | + output: 'BUILT_PRODUCTS_DIR = /tmp/build\nFULL_PRODUCT_NAME = MyApp.app\n', |
| 201 | + }); |
| 202 | + } |
| 203 | + |
| 204 | + if (command.includes('install')) { |
| 205 | + return createMockCommandResponse({ success: false, error: 'install failed' }); |
| 206 | + } |
| 207 | + |
| 208 | + return createMockCommandResponse({ success: true, output: 'io.sentry.MyApp' }); |
| 209 | + }; |
| 210 | + |
| 211 | + const result = await build_run_deviceLogic( |
| 212 | + { |
| 213 | + projectPath: '/tmp/MyApp.xcodeproj', |
| 214 | + scheme: 'MyApp', |
| 215 | + deviceId: 'DEVICE-UDID', |
| 216 | + }, |
| 217 | + mockExecutor, |
| 218 | + createMockFileSystemExecutor({ existsSync: () => true }), |
| 219 | + ); |
| 220 | + |
| 221 | + expect(result.isError).toBe(true); |
| 222 | + expect(result.content[0].text).toContain('error installing app on device'); |
| 223 | + }); |
| 224 | + |
| 225 | + it('returns an error when launch fails', async () => { |
| 226 | + const mockExecutor: CommandExecutor = async (command) => { |
| 227 | + if (command.includes('-showBuildSettings')) { |
| 228 | + return createMockCommandResponse({ |
| 229 | + success: true, |
| 230 | + output: 'BUILT_PRODUCTS_DIR = /tmp/build\nFULL_PRODUCT_NAME = MyApp.app\n', |
| 231 | + }); |
| 232 | + } |
| 233 | + |
| 234 | + if (command.includes('launch')) { |
| 235 | + return createMockCommandResponse({ success: false, error: 'launch failed' }); |
| 236 | + } |
| 237 | + |
| 238 | + return createMockCommandResponse({ success: true, output: 'io.sentry.MyApp' }); |
| 239 | + }; |
| 240 | + |
| 241 | + const result = await build_run_deviceLogic( |
| 242 | + { |
| 243 | + projectPath: '/tmp/MyApp.xcodeproj', |
| 244 | + scheme: 'MyApp', |
| 245 | + deviceId: 'DEVICE-UDID', |
| 246 | + }, |
| 247 | + mockExecutor, |
| 248 | + createMockFileSystemExecutor({ existsSync: () => true }), |
| 249 | + ); |
| 250 | + |
| 251 | + expect(result.isError).toBe(true); |
| 252 | + expect(result.content[0].text).toContain('error launching app on device'); |
| 253 | + }); |
| 254 | +}); |
0 commit comments