Skip to content

Commit 57ab7ad

Browse files
committed
test(ios-runner): chain close release from a real MAIN_THREAD_TIMEOUT, pin daemon close by call arguments (#2552)
The release-on-close test now trips MAIN_THREAD_TIMEOUT through executeRunnerCommandWithSession instead of setting runnerMainThreadBusy by hand, so it fails if the catch-side busy marking stops reaching close. The daemon mocks for releaseIosRunnerOnClose no longer restate the retain policy; the close tests assert the { retain } argument instead. The unrelated session-test-harness un-exports are reverted.
1 parent 029c9e9 commit 57ab7ad

7 files changed

Lines changed: 72 additions & 47 deletions

File tree

‎packages/platform-apple/src/runner/__tests__/runner-session-close.test.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,22 @@ test('releaseIosRunnerOnClose retains an idle runner, disposes a busy one, and t
296296
assert.ok(getRunnerSessionSnapshot(device.id));
297297
assert.equal((await ensureRunnerSession(device, {})).sessionId, session.sessionId);
298298

299-
// Busy + retain: the stalled runner is disposed, so the next open boots a clean one.
300-
session.runnerMainThreadBusy = true;
299+
// Busy + retain: a command that stalls answers MAIN_THREAD_TIMEOUT, so the stalled runner is
300+
// disposed and the next open boots a clean one.
301+
// The session has not served a command yet, so the read-only snapshot answers over the startup
302+
// transport with no preflight.
303+
mockWaitForRunner.mockResolvedValueOnce(
304+
runnerError({ code: 'MAIN_THREAD_TIMEOUT', message: 'main thread execution timed out' }),
305+
);
306+
await assert.rejects(() =>
307+
executeRunnerCommandWithSession(
308+
device,
309+
session,
310+
{ command: 'snapshot', appBundleId: 'com.example.demo' },
311+
'/tmp/runner.log',
312+
30_000,
313+
),
314+
);
301315
await releaseIosRunnerOnClose(device.id, { retain: true });
302316
assert.equal(getRunnerSessionSnapshot(device.id), null);
303317
assert.notEqual((await ensureRunnerSession(device, {})).sessionId, session.sessionId);

‎src/daemon/handlers/__tests__/session-relaunch-close.test.ts‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ vi.mock('../../device-ready.ts', () => ({ ensureDeviceReady: vi.fn(async () => {
2525
vi.mock('@agent-device/platform-apple/runner/operations', async (importOriginal) => {
2626
const actual =
2727
await importOriginal<typeof import('@agent-device/platform-apple/runner/operations')>();
28-
const stopIosRunnerSession = vi.fn(async (_deviceId: string) => {});
2928
return {
3029
...actual,
3130
prepareIosRunner: vi.fn(async () => ({
@@ -38,11 +37,8 @@ vi.mock('@agent-device/platform-apple/runner/operations', async (importOriginal)
3837
notifyIosRunnerAppRelaunched: vi.fn(async () => {}),
3938
// A retained Simulator runner survives the relaunch, so its cached target is reset.
4039
hasLiveIosRunnerSession: vi.fn(() => true),
41-
stopIosRunnerSession,
42-
// Mirrors the runner module: a retained close keeps warm reuse; any other close stops it.
43-
releaseIosRunnerOnClose: vi.fn(async (deviceId: string, options: { retain: boolean }) => {
44-
if (!options.retain) await stopIosRunnerSession(deviceId);
45-
}),
40+
stopIosRunnerSession: vi.fn(async () => {}),
41+
releaseIosRunnerOnClose: vi.fn(async () => {}),
4642
};
4743
});
4844
vi.mock('@agent-device/platform-apple/macos', async (importOriginal) => {
@@ -115,7 +111,7 @@ beforeEach(() => {
115111
mockNotifyIosRunnerAppRelaunched.mockResolvedValue(undefined);
116112
mockStopIosRunner.mockReset();
117113
mockStopIosRunner.mockResolvedValue(undefined);
118-
mockReleaseRunnerOnClose.mockClear();
114+
mockReleaseRunnerOnClose.mockReset();
119115
mockDismissMacOsAlert.mockReset();
120116
mockDismissMacOsAlert.mockResolvedValue({} as any);
121117
});
@@ -523,8 +519,8 @@ test('close on macOS session stops runner and dismisses automation alert before
523519
);
524520

525521
const calls: string[] = [];
526-
mockStopIosRunner.mockImplementation(async (deviceId) => {
527-
calls.push(`stop-runner:${deviceId}`);
522+
mockReleaseRunnerOnClose.mockImplementation(async (deviceId, options) => {
523+
calls.push(`release-runner:${deviceId}:${options.retain ? 'retain' : 'stop'}`);
528524
});
529525
mockDismissMacOsAlert.mockImplementation(async (action, options) => {
530526
calls.push(
@@ -537,7 +533,7 @@ test('close on macOS session stops runner and dismisses automation alert before
537533

538534
expect(response.ok).toBe(true);
539535
expect(calls).toEqual([
540-
'stop-runner:host-macos-local',
536+
'release-runner:host-macos-local:stop',
541537
'dismiss-alert:dismiss:com.apple.systempreferences',
542538
]);
543539
expect(sessionStore.get(sessionName)).toBe(undefined);
@@ -589,7 +585,7 @@ test('close on iOS simulator with scoped simulator set stops runner before delet
589585
const response = await createHandler(sessionStore)(sessionRequest(sessionName, 'close'));
590586

591587
expect(response.ok).toBe(true);
592-
expect(mockStopIosRunner).toHaveBeenCalledWith('sim-1');
588+
expect(mockReleaseRunnerOnClose).toHaveBeenCalledWith('sim-1', { retain: false });
593589
expect(sessionStore.get(sessionName)).toBeUndefined();
594590
});
595591

@@ -633,7 +629,7 @@ test('close on leased iOS simulator session stops runner before deleting session
633629
)(sessionRequest(sessionName, 'close'));
634630

635631
expect(response.ok).toBe(true);
636-
expect(mockStopIosRunner).toHaveBeenCalledWith('sim-1');
632+
expect(mockReleaseRunnerOnClose).toHaveBeenCalledWith('sim-1', { retain: false });
637633
expect(sessionStore.get(sessionName)).toBeUndefined();
638634
});
639635

@@ -659,7 +655,7 @@ test('close --shutdown on iOS simulator stops runner before deleting session', a
659655
);
660656

661657
expect(response.ok).toBe(true);
662-
expect(mockStopIosRunner).toHaveBeenCalledWith('sim-1');
658+
expect(mockReleaseRunnerOnClose).toHaveBeenCalledWith('sim-1', { retain: false });
663659
expect(sessionStore.get(sessionName)).toBeUndefined();
664660
});
665661

@@ -684,6 +680,9 @@ test('close <app> on iOS stops runner before app close dispatch and performs fin
684680
mockStopIosRunner.mockImplementation(async () => {
685681
calls.push('stop-runner');
686682
});
683+
mockReleaseRunnerOnClose.mockImplementation(async (_deviceId, options) => {
684+
calls.push(`release-runner:${options.retain ? 'retain' : 'stop'}`);
685+
});
687686
mockDispatch.mockImplementation(async (_device, command, positionals) => {
688687
calls.push(`${command}:${(positionals ?? []).join(' ')}`);
689688
return {};
@@ -694,7 +693,7 @@ test('close <app> on iOS stops runner before app close dispatch and performs fin
694693
);
695694

696695
expect(response.ok).toBe(true);
697-
expect(calls).toEqual(['stop-runner', 'close:com.example.app', 'stop-runner']);
696+
expect(calls).toEqual(['stop-runner', 'close:com.example.app', 'release-runner:stop']);
698697
});
699698

700699
test('close <app> on iOS simulator retains runner while terminating app', async () => {
@@ -718,6 +717,9 @@ test('close <app> on iOS simulator retains runner while terminating app', async
718717
mockStopIosRunner.mockImplementation(async () => {
719718
calls.push('stop-runner');
720719
});
720+
mockReleaseRunnerOnClose.mockImplementation(async (_deviceId, options) => {
721+
calls.push(`release-runner:${options.retain ? 'retain' : 'stop'}`);
722+
});
721723
mockDispatch.mockImplementation(async (_device, command, positionals) => {
722724
calls.push(`${command}:${(positionals ?? []).join(' ')}`);
723725
return {};
@@ -728,7 +730,7 @@ test('close <app> on iOS simulator retains runner while terminating app', async
728730
);
729731

730732
expect(response.ok).toBe(true);
731-
expect(calls).toEqual(['close:com.example.app']);
733+
expect(calls).toEqual(['close:com.example.app', 'release-runner:retain']);
732734
});
733735

734736
test('app-only close terminates an iOS simulator app without ending its session', async () => {
@@ -794,6 +796,9 @@ test('close <app> on macOS stops runner before app close dispatch and dismisses
794796
mockStopIosRunner.mockImplementation(async (deviceId) => {
795797
calls.push(`stop-runner:${deviceId}`);
796798
});
799+
mockReleaseRunnerOnClose.mockImplementation(async (deviceId, options) => {
800+
calls.push(`release-runner:${deviceId}:${options.retain ? 'retain' : 'stop'}`);
801+
});
797802
mockDismissMacOsAlert.mockImplementation(async (action, options) => {
798803
calls.push(
799804
`dismiss-alert:${action}:${(options as any)?.bundleId ?? (options as any)?.surface ?? 'frontmost'}`,
@@ -813,7 +818,7 @@ test('close <app> on macOS stops runner before app close dispatch and dismisses
813818
expect(calls).toEqual([
814819
'stop-runner:host-macos-local',
815820
'close:System Settings',
816-
'stop-runner:host-macos-local',
821+
'release-runner:host-macos-local:stop',
817822
'dismiss-alert:dismiss:com.apple.systempreferences',
818823
]);
819824
});

‎src/daemon/handlers/__tests__/session-test-harness.ts‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ vi.mock('../../../platform-runtime-runtime-hints.ts', async (importOriginal) =>
3838
vi.mock('@agent-device/platform-apple/runner/operations', async (importOriginal) => {
3939
const actual =
4040
await importOriginal<typeof import('@agent-device/platform-apple/runner/operations')>();
41-
const stopIosRunnerSession = vi.fn(async (_deviceId: string) => {});
4241
return {
4342
...actual,
4443
prepareIosRunner: vi.fn(async () => ({
@@ -49,11 +48,8 @@ vi.mock('@agent-device/platform-apple/runner/operations', async (importOriginal)
4948
prewarmAppleRunnerCache: vi.fn(),
5049
prewarmIosRunnerSession: vi.fn(),
5150
notifyIosRunnerAppRelaunched: vi.fn(async () => {}),
52-
stopIosRunnerSession,
53-
// Mirrors the runner module: a retained close keeps warm reuse; any other close stops it.
54-
releaseIosRunnerOnClose: vi.fn(async (deviceId: string, options: { retain: boolean }) => {
55-
if (!options.retain) await stopIosRunnerSession(deviceId);
56-
}),
51+
stopIosRunnerSession: vi.fn(async () => {}),
52+
releaseIosRunnerOnClose: vi.fn(async () => {}),
5753
};
5854
});
5955
vi.mock('@agent-device/platform-apple/macos', async (importOriginal) => {
@@ -123,20 +119,24 @@ export const mockResolveTargetDevice = vi.mocked(resolveTargetDevice);
123119
export const mockEnsureDeviceReady = vi.mocked(ensureDeviceReady);
124120
const mockApplyRuntimeHints = vi.mocked(applyRuntimeHintValues);
125121
export const mockClearRuntimeHints = vi.mocked(clearRuntimeHintValues);
126-
const mockPrewarmIosRunnerSession = vi.mocked(prewarmIosRunnerSession);
127-
const mockNotifyIosRunnerAppRelaunched = vi.mocked(notifyIosRunnerAppRelaunched);
122+
export const mockPrewarmIosRunnerSession = vi.mocked(prewarmIosRunnerSession);
123+
export const mockNotifyIosRunnerAppRelaunched = vi.mocked(notifyIosRunnerAppRelaunched);
128124
export const mockPrewarmAppleRunnerCache = vi.mocked(prewarmAppleRunnerCache);
129-
const mockPrepareIosRunner = vi.mocked(prepareIosRunner);
130-
const mockStopIosRunner = vi.mocked(stopIosRunnerSession);
125+
export const mockPrepareIosRunner = vi.mocked(prepareIosRunner);
126+
export const mockStopIosRunner = vi.mocked(stopIosRunnerSession);
131127
const mockReleaseRunnerOnClose = vi.mocked(releaseIosRunnerOnClose);
132-
const mockDismissMacOsAlert = vi.mocked(runMacOsAlertAction);
128+
export const mockDismissMacOsAlert = vi.mocked(runMacOsAlertAction);
133129
export const mockResolveAndroidPackage = vi.mocked(resolveAndroidPackageForOpen);
134-
const mockCleanupRetainedMaterializedPaths = vi.mocked(cleanupRetainedMaterializedPathsForSession);
130+
export const mockCleanupRetainedMaterializedPaths = vi.mocked(
131+
cleanupRetainedMaterializedPathsForSession,
132+
);
135133
export const mockRunCmd = vi.mocked(runCmd);
136134
/** The retired dispatcher's snapshot leg, now an owned test double (R58). */
137135
export const mockDispatch = legacyDispatchCapture;
138-
const mockResolveIosApp = vi.mocked(resolveIosApp);
139-
const mockResolveIosSimulatorDeepLinkBundleId = vi.mocked(resolveIosSimulatorDeepLinkBundleId);
136+
export const mockResolveIosApp = vi.mocked(resolveIosApp);
137+
export const mockResolveIosSimulatorDeepLinkBundleId = vi.mocked(
138+
resolveIosSimulatorDeepLinkBundleId,
139+
);
140140

141141
beforeEach(() => {
142142
vi.useRealTimers();
@@ -162,7 +162,7 @@ beforeEach(() => {
162162
healthCheckMs: 3,
163163
});
164164
mockStopIosRunner.mockReset();
165-
mockReleaseRunnerOnClose.mockClear();
165+
mockReleaseRunnerOnClose.mockReset();
166166
mockStopIosRunner.mockResolvedValue(undefined);
167167
mockDismissMacOsAlert.mockReset();
168168
mockDismissMacOsAlert.mockResolvedValue({} as any);

‎src/daemon/session-lifecycle/internal/__tests__/session-close-error-precedence.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616
makeSessionStore,
1717
mkdtempForTestSync,
1818
mockDispatchCommand,
19+
mockReleaseRunnerOnClose,
1920
mockStopIosRunnerSession,
2021
noopInvoke,
2122
os,
@@ -72,7 +73,7 @@ test('targeted close preserves the platform-close AppError and still runs later
7273
// A failed close is not recorded as `Closed`.
7374
expect(session.actions.some((action) => action.command === 'close')).toBe(false);
7475
// Subsequent independent cleanup still ran, and the session was still deleted.
75-
expect(mockStopIosRunnerSession).toHaveBeenCalledWith(session.device.id);
76+
expect(mockReleaseRunnerOnClose).toHaveBeenCalledWith(session.device.id, { retain: false });
7677
expect(sessionStore.get(sessionName)).toBeUndefined();
7778
});
7879

@@ -401,9 +402,10 @@ test('targeted close skips platform dispatch and preserves the error when the re
401402
});
402403
// A skipped close is not recorded as `Closed`.
403404
expect(session.actions.some((action) => action.command === 'close')).toBe(false);
404-
// Later independent cleanup still ran (runner stop re-attempted), and the
405+
// Later independent cleanup still ran (runner release re-attempted), and the
405406
// session was still deleted.
406-
expect(mockStopIosRunnerSession.mock.calls.length).toBeGreaterThan(1);
407+
expect(mockStopIosRunnerSession).toHaveBeenCalledOnce();
408+
expect(mockReleaseRunnerOnClose).toHaveBeenCalledWith(session.device.id, { retain: false });
407409
expect(sessionStore.get(sessionName)).toBeUndefined();
408410
});
409411

‎src/daemon/session-lifecycle/internal/__tests__/session-close-save-script.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
makeIosSimulatorRecordingSession,
88
makeSession,
99
makeSessionStore,
10+
mockReleaseRunnerOnClose,
1011
mockStopIosRunnerSession,
1112
noopInvoke,
1213
os,
@@ -61,6 +62,7 @@ test('close --save-script on a never-armed session is rejected before teardown,
6162
expect(finish).not.toHaveBeenCalled();
6263
expect(session.screenRecording).toBeDefined();
6364
expect(mockStopIosRunnerSession).not.toHaveBeenCalled();
65+
expect(mockReleaseRunnerOnClose).not.toHaveBeenCalled();
6466

6567
// A plain close (no --save-script) still closes the same session cleanly afterward, and now
6668
// teardown genuinely does run: the recorder is signaled and the session deleted.
@@ -79,7 +81,7 @@ test('close --save-script on a never-armed session is rejected before teardown,
7981
});
8082
expect(plainClose?.ok).toBe(true);
8183
expect(finish).toHaveBeenCalledOnce();
82-
expect(mockStopIosRunnerSession).toHaveBeenCalledWith(session.device.id);
84+
expect(mockReleaseRunnerOnClose).toHaveBeenCalledWith(session.device.id, { retain: false });
8385
expect(sessionStore.get(sessionName)).toBeUndefined();
8486
});
8587

‎src/daemon/session-lifecycle/internal/__tests__/session-close-shutdown.fixtures.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@ import { mkdtempForTestSync } from '../../../../__tests__/test-utils/tmp-dir.ts'
1111
vi.mock('@agent-device/platform-apple/runner/operations', async (importOriginal) => {
1212
const actual =
1313
await importOriginal<typeof import('@agent-device/platform-apple/runner/operations')>();
14-
const stopIosRunnerSession = vi.fn(async (_deviceId: string) => {});
1514
return {
1615
...actual,
17-
stopIosRunnerSession,
18-
// Mirrors the runner module: a retained close keeps warm reuse; any other close stops it.
19-
releaseIosRunnerOnClose: vi.fn(async (deviceId: string, options: { retain: boolean }) => {
20-
if (!options.retain) await stopIosRunnerSession(deviceId);
21-
}),
16+
stopIosRunnerSession: vi.fn(async () => {}),
17+
releaseIosRunnerOnClose: vi.fn(async () => {}),
2218
};
2319
});
2420
vi.mock('@agent-device/platform-apple/perf', async (importOriginal) => {
@@ -69,7 +65,10 @@ import {
6965
cleanupAndroidNativePerfSession,
7066
stopAndroidSnapshotHelperSessionForDevice,
7167
} from '@agent-device/platform-android/mechanics';
72-
import { stopIosRunnerSession } from '@agent-device/platform-apple/runner/operations';
68+
import {
69+
releaseIosRunnerOnClose,
70+
stopIosRunnerSession,
71+
} from '@agent-device/platform-apple/runner/operations';
7372
import { cleanupAppleXctracePerfCapture } from '@agent-device/platform-apple/perf';
7473
import { WEB_DESKTOP_DEVICE } from '../../../../__tests__/test-utils/device-fixtures.ts';
7574
import { acquireDeviceClaim } from '../../../device-claims.ts';
@@ -98,6 +97,7 @@ const mockStopAndroidSnapshotHelperSessionForDevice = vi.mocked(
9897
stopAndroidSnapshotHelperSessionForDevice,
9998
);
10099
const mockStopIosRunnerSession = vi.mocked(stopIosRunnerSession);
100+
const mockReleaseRunnerOnClose = vi.mocked(releaseIosRunnerOnClose);
101101

102102
const teardownSessionResources = (
103103
request: Parameters<typeof teardownProductionSessionResources>[0],
@@ -258,6 +258,7 @@ export const sessionCloseShutdownFixture = Object.freeze({
258258
mockShutdownTargetRuntime,
259259
mockRunCmd,
260260
mockStopAndroidSnapshotHelperSessionForDevice,
261+
mockReleaseRunnerOnClose,
261262
mockStopIosRunnerSession,
262263
narrowDeviceBinding,
263264
noopInvoke,

‎src/daemon/session-lifecycle/internal/__tests__/session-teardown-resources.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
makeSession,
1313
makeSessionStore,
1414
mockRunCmd,
15+
mockReleaseRunnerOnClose,
1516
mockStopAndroidSnapshotHelperSessionForDevice,
1617
mockStopIosRunnerSession,
1718
mockStopIosRunnerSession: stopIosRunnerSession,
@@ -60,9 +61,9 @@ test('close finalizes an active iOS simulator recording before deleting the sess
6061
expect(sessionStore.get(sessionName)).toBeUndefined();
6162
// An active recording at close time still defeats iOS runner retention even
6263
// though the recording is finalized (and cleared) before the retention step.
63-
expect(mockStopIosRunnerSession).toHaveBeenCalledWith(session.device.id);
64+
expect(mockReleaseRunnerOnClose).toHaveBeenCalledWith(session.device.id, { retain: false });
6465
expect(finish.mock.invocationCallOrder[0]).toBeLessThan(
65-
mockStopIosRunnerSession.mock.invocationCallOrder[0]!,
66+
mockReleaseRunnerOnClose.mock.invocationCallOrder[0]!,
6667
);
6768
});
6869

@@ -95,7 +96,7 @@ test('close surfaces a recording finalization failure through the cleanup-failur
9596
// Cleanup failure is reported, later cleanup still ran, session still deleted.
9697
expect(finish).toHaveBeenCalledOnce();
9798
expect(forceCleanup).toHaveBeenCalledOnce();
98-
expect(mockStopIosRunnerSession).toHaveBeenCalledWith(session.device.id);
99+
expect(mockReleaseRunnerOnClose).toHaveBeenCalledWith(session.device.id, { retain: false });
99100
expect(sessionStore.get(sessionName)).toBeUndefined();
100101
});
101102

0 commit comments

Comments
 (0)