Skip to content

Commit faec8d2

Browse files
committed
fix(test): make the iOS live scroll-search recover from a stale post-scroll probe
smoke:automation-input's scroll-to-visible helper only scrolled forward: 4 probes, 3 passes of `scroll down 0.75`. Measuring the fixture screen showed a single 0.75 scroll lands automation-longpress on screen with margin and no further drift, so the flake isn't gesture overshoot. The daemon's own post-gesture stabilization can time out under CI load and hand back a still-settling capture right after the correct scroll (post_gesture_snapshot_stabilization_timeout), which the forward-only search reads as "not there yet" and scrolls past with no way back. Replace the fixed forward sweep with a plan that scrolls down in smaller 0.4 steps and then reverses, so a false negative right after the target arrives can still be recovered. On exhaustion the assertion now names every probe and scroll response it made, not just the last, so the next failure is self-diagnosing.
1 parent 7e9031a commit faec8d2

2 files changed

Lines changed: 125 additions & 21 deletions

File tree

‎test/integration/ios-simulator-e2e-visibility-scroll.test.ts‎

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import assert from 'node:assert/strict';
22
import test from 'node:test';
33

44
import type { CliJsonResult } from './cli-json.ts';
5-
import { searchForVisibleElement } from './ios-simulator-e2e/live-assertions.ts';
5+
import {
6+
searchForVisibleElement,
7+
type ScrollSearchDirection,
8+
} from './ios-simulator-e2e/live-assertions.ts';
69

710
function result(status: number, details?: Record<string, unknown>): CliJsonResult {
811
return {
@@ -16,39 +19,110 @@ function result(status: number, details?: Record<string, unknown>): CliJsonResul
1619
test('an existing offscreen element scrolls until the visibility probe passes', async () => {
1720
const probes = [result(1), result(0)];
1821
const probeAttempts: number[] = [];
19-
const scrollAttempts: number[] = [];
22+
const scrolls: Array<[ScrollSearchDirection, number]> = [];
2023

2124
await searchForVisibleElement(
2225
'id="automation-longpress"',
2326
async (attempt) => {
2427
probeAttempts.push(attempt);
2528
return probes.shift() ?? result(1);
2629
},
27-
async (attempt) => {
28-
scrollAttempts.push(attempt);
30+
async (direction, attempt) => {
31+
scrolls.push([direction, attempt]);
2932
},
3033
);
3134

3235
assert.deepEqual(probeAttempts, [1, 2]);
33-
assert.deepEqual(scrollAttempts, [1]);
36+
assert.deepEqual(scrolls, [['down', 1]]);
3437
});
3538

3639
test('a stalled capture retries without scrolling or consuming an attempt', async () => {
3740
const probes = [result(1, { captureStalled: true }), result(0)];
3841
const probeAttempts: number[] = [];
39-
const scrollAttempts: number[] = [];
42+
const scrolls: Array<[ScrollSearchDirection, number]> = [];
4043

4144
await searchForVisibleElement(
4245
'id="automation-longpress"',
4346
async (attempt) => {
4447
probeAttempts.push(attempt);
4548
return probes.shift() ?? result(1);
4649
},
47-
async (attempt) => {
48-
scrollAttempts.push(attempt);
50+
async (direction, attempt) => {
51+
scrolls.push([direction, attempt]);
4952
},
5053
);
5154

5255
assert.deepEqual(probeAttempts, [1, 1]);
53-
assert.deepEqual(scrollAttempts, []);
56+
assert.deepEqual(scrolls, []);
57+
});
58+
59+
// Regression for the CI flake this policy fixes (id="automation-longpress" in
60+
// smoke:automation-input, #bucket-longpress-scroll): a probe right after the scroll that put the
61+
// target on screen can still read `selector_not_found` because the daemon's post-gesture capture
62+
// was still settling (`post_gesture_snapshot_stabilization_timeout`), not because the target moved
63+
// further away. The old forward-only policy (4 probes, 3 down-only scrolls) had no way back from
64+
// that misread — the next scroll carries an already-visible target off the top of the screen and
65+
// nothing ever brings it back. This plants exactly that misread and proves only the reversal phase
66+
// recovers it; deleting the 'up' entries from SCROLL_SEARCH_PLAN (i.e. reverting to the old
67+
// forward-only policy) makes this fail, because the passing probe never runs within a 4-probe budget.
68+
test('a false-negative probe right after the correct scroll is recovered by reversing, not by scrolling further forward', async () => {
69+
// Fails once before any scroll (real absence), then fails after every forward scroll even though
70+
// attempt 2 is actually the correct position (a stale post-gesture capture), then only turns up
71+
// once the search reverses.
72+
const probes = [
73+
result(1), // attempt 1: genuinely off-screen
74+
result(1), // attempt 2: on screen now, but a stale capture misreports it as not found
75+
result(1), // attempt 3: forward scroll carried it back off screen
76+
result(1), // attempt 4: still forward of it
77+
result(1), // attempt 5: exhausts the forward budget
78+
result(0), // attempt 6: first reversal ('up') scroll finds it again
79+
];
80+
const probeAttempts: number[] = [];
81+
const scrolls: Array<[ScrollSearchDirection, number]> = [];
82+
83+
await searchForVisibleElement(
84+
'id="automation-longpress"',
85+
async (attempt) => {
86+
probeAttempts.push(attempt);
87+
return probes.shift() ?? result(1);
88+
},
89+
async (direction, attempt) => {
90+
scrolls.push([direction, attempt]);
91+
},
92+
);
93+
94+
assert.deepEqual(probeAttempts, [1, 2, 3, 4, 5, 6]);
95+
assert.deepEqual(scrolls, [
96+
['down', 1],
97+
['down', 2],
98+
['down', 3],
99+
['down', 4],
100+
['up', 5],
101+
]);
102+
});
103+
104+
test('exhausting the whole plan fails with every probe and scroll response, not just the last', async () => {
105+
await assert.rejects(
106+
searchForVisibleElement(
107+
'id="automation-longpress"',
108+
async () => result(1, { reason: 'selector_not_found' }),
109+
async () => ({ pixels: 300 }),
110+
),
111+
(error: Error) => {
112+
// Every probe (7) and every scroll (6) must be named, not only the final failure.
113+
for (let attempt = 1; attempt <= 7; attempt += 1) {
114+
assert.ok(
115+
error.message.includes(`probe ${attempt}:`),
116+
`expected failure message to include probe ${attempt}: ${error.message}`,
117+
);
118+
}
119+
for (const direction of ['down', 'up'] as const) {
120+
assert.ok(
121+
error.message.includes(`scroll ${direction} (after attempt`),
122+
`expected failure message to include a ${direction} scroll: ${error.message}`,
123+
);
124+
}
125+
return true;
126+
},
127+
);
54128
});

‎test/integration/ios-simulator-e2e/live-assertions.ts‎

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,28 @@ export function snapshotNodes(result: { json?: any }): LiveSnapshotNode[] {
3939
return nodes as LiveSnapshotNode[];
4040
}
4141

42-
const SCROLL_SEARCH_ATTEMPTS = 4;
42+
export type ScrollSearchDirection = 'down' | 'up';
43+
44+
// Each entry is one scroll pass between two visibility probes, so probe count is always
45+
// SCROLL_SEARCH_PLAN.length + 1. The plan scrolls forward in small steps first, then reverses:
46+
// a probe taken right after the correct forward scroll can still read `selector_not_found` (a
47+
// stale/still-settling capture, not a real absence — see `post_gesture_snapshot_stabilization_timeout`
48+
// in the daemon log), and a purely-forward search has no way back from that false negative once a
49+
// later scroll has carried the target off the top of the screen. The reversal is what recovers it.
50+
const SCROLL_SEARCH_PLAN: readonly ScrollSearchDirection[] = [
51+
'down',
52+
'down',
53+
'down',
54+
'down',
55+
'up',
56+
'up',
57+
];
4358
// A stalled capture says nothing about where the element is, so it must not consume the scroll
4459
// budget outright; a couple of retries absorb a slow runner without masking a real absence.
4560
const SCROLL_SEARCH_STALL_RETRIES = 2;
61+
// Smaller than the viewport-fraction default so a single pass cannot carry a card-sized target
62+
// past both edges of the screen between two probes.
63+
const SCROLL_SEARCH_STEP = '0.4';
4664

4765
export async function assertElementTextAfterScrolling(
4866
context: LiveContext,
@@ -58,12 +76,12 @@ export async function assertElementTextAfterScrolling(
5876
['is', 'visible', selector],
5977
{ allowFailure: true },
6078
),
61-
(attempt) =>
62-
runStep(context, `scroll toward ${selector} after attempt ${attempt}`, [
79+
(direction, attempt) =>
80+
runStep(context, `scroll ${direction} toward ${selector} after attempt ${attempt}`, [
6381
'scroll',
64-
'down',
65-
'0.75',
66-
]).then(() => undefined),
82+
direction,
83+
SCROLL_SEARCH_STEP,
84+
]).then((result) => result.json?.data),
6785
);
6886
await assertElementText(context, selector, expected);
6987
}
@@ -73,17 +91,26 @@ export async function assertElementTextAfterScrolling(
7391
* the accessibility tree, so a successful `wait <selector>` is not sufficient evidence to skip
7492
* scrolling. The callbacks keep this live-device policy deterministic and unit-testable without a
7593
* simulator.
94+
*
95+
* The search is bidirectional (`SCROLL_SEARCH_PLAN`) rather than a single forward sweep: a probe
96+
* reading `selector_not_found` right after the scroll that put the target on screen is evidence
97+
* the daemon's post-gesture capture was still settling, not evidence the target is further away.
98+
* A forward-only search commits to that misread — the next scroll carries an already-visible
99+
* target back off screen with nothing left to undo it. Reversing after the forward budget is
100+
* what a permanently one-directional search cannot do.
76101
*/
77102
export async function searchForVisibleElement(
78103
selector: string,
79104
probeVisibility: (attempt: number) => Promise<CliJsonResult>,
80-
scrollAfterAttempt: (attempt: number) => Promise<void>,
105+
scroll: (direction: ScrollSearchDirection, attempt: number) => Promise<unknown>,
81106
): Promise<void> {
82107
let stallRetriesLeft = SCROLL_SEARCH_STALL_RETRIES;
83108
let lastFailure: CliJsonResult | undefined;
109+
const history: string[] = [];
84110

85-
for (let attempt = 1; attempt <= SCROLL_SEARCH_ATTEMPTS;) {
111+
for (let attempt = 1; attempt <= SCROLL_SEARCH_PLAN.length + 1;) {
86112
const probe = await probeVisibility(attempt);
113+
history.push(`probe ${attempt}: ${JSON.stringify(probe.json ?? { status: probe.status })}`);
87114
if (probe.status === 0) return;
88115
lastFailure = probe;
89116

@@ -94,13 +121,16 @@ export async function searchForVisibleElement(
94121
continue;
95122
}
96123

124+
const direction = SCROLL_SEARCH_PLAN[attempt - 1];
97125
attempt += 1;
98-
if (attempt <= SCROLL_SEARCH_ATTEMPTS) {
99-
await scrollAfterAttempt(attempt - 1);
100-
}
126+
if (direction === undefined) break;
127+
const scrollResult = await scroll(direction, attempt - 1);
128+
history.push(
129+
`scroll ${direction} (after attempt ${attempt - 1}): ${JSON.stringify(scrollResult ?? null)}`,
130+
);
101131
}
102132
assert.fail(
103-
`${selector} did not become visible after scrolling\nlast visibility probe: ${JSON.stringify(lastFailure?.json ?? null)}`,
133+
`${selector} did not become visible after scrolling down and back\n${history.join('\n')}\nlast visibility probe: ${JSON.stringify(lastFailure?.json ?? null)}`,
104134
);
105135
}
106136

0 commit comments

Comments
 (0)