Skip to content

Commit 4b07e86

Browse files
thymikeeclaude
andcommitted
test(daemon): run every snapshot-handler diff on the forwarding surface and tighten the order and claim assertions
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f198599 commit 4b07e86

9 files changed

Lines changed: 38 additions & 84 deletions

‎packages/daemon/src/__tests__/support/snapshot-command-surface-fakes.ts‎

Lines changed: 0 additions & 65 deletions
This file was deleted.

‎packages/daemon/src/core/snapshot-session.test.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,25 @@ afterEach(() => {
1212
});
1313

1414
test('sessionless cleanup releases the local execution host after the task', async () => {
15-
const platformCleanup = platformResourceCleanupFixture();
15+
const events: string[] = [];
16+
const platformCleanup = platformResourceCleanupFixture({
17+
cleanupSessionlessExecutionHost: async () => {
18+
events.push('cleanup');
19+
},
20+
});
1621

1722
const result = await withSessionlessRunnerCleanup(
1823
undefined,
1924
IOS_SIMULATOR,
20-
returnOk,
25+
async () => {
26+
events.push('task');
27+
return 'ok';
28+
},
2129
platformCleanup,
2230
);
2331

2432
expect(result).toBe('ok');
33+
expect(events).toEqual(['task', 'cleanup']);
2534
expect(platformCleanup.cleanupSessionlessExecutionHost).toHaveBeenCalledWith(IOS_SIMULATOR);
2635
});
2736

‎packages/daemon/src/sessions/handlers/__tests__/session-device-claims.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,9 @@ test('cancellation after local device setup retains the device claim for recover
331331

332332
assert.equal(response.ok, false);
333333
assert.equal(mockDispatch.mock.calls.length, 1);
334-
assert.equal(inspectDeviceClaims({ serial: android.id })[0]?.classification, 'live');
334+
const retainedClaim = inspectDeviceClaims({ serial: android.id })[0];
335+
assert.equal(retainedClaim?.classification, 'live');
336+
assert.equal(typeof retainedClaim?.claim?.abandonedAtMs, 'number');
335337
assert.equal(store.get('claim-canceled-after-dispatch'), undefined);
336338
} finally {
337339
clearRequestCanceled(requestId);

‎packages/daemon/src/sessions/handlers/snapshot-handler-freshness.test.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { AppError } from '@agent-device/kernel/errors';
44
import { daemonPlatformServicesFixture } from '../../__tests__/support/platform-services-fixture.ts';
55
import { legacyDispatchCapture } from '../../__tests__/support/legacy-snapshot-capture-fixture.ts';
66
import { forwardingCreateCommandSurface } from '../../__tests__/support/command-surface-fake.ts';
7-
import { diffForwardingCreateCommandSurface } from '../../__tests__/support/snapshot-command-surface-fakes.ts';
87
import { SessionStore } from '../../core/session-store.ts';
98
import {
109
fixtureScreenshotCaptures,
@@ -159,8 +158,7 @@ test.each([
159158
);
160159

161160
const response = await handleSnapshotCommands({
162-
createCommandSurface:
163-
route === 'diff' ? diffForwardingCreateCommandSurface : forwardingCreateCommandSurface,
161+
createCommandSurface: forwardingCreateCommandSurface,
164162
platformServices: daemonPlatformServicesFixture(),
165163
req: snapshotRequest(sessionName, route, {
166164
...(route === 'diff' ? { positionals: ['snapshot'] } : {}),

‎packages/daemon/src/sessions/handlers/snapshot-handler.test.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { legacyDispatchCapture } from '../../__tests__/support/legacy-snapshot-c
55
import { resetSnapshotRuntimeFixture } from '../../__tests__/support/snapshot-runtime-fixture.ts';
66
import { forwardingCreateCommandSurface } from '../../__tests__/support/command-surface-fake.ts';
77
import { providerDeviceAdmissionFixture } from '../../__tests__/support/provider-device-admission-fixture.ts';
8-
import { diffForwardingCreateCommandSurface } from '../../__tests__/support/snapshot-command-surface-fakes.ts';
98
import type { SessionState } from '../../core/session-state.ts';
109
import {
1110
androidCapture,
@@ -279,10 +278,7 @@ async function runVersionedRefsCommand(params: {
279278
command: 'snapshot' | 'diff';
280279
}): Promise<Record<string, unknown> | undefined> {
281280
const response = await handleSnapshotCommands({
282-
createCommandSurface:
283-
params.command === 'diff'
284-
? diffForwardingCreateCommandSurface
285-
: forwardingCreateCommandSurface,
281+
createCommandSurface: forwardingCreateCommandSurface,
286282
platformServices: daemonPlatformServicesFixture(),
287283
req: snapshotRequest(params.sessionName, params.command, {
288284
positionals: params.command === 'diff' ? ['snapshot'] : [],

‎packages/daemon/src/sessions/handlers/snapshot-scoped-refs.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { makeAndroidSession } from '../../__tests__/support/session-factories.ts
66
import { makeSessionStore } from '../../__tests__/support/store-factory.ts';
77
import { expireRefFrame } from '../../core/ref-frame.ts';
88
import { snapshotRuntimeFixture } from '../../__tests__/support/snapshot-runtime-fixture.ts';
9-
import { storingCreateCommandSurface } from '../../__tests__/support/snapshot-command-surface-fakes.ts';
9+
import { forwardingCreateCommandSurface } from '../../__tests__/support/command-surface-fake.ts';
1010

1111
const ANDROID_SCRIPT_ERROR = 'Unable to load script. Make sure you are running Metro.';
1212

@@ -98,7 +98,7 @@ function requestScopedSnapshot(
9898
) {
9999
const runtime = snapshotRuntimeFixture();
100100
return handleProductionSnapshotCommands({
101-
createCommandSurface: storingCreateCommandSurface,
101+
createCommandSurface: forwardingCreateCommandSurface,
102102
req: {
103103
token: 't',
104104
session: sessionName,

‎packages/daemon/src/sessions/session-lifecycle/internal/session-open-execution-relaunch.test.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,12 @@ async function openRelaunch(params: {
9595
onOpenApplication?: (input: OpenApplicationInput) => Promise<void> | void;
9696
}) {
9797
const opened: OpenApplicationInput[] = [];
98+
const boundDevices: DeviceInfo[] = [];
9899
const bindDevice: Parameters<typeof handleSessionCommands>[0]['bindDevice'] = async (
99100
device,
100101
use,
101102
) => {
103+
boundDevices.push(device);
102104
const binding = await mockBindDeviceRuntime(device, use);
103105
return {
104106
...binding,
@@ -128,7 +130,7 @@ async function openRelaunch(params: {
128130
invoke: noopInvoke,
129131
bindDevice,
130132
});
131-
return { response, opened };
133+
return { response, opened, boundDevices };
132134
}
133135

134136
test('open --relaunch asks the lifecycle to relaunch the active session app', async () => {
@@ -179,29 +181,31 @@ test('open --relaunch does not let an ambient provider claim suppress the relaun
179181
installProviderDeviceAdmission({ isActive: (device) => device.id === ANDROID_EMULATOR.id });
180182
mockResolveTargetDevice.mockResolvedValue(ANDROID_EMULATOR);
181183

182-
const { response, opened } = await openRelaunch({
184+
const { response, opened, boundDevices } = await openRelaunch({
183185
sessionStore,
184186
sessionName: 'provider-android-session',
185187
positionals: ['com.example.app'],
186188
flags: { platform: 'android' },
187189
});
188190

189191
expect(response?.ok).toBe(true);
192+
expect(boundDevices).toEqual([ANDROID_EMULATOR]);
190193
expect(opened).toEqual([expect.objectContaining({ relaunch: true, hasExistingSession: false })]);
191194
});
192195

193196
test('open --relaunch without a session relaunches the requested app on the resolved device', async () => {
194197
const sessionStore = makeSessionStore();
195198
mockResolveTargetDevice.mockResolvedValue(IOS_DEVICE);
196199

197-
const { response, opened } = await openRelaunch({
200+
const { response, opened, boundDevices } = await openRelaunch({
198201
sessionStore,
199202
sessionName: 'ios-new-session',
200203
positionals: ['com.example.app'],
201204
flags: { platform: 'ios' },
202205
});
203206

204207
expect(response?.ok).toBe(true);
208+
expect(boundDevices).toEqual([IOS_DEVICE]);
205209
expect(opened).toEqual([
206210
expect.objectContaining({
207211
relaunch: true,

‎packages/platform-apple/src/lifecycle-relaunch-close.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ afterEach(() => {
2222

2323
type OpenCall = Readonly<{ app: string; options?: Record<string, unknown> }>;
2424

25+
/** Completes after the caller has had a chance to run ahead, so only an awaited step is recorded first. */
26+
const settleLater = async () => await new Promise<void>((resolve) => setTimeout(resolve, 0));
27+
2528
function lifecycleHost(params: {
2629
events: string[];
2730
hasLiveRunnerSession?: boolean;
@@ -42,6 +45,7 @@ function lifecycleHost(params: {
4245
const appleApplications = {
4346
...baseHost.appleApplications,
4447
stopRunnerSession: vi.fn(async (deviceId: string) => {
48+
await settleLater();
4549
params.events.push(`stop-runner:${deviceId}`);
4650
}),
4751
prewarmRunnerSession: vi.fn(
@@ -55,6 +59,7 @@ function lifecycleHost(params: {
5559
}),
5660
hasLiveRunnerSession: vi.fn(async () => params.hasLiveRunnerSession ?? false),
5761
releaseRunnerOnClose: vi.fn(async (deviceId: string, options: { retain: boolean }) => {
62+
await settleLater();
5863
params.events.push(`release-runner:${deviceId}:${options.retain ? 'retain' : 'stop'}`);
5964
}),
6065
dismissCloseAlerts: vi.fn(async () => {

‎src/platform-runtime-resource-cleanup-sessionless-host.test.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import { beforeEach, expect, test, vi } from 'vitest';
22
import { IOS_SIMULATOR, ANDROID_EMULATOR } from '@agent-device/test-kit/device-fixtures';
33
import { platformResourceCleanup } from './platform-runtime-resource-cleanup.ts';
44

5-
const { closeIosApp, stopIosRunnerSession } = vi.hoisted(() => ({
5+
const { RUNNER_BUNDLE_ID, closeIosApp, stopIosRunnerSession } = vi.hoisted(() => ({
6+
RUNNER_BUNDLE_ID: 'com.example.test-runner',
67
closeIosApp: vi.fn(async () => {}),
78
stopIosRunnerSession: vi.fn(async () => {}),
89
}));
910

1011
vi.mock('@agent-device/platform-apple/runner/operations', async (importOriginal) => ({
1112
...(await importOriginal<typeof import('@agent-device/platform-apple/runner/operations')>()),
1213
stopIosRunnerSession,
14+
resolveRunnerAppBundleId: () => RUNNER_BUNDLE_ID,
1315
}));
1416
vi.mock('@agent-device/platform-apple/app-lifecycle', async (importOriginal) => ({
1517
...(await importOriginal<typeof import('@agent-device/platform-apple/app-lifecycle')>()),
@@ -25,7 +27,10 @@ test('sessionless iOS runner cleanup stops the runner host app', async () => {
2527
await platformResourceCleanup.cleanupSessionlessExecutionHost(IOS_SIMULATOR);
2628

2729
expect(stopIosRunnerSession).toHaveBeenCalledWith(IOS_SIMULATOR.id);
28-
expect(closeIosApp).toHaveBeenCalledWith(IOS_SIMULATOR, 'com.callstack.agentdevice.runner');
30+
expect(closeIosApp).toHaveBeenCalledWith(IOS_SIMULATOR, RUNNER_BUNDLE_ID);
31+
expect(stopIosRunnerSession.mock.invocationCallOrder[0]!).toBeLessThan(
32+
closeIosApp.mock.invocationCallOrder[0]!,
33+
);
2934
});
3035

3136
test('sessionless iOS runner host close is best effort', async () => {
@@ -36,7 +41,7 @@ test('sessionless iOS runner host close is best effort', async () => {
3641
).resolves.toBeUndefined();
3742

3843
expect(stopIosRunnerSession).toHaveBeenCalledWith(IOS_SIMULATOR.id);
39-
expect(closeIosApp).toHaveBeenCalledWith(IOS_SIMULATOR, 'com.callstack.agentdevice.runner');
44+
expect(closeIosApp).toHaveBeenCalledWith(IOS_SIMULATOR, RUNNER_BUNDLE_ID);
4045
});
4146

4247
test('sessionless cleanup touches no runner host for a non-Apple device', async () => {

0 commit comments

Comments
 (0)