Skip to content

Commit f27156c

Browse files
committed
refactor(test): move the cross-surface settle tests onto their source mirror
The #2438 cross-surface cases were appended to `settle.test.ts`, taking it over the test-file size ratchet (2528 lines, 2359 at the merge-base). They assert the comparison `post-action-surface.ts` owns, so they move to that module's mirror test file, and the device double plus the trees both files drive move to a sibling fixtures module under `__tests__/` rather than being duplicated. Pure move: every test and every assertion is unchanged, and `settle.test.ts` is back under its merge-base length.
1 parent 36fdde3 commit f27156c

3 files changed

Lines changed: 275 additions & 243 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Doubles the `--settle` routes share: the backend whose captures the settle loop
2+
// reads, and the small trees it serves. `settle.test.ts` drives the loop itself and
3+
// `post-action-surface.test.ts` the cross-surface comparison over it, so both read
4+
// these builders from here instead of keeping a private copy.
5+
//
6+
// Budgets are injected (fake clock) — no real waiting.
7+
8+
import type { AgentDeviceBackend, BackendSnapshotResult } from '../../../../backend.ts';
9+
import type { SnapshotState } from '@agent-device/kernel/snapshot';
10+
import { createLocalArtifactAdapter } from '../../../../io.ts';
11+
import {
12+
createAgentDevice,
13+
createMemorySessionStore,
14+
localCommandPolicy,
15+
} from '../../../../runtime.ts';
16+
import { makeSnapshotState } from '@agent-device/selectors/snapshot-geometry-fixtures';
17+
18+
export function createFakeClock(stepMs = 300): {
19+
now: () => number;
20+
sleep: (ms: number) => Promise<void>;
21+
advance: (ms: number) => void;
22+
} {
23+
let elapsed = 0;
24+
return {
25+
now: () => elapsed,
26+
sleep: async (ms: number) => {
27+
elapsed += ms > 0 ? ms : stepMs;
28+
},
29+
advance: (ms: number) => {
30+
elapsed += ms;
31+
},
32+
};
33+
}
34+
35+
export function buttonSnapshot(): SnapshotState {
36+
return makeSnapshotState([
37+
{
38+
index: 0,
39+
depth: 0,
40+
type: 'Button',
41+
label: 'Continue',
42+
rect: { x: 10, y: 20, width: 100, height: 40 },
43+
hittable: true,
44+
},
45+
]);
46+
}
47+
48+
// Five nodes so a settled capture clears the tiny-tree readiness heuristic.
49+
export function welcomeSnapshot(): SnapshotState {
50+
return makeSnapshotState(
51+
['Welcome!', 'Next', 'Back', 'Home', 'Menu'].map((label, index) => ({
52+
index,
53+
depth: index === 0 ? 0 : 1,
54+
...(index === 0 ? {} : { parentIndex: 0 }),
55+
type: index === 0 ? 'StaticText' : 'Button',
56+
label,
57+
rect: { x: 10, y: 20 + index * 60, width: 100, height: 40 },
58+
hittable: true,
59+
})),
60+
);
61+
}
62+
63+
export function createSettleDevice(params: {
64+
stored: SnapshotState;
65+
captureSnapshot: () => Promise<BackendSnapshotResult> | BackendSnapshotResult;
66+
tap?: () => Promise<Record<string, unknown>>;
67+
clock?: ReturnType<typeof createFakeClock>;
68+
appBundleId?: string;
69+
}): ReturnType<typeof createAgentDevice> {
70+
return createAgentDevice({
71+
backend: {
72+
platform: 'ios',
73+
captureSnapshot: async () => await params.captureSnapshot(),
74+
tap: async () => (params.tap ? await params.tap() : { ok: true }),
75+
fill: async () => ({ ok: true }),
76+
longPress: async () => ({ ok: true }),
77+
} satisfies AgentDeviceBackend,
78+
artifacts: createLocalArtifactAdapter(),
79+
sessions: createMemorySessionStore([
80+
{
81+
name: 'default',
82+
snapshot: params.stored,
83+
...(params.appBundleId ? { appBundleId: params.appBundleId } : {}),
84+
},
85+
]),
86+
policy: localCommandPolicy(),
87+
clock: params.clock ?? createFakeClock(),
88+
});
89+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import assert from 'node:assert/strict';
2+
import { test } from 'vitest';
3+
import type { SnapshotState } from '@agent-device/kernel/snapshot';
4+
import { makeSnapshotState } from '@agent-device/selectors/snapshot-geometry-fixtures';
5+
import { summarizeAxEvidence } from '@agent-device/capture-kit/snapshot-evidence';
6+
import { IOS_SYSTEM_SURFACE_DISCLOSURE } from '@agent-device/contracts/ios-system-surface';
7+
import { selector } from './selector-read-utils.ts';
8+
import {
9+
buttonSnapshot,
10+
createSettleDevice,
11+
welcomeSnapshot,
12+
} from './__tests__/settle-device-fixtures.ts';
13+
14+
// #2438 cross-surface settle: iOS serves a web sign-in sheet
15+
// (com.apple.SafariViewService) IN PLACE over a still-foreground app, so a settled
16+
// capture of the sheet and a pre-action capture of the app describe DIFFERENT
17+
// surfaces. A diff between them is a whole-surface replacement dressed as change
18+
// within one surface — and, since diff presence is what issues refs, it would hand
19+
// the caller refs for that claim.
20+
const WEB_SIGN_IN_SHEET_BUNDLE_ID = 'com.apple.SafariViewService';
21+
22+
function webSignInSheetSnapshot(labels: string[]): SnapshotState {
23+
return {
24+
...makeSnapshotState(
25+
labels.map((label, index) => ({
26+
index,
27+
depth: 0,
28+
type: 'Button',
29+
label,
30+
rect: { x: 10, y: 20 + index * 50, width: 200, height: 40 },
31+
hittable: true,
32+
})),
33+
),
34+
iosSystemSurfaceBundleId: WEB_SIGN_IN_SHEET_BUNDLE_ID,
35+
};
36+
}
37+
38+
// Five labels so a settled sheet clears the tiny-tree readiness heuristic, and a
39+
// `Continue` control so the same selector also resolves on the sheet.
40+
const WEB_SIGN_IN_SHEET_LABELS = [
41+
'Continue',
42+
'Sign in with Example',
43+
'Email',
44+
'Password',
45+
'Cancel',
46+
];
47+
48+
test('press --settle attaches no diff across an app-to-sheet surface change and discloses it', async () => {
49+
const before = buttonSnapshot();
50+
const sheet = webSignInSheetSnapshot(WEB_SIGN_IN_SHEET_LABELS);
51+
let captures = 0;
52+
const device = createSettleDevice({
53+
stored: before,
54+
captureSnapshot: () => {
55+
captures += 1;
56+
// Capture 1 = selector resolution (app baseline). Captures 2+ = the settle
57+
// loop, reading the sheet now presented over that app.
58+
return { snapshot: captures === 1 ? before : sheet };
59+
},
60+
});
61+
62+
const result = await device.interactions.press(selector('label=Continue'), {
63+
session: 'default',
64+
settle: {},
65+
});
66+
67+
const settle = result.settle;
68+
assert.ok(settle);
69+
assert.equal(settle.settled, true);
70+
assert.deepEqual(settle.surfaceChange, {
71+
from: 'app',
72+
to: WEB_SIGN_IN_SHEET_BUNDLE_ID,
73+
disclosure: IOS_SYSTEM_SURFACE_DISCLOSURE,
74+
});
75+
// No same-surface claim: no diff, so no issued refs, and no tail either.
76+
assert.equal(settle.diff, undefined);
77+
assert.equal(settle.tail, undefined);
78+
assert.match(settle.hint ?? '', /different surfaces/);
79+
assert.match(settle.hint ?? '', /take a snapshot/i);
80+
// Disclosed, not hidden: the settled sheet still becomes the stored observation
81+
// a follow-up snapshot reads.
82+
const stored = (await device.sessions.get('default')) as { snapshot?: SnapshotState };
83+
assert.equal(stored.snapshot?.iosSystemSurfaceBundleId, WEB_SIGN_IN_SHEET_BUNDLE_ID);
84+
});
85+
86+
test('press --settle attaches no diff across a sheet-to-app surface change and discloses it', async () => {
87+
const sheet = webSignInSheetSnapshot(WEB_SIGN_IN_SHEET_LABELS);
88+
const app = welcomeSnapshot();
89+
let captures = 0;
90+
const device = createSettleDevice({
91+
stored: sheet,
92+
captureSnapshot: () => {
93+
captures += 1;
94+
// The baseline is the sheet this press acts on; the settled tree is the app
95+
// content that returns once the sheet completes and dismisses itself.
96+
return { snapshot: captures === 1 ? sheet : app };
97+
},
98+
});
99+
100+
const result = await device.interactions.press(selector('label=Continue'), {
101+
session: 'default',
102+
settle: {},
103+
});
104+
105+
const settle = result.settle;
106+
assert.ok(settle);
107+
assert.equal(settle.settled, true);
108+
assert.equal(settle.surfaceChange?.from, WEB_SIGN_IN_SHEET_BUNDLE_ID);
109+
assert.equal(settle.surfaceChange?.to, 'app');
110+
assert.match(settle.surfaceChange?.disclosure ?? '', /sign-in sheet/);
111+
// The sheet is gone, so the standing "is presented over the app" sentence cannot
112+
// be the one used.
113+
assert.notEqual(settle.surfaceChange?.disclosure, IOS_SYSTEM_SURFACE_DISCLOSURE);
114+
assert.equal(settle.diff, undefined);
115+
assert.equal(settle.tail, undefined);
116+
assert.match(settle.hint ?? '', /different surfaces/);
117+
});
118+
119+
test('press --settle --verify reports one app-to-sheet surface change on both payloads', async () => {
120+
const before = buttonSnapshot();
121+
const sheet = webSignInSheetSnapshot(WEB_SIGN_IN_SHEET_LABELS);
122+
let captures = 0;
123+
const device = createSettleDevice({
124+
stored: before,
125+
captureSnapshot: () => {
126+
captures += 1;
127+
return { snapshot: captures === 1 ? before : sheet };
128+
},
129+
});
130+
131+
const result = await device.interactions.press(selector('label=Continue'), {
132+
session: 'default',
133+
settle: {},
134+
verify: true,
135+
});
136+
137+
// The settle loop's final capture doubles as the verify evidence source, so the
138+
// shared evidence path must refuse the cross-surface digest comparison too.
139+
assert.deepEqual(result.evidence?.surfaceChange, {
140+
from: 'app',
141+
to: WEB_SIGN_IN_SHEET_BUNDLE_ID,
142+
disclosure: IOS_SYSTEM_SURFACE_DISCLOSURE,
143+
});
144+
assert.equal(result.evidence?.changedFromBefore, true);
145+
assert.equal(result.settle?.surfaceChange?.to, WEB_SIGN_IN_SHEET_BUNDLE_ID);
146+
assert.equal(result.settle?.diff, undefined);
147+
});
148+
149+
test('press --settle --verify reports a sheet-to-app surface change when the digests coincide', async () => {
150+
const sheet: SnapshotState = {
151+
...welcomeSnapshot(),
152+
iosSystemSurfaceBundleId: WEB_SIGN_IN_SHEET_BUNDLE_ID,
153+
};
154+
const app = welcomeSnapshot();
155+
// The premise this test exists for: the sheet tree and the app tree that replaces
156+
// it digest identically, so a digest comparison would report "nothing changed"
157+
// across a whole-surface replacement.
158+
assert.equal(summarizeAxEvidence(sheet.nodes).digest, summarizeAxEvidence(app.nodes).digest);
159+
let captures = 0;
160+
const device = createSettleDevice({
161+
stored: sheet,
162+
captureSnapshot: () => {
163+
captures += 1;
164+
return { snapshot: captures === 1 ? sheet : app };
165+
},
166+
});
167+
168+
const result = await device.interactions.press(selector('label=Next'), {
169+
session: 'default',
170+
settle: {},
171+
verify: true,
172+
});
173+
174+
assert.equal(result.evidence?.surfaceChange?.from, WEB_SIGN_IN_SHEET_BUNDLE_ID);
175+
assert.equal(result.evidence?.surfaceChange?.to, 'app');
176+
assert.equal(result.evidence?.changedFromBefore, true);
177+
assert.equal(result.settle?.surfaceChange?.from, WEB_SIGN_IN_SHEET_BUNDLE_ID);
178+
assert.equal(result.settle?.diff, undefined);
179+
});

0 commit comments

Comments
 (0)