|
| 1 | +/** |
| 2 | + * Tests for build-utils Sentry classification logic |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect } from 'vitest'; |
| 6 | +import { createMockExecutor } from '../../test-utils/mock-executors.ts'; |
| 7 | +import { executeXcodeBuildCommand } from '../build-utils.ts'; |
| 8 | +import { XcodePlatform } from '../xcode.ts'; |
| 9 | + |
| 10 | +describe('build-utils Sentry Classification', () => { |
| 11 | + const mockPlatformOptions = { |
| 12 | + platform: XcodePlatform.macOS, |
| 13 | + logPrefix: 'Test Build', |
| 14 | + }; |
| 15 | + |
| 16 | + const mockParams = { |
| 17 | + scheme: 'TestScheme', |
| 18 | + configuration: 'Debug', |
| 19 | + projectPath: '/path/to/project.xcodeproj', |
| 20 | + }; |
| 21 | + |
| 22 | + describe('Exit Code 64 Classification (MCP Error)', () => { |
| 23 | + it('should trigger Sentry logging for exit code 64 (invalid arguments)', async () => { |
| 24 | + const mockExecutor = createMockExecutor({ |
| 25 | + success: false, |
| 26 | + error: 'xcodebuild: error: invalid option', |
| 27 | + exitCode: 64, |
| 28 | + }); |
| 29 | + |
| 30 | + const result = await executeXcodeBuildCommand( |
| 31 | + mockParams, |
| 32 | + mockPlatformOptions, |
| 33 | + false, |
| 34 | + 'build', |
| 35 | + mockExecutor, |
| 36 | + ); |
| 37 | + |
| 38 | + expect(result.isError).toBe(true); |
| 39 | + expect(result.content[0].text).toContain('❌ [stderr] xcodebuild: error: invalid option'); |
| 40 | + expect(result.content[1].text).toContain('❌ Test Build build failed for scheme TestScheme'); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('Other Exit Codes Classification (User Error)', () => { |
| 45 | + it('should not trigger Sentry logging for exit code 65 (user error)', async () => { |
| 46 | + const mockExecutor = createMockExecutor({ |
| 47 | + success: false, |
| 48 | + error: 'Scheme TestScheme was not found', |
| 49 | + exitCode: 65, |
| 50 | + }); |
| 51 | + |
| 52 | + const result = await executeXcodeBuildCommand( |
| 53 | + mockParams, |
| 54 | + mockPlatformOptions, |
| 55 | + false, |
| 56 | + 'build', |
| 57 | + mockExecutor, |
| 58 | + ); |
| 59 | + |
| 60 | + expect(result.isError).toBe(true); |
| 61 | + expect(result.content[0].text).toContain('❌ [stderr] Scheme TestScheme was not found'); |
| 62 | + expect(result.content[1].text).toContain('❌ Test Build build failed for scheme TestScheme'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should not trigger Sentry logging for exit code 66 (file not found)', async () => { |
| 66 | + const mockExecutor = createMockExecutor({ |
| 67 | + success: false, |
| 68 | + error: 'project.xcodeproj cannot be opened', |
| 69 | + exitCode: 66, |
| 70 | + }); |
| 71 | + |
| 72 | + const result = await executeXcodeBuildCommand( |
| 73 | + mockParams, |
| 74 | + mockPlatformOptions, |
| 75 | + false, |
| 76 | + 'build', |
| 77 | + mockExecutor, |
| 78 | + ); |
| 79 | + |
| 80 | + expect(result.isError).toBe(true); |
| 81 | + expect(result.content[0].text).toContain('❌ [stderr] project.xcodeproj cannot be opened'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should not trigger Sentry logging for exit code 70 (destination error)', async () => { |
| 85 | + const mockExecutor = createMockExecutor({ |
| 86 | + success: false, |
| 87 | + error: 'Unable to find a destination matching the provided destination specifier', |
| 88 | + exitCode: 70, |
| 89 | + }); |
| 90 | + |
| 91 | + const result = await executeXcodeBuildCommand( |
| 92 | + mockParams, |
| 93 | + mockPlatformOptions, |
| 94 | + false, |
| 95 | + 'build', |
| 96 | + mockExecutor, |
| 97 | + ); |
| 98 | + |
| 99 | + expect(result.isError).toBe(true); |
| 100 | + expect(result.content[0].text).toContain('❌ [stderr] Unable to find a destination matching'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should not trigger Sentry logging for exit code 1 (general build failure)', async () => { |
| 104 | + const mockExecutor = createMockExecutor({ |
| 105 | + success: false, |
| 106 | + error: 'Build failed with errors', |
| 107 | + exitCode: 1, |
| 108 | + }); |
| 109 | + |
| 110 | + const result = await executeXcodeBuildCommand( |
| 111 | + mockParams, |
| 112 | + mockPlatformOptions, |
| 113 | + false, |
| 114 | + 'build', |
| 115 | + mockExecutor, |
| 116 | + ); |
| 117 | + |
| 118 | + expect(result.isError).toBe(true); |
| 119 | + expect(result.content[0].text).toContain('❌ [stderr] Build failed with errors'); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('Spawn Error Classification (Environment Error)', () => { |
| 124 | + it('should not trigger Sentry logging for ENOENT spawn error', async () => { |
| 125 | + const spawnError = new Error('spawn xcodebuild ENOENT') as NodeJS.ErrnoException; |
| 126 | + spawnError.code = 'ENOENT'; |
| 127 | + |
| 128 | + const mockExecutor = createMockExecutor({ |
| 129 | + success: false, |
| 130 | + error: '', |
| 131 | + shouldThrow: spawnError, |
| 132 | + }); |
| 133 | + |
| 134 | + const result = await executeXcodeBuildCommand( |
| 135 | + mockParams, |
| 136 | + mockPlatformOptions, |
| 137 | + false, |
| 138 | + 'build', |
| 139 | + mockExecutor, |
| 140 | + ); |
| 141 | + |
| 142 | + expect(result.isError).toBe(true); |
| 143 | + expect(result.content[0].text).toContain( |
| 144 | + 'Error during Test Build build: spawn xcodebuild ENOENT', |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should not trigger Sentry logging for EACCES spawn error', async () => { |
| 149 | + const spawnError = new Error('spawn xcodebuild EACCES') as NodeJS.ErrnoException; |
| 150 | + spawnError.code = 'EACCES'; |
| 151 | + |
| 152 | + const mockExecutor = createMockExecutor({ |
| 153 | + success: false, |
| 154 | + error: '', |
| 155 | + shouldThrow: spawnError, |
| 156 | + }); |
| 157 | + |
| 158 | + const result = await executeXcodeBuildCommand( |
| 159 | + mockParams, |
| 160 | + mockPlatformOptions, |
| 161 | + false, |
| 162 | + 'build', |
| 163 | + mockExecutor, |
| 164 | + ); |
| 165 | + |
| 166 | + expect(result.isError).toBe(true); |
| 167 | + expect(result.content[0].text).toContain( |
| 168 | + 'Error during Test Build build: spawn xcodebuild EACCES', |
| 169 | + ); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should not trigger Sentry logging for EPERM spawn error', async () => { |
| 173 | + const spawnError = new Error('spawn xcodebuild EPERM') as NodeJS.ErrnoException; |
| 174 | + spawnError.code = 'EPERM'; |
| 175 | + |
| 176 | + const mockExecutor = createMockExecutor({ |
| 177 | + success: false, |
| 178 | + error: '', |
| 179 | + shouldThrow: spawnError, |
| 180 | + }); |
| 181 | + |
| 182 | + const result = await executeXcodeBuildCommand( |
| 183 | + mockParams, |
| 184 | + mockPlatformOptions, |
| 185 | + false, |
| 186 | + 'build', |
| 187 | + mockExecutor, |
| 188 | + ); |
| 189 | + |
| 190 | + expect(result.isError).toBe(true); |
| 191 | + expect(result.content[0].text).toContain( |
| 192 | + 'Error during Test Build build: spawn xcodebuild EPERM', |
| 193 | + ); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should trigger Sentry logging for non-spawn exceptions', async () => { |
| 197 | + const otherError = new Error('Unexpected internal error'); |
| 198 | + |
| 199 | + const mockExecutor = createMockExecutor({ |
| 200 | + success: false, |
| 201 | + error: '', |
| 202 | + shouldThrow: otherError, |
| 203 | + }); |
| 204 | + |
| 205 | + const result = await executeXcodeBuildCommand( |
| 206 | + mockParams, |
| 207 | + mockPlatformOptions, |
| 208 | + false, |
| 209 | + 'build', |
| 210 | + mockExecutor, |
| 211 | + ); |
| 212 | + |
| 213 | + expect(result.isError).toBe(true); |
| 214 | + expect(result.content[0].text).toContain( |
| 215 | + 'Error during Test Build build: Unexpected internal error', |
| 216 | + ); |
| 217 | + }); |
| 218 | + }); |
| 219 | + |
| 220 | + describe('Success Case (No Sentry Logging)', () => { |
| 221 | + it('should not trigger any error logging for successful builds', async () => { |
| 222 | + const mockExecutor = createMockExecutor({ |
| 223 | + success: true, |
| 224 | + output: 'BUILD SUCCEEDED', |
| 225 | + exitCode: 0, |
| 226 | + }); |
| 227 | + |
| 228 | + const result = await executeXcodeBuildCommand( |
| 229 | + mockParams, |
| 230 | + mockPlatformOptions, |
| 231 | + false, |
| 232 | + 'build', |
| 233 | + mockExecutor, |
| 234 | + ); |
| 235 | + |
| 236 | + expect(result.isError).toBeFalsy(); |
| 237 | + expect(result.content[0].text).toContain( |
| 238 | + '✅ Test Build build succeeded for scheme TestScheme', |
| 239 | + ); |
| 240 | + }); |
| 241 | + }); |
| 242 | + |
| 243 | + describe('Exit Code Undefined Cases', () => { |
| 244 | + it('should not trigger Sentry logging when exitCode is undefined', async () => { |
| 245 | + const mockExecutor = createMockExecutor({ |
| 246 | + success: false, |
| 247 | + error: 'Some error without exit code', |
| 248 | + exitCode: undefined, |
| 249 | + }); |
| 250 | + |
| 251 | + const result = await executeXcodeBuildCommand( |
| 252 | + mockParams, |
| 253 | + mockPlatformOptions, |
| 254 | + false, |
| 255 | + 'build', |
| 256 | + mockExecutor, |
| 257 | + ); |
| 258 | + |
| 259 | + expect(result.isError).toBe(true); |
| 260 | + expect(result.content[0].text).toContain('❌ [stderr] Some error without exit code'); |
| 261 | + }); |
| 262 | + }); |
| 263 | +}); |
0 commit comments