|
| 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