|
1 | 1 | import { beforeEach, expect, test, vi } from 'vitest'; |
2 | 2 | import type { CommandFlags } from '@agent-device/contracts/command'; |
| 3 | +import type { RawSnapshotNode } from '@agent-device/kernel/snapshot'; |
| 4 | +import { |
| 5 | + IOS_SYSTEM_SURFACE_DISCLOSURE, |
| 6 | + iosSystemSurfaceTransitionDisclosure, |
| 7 | + type IosSystemSurfaceProvenance, |
| 8 | +} from '@agent-device/contracts/ios-system-surface'; |
3 | 9 | import { makeIosSession } from '../../__tests__/test-utils/session-factories.ts'; |
4 | 10 | import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts'; |
5 | 11 | import { activateCompleteRefFrame, refFrameState } from '../ref-frame.ts'; |
@@ -70,6 +76,7 @@ type SettlePayload = { |
70 | 76 | lines: Array<{ kind: string; text: string; ref?: string }>; |
71 | 77 | }; |
72 | 78 | tail?: Array<{ ref: string; role: string; label?: string }>; |
| 79 | + surfaceChange?: { from: string; to: string; disclosure: string }; |
73 | 80 | hint?: string; |
74 | 81 | }; |
75 | 82 |
|
@@ -119,17 +126,49 @@ const platformExecution = vi.fn( |
119 | 126 | /** Stands in for the device work a bound generic leaf performs, keyed by command name. */ |
120 | 127 | const mockDispatch = vi.fn<(command: string) => Promise<Record<string, unknown>>>(async () => ({})); |
121 | 128 |
|
122 | | -function seedSession(sessionName: string, sessionStore: SessionStore): SessionState { |
| 129 | +/** |
| 130 | + * The session as it stands before the generic command runs. `baseline` is the STORED pre-action |
| 131 | + * tree the settled diff is taken against, and the surface that capture described — which is how a |
| 132 | + * session whose last observation was an in-place system surface (#2438) is set up. |
| 133 | + */ |
| 134 | +function seedSession( |
| 135 | + sessionName: string, |
| 136 | + sessionStore: SessionStore, |
| 137 | + baseline: GenericSettleTree = { nodes: BEFORE_NODES }, |
| 138 | +): SessionState { |
123 | 139 | const session = makeIosSession(sessionName); |
124 | | - setSessionSnapshot( |
125 | | - session, |
126 | | - buildSnapshotState({ nodes: BEFORE_NODES, backend: 'xctest', producer: 'apple-runner' }, {}), |
127 | | - ); |
| 140 | + setSessionSnapshot(session, buildSnapshotState(snapshotPayload(baseline), {})); |
128 | 141 | activateCompleteRefFrame(session); |
129 | 142 | sessionStore.set(sessionName, session); |
130 | 143 | return session; |
131 | 144 | } |
132 | 145 |
|
| 146 | +/** A tree plus the surface a capture of it describes: app content, or an in-place system surface. */ |
| 147 | +type GenericSettleTree = { |
| 148 | + nodes: RawSnapshotNode[]; |
| 149 | + systemSurface?: IosSystemSurfaceProvenance; |
| 150 | +}; |
| 151 | + |
| 152 | +function snapshotPayload(tree: GenericSettleTree) { |
| 153 | + return { |
| 154 | + nodes: tree.nodes, |
| 155 | + backend: 'xctest' as const, |
| 156 | + producer: 'apple-runner' as const, |
| 157 | + ...(tree.systemSurface ? { systemSurface: tree.systemSurface } : {}), |
| 158 | + }; |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * Every settle capture reads `tree`; any other command answers `result`. Unlike |
| 163 | + * {@link mockCommandDispatch} the captured tree carries its surface provenance, so the settled |
| 164 | + * capture can describe a surface other than the one the stored baseline described. |
| 165 | + */ |
| 166 | +function mockSurfaceDispatch(tree: GenericSettleTree, result: Record<string, unknown> = {}) { |
| 167 | + mockDispatch.mockImplementation(async (command) => |
| 168 | + command === 'snapshot' ? snapshotPayload(tree) : result, |
| 169 | + ); |
| 170 | +} |
| 171 | + |
133 | 172 | async function dispatchGeneric(params: { |
134 | 173 | sessionName: string; |
135 | 174 | sessionStore: SessionStore; |
@@ -250,6 +289,217 @@ test('back --settle answers with the settled diff alongside the command result', |
250 | 289 | }); |
251 | 290 | }); |
252 | 291 |
|
| 292 | +// #2438 cross-surface settle on the GENERIC route: iOS serves a web sign-in sheet |
| 293 | +// (com.apple.SafariViewService) IN PLACE over a still-foreground app, so a settled capture of the |
| 294 | +// sheet and the session's stored pre-action tree of the app describe DIFFERENT surfaces. The diff |
| 295 | +// above would then be a whole-surface replacement presented as change within one surface — and |
| 296 | +// since the daemon treats `diff` presence as "this response issues refs", it would hand the caller |
| 297 | +// refs for that claim. This route plumbs the baseline's surface identity through |
| 298 | +// `baselineSurfaceBundleId`; the element-targeted route guarantees the same contract in |
| 299 | +// `src/commands/interaction/runtime/post-action-surface.test.ts`, and these tests assert it for |
| 300 | +// scroll/back so the two routes cannot drift. |
| 301 | +// |
| 302 | +// Both directions are covered because each falsifies a DIFFERENT half of that plumbing, and |
| 303 | +// neither substitutes for the other: drop the baseline's surface identity here and only the |
| 304 | +// sheet-to-app tests fail (an app baseline has no surface id to lose); drop the settled capture's |
| 305 | +// surface identity and only the app-to-sheet tests fail. |
| 306 | +const WEB_SIGN_IN_SHEET: IosSystemSurfaceProvenance = { |
| 307 | + bundleId: 'com.apple.SafariViewService', |
| 308 | + kind: 'web-auth', |
| 309 | +}; |
| 310 | + |
| 311 | +/** Five nodes, so a settled sheet clears the tiny-tree readiness hint and the hint under test is |
| 312 | + * the cross-surface one. No Application root: the sheet is hosted out of the app's process. */ |
| 313 | +const WEB_SIGN_IN_SHEET_NODES = [ |
| 314 | + 'Sign in with Example', |
| 315 | + 'Email', |
| 316 | + 'Password', |
| 317 | + 'Continue', |
| 318 | + 'Cancel', |
| 319 | +].map((label, index) => ({ |
| 320 | + index, |
| 321 | + type: 'Button', |
| 322 | + label, |
| 323 | + rect: { x: 10, y: 120 + index * 50, width: 200, height: 44 }, |
| 324 | + hittable: true, |
| 325 | +})); |
| 326 | + |
| 327 | +/** The app content that returns once the sheet completes and dismisses itself, also past the |
| 328 | + * tiny-tree count so the return direction asserts the same hint contract. */ |
| 329 | +const APP_CONTENT_NODES = [ |
| 330 | + { index: 0, type: 'Application', rect: { x: 0, y: 0, width: 390, height: 844 } }, |
| 331 | + ...['Load more', 'Profile', 'Settings', 'Sign out'].map((label, offset) => ({ |
| 332 | + index: offset + 1, |
| 333 | + parentIndex: 0, |
| 334 | + type: 'Button', |
| 335 | + label, |
| 336 | + rect: { x: 10, y: 20 + offset * 50, width: 120, height: 44 }, |
| 337 | + hittable: true, |
| 338 | + })), |
| 339 | +]; |
| 340 | + |
| 341 | +/** |
| 342 | + * The cross-surface contract a settle route owes, asserted as one unit so `scroll` and `back` |
| 343 | + * cannot drift from each other or from the targeted route: no diff is attached across the |
| 344 | + * boundary — therefore no tail and no issued refs — the transition is disclosed, and the settle |
| 345 | + * observation still reports its own verdict alongside that disclosure rather than instead of it. |
| 346 | + */ |
| 347 | +function expectCrossSurfaceSettle( |
| 348 | + settle: SettlePayload, |
| 349 | + change: { from: string; to: string; disclosure: string }, |
| 350 | +): void { |
| 351 | + expect(settle.surfaceChange).toEqual(change); |
| 352 | + expect(settle.diff).toBeUndefined(); |
| 353 | + expect(settle.tail).toBeUndefined(); |
| 354 | + // `refsGeneration` is folded in only when the settled diff published refs (ADR 0014); a refused |
| 355 | + // diff publishes none, so the payload must not name a generation either. |
| 356 | + expect(settle.refsGeneration).toBeUndefined(); |
| 357 | + expect(settle.hint).toContain('different surfaces'); |
| 358 | + expect(settle.hint).toMatch(/take a snapshot/i); |
| 359 | + expect(settle.settled).toBe(true); |
| 360 | + expect(settle.captures).toBeGreaterThanOrEqual(2); |
| 361 | + expect(settle.quietMs).toBe(25); |
| 362 | + expect(settle.timeoutMs).toBe(2_000); |
| 363 | +} |
| 364 | + |
| 365 | +/** No refs were published, so the frame the mutating leaf expired stays expired (ADR 0014). */ |
| 366 | +function expectNoPublishedRefFrame(sessionStore: SessionStore, sessionName: string): SessionState { |
| 367 | + const stored = sessionStore.get(sessionName) as SessionState; |
| 368 | + expect(refFrameState(stored)).toBe('expired'); |
| 369 | + return stored; |
| 370 | +} |
| 371 | + |
| 372 | +test('scroll --settle attaches no diff across an app-to-sheet surface change and discloses it', async () => { |
| 373 | + const sessionStore = makeSessionStore(); |
| 374 | + const sessionName = 'generic-settle-scroll-to-sheet'; |
| 375 | + const session = seedSession(sessionName, sessionStore); |
| 376 | + // The stored pre-action tree is app content; every settle capture reads the sheet now presented |
| 377 | + // over that still-foreground app. |
| 378 | + mockSurfaceDispatch({ nodes: WEB_SIGN_IN_SHEET_NODES, systemSurface: WEB_SIGN_IN_SHEET }); |
| 379 | + |
| 380 | + const response = await dispatchGeneric({ |
| 381 | + sessionName, |
| 382 | + sessionStore, |
| 383 | + session, |
| 384 | + command: 'scroll', |
| 385 | + positionals: ['down'], |
| 386 | + flags: { ...SETTLE_FLAGS }, |
| 387 | + }); |
| 388 | + |
| 389 | + expectCrossSurfaceSettle(expectOkData(response).settle as SettlePayload, { |
| 390 | + from: 'app', |
| 391 | + to: WEB_SIGN_IN_SHEET.bundleId, |
| 392 | + disclosure: IOS_SYSTEM_SURFACE_DISCLOSURE, |
| 393 | + }); |
| 394 | + // Disclosed, not hidden: the settled sheet still becomes the stored observation a follow-up |
| 395 | + // snapshot reads — and the surface identity the NEXT command's baseline is built from. |
| 396 | + const stored = expectNoPublishedRefFrame(sessionStore, sessionName); |
| 397 | + expect(stored.snapshot?.iosSystemSurfaceBundleId).toBe(WEB_SIGN_IN_SHEET.bundleId); |
| 398 | +}); |
| 399 | + |
| 400 | +test('scroll --settle attaches no diff across a sheet-to-app surface change and discloses it', async () => { |
| 401 | + const sessionStore = makeSessionStore(); |
| 402 | + const sessionName = 'generic-settle-scroll-from-sheet'; |
| 403 | + // The baseline is the sheet this scroll acts on; the settled tree is the app content that |
| 404 | + // returns once the sheet completes and dismisses itself. |
| 405 | + const session = seedSession(sessionName, sessionStore, { |
| 406 | + nodes: WEB_SIGN_IN_SHEET_NODES, |
| 407 | + systemSurface: WEB_SIGN_IN_SHEET, |
| 408 | + }); |
| 409 | + mockSurfaceDispatch({ nodes: APP_CONTENT_NODES }); |
| 410 | + |
| 411 | + const response = await dispatchGeneric({ |
| 412 | + sessionName, |
| 413 | + sessionStore, |
| 414 | + session, |
| 415 | + command: 'scroll', |
| 416 | + positionals: ['down'], |
| 417 | + flags: { ...SETTLE_FLAGS }, |
| 418 | + }); |
| 419 | + |
| 420 | + const settle = expectOkData(response).settle as SettlePayload; |
| 421 | + expectCrossSurfaceSettle(settle, { |
| 422 | + from: WEB_SIGN_IN_SHEET.bundleId, |
| 423 | + to: 'app', |
| 424 | + // The sheet is gone, so the standing "is presented over the app" sentence cannot be the one |
| 425 | + // used — the transition disclosure has to say it left. |
| 426 | + disclosure: iosSystemSurfaceTransitionDisclosure(undefined), |
| 427 | + }); |
| 428 | + expect(settle.surfaceChange?.disclosure).not.toBe(IOS_SYSTEM_SURFACE_DISCLOSURE); |
| 429 | + expect(settle.surfaceChange?.disclosure).toMatch(/sign-in sheet/); |
| 430 | + const stored = expectNoPublishedRefFrame(sessionStore, sessionName); |
| 431 | + expect(stored.snapshot?.iosSystemSurfaceBundleId).toBeUndefined(); |
| 432 | +}); |
| 433 | + |
| 434 | +test('back --settle attaches no diff across an app-to-sheet surface change and discloses it', async () => { |
| 435 | + const sessionStore = makeSessionStore(); |
| 436 | + const sessionName = 'generic-settle-back-to-sheet'; |
| 437 | + const session = seedSession(sessionName, sessionStore); |
| 438 | + mockSurfaceDispatch( |
| 439 | + { nodes: WEB_SIGN_IN_SHEET_NODES, systemSurface: WEB_SIGN_IN_SHEET }, |
| 440 | + { |
| 441 | + action: 'back', |
| 442 | + mode: 'in-app', |
| 443 | + message: 'Back', |
| 444 | + }, |
| 445 | + ); |
| 446 | + |
| 447 | + const response = await dispatchGeneric({ |
| 448 | + sessionName, |
| 449 | + sessionStore, |
| 450 | + session, |
| 451 | + command: 'back', |
| 452 | + flags: { ...SETTLE_FLAGS }, |
| 453 | + }); |
| 454 | + |
| 455 | + const data = expectOkData(response); |
| 456 | + // The refusal rides ALONGSIDE the command's own closed result shape, same as the diff does. |
| 457 | + expect(data.action).toBe('back'); |
| 458 | + expect(data.message).toBe('Back'); |
| 459 | + expectCrossSurfaceSettle(data.settle as SettlePayload, { |
| 460 | + from: 'app', |
| 461 | + to: WEB_SIGN_IN_SHEET.bundleId, |
| 462 | + disclosure: IOS_SYSTEM_SURFACE_DISCLOSURE, |
| 463 | + }); |
| 464 | + const stored = expectNoPublishedRefFrame(sessionStore, sessionName); |
| 465 | + expect(stored.snapshot?.iosSystemSurfaceBundleId).toBe(WEB_SIGN_IN_SHEET.bundleId); |
| 466 | +}); |
| 467 | + |
| 468 | +test('back --settle attaches no diff across a sheet-to-app surface change and discloses it', async () => { |
| 469 | + const sessionStore = makeSessionStore(); |
| 470 | + const sessionName = 'generic-settle-back-from-sheet'; |
| 471 | + const session = seedSession(sessionName, sessionStore, { |
| 472 | + nodes: WEB_SIGN_IN_SHEET_NODES, |
| 473 | + systemSurface: WEB_SIGN_IN_SHEET, |
| 474 | + }); |
| 475 | + mockSurfaceDispatch( |
| 476 | + { nodes: APP_CONTENT_NODES }, |
| 477 | + { |
| 478 | + action: 'back', |
| 479 | + mode: 'in-app', |
| 480 | + message: 'Back', |
| 481 | + }, |
| 482 | + ); |
| 483 | + |
| 484 | + const response = await dispatchGeneric({ |
| 485 | + sessionName, |
| 486 | + sessionStore, |
| 487 | + session, |
| 488 | + command: 'back', |
| 489 | + flags: { ...SETTLE_FLAGS }, |
| 490 | + }); |
| 491 | + |
| 492 | + const data = expectOkData(response); |
| 493 | + expect(data.action).toBe('back'); |
| 494 | + expectCrossSurfaceSettle(data.settle as SettlePayload, { |
| 495 | + from: WEB_SIGN_IN_SHEET.bundleId, |
| 496 | + to: 'app', |
| 497 | + disclosure: iosSystemSurfaceTransitionDisclosure(undefined), |
| 498 | + }); |
| 499 | + const stored = expectNoPublishedRefFrame(sessionStore, sessionName); |
| 500 | + expect(stored.snapshot?.iosSystemSurfaceBundleId).toBeUndefined(); |
| 501 | +}); |
| 502 | + |
253 | 503 | test('the settle observation runs after the post-gesture stabilization marker', async () => { |
254 | 504 | const sessionStore = makeSessionStore(); |
255 | 505 | const sessionName = 'generic-settle-order'; |
|
0 commit comments