|
1 | 1 | import { beforeEach, test, vi } from 'vitest'; |
2 | 2 | import assert from 'node:assert/strict'; |
3 | 3 | import { IOS_SIMULATOR } from '../../../__tests__/test-utils/index.ts'; |
| 4 | +import { clearRequestCanceled, markRequestCanceled } from '../../../daemon/request-cancel.ts'; |
4 | 5 | import { AppError } from '../../../utils/errors.ts'; |
5 | 6 | import type { RunnerSession } from '../runner-session-types.ts'; |
6 | 7 |
|
@@ -128,6 +129,134 @@ test('prepareIosRunner invalidates rebuilt sessions when bad-cache recovery heal |
128 | 129 | ]); |
129 | 130 | }); |
130 | 131 |
|
| 132 | +test('prepareIosRunner retries a fresh launch session when the health check cannot connect', async () => { |
| 133 | + const stuckSession = makeRunnerSession({ port: 8100 }); |
| 134 | + const relaunchedSession = makeRunnerSession({ port: 8101 }); |
| 135 | + |
| 136 | + mockEnsureRunnerSession.mockResolvedValueOnce(stuckSession).mockResolvedValueOnce(relaunchedSession); |
| 137 | + mockExecuteRunnerCommandWithSession |
| 138 | + .mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'Runner did not accept connection')) |
| 139 | + .mockResolvedValueOnce({ uptimeMs: 42 }); |
| 140 | + |
| 141 | + const result = await prepareIosRunner(IOS_SIMULATOR, { |
| 142 | + healthTimeoutMs: 90_000, |
| 143 | + buildTimeoutMs: 300_000, |
| 144 | + }); |
| 145 | + |
| 146 | + assert.deepEqual(result.runner, { uptimeMs: 42 }); |
| 147 | + assert.equal(result.recoveryReason, 'Runner did not accept connection'); |
| 148 | + assert.equal(mockEnsureRunnerSession.mock.calls[0]?.[1]?.cleanStaleBundles, undefined); |
| 149 | + assert.deepEqual(mockInvalidateRunnerSession.mock.calls, [ |
| 150 | + [stuckSession, 'prepare_runner_health_retry'], |
| 151 | + ]); |
| 152 | + assert.equal(mockEnsureRunnerSession.mock.calls.length, 2); |
| 153 | + assert.equal(mockEnsureRunnerSession.mock.calls[1]?.[1]?.cleanStaleBundles, true); |
| 154 | + assert.equal(mockEnsureRunnerSession.mock.calls[1]?.[1]?.forceRunnerXctestrunRebuild, undefined); |
| 155 | + assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], relaunchedSession); |
| 156 | + assert.deepEqual( |
| 157 | + mockEmitDiagnostic.mock.calls.find( |
| 158 | + ([event]) => event.phase === 'ios_runner_prepare_health_retry', |
| 159 | + )?.[0].data, |
| 160 | + { |
| 161 | + command: 'uptime', |
| 162 | + commandId: mockExecuteRunnerCommandWithSession.mock.calls[0]?.[2].commandId, |
| 163 | + sessionId: stuckSession.sessionId, |
| 164 | + attempt: 1, |
| 165 | + maxAttempts: 2, |
| 166 | + reason: 'Runner did not accept connection', |
| 167 | + }, |
| 168 | + ); |
| 169 | +}); |
| 170 | + |
| 171 | +test('prepareIosRunner does not force a rebuild when the relaunched fresh session still cannot connect', async () => { |
| 172 | + const missArtifact = makeRunnerArtifact({ |
| 173 | + xctestrunPath: '/tmp/miss.xctestrun', |
| 174 | + cache: 'miss', |
| 175 | + artifact: 'valid', |
| 176 | + }); |
| 177 | + const exactArtifact = makeRunnerArtifact({ |
| 178 | + xctestrunPath: '/tmp/exact.xctestrun', |
| 179 | + cache: 'exact', |
| 180 | + artifact: 'valid', |
| 181 | + }); |
| 182 | + const stuckSession = makeRunnerSession({ |
| 183 | + port: 8100, |
| 184 | + xctestrunPath: missArtifact.xctestrunPath, |
| 185 | + xctestrunArtifact: missArtifact, |
| 186 | + }); |
| 187 | + const relaunchedSession = makeRunnerSession({ |
| 188 | + port: 8101, |
| 189 | + xctestrunPath: exactArtifact.xctestrunPath, |
| 190 | + xctestrunArtifact: exactArtifact, |
| 191 | + }); |
| 192 | + |
| 193 | + mockEnsureRunnerSession.mockResolvedValueOnce(stuckSession).mockResolvedValueOnce(relaunchedSession); |
| 194 | + mockExecuteRunnerCommandWithSession |
| 195 | + .mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'Runner did not accept connection')) |
| 196 | + .mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'Runner did not accept connection')); |
| 197 | + |
| 198 | + await assert.rejects( |
| 199 | + () => |
| 200 | + prepareIosRunner(IOS_SIMULATOR, { |
| 201 | + healthTimeoutMs: 90_000, |
| 202 | + forceRunnerXctestrunRebuild: false, |
| 203 | + }), |
| 204 | + /Runner did not accept connection/, |
| 205 | + ); |
| 206 | + |
| 207 | + assert.deepEqual(mockInvalidateRunnerSession.mock.calls, [ |
| 208 | + [stuckSession, 'prepare_runner_health_retry'], |
| 209 | + [relaunchedSession, 'prepare_runner_health_failed'], |
| 210 | + ]); |
| 211 | + assert.equal(mockMarkRunnerXctestrunArtifactBadForRun.mock.calls.length, 0); |
| 212 | + assert.equal(mockEnsureRunnerSession.mock.calls.length, 2); |
| 213 | + assert.equal(mockEnsureRunnerSession.mock.calls[0]?.[1]?.cleanStaleBundles, undefined); |
| 214 | + assert.equal(mockEnsureRunnerSession.mock.calls[0]?.[1]?.forceRunnerXctestrunRebuild, false); |
| 215 | + assert.equal(mockEnsureRunnerSession.mock.calls[1]?.[1]?.cleanStaleBundles, true); |
| 216 | + assert.equal(mockEnsureRunnerSession.mock.calls[1]?.[1]?.forceRunnerXctestrunRebuild, false); |
| 217 | +}); |
| 218 | + |
| 219 | +test('prepareIosRunner does not relaunch after non-retryable runner startup failures', async () => { |
| 220 | + const failedSession = makeRunnerSession({ port: 8100 }); |
| 221 | + |
| 222 | + mockEnsureRunnerSession.mockResolvedValueOnce(failedSession); |
| 223 | + mockExecuteRunnerCommandWithSession.mockRejectedValueOnce( |
| 224 | + new AppError('COMMAND_FAILED', 'xcodebuild exited early'), |
| 225 | + ); |
| 226 | + |
| 227 | + await assert.rejects( |
| 228 | + () => prepareIosRunner(IOS_SIMULATOR, { healthTimeoutMs: 90_000 }), |
| 229 | + /xcodebuild exited early/, |
| 230 | + ); |
| 231 | + |
| 232 | + assert.equal(mockEnsureRunnerSession.mock.calls.length, 1); |
| 233 | + assert.equal(mockInvalidateRunnerSession.mock.calls.length, 0); |
| 234 | + assert.equal(mockMarkRunnerXctestrunArtifactBadForRun.mock.calls.length, 0); |
| 235 | +}); |
| 236 | + |
| 237 | +test('prepareIosRunner does not relaunch after request cancellation', async () => { |
| 238 | + const requestId = 'prepare-canceled-before-retry'; |
| 239 | + const stuckSession = makeRunnerSession({ port: 8100 }); |
| 240 | + |
| 241 | + mockEnsureRunnerSession.mockResolvedValueOnce(stuckSession); |
| 242 | + mockExecuteRunnerCommandWithSession.mockImplementationOnce(() => { |
| 243 | + markRequestCanceled(requestId); |
| 244 | + throw new AppError('COMMAND_FAILED', 'Runner did not accept connection'); |
| 245 | + }); |
| 246 | + |
| 247 | + try { |
| 248 | + await assert.rejects( |
| 249 | + () => prepareIosRunner(IOS_SIMULATOR, { healthTimeoutMs: 90_000, requestId }), |
| 250 | + /request canceled/, |
| 251 | + ); |
| 252 | + } finally { |
| 253 | + clearRequestCanceled(requestId); |
| 254 | + } |
| 255 | + |
| 256 | + assert.equal(mockEnsureRunnerSession.mock.calls.length, 1); |
| 257 | + assert.equal(mockInvalidateRunnerSession.mock.calls.length, 0); |
| 258 | +}); |
| 259 | + |
131 | 260 | test('mutating commands restart stale ready sessions when the preflight probe never reaches the runner', async () => { |
132 | 261 | const staleSession = makeRunnerSession({ port: 8100, ready: true }); |
133 | 262 | const freshSession = makeRunnerSession({ port: 8101, ready: false }); |
|
0 commit comments