|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { beforeEach, test, vi } from 'vitest'; |
| 3 | +import { IOS_SIMULATOR } from './device-fixtures.ts'; |
| 4 | +import { appleRunnerTestHost } from '../test-host.ts'; |
| 5 | +import { |
| 6 | + makeClassifyOwnerLivenessViaMocks, |
| 7 | + makeBackgroundRunner, |
| 8 | + makeRunnerSession, |
| 9 | + runnerError, |
| 10 | + runnerResponse, |
| 11 | +} from './runner-session-fixtures.ts'; |
| 12 | +import { mkdtempForTestSync } from './tmp-dir.ts'; |
| 13 | + |
| 14 | +// The main-thread-occupancy half of the runner-session lifecycle: what the daemon records from a |
| 15 | +// runner's busy signals, and what `close` does with a runner that reported abandoned main-thread |
| 16 | +// work still draining (#2552). Split out of `runner-session.test.ts`, which already mirrors the |
| 17 | +// rest of `runner-session.ts` and is held at the test-file size tripwire. |
| 18 | + |
| 19 | +const { |
| 20 | + mockAcquireXcodebuildSimulatorSetRedirect, |
| 21 | + mockCleanupTempFile, |
| 22 | + mockEnsureXctestrunArtifact, |
| 23 | + mockGetFreePort, |
| 24 | + mockIsProcessAlive, |
| 25 | + mockIsProcessGroupAlive, |
| 26 | + mockPrepareXctestrunWithEnv, |
| 27 | + mockReadProcessCommand, |
| 28 | + mockReadProcessStartTime, |
| 29 | + mockResolveExpectedRunnerCacheMetadata, |
| 30 | + mockResolveRunnerDerivedPath, |
| 31 | + mockRunAppleToolCommand, |
| 32 | + mockRunCmdBackground, |
| 33 | + mockRunXcrun, |
| 34 | + mockSendRunnerCommandOnce, |
| 35 | + mockSignalPidsBestEffort, |
| 36 | + mockSignalProcessGroupBestEffort, |
| 37 | + mockWaitForRunner, |
| 38 | + mockRedirectRelease, |
| 39 | +} = vi.hoisted(() => ({ |
| 40 | + mockAcquireXcodebuildSimulatorSetRedirect: vi.fn(), |
| 41 | + mockCleanupTempFile: vi.fn(), |
| 42 | + mockEnsureXctestrunArtifact: vi.fn(), |
| 43 | + mockGetFreePort: vi.fn(), |
| 44 | + mockIsProcessAlive: vi.fn(), |
| 45 | + mockIsProcessGroupAlive: vi.fn(), |
| 46 | + mockPrepareXctestrunWithEnv: vi.fn(), |
| 47 | + mockReadProcessCommand: vi.fn((_pid: number) => null as string | null), |
| 48 | + mockReadProcessStartTime: vi.fn((_pid: number) => 'fixed-test-owner-start-time' as string | null), |
| 49 | + mockResolveExpectedRunnerCacheMetadata: vi.fn(), |
| 50 | + mockResolveRunnerDerivedPath: vi.fn(), |
| 51 | + mockRunAppleToolCommand: vi.fn(), |
| 52 | + mockRunCmdBackground: vi.fn(), |
| 53 | + mockRunXcrun: vi.fn(), |
| 54 | + mockSendRunnerCommandOnce: vi.fn(), |
| 55 | + mockSignalPidsBestEffort: vi.fn(), |
| 56 | + mockSignalProcessGroupBestEffort: vi.fn(), |
| 57 | + mockWaitForRunner: vi.fn(), |
| 58 | + mockRedirectRelease: vi.fn(), |
| 59 | +})); |
| 60 | + |
| 61 | +const TEST_OWNER_START_TIME = 'fixed-test-owner-start-time'; |
| 62 | + |
| 63 | +vi.mock('../runner-io.ts', async () => { |
| 64 | + const actual = await vi.importActual<typeof import('../runner-io.ts')>('../runner-io.ts'); |
| 65 | + return { |
| 66 | + ...actual, |
| 67 | + cleanupTempFile: mockCleanupTempFile, |
| 68 | + getFreePort: mockGetFreePort, |
| 69 | + }; |
| 70 | +}); |
| 71 | + |
| 72 | +vi.mock('../runner-startup-transport.ts', async () => { |
| 73 | + const actual = await vi.importActual<typeof import('../runner-startup-transport.ts')>( |
| 74 | + '../runner-startup-transport.ts', |
| 75 | + ); |
| 76 | + return { |
| 77 | + ...actual, |
| 78 | + waitForRunner: mockWaitForRunner, |
| 79 | + }; |
| 80 | +}); |
| 81 | + |
| 82 | +vi.mock('../runner-transport.ts', async () => { |
| 83 | + const actual = |
| 84 | + await vi.importActual<typeof import('../runner-transport.ts')>('../runner-transport.ts'); |
| 85 | + return { |
| 86 | + ...actual, |
| 87 | + sendRunnerCommandOnce: mockSendRunnerCommandOnce, |
| 88 | + }; |
| 89 | +}); |
| 90 | + |
| 91 | +vi.mock('../runner-xctestrun.ts', async () => { |
| 92 | + const actual = |
| 93 | + await vi.importActual<typeof import('../runner-xctestrun.ts')>('../runner-xctestrun.ts'); |
| 94 | + return { |
| 95 | + ...actual, |
| 96 | + acquireXcodebuildSimulatorSetRedirect: mockAcquireXcodebuildSimulatorSetRedirect, |
| 97 | + ensureXctestrunArtifact: mockEnsureXctestrunArtifact, |
| 98 | + prepareXctestrunWithEnv: mockPrepareXctestrunWithEnv, |
| 99 | + resolveExpectedRunnerCacheMetadata: mockResolveExpectedRunnerCacheMetadata, |
| 100 | + resolveRunnerDerivedPath: mockResolveRunnerDerivedPath, |
| 101 | + }; |
| 102 | +}); |
| 103 | + |
| 104 | +import { |
| 105 | + abortAllIosRunnerSessions, |
| 106 | + cancelIosRunnerIdleStop, |
| 107 | + ensureRunnerSession, |
| 108 | + scheduleIosRunnerIdleStop, |
| 109 | + executeRunnerCommandWithSession, |
| 110 | + getRunnerSessionSnapshot, |
| 111 | +} from '../runner-session.ts'; |
| 112 | + |
| 113 | +// Test-only stand-in for the daemon's runtime lease-owner-state-dir setter (root-only; the package |
| 114 | +// cannot import it). Backs the host.leaseOwnerStateDir() getter the package reads instead. |
| 115 | +let leaseOwnerStateDirOverride: string | undefined; |
| 116 | + |
| 117 | +beforeEach(async () => { |
| 118 | + appleRunnerTestHost.update({ |
| 119 | + runCmdBackground: mockRunCmdBackground, |
| 120 | + isProcessAlive: mockIsProcessAlive, |
| 121 | + isProcessGroupAlive: mockIsProcessGroupAlive, |
| 122 | + readProcessCommand: mockReadProcessCommand, |
| 123 | + readProcessStartTime: mockReadProcessStartTime, |
| 124 | + signalPidsBestEffort: mockSignalPidsBestEffort, |
| 125 | + signalProcessGroupBestEffort: mockSignalProcessGroupBestEffort, |
| 126 | + runAppleToolCommand: mockRunAppleToolCommand, |
| 127 | + runXcrun: mockRunXcrun, |
| 128 | + leaseOwnerStateDir: () => leaseOwnerStateDirOverride, |
| 129 | + classifyOwnerLiveness: makeClassifyOwnerLivenessViaMocks({ |
| 130 | + isProcessAlive: (pid) => Boolean(mockIsProcessAlive(pid)), |
| 131 | + readProcessStartTime: (pid) => (mockReadProcessStartTime(pid) as string | null) ?? null, |
| 132 | + }), |
| 133 | + }); |
| 134 | + await abortAllIosRunnerSessions(); |
| 135 | + vi.resetAllMocks(); |
| 136 | + leaseOwnerStateDirOverride = undefined; |
| 137 | + process.env.AGENT_DEVICE_IOS_RUNNER_LEASE_DIR = mkdtempForTestSync( |
| 138 | + 'agent-device-runner-close-test-', |
| 139 | + ); |
| 140 | + mockRunXcrun.mockResolvedValue({ exitCode: 0, stdout: '', stderr: '' }); |
| 141 | + mockEnsureXctestrunArtifact.mockResolvedValue({ |
| 142 | + xctestrunPath: '/tmp/base-runner.xctestrun', |
| 143 | + derived: '/tmp/derived', |
| 144 | + cache: 'miss', |
| 145 | + artifact: 'rebuilt', |
| 146 | + buildMs: 12, |
| 147 | + xctestrunPathSource: 'build', |
| 148 | + }); |
| 149 | + mockGetFreePort.mockResolvedValue(8123); |
| 150 | + mockPrepareXctestrunWithEnv.mockResolvedValue({ |
| 151 | + xctestrunPath: '/tmp/session-runner.xctestrun', |
| 152 | + jsonPath: '/tmp/session-runner.json', |
| 153 | + }); |
| 154 | + mockResolveExpectedRunnerCacheMetadata.mockReturnValue({ schemaVersion: 1 }); |
| 155 | + mockResolveRunnerDerivedPath.mockReturnValue('/tmp/derived'); |
| 156 | + mockAcquireXcodebuildSimulatorSetRedirect.mockResolvedValue({ release: mockRedirectRelease }); |
| 157 | + mockRunCmdBackground.mockReturnValue(makeBackgroundRunner(4242)); |
| 158 | + mockRunAppleToolCommand.mockResolvedValue({ exitCode: 0, stdout: '', stderr: '' }); |
| 159 | + mockIsProcessAlive.mockReturnValue(true); |
| 160 | + mockIsProcessGroupAlive.mockReturnValue(false); |
| 161 | + mockReadProcessCommand.mockReturnValue(null); |
| 162 | + mockReadProcessStartTime.mockImplementation((pid: number) => |
| 163 | + pid === process.pid ? TEST_OWNER_START_TIME : null, |
| 164 | + ); |
| 165 | + mockWaitForRunner.mockResolvedValue(runnerResponse({ uptimeMs: 1 })); |
| 166 | +}); |
| 167 | + |
| 168 | +async function waitForRunnerSessionGone(deviceId: string): Promise<boolean> { |
| 169 | + const deadline = Date.now() + 1000; |
| 170 | + while (Date.now() < deadline) { |
| 171 | + if (getRunnerSessionSnapshot(deviceId) === null) return true; |
| 172 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 173 | + } |
| 174 | + return getRunnerSessionSnapshot(deviceId) === null; |
| 175 | +} |
| 176 | + |
| 177 | +test('a RUNNER_BUSY refusal records the runner as still draining main-thread work', async () => { |
| 178 | + const session = makeRunnerSession({ ready: true }); |
| 179 | + mockWaitForRunner.mockResolvedValueOnce(runnerResponse({ uptimeMs: 42 })); |
| 180 | + mockSendRunnerCommandOnce.mockResolvedValueOnce( |
| 181 | + runnerError({ |
| 182 | + code: 'RUNNER_BUSY', |
| 183 | + message: 'The iOS runner is still finishing a previous command that exceeded its watchdog', |
| 184 | + }), |
| 185 | + ); |
| 186 | + |
| 187 | + await assert.rejects(() => |
| 188 | + executeRunnerCommandWithSession( |
| 189 | + IOS_SIMULATOR, |
| 190 | + session, |
| 191 | + { command: 'tap', x: 120, y: 240, appBundleId: 'com.example.demo' }, |
| 192 | + '/tmp/runner.log', |
| 193 | + 30_000, |
| 194 | + ), |
| 195 | + ); |
| 196 | + |
| 197 | + assert.equal(session.runnerMainThreadBusy, true); |
| 198 | +}); |
| 199 | + |
| 200 | +test('a healthy response stamped with busy main-thread work keeps the session marked busy', async () => { |
| 201 | + const session = makeRunnerSession({ ready: true }); |
| 202 | + mockWaitForRunner.mockResolvedValueOnce(runnerResponse({ uptimeMs: 42 })); |
| 203 | + mockSendRunnerCommandOnce.mockResolvedValueOnce( |
| 204 | + runnerResponse({ tapped: true, runnerMainThreadBusy: true }), |
| 205 | + ); |
| 206 | + |
| 207 | + await executeRunnerCommandWithSession( |
| 208 | + IOS_SIMULATOR, |
| 209 | + session, |
| 210 | + { command: 'tap', x: 120, y: 240, appBundleId: 'com.example.demo' }, |
| 211 | + '/tmp/runner.log', |
| 212 | + 30_000, |
| 213 | + ); |
| 214 | + assert.equal(session.runnerMainThreadBusy, true); |
| 215 | + |
| 216 | + mockWaitForRunner.mockResolvedValueOnce(runnerResponse({ uptimeMs: 43 })); |
| 217 | + mockSendRunnerCommandOnce.mockResolvedValueOnce( |
| 218 | + runnerResponse({ tapped: true, runnerMainThreadBusy: false }), |
| 219 | + ); |
| 220 | + await executeRunnerCommandWithSession( |
| 221 | + IOS_SIMULATOR, |
| 222 | + session, |
| 223 | + { command: 'tap', x: 130, y: 250, appBundleId: 'com.example.demo' }, |
| 224 | + '/tmp/runner.log', |
| 225 | + 30_000, |
| 226 | + ); |
| 227 | + assert.equal(session.runnerMainThreadBusy, false); |
| 228 | +}); |
| 229 | + |
| 230 | +test('an unstamped healthy response leaves a busy main-thread report intact', async () => { |
| 231 | + // Recovered and journal-replayed responses are written without the occupancy stamp; their |
| 232 | + // absence must not be read as "the runner drained" (#2552). |
| 233 | + const session = makeRunnerSession({ ready: true, runnerMainThreadBusy: true }); |
| 234 | + mockWaitForRunner.mockResolvedValueOnce(runnerResponse({ uptimeMs: 42 })); |
| 235 | + mockSendRunnerCommandOnce.mockResolvedValueOnce(runnerResponse({ tapped: true })); |
| 236 | + |
| 237 | + await executeRunnerCommandWithSession( |
| 238 | + IOS_SIMULATOR, |
| 239 | + session, |
| 240 | + { command: 'tap', x: 120, y: 240, appBundleId: 'com.example.demo' }, |
| 241 | + '/tmp/runner.log', |
| 242 | + 30_000, |
| 243 | + ); |
| 244 | + |
| 245 | + assert.equal(session.runnerMainThreadBusy, true); |
| 246 | +}); |
| 247 | + |
| 248 | +test('close disposes a runner still draining main-thread work instead of pooling it for reopen (#2552)', async () => { |
| 249 | + const device = { ...IOS_SIMULATOR, id: 'runner-session-close-busy-sim' }; |
| 250 | + const previousIdleMs = process.env.AGENT_DEVICE_IOS_RUNNER_IDLE_STOP_MS; |
| 251 | + // A long idle window proves the teardown below is the busy-dispose branch, not the idle timer. |
| 252 | + process.env.AGENT_DEVICE_IOS_RUNNER_IDLE_STOP_MS = '300000'; |
| 253 | + try { |
| 254 | + const session = await ensureRunnerSession(device, {}); |
| 255 | + session.runnerMainThreadBusy = true; |
| 256 | + |
| 257 | + scheduleIosRunnerIdleStop(device.id); |
| 258 | + |
| 259 | + assert.ok(await waitForRunnerSessionGone(device.id)); |
| 260 | + } finally { |
| 261 | + cancelIosRunnerIdleStop(device.id); |
| 262 | + if (previousIdleMs === undefined) delete process.env.AGENT_DEVICE_IOS_RUNNER_IDLE_STOP_MS; |
| 263 | + else process.env.AGENT_DEVICE_IOS_RUNNER_IDLE_STOP_MS = previousIdleMs; |
| 264 | + } |
| 265 | +}); |
| 266 | + |
| 267 | +test('close retains an idle runner that never reported busy main-thread work', async () => { |
| 268 | + const device = { ...IOS_SIMULATOR, id: 'runner-session-close-idle-sim' }; |
| 269 | + const previousIdleMs = process.env.AGENT_DEVICE_IOS_RUNNER_IDLE_STOP_MS; |
| 270 | + process.env.AGENT_DEVICE_IOS_RUNNER_IDLE_STOP_MS = '300000'; |
| 271 | + try { |
| 272 | + await ensureRunnerSession(device, {}); |
| 273 | + |
| 274 | + scheduleIosRunnerIdleStop(device.id); |
| 275 | + |
| 276 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 277 | + assert.ok(getRunnerSessionSnapshot(device.id)); |
| 278 | + } finally { |
| 279 | + cancelIosRunnerIdleStop(device.id); |
| 280 | + if (previousIdleMs === undefined) delete process.env.AGENT_DEVICE_IOS_RUNNER_IDLE_STOP_MS; |
| 281 | + else process.env.AGENT_DEVICE_IOS_RUNNER_IDLE_STOP_MS = previousIdleMs; |
| 282 | + } |
| 283 | +}); |
0 commit comments