@@ -6,6 +6,7 @@ import type { CommandFlags } from '../../../core/dispatch.ts';
66import { attachRefs , type SnapshotBackend } from '../../../kernel/snapshot.ts' ;
77import { AppError } from '../../../kernel/errors.ts' ;
88import { buildSnapshotState } from '../snapshot-capture.ts' ;
9+ import { setSessionSnapshot , STALE_SNAPSHOT_REFS_WARNING } from '../../session-snapshot.ts' ;
910import { makeSessionStore } from '../../../__tests__/test-utils/store-factory.ts' ;
1011import {
1112 makeIosSession ,
@@ -106,7 +107,9 @@ async function emulateCaptureSnapshotForSession(
106107 contextFromFlags ( effectiveFlags , session . appBundleId , session . trace ?. outPath ) ,
107108 ) ) as { nodes ?: never [ ] ; truncated ?: boolean ; backend ?: SnapshotBackend } ;
108109 const snapshot = buildSnapshotState ( snapshotData ?? { } , effectiveFlags ) ;
109- session . snapshot = snapshot ;
110+ // Mirror the real captureSnapshotForSession: session snapshot writes go
111+ // through setSessionSnapshot so snapshotRefsStale tracking (#1076) applies.
112+ setSessionSnapshot ( session , snapshot ) ;
110113 sessionStore . set ( session . name , session ) ;
111114 return snapshot ;
112115}
@@ -3196,3 +3199,210 @@ test('is reports Android permission dialog blocker when app content assertion fa
31963199 } ) ;
31973200 }
31983201} ) ;
3202+
3203+ // --- Stale @ref warnings (#1076) ---
3204+
3205+ function makeTwoButtonNodes ( ) {
3206+ return [
3207+ {
3208+ index : 0 ,
3209+ type : 'Application' ,
3210+ rect : { x : 0 , y : 0 , width : 390 , height : 844 } ,
3211+ } ,
3212+ {
3213+ index : 1 ,
3214+ parentIndex : 0 ,
3215+ type : 'XCUIElementTypeButton' ,
3216+ label : 'Continue' ,
3217+ rect : { x : 10 , y : 20 , width : 100 , height : 40 } ,
3218+ enabled : true ,
3219+ hittable : true ,
3220+ } ,
3221+ {
3222+ index : 2 ,
3223+ parentIndex : 0 ,
3224+ type : 'XCUIElementTypeButton' ,
3225+ label : 'Cancel' ,
3226+ rect : { x : 10 , y : 80 , width : 100 , height : 40 } ,
3227+ enabled : true ,
3228+ hittable : true ,
3229+ } ,
3230+ ] ;
3231+ }
3232+
3233+ function makeStaleRefSession ( sessionName : string ) : SessionState {
3234+ const session = makeSession ( sessionName ) ;
3235+ session . snapshot = {
3236+ nodes : attachRefs ( makeTwoButtonNodes ( ) as never ) ,
3237+ createdAt : Date . now ( ) ,
3238+ backend : 'xctest' ,
3239+ } ;
3240+ // As if the snapshot command just returned these refs to the client.
3241+ session . snapshotRefsStale = false ;
3242+ return session ;
3243+ }
3244+
3245+ async function runInteraction (
3246+ sessionStore : SessionStore ,
3247+ sessionName : string ,
3248+ command : string ,
3249+ positionals : string [ ] ,
3250+ flags : Record < string , unknown > = { } ,
3251+ ) {
3252+ return await handleInteractionCommands ( {
3253+ req : { token : 't' , session : sessionName , command, positionals, flags } ,
3254+ sessionName,
3255+ sessionStore,
3256+ contextFromFlags,
3257+ } ) ;
3258+ }
3259+
3260+ test ( 'press selector then press @ref warns that refs outlived the stored snapshot' , async ( ) => {
3261+ const sessionStore = makeSessionStore ( ) ;
3262+ const sessionName = 'stale-ref-warns' ;
3263+ const session = makeStaleRefSession ( sessionName ) ;
3264+ sessionStore . set ( sessionName , session ) ;
3265+ mockDispatch . mockImplementation ( async ( _device , command ) =>
3266+ command === 'snapshot' ? { nodes : makeTwoButtonNodes ( ) , backend : 'xctest' } : { } ,
3267+ ) ;
3268+
3269+ // Selector press: its resolution capture replaces the stored snapshot
3270+ // without handing the new refs back to the client.
3271+ const selectorPress = await runInteraction ( sessionStore , sessionName , 'press' , [
3272+ 'label=Continue' ,
3273+ ] ) ;
3274+ if ( ! selectorPress ?. ok ) {
3275+ throw new Error ( `selector press failed: ${ JSON . stringify ( selectorPress ) } ` ) ;
3276+ }
3277+ expect ( selectorPress . data ?. warning ) . toBeUndefined ( ) ;
3278+ expect ( sessionStore . get ( sessionName ) ?. snapshotRefsStale ) . toBe ( true ) ;
3279+
3280+ const refPress = await runInteraction ( sessionStore , sessionName , 'press' , [ '@e1' ] ) ;
3281+ expect ( refPress ?. ok ) . toBe ( true ) ;
3282+ if ( refPress ?. ok ) {
3283+ expect ( refPress . data ?. warning ) . toBe ( STALE_SNAPSHOT_REFS_WARNING ) ;
3284+ }
3285+ } ) ;
3286+
3287+ test ( 'press @ref directly after refs were issued does not warn' , async ( ) => {
3288+ const sessionStore = makeSessionStore ( ) ;
3289+ const sessionName = 'fresh-ref-no-warning' ;
3290+ const session = makeStaleRefSession ( sessionName ) ;
3291+ sessionStore . set ( sessionName , session ) ;
3292+
3293+ const response = await runInteraction ( sessionStore , sessionName , 'press' , [ '@e1' ] ) ;
3294+ expect ( response ?. ok ) . toBe ( true ) ;
3295+ if ( response ?. ok ) {
3296+ expect ( response . data ?. warning ) . toBeUndefined ( ) ;
3297+ }
3298+ } ) ;
3299+
3300+ test ( 're-issuing refs clears the stale marker so press @ref does not warn' , async ( ) => {
3301+ const sessionStore = makeSessionStore ( ) ;
3302+ const sessionName = 'reissued-refs-no-warning' ;
3303+ const session = makeStaleRefSession ( sessionName ) ;
3304+ sessionStore . set ( sessionName , session ) ;
3305+ mockDispatch . mockImplementation ( async ( _device , command ) =>
3306+ command === 'snapshot' ? { nodes : makeTwoButtonNodes ( ) , backend : 'xctest' } : { } ,
3307+ ) ;
3308+
3309+ const selectorPress = await runInteraction ( sessionStore , sessionName , 'press' , [
3310+ 'label=Continue' ,
3311+ ] ) ;
3312+ expect ( selectorPress ?. ok ) . toBe ( true ) ;
3313+ expect ( sessionStore . get ( sessionName ) ?. snapshotRefsStale ) . toBe ( true ) ;
3314+
3315+ // Simulate the snapshot command re-issuing refs (its handler clears the
3316+ // marker through buildNextSnapshotSession; covered in snapshot-handler tests).
3317+ const stored = sessionStore . get ( sessionName ) ! ;
3318+ stored . snapshotRefsStale = false ;
3319+ sessionStore . set ( sessionName , stored ) ;
3320+
3321+ const refPress = await runInteraction ( sessionStore , sessionName , 'press' , [ '@e1' ] ) ;
3322+ expect ( refPress ?. ok ) . toBe ( true ) ;
3323+ if ( refPress ?. ok ) {
3324+ expect ( refPress . data ?. warning ) . toBeUndefined ( ) ;
3325+ }
3326+ } ) ;
3327+
3328+ test ( 'fill @ref warns while refs are stale' , async ( ) => {
3329+ const sessionStore = makeSessionStore ( ) ;
3330+ const sessionName = 'stale-ref-fill' ;
3331+ const session = makeStaleRefSession ( sessionName ) ;
3332+ session . snapshot = {
3333+ nodes : attachRefs ( [
3334+ {
3335+ index : 0 ,
3336+ type : 'XCUIElementTypeTextField' ,
3337+ label : 'Email' ,
3338+ rect : { x : 10 , y : 20 , width : 200 , height : 40 } ,
3339+ enabled : true ,
3340+ hittable : true ,
3341+ } ,
3342+ ] ) ,
3343+ createdAt : Date . now ( ) ,
3344+ backend : 'xctest' ,
3345+ } ;
3346+ session . snapshotRefsStale = true ;
3347+ sessionStore . set ( sessionName , session ) ;
3348+
3349+ const response = await runInteraction ( sessionStore , sessionName , 'fill' , [
3350+ '@e1' ,
3351+ 3352+ ] ) ;
3353+ expect ( response ?. ok ) . toBe ( true ) ;
3354+ if ( response ?. ok ) {
3355+ expect ( response . data ?. warning ) . toBe ( STALE_SNAPSHOT_REFS_WARNING ) ;
3356+ }
3357+ } ) ;
3358+
3359+ test ( 'get text @ref warns while refs are stale' , async ( ) => {
3360+ const sessionStore = makeSessionStore ( ) ;
3361+ const sessionName = 'stale-ref-get-text' ;
3362+ const session = makeStaleRefSession ( sessionName ) ;
3363+ session . snapshotRefsStale = true ;
3364+ sessionStore . set ( sessionName , session ) ;
3365+ mockDispatch . mockRejectedValue (
3366+ new Error ( 'dispatch should not be called for snapshot-derived get text' ) ,
3367+ ) ;
3368+
3369+ const response = await runInteraction ( sessionStore , sessionName , 'get' , [ 'text' , '@e1' ] ) ;
3370+ expect ( response ?. ok ) . toBe ( true ) ;
3371+ if ( response ?. ok ) {
3372+ expect ( response . data ?. warning ) . toBe ( STALE_SNAPSHOT_REFS_WARNING ) ;
3373+ expect ( response . data ?. ref ) . toBe ( 'e1' ) ;
3374+ }
3375+ } ) ;
3376+
3377+ test ( 'stale-ref warning appends to an existing interaction warning' , async ( ) => {
3378+ const sessionStore = makeSessionStore ( ) ;
3379+ const sessionName = 'stale-ref-appends' ;
3380+ const session = makeStaleRefSession ( sessionName ) ;
3381+ session . snapshot = {
3382+ nodes : attachRefs ( [
3383+ {
3384+ index : 0 ,
3385+ // Non-fillable type produces the runtime fill warning the stale-ref
3386+ // warning must append to, not replace.
3387+ type : 'XCUIElementTypeStaticText' ,
3388+ label : 'Read-only' ,
3389+ rect : { x : 10 , y : 20 , width : 200 , height : 40 } ,
3390+ enabled : true ,
3391+ hittable : true ,
3392+ } ,
3393+ ] ) ,
3394+ createdAt : Date . now ( ) ,
3395+ backend : 'xctest' ,
3396+ } ;
3397+ session . snapshotRefsStale = true ;
3398+ sessionStore . set ( sessionName , session ) ;
3399+
3400+ const response = await runInteraction ( sessionStore , sessionName , 'fill' , [ '@e1' , 'nope' ] ) ;
3401+ expect ( response ?. ok ) . toBe ( true ) ;
3402+ if ( response ?. ok ) {
3403+ const warning = String ( response . data ?. warning ?? '' ) ;
3404+ expect ( warning ) . toContain ( 'attempting fill anyway' ) ;
3405+ expect ( warning ) . toContain ( STALE_SNAPSHOT_REFS_WARNING ) ;
3406+ expect ( warning . endsWith ( STALE_SNAPSHOT_REFS_WARNING ) ) . toBe ( true ) ;
3407+ }
3408+ } ) ;
0 commit comments