Skip to content

Commit c2afd78

Browse files
committed
fix: keep every candidate when a policy resolves one winner (#1649 review P1)
A real regression I introduced, not a test gap: routing wait through the policy interface collapsed the candidate set to the winner, and the #1349 landmark check is satisfied when SOME match carries the recorded identity. A first same-selector impostor therefore hid a later genuine landmark and timed the wait out. The resolved outcome now carries `matchedNodes` — the full candidate set of the alternative the winner came from — so a policy that picks one node no longer throws the rest away. wait passes that straight to the landmark check, restoring the original semantics. Regression test added at the within-one-poll shape the existing suite did not cover (both candidates in the SAME capture, impostor first); verified it goes red against the singleton reconstruction it replaces.
1 parent 633c41a commit c2afd78

3 files changed

Lines changed: 76 additions & 4 deletions

File tree

‎packages/selectors/src/internal/resolve-with-policy.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@ import type { SelectorResolutionPolicy } from './resolution-policy.ts';
2626
export type PolicyResolutionOutcome =
2727
/** No selector alternative matched anything. */
2828
| { kind: 'none' }
29-
/** Exactly the node this policy authorizes acting on. */
30-
| { kind: 'resolved'; resolution: AstSelectorResolution }
29+
/**
30+
* The node this policy authorizes acting on, plus the full candidate set
31+
* of the alternative it came from. Callers that verify identity across
32+
* candidates (wait's #1349 landmark check) need the whole set — a policy
33+
* that picks one winner must not throw the rest away, or a first impostor
34+
* would hide a later genuine match.
35+
*/
36+
| {
37+
kind: 'resolved';
38+
resolution: AstSelectorResolution;
39+
matchedNodes: SnapshotState['nodes'];
40+
}
3141
/**
3242
* Several matches and the policy refuses to choose. `fail-closed` returns
3343
* this instead of guessing; `reject-candidates` returns it so the caller
@@ -73,7 +83,14 @@ export function resolveSelectorChainWithPolicy(
7383
requireUnique: true,
7484
disambiguateAmbiguous: policy.ambiguity === 'disambiguate',
7585
});
76-
if (resolution) return { kind: 'resolved', resolution };
86+
if (resolution) {
87+
const list = listSelectorChainMatches(nodes, chain, matchOptions);
88+
return {
89+
kind: 'resolved',
90+
resolution,
91+
matchedNodes: list?.matchedNodes ?? [resolution.node],
92+
};
93+
}
7794

7895
// Distinguish "nothing matched" from "matched but this policy will not
7996
// choose" — a fail-closed caller must report ambiguity, not absence.
@@ -94,6 +111,7 @@ function resolvedFromList(
94111
if (!node) return { kind: 'none' };
95112
return {
96113
kind: 'resolved',
114+
matchedNodes: list.matchedNodes,
97115
resolution: {
98116
node,
99117
selector: list.selector,

‎src/commands/interaction/runtime/selector-wait.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,57 @@ function landmarkWaitDevice(captures: Array<ReturnType<typeof landmarkScreen>>)
123123
return device;
124124
}
125125

126+
/**
127+
* The within-one-poll twin of the test below (#1649 review P1): both
128+
* candidates are on screen in the SAME capture, impostor first. The landmark
129+
* check is satisfied when SOME match carries the recorded identity, so
130+
* resolution must hand it every candidate — a policy that returns only its
131+
* first-match winner would hide the genuine landmark behind the impostor and
132+
* make this wait time out.
133+
*/
134+
function twoCandidateScreen(): ReturnType<typeof landmarkScreen> {
135+
return makeSnapshotState([
136+
{ index: 0, depth: 0, type: 'Other', label: 'List Screen' },
137+
{
138+
index: 1,
139+
depth: 1,
140+
parentIndex: 0,
141+
type: 'StaticText',
142+
label: 'Screen X',
143+
rect: { x: 0, y: 0, width: 100, height: 20 },
144+
},
145+
{ index: 2, depth: 0, type: 'Other', label: 'Detail Screen' },
146+
{
147+
index: 3,
148+
depth: 1,
149+
parentIndex: 2,
150+
type: 'StaticText',
151+
label: 'Screen X',
152+
rect: { x: 0, y: 40, width: 100, height: 20 },
153+
},
154+
]);
155+
}
156+
157+
test('runtime wait finds the recorded landmark behind a same-selector impostor in one capture', async () => {
158+
const recorded = recordedLandmarkFor(landmarkScreen('Detail Screen'));
159+
const device = landmarkWaitDevice([twoCandidateScreen()]);
160+
161+
const result = await device.selectors.wait({
162+
session: 'default',
163+
target: {
164+
kind: 'selector',
165+
selector: 'label="Screen X"',
166+
timeoutMs: 2_000,
167+
recordedLandmark: recorded,
168+
},
169+
});
170+
171+
assert.equal(result.kind, 'selector');
172+
if (result.kind !== 'selector') throw new Error('unreachable');
173+
// The SECOND candidate is the one carrying the recorded ancestry.
174+
assert.equal(result.node?.index, 3);
175+
});
176+
126177
test('runtime wait keeps polling past a same-selector impostor and succeeds on the recorded landmark', async () => {
127178
const recordTime = landmarkScreen('Detail Screen');
128179
const recorded = recordedLandmarkFor(recordTime);

‎src/commands/interaction/runtime/selector-wait.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ function policyMatchList(outcome: PolicyResolutionOutcome): SelectorChainMatchLi
4747
return {
4848
selector: outcome.resolution.selector.raw,
4949
selectorIndex: outcome.resolution.selectorIndex,
50-
matchedNodes: [outcome.resolution.node],
50+
// Every candidate, not just the winner: the landmark check is satisfied
51+
// when SOME match carries the recorded identity, so a first impostor
52+
// must not hide a later genuine landmark (#1349).
53+
matchedNodes: outcome.matchedNodes,
5154
};
5255
}
5356
return undefined;

0 commit comments

Comments
 (0)