Skip to content

Commit fc5ca3b

Browse files
committed
fix(interaction): reject overflowed tap centers
1 parent 34854d7 commit fc5ca3b

4 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎packages/command-runtime/src/commands/interaction/runtime/target-visibility-stages-input-bounds.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,48 @@ import { buildSnapshotState } from '@agent-device/capture-kit/snapshot-state';
66
import { createInteractionDevice } from './__tests__/test-utils/index.ts';
77
import { ref, selector } from './selector-read-utils.ts';
88

9+
test('selector refuses an overflowed center before backend input', async () => {
10+
const snapshot = buildSnapshotState(
11+
{
12+
backend: 'android',
13+
producer: 'android-uiautomator',
14+
nodes: [
15+
{
16+
index: 0,
17+
type: 'Button',
18+
identifier: 'overflow',
19+
label: 'Overflow',
20+
hittable: true,
21+
rect: { x: Number.MAX_VALUE, y: 0, width: Number.MAX_VALUE, height: 10 },
22+
},
23+
],
24+
},
25+
{ snapshotRaw: true },
26+
);
27+
const calls: unknown[] = [];
28+
const device = createInteractionDevice(snapshot, {
29+
platform: 'android',
30+
tap: async (_context, point) => {
31+
calls.push(point);
32+
},
33+
});
34+
const observed = await device.capture.snapshot({ session: 'default' });
35+
const node = observed.nodes.find((entry) => entry.identifier === 'overflow');
36+
assert.ok(node?.ref);
37+
await assert.rejects(
38+
device.interactions.click(selector('id=overflow'), {
39+
session: 'default',
40+
}),
41+
(error: unknown) => {
42+
assert.ok(error instanceof AppError);
43+
assert.equal(error.details?.reason, 'target_bounds_invalid');
44+
assert.equal(error.details?.dispatched, 'no');
45+
return true;
46+
},
47+
);
48+
assert.deepEqual(calls, []);
49+
});
50+
951
for (const route of ['ref', 'selector'] as const) {
1052
for (const covered of [false, true]) {
1153
test(`${route} honors measured input bounds through raw ingestion and session cloning: covered=${covered}`, async () => {

‎packages/selectors/src/interaction-touch-point.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,49 @@ import {
88
makeSnapshotState,
99
PROPERTY_RUNS,
1010
pixelRectArb,
11+
finiteRectArb,
1112
} from './snapshot-geometry.fixtures.ts';
1213
import { resolveInteractionTouchPoint } from './interaction-touch-point.ts';
1314

15+
test.each([
16+
{ x: Number.MAX_VALUE, y: 0, width: Number.MAX_VALUE, height: 10 },
17+
{ x: 0, y: Number.MAX_VALUE, width: 10, height: Number.MAX_VALUE },
18+
])('rejects a center that overflows finite rectangle components: %j', (rect) => {
19+
const node = { index: 0, rect };
20+
assert.deepEqual(resolveInteractionTouchPoint([node], node), { kind: 'invalid' });
21+
});
22+
23+
test('property: finite rectangles never resolve a non-finite tap coordinate', () => {
24+
fc.assert(
25+
fc.property(finiteRectArb, (rect) => {
26+
const node = { index: 0, rect };
27+
const result = resolveInteractionTouchPoint([node], node);
28+
if (result.kind !== 'resolved') return;
29+
assert.ok(Number.isFinite(result.point.x) && Number.isFinite(result.point.y));
30+
}),
31+
{ numRuns: PROPERTY_RUNS },
32+
);
33+
});
34+
35+
test('the narrow raw parent cannot choose the navigation edge to avoid its child', () => {
36+
const parent = attachSnapshotNodeInteractionBounds(
37+
{ index: 0, type: 'Link', rect: { x: 539, y: 2259, width: 2, height: 131 } },
38+
{ x: 0, y: 0, width: 1080, height: 2274 },
39+
);
40+
const child = {
41+
index: 1,
42+
parentIndex: 0,
43+
type: 'Button',
44+
ref: 'e2',
45+
rect: { x: 539, y: 2259, width: 2, height: 3 },
46+
};
47+
assert.deepEqual(resolveInteractionTouchPoint([parent, child], parent), {
48+
kind: 'blocked',
49+
competitorCount: 1,
50+
competitorRefs: ['e2'],
51+
});
52+
});
53+
1454
test('a raw target without competing descendants uses its measured input region', () => {
1555
const node = attachSnapshotNodeInteractionBounds(
1656
{ index: 0, rect: { x: 0, y: 2259, width: 1080, height: 131 } },

‎packages/selectors/src/interaction-touch-point.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function resolveInteractionTouchPoint(
4040
const inputRect = inputBounds ? intersectRects(targetRect, inputBounds) : targetRect;
4141
if (!inputRect || !hasArea(inputRect)) return { kind: 'invalid' };
4242
const center = pointInsideRect(inputRect);
43+
if (!Number.isFinite(center.x) || !Number.isFinite(center.y)) return { kind: 'invalid' };
4344

4445
const competitors = collectInteractiveDescendantRects(nodes, node, targetRect);
4546
if (competitors.length === 0) {

‎packages/selectors/src/snapshot-geometry.fixtures.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ export const pixelRectArb: fc.Arbitrary<Rect> = fc.record({
3030
height: fc.integer({ min: 1, max: 8000 }),
3131
});
3232

33+
export const finiteRectArb: fc.Arbitrary<Rect> = fc.record({
34+
x: fc.double({ noNaN: true, noDefaultInfinity: true }),
35+
y: fc.double({ noNaN: true, noDefaultInfinity: true }),
36+
width: fc.double({ min: 0, noNaN: true, noDefaultInfinity: true }),
37+
height: fc.double({ min: 0, noNaN: true, noDefaultInfinity: true }),
38+
});
39+
3340
export const distinctRectPairArb: fc.Arbitrary<{ ancestor: Rect; target: Rect }> = fc
3441
.record({
3542
x: fc.integer({ min: -200, max: 200 }),

0 commit comments

Comments
 (0)