|
1 | 1 | import assert from 'node:assert/strict'; |
2 | 2 | import { afterEach, beforeEach, expect, test, vi } from 'vitest'; |
3 | | -import { AppError } from '@agent-device/kernel/errors'; |
| 3 | +import http from 'node:http'; |
| 4 | +import type { AddressInfo } from 'node:net'; |
| 5 | +import { AppError, createRequestCanceledError } from '@agent-device/kernel/errors'; |
4 | 6 | import { resetAllProcessMemosForTests } from '@agent-device/kernel/ttl-memo'; |
5 | 7 | import { IOS_SIMULATOR } from './device-fixtures.ts'; |
6 | 8 | import type { ExecResult } from '@agent-device/host-kit/command'; |
@@ -220,6 +222,66 @@ test('a failed restart preserves the invalidated runner evidence', async () => { |
220 | 222 | } |
221 | 223 | }); |
222 | 224 |
|
| 225 | +// A runner that has not answered yet is readiness work, not observation: a caller whose deadline |
| 226 | +// lands there needs to know the start consumed it, so the cancellation names the phase (#2343). |
| 227 | +test('a cancellation during a runner start names the start as the readiness phase', async () => { |
| 228 | + const unanswered = await startFakeRunnerServer([]); |
| 229 | + await unanswered.close(); |
| 230 | + const starting = { ...makeRunnerSession(unanswered.port), state: 'starting' as const }; |
| 231 | + ensureRunnerSessionMock.mockResolvedValue(starting); |
| 232 | + const controller = new AbortController(); |
| 233 | + setTimeout(() => controller.abort(), 50); |
| 234 | + |
| 235 | + await expect( |
| 236 | + runAppleRunnerCommand(IOS_SIMULATOR, { command: 'snapshot' }, { signal: controller.signal }), |
| 237 | + ).rejects.toMatchObject({ |
| 238 | + details: { reason: 'request_canceled', readinessPhase: 'runner-start' }, |
| 239 | + }); |
| 240 | + expect(invalidateRunnerSessionMock).toHaveBeenCalledWith( |
| 241 | + starting, |
| 242 | + 'runner_startup_request_canceled', |
| 243 | + ); |
| 244 | +}); |
| 245 | + |
| 246 | +test('a cancellation before any runner session exists names the start as the readiness phase', async () => { |
| 247 | + ensureRunnerSessionMock.mockImplementation( |
| 248 | + async (_device: unknown, options: { signal?: AbortSignal }) => |
| 249 | + await new Promise((_resolve, reject) => { |
| 250 | + options.signal?.addEventListener('abort', () => reject(createRequestCanceledError()), { |
| 251 | + once: true, |
| 252 | + }); |
| 253 | + }), |
| 254 | + ); |
| 255 | + const controller = new AbortController(); |
| 256 | + setTimeout(() => controller.abort(), 20); |
| 257 | + |
| 258 | + await expect( |
| 259 | + runAppleRunnerCommand(IOS_SIMULATOR, { command: 'snapshot' }, { signal: controller.signal }), |
| 260 | + ).rejects.toMatchObject({ details: { readinessPhase: 'runner-start' } }); |
| 261 | +}); |
| 262 | + |
| 263 | +test('a cancellation of a command on a ready runner is not readiness work', async () => { |
| 264 | + const silent = http.createServer(() => {}); |
| 265 | + await new Promise<void>((resolve) => silent.listen(0, '127.0.0.1', resolve)); |
| 266 | + try { |
| 267 | + seedSession((silent.address() as AddressInfo).port); |
| 268 | + const controller = new AbortController(); |
| 269 | + setTimeout(() => controller.abort(), 50); |
| 270 | + |
| 271 | + const command = runAppleRunnerCommand( |
| 272 | + IOS_SIMULATOR, |
| 273 | + { command: 'snapshot' }, |
| 274 | + { signal: controller.signal }, |
| 275 | + ); |
| 276 | + |
| 277 | + await expect(command).rejects.toMatchObject({ details: { reason: 'request_canceled' } }); |
| 278 | + await expect(command).rejects.not.toHaveProperty('details.readinessPhase'); |
| 279 | + } finally { |
| 280 | + silent.closeAllConnections(); |
| 281 | + await new Promise<void>((resolve) => silent.close(() => resolve())); |
| 282 | + } |
| 283 | +}); |
| 284 | + |
223 | 285 | test('an exact-session command never dispatches to a replacement runner', async () => { |
224 | 286 | server = await startFakeRunnerServer({ recordStop: [{ kind: 'ok', data: {} }] }); |
225 | 287 | const replacement = seedSession(server.port); |
|
0 commit comments