@@ -2,7 +2,10 @@ import assert from 'node:assert/strict';
22import test from 'node:test' ;
33
44import type { CliJsonResult } from './cli-json.ts' ;
5- import { searchForVisibleElement } from './ios-simulator-e2e/live-assertions.ts' ;
5+ import {
6+ searchForVisibleElement ,
7+ type ScrollSearchDevice ,
8+ } from './ios-simulator-e2e/live-assertions.ts' ;
69
710function result ( status : number , details ?: Record < string , unknown > ) : CliJsonResult {
811 return {
@@ -13,84 +16,114 @@ function result(status: number, details?: Record<string, unknown>): CliJsonResul
1316 } ;
1417}
1518
16- test ( 'an existing offscreen element scrolls until the visibility probe passes' , async ( ) => {
17- const probes = [ result ( 1 ) , result ( 0 ) ] ;
18- const probeAttempts : number [ ] = [ ] ;
19- const scrollAttempts : number [ ] = [ ] ;
20-
21- await searchForVisibleElement (
22- 'id="automation-longpress"' ,
23- async ( attempt ) => {
24- probeAttempts . push ( attempt ) ;
25- return probes . shift ( ) ?? result ( 1 ) ;
19+ const UNSETTLED = { unsettledGesture : { action : 'scroll' , positionals : [ ] } } ;
20+
21+ /**
22+ * A vertical list the search drives. Offsets are in viewports; the target is visible while the
23+ * offset lies inside `visible`. Each scroll moves by the next planned travel, clamped to the list
24+ * bounds. `movesUntilSettled` keeps the surface moving after every scroll that moved until the
25+ * search pauses to settle it, so each read before that misses with `unsettledGesture`: the CI
26+ * failure shape.
27+ */
28+ function list ( options : {
29+ visible ?: readonly [ number , number ] ;
30+ downTravel ?: readonly number [ ] ;
31+ end ?: number ;
32+ movesUntilSettled ?: boolean ;
33+ } ) {
34+ const downTravel = [ ...( options . downTravel ?? [ ] ) ] ;
35+ const end = options . end ?? 10 ;
36+ let offset = 0 ;
37+ let moving = false ;
38+ const log : string [ ] = [ ] ;
39+ const device : ScrollSearchDevice = {
40+ probeVisibility : async ( probe ) => {
41+ log . push ( `probe ${ probe } ` ) ;
42+ const [ from , to ] = options . visible ?? [ Infinity , Infinity ] ;
43+ if ( ! moving && offset >= from && offset <= to ) return result ( 0 ) ;
44+ return result ( 1 , moving ? UNSETTLED : { reason : 'selector_not_found' } ) ;
2645 } ,
27- async ( attempt ) => {
28- scrollAttempts . push ( attempt ) ;
46+ settle : async ( ) => {
47+ log . push ( 'settle' ) ;
48+ moving = false ;
2949 } ,
30- ) ;
50+ scroll : async ( step ) => {
51+ const travel = step . direction === 'down' ? ( downTravel . shift ( ) ?? 0.75 ) : - 0.5 ;
52+ const next = Math . min ( end , Math . max ( 0 , offset + travel ) ) ;
53+ const moved = next !== offset ;
54+ offset = next ;
55+ moving = options . movesUntilSettled === true && moved ;
56+ log . push ( `scroll ${ step . direction } ${ step . amount } ` ) ;
57+ } ,
58+ } ;
59+ return { device, log } ;
60+ }
3161
32- assert . deepEqual ( probeAttempts , [ 1 , 2 ] ) ;
33- assert . deepEqual ( scrollAttempts , [ 1 ] ) ;
62+ test ( 'an existing offscreen element scrolls until the visibility probe passes' , async ( ) => {
63+ const { device, log } = list ( { visible : [ 0.5 , 1.2 ] } ) ;
64+
65+ await searchForVisibleElement ( 'id="target"' , device ) ;
66+
67+ assert . deepEqual ( log , [ 'probe 1' , 'scroll down 0.75' , 'probe 2' ] ) ;
3468} ) ;
3569
3670test ( 'a stalled capture retries without scrolling or consuming an attempt' , async ( ) => {
3771 const probes = [ result ( 1 , { captureStalled : true } ) , result ( 0 ) ] ;
38- const probeAttempts : number [ ] = [ ] ;
39- const scrollAttempts : number [ ] = [ ] ;
40-
41- await searchForVisibleElement (
42- 'id="automation-longpress"' ,
43- async ( attempt ) => {
44- probeAttempts . push ( attempt ) ;
45- return probes . shift ( ) ?? result ( 1 ) ;
46- } ,
47- async ( attempt ) => {
48- scrollAttempts . push ( attempt ) ;
72+ const scrolls : string [ ] = [ ] ;
73+
74+ await searchForVisibleElement ( 'id="target"' , {
75+ probeVisibility : async ( ) => probes . shift ( ) ?? result ( 1 ) ,
76+ settle : async ( ) => assert . fail ( 'a stalled capture is not a moving surface' ) ,
77+ scroll : async ( step ) => {
78+ scrolls . push ( step . direction ) ;
4979 } ,
50- ) ;
80+ } ) ;
5181
52- assert . deepEqual ( probeAttempts , [ 1 , 1 ] ) ;
53- assert . deepEqual ( scrollAttempts , [ ] ) ;
82+ assert . deepEqual ( [ probes . length , scrolls ] , [ 0 , [ ] ] ) ;
5483} ) ;
5584
56- /**
57- * The CI failure shape: the element is on screen only at offset 1, and the first read after each
58- * scroll lands on a surface still moving, so it misses with `unsettledGesture`.
59- */
60- function listWithUnsettledFirstReads ( visibleAt ?: number ) {
61- let offset = 0 ;
62- let moving = false ;
63- const probes : number [ ] = [ ] ;
64- const scrolls : number [ ] = [ ] ;
65- const probe = async ( attempt : number ) => {
66- probes . push ( attempt ) ;
67- const unsettled = moving ;
68- moving = false ;
69- if ( ! unsettled && offset === visibleAt ) return result ( 0 ) ;
70- return result ( 1 , unsettled ? { unsettledGesture : { action : 'scroll' , positionals : [ ] } } : { } ) ;
71- } ;
72- const scroll = async ( attempt : number ) => {
73- scrolls . push ( attempt ) ;
74- offset += 1 ;
75- moving = true ;
76- } ;
77- return { probes, scrolls, probe, scroll } ;
78- }
85+ test ( 'an unsettled miss waits for the surface to settle and re-reads at the same offset' , async ( ) => {
86+ const { device, log } = list ( { visible : [ 0.5 , 1.2 ] , movesUntilSettled : true } ) ;
7987
80- test ( 'an unsettled miss after the scroll that reached the element is re-read at the same offset' , async ( ) => {
81- const list = listWithUnsettledFirstReads ( 1 ) ;
88+ await searchForVisibleElement ( 'id="target"' , device ) ;
89+
90+ assert . deepEqual ( log , [ 'probe 1' , 'scroll down 0.75' , 'probe 2' , 'settle' , 'probe 3' ] ) ;
91+ } ) ;
8292
83- await searchForVisibleElement ( 'id="target"' , list . probe , list . scroll ) ;
93+ test ( 'a forward scroll that overshoots the element is recovered by scrolling back' , async ( ) => {
94+ // Measured on an iOS 26 simulator under host load: one `scroll down 0.75` moved content 852 pt
95+ // instead of its usual 439-505 pt, which carried a one-row target past the viewport.
96+ const { device, log } = list ( {
97+ visible : [ 0.6 , 1.25 ] ,
98+ downTravel : [ 1.4 , 0.75 , 0.75 ] ,
99+ end : 2.2 ,
100+ movesUntilSettled : true ,
101+ } ) ;
84102
85- assert . deepEqual ( [ list . probes , list . scrolls ] , [ [ 1 , 2 , 2 ] , [ 1 ] ] ) ;
103+ await searchForVisibleElement ( 'id="target"' , device ) ;
104+
105+ assert . deepEqual (
106+ log . filter ( ( entry ) => entry . startsWith ( 'scroll' ) ) ,
107+ [ 'scroll down 0.75' , 'scroll down 0.75' , 'scroll down 0.75' , 'scroll up 0.5' , 'scroll up 0.5' ] ,
108+ ) ;
86109} ) ;
87110
88- test ( 'a real absence still fails after the forward scrolls , naming every step' , async ( ) => {
89- const list = listWithUnsettledFirstReads ( ) ;
111+ test ( 'a real absence still fails after both sweeps , naming every step' , async ( ) => {
112+ const { device , log } = list ( { movesUntilSettled : true } ) ;
90113
91114 await assert . rejects (
92- searchForVisibleElement ( 'id="target"' , list . probe , list . scroll ) ,
93- / s c r o l l a f t e r a t t e m p t 3 : [ \s \S ] * p r o b e 4 : / ,
115+ searchForVisibleElement ( 'id="target"' , device ) ,
116+ / s c r o l l d o w n 3 : [ \s \S ] * s c r o l l u p 6 : [ \s \S ] * p r o b e \d + : / ,
117+ ) ;
118+ assert . deepEqual (
119+ log . filter ( ( entry ) => entry . startsWith ( 'scroll' ) ) ,
120+ [
121+ 'scroll down 0.75' ,
122+ 'scroll down 0.75' ,
123+ 'scroll down 0.75' ,
124+ 'scroll up 0.5' ,
125+ 'scroll up 0.5' ,
126+ 'scroll up 0.5' ,
127+ ] ,
94128 ) ;
95- assert . deepEqual ( list . scrolls , [ 1 , 2 , 3 ] ) ;
96129} ) ;
0 commit comments