|
| 1 | +import { beforeEach, test, vi } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { IOS_SIMULATOR } from '../../../__tests__/test-utils/index.ts'; |
| 4 | +import { AppError } from '../../../utils/errors.ts'; |
| 5 | +import type { RunnerSession } from '../runner-session-types.ts'; |
| 6 | + |
| 7 | +const { mockEnsureRunnerSession, mockExecuteRunnerCommandWithSession, mockStopRunnerSession } = |
| 8 | + vi.hoisted(() => ({ |
| 9 | + mockEnsureRunnerSession: vi.fn(), |
| 10 | + mockExecuteRunnerCommandWithSession: vi.fn(), |
| 11 | + mockStopRunnerSession: vi.fn(), |
| 12 | + })); |
| 13 | + |
| 14 | +vi.mock('../runner-session.ts', async () => { |
| 15 | + const actual = |
| 16 | + await vi.importActual<typeof import('../runner-session.ts')>('../runner-session.ts'); |
| 17 | + return { |
| 18 | + ...actual, |
| 19 | + ensureRunnerSession: mockEnsureRunnerSession, |
| 20 | + executeRunnerCommandWithSession: mockExecuteRunnerCommandWithSession, |
| 21 | + stopRunnerSession: mockStopRunnerSession, |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +import { runIosRunnerCommand } from '../runner-client.ts'; |
| 26 | + |
| 27 | +beforeEach(() => { |
| 28 | + vi.resetAllMocks(); |
| 29 | +}); |
| 30 | + |
| 31 | +test('mutating commands restart stale ready sessions when the preflight probe never reaches the runner', async () => { |
| 32 | + const staleSession = makeRunnerSession({ port: 8100, ready: true }); |
| 33 | + const freshSession = makeRunnerSession({ port: 8101, ready: false }); |
| 34 | + |
| 35 | + mockEnsureRunnerSession.mockResolvedValueOnce(staleSession).mockResolvedValueOnce(freshSession); |
| 36 | + mockExecuteRunnerCommandWithSession |
| 37 | + .mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'Runner did not accept connection')) |
| 38 | + .mockResolvedValueOnce({ message: 'tapped' }); |
| 39 | + |
| 40 | + const result = await runIosRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 }); |
| 41 | + |
| 42 | + assert.deepEqual(result, { message: 'tapped' }); |
| 43 | + assert.equal(mockEnsureRunnerSession.mock.calls.length, 2); |
| 44 | + assert.equal(mockStopRunnerSession.mock.calls.length, 1); |
| 45 | + assert.equal(mockStopRunnerSession.mock.calls[0]?.[0], staleSession); |
| 46 | + assert.equal(mockExecuteRunnerCommandWithSession.mock.calls.length, 2); |
| 47 | + assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[0]?.[2].command, 'tap'); |
| 48 | + assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], freshSession); |
| 49 | +}); |
| 50 | + |
| 51 | +test('mutating commands do not restart or replay after command send failure', async () => { |
| 52 | + const session = makeRunnerSession({ port: 8100, ready: true }); |
| 53 | + |
| 54 | + mockEnsureRunnerSession.mockResolvedValueOnce(session); |
| 55 | + mockExecuteRunnerCommandWithSession.mockRejectedValueOnce( |
| 56 | + new Error('request timed out after reaching runner'), |
| 57 | + ); |
| 58 | + |
| 59 | + await assert.rejects(() => |
| 60 | + runIosRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 }), |
| 61 | + ); |
| 62 | + |
| 63 | + assert.equal(mockEnsureRunnerSession.mock.calls.length, 1); |
| 64 | + assert.equal(mockStopRunnerSession.mock.calls.length, 0); |
| 65 | + assert.equal(mockExecuteRunnerCommandWithSession.mock.calls.length, 1); |
| 66 | +}); |
| 67 | + |
| 68 | +function makeRunnerSession(overrides: Partial<RunnerSession> = {}): RunnerSession { |
| 69 | + return { |
| 70 | + sessionId: `session-${overrides.port ?? 8100}`, |
| 71 | + device: IOS_SIMULATOR, |
| 72 | + deviceId: IOS_SIMULATOR.id, |
| 73 | + port: 8100, |
| 74 | + xctestrunPath: '/tmp/runner.xctestrun', |
| 75 | + jsonPath: '/tmp/runner.json', |
| 76 | + testPromise: Promise.resolve({ exitCode: 0, stdout: '', stderr: '' }), |
| 77 | + child: { pid: 1234, exitCode: null }, |
| 78 | + ready: true, |
| 79 | + ...overrides, |
| 80 | + } as RunnerSession; |
| 81 | +} |
0 commit comments