Skip to content

Commit 4a27d36

Browse files
claudethymikee
authored andcommitted
test(find): prove the production route, and correct the divergence claim (#1654 review P2)
The regression tests built `internal.findPreresolvedTarget` by hand and called handleInteractionCommands directly, so they proved the leaf consumes the channel but never that find ATTACHES it. Either producer could have stopped doing so and they would all have stayed green — the exact gap #1649's review named, reappearing one layer up. Four tests now drive the real handleFindCommands with invoke wired to the real handleInteractionCommands. Two assert click and fill each carry the selected node (separate producers, separate forwarding hops, so asserted separately); two advance the session tree between find's match and the leaf's resolution and assert the dispatched point is still find's node. That last pair also corrects the record. The claim that the leaf resolves against a different tree than find matched is too strong for the common path: `omitRefFrameSnapshot` (interaction-runtime.ts) makes find's internal dispatch skip the authorized frame tree and resolve against `session.snapshot`, which find's own capture just wrote — so the second lookup normally AGREES, and the two non-diverged tests above pass with or without the fix. The divergence is real but narrower: it needs something to advance `session.snapshot` between find's match and the leaf's resolution, which `refreshAndroidRefSnapshotIfFreshnessActive` does on the ref path. Measured at this head, the old code taps (60,720) "Delete" where find matched "Save" at (310,510) — a wrong-element mutation, now pinned by both click and fill. So the change's value is what #1654 asked for — one resolution end to end — plus closing that window, not a fix for a divergence occurring on every find. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_0117wfrvC6MRDJUWdRBErNEF
1 parent 69a0b87 commit 4a27d36

1 file changed

Lines changed: 161 additions & 0 deletions

File tree

‎src/daemon/handlers/__tests__/find.test.ts‎

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { test, expect, vi, beforeEach } from 'vitest';
22
import { handleFindCommands } from '../find.ts';
3+
import { handleInteractionCommands } from '../interaction.ts';
4+
import type { CommandFlags } from '@agent-device/contracts/command';
35
import type { DaemonRequest, DaemonResponse, SessionState } from '../../types.ts';
46
import { buildSnapshotSignatures } from '../../android-snapshot-freshness.ts';
57
import { makeSessionStore } from '../../../__tests__/test-utils/store-factory.ts';
@@ -1068,3 +1070,162 @@ test('read-only find while recording is intentionally deferred from target-v1 ev
10681070
// an explicit deferral, not an accident.
10691071
expect(recordedAction?.targetEvidence).toBeUndefined();
10701072
});
1073+
1074+
// --- #1654: the production route, find handler through the real interaction leaf ---
1075+
//
1076+
// The tests above stub `invoke`, so they prove what find SENDS. These drive the
1077+
// real `handleInteractionCommands` on the other end, so they prove what the
1078+
// action actually acts on — and they fail if either producer (click or fill)
1079+
// stops attaching the channel, which a hand-built request cannot catch.
1080+
1081+
const findRouteContextFromFlags = (flags: CommandFlags | undefined) => ({
1082+
count: flags?.count,
1083+
intervalMs: flags?.intervalMs,
1084+
delayMs: flags?.delayMs,
1085+
holdMs: flags?.holdMs,
1086+
jitterPx: flags?.jitterPx,
1087+
doubleTap: flags?.doubleTap,
1088+
clickButton: flags?.clickButton,
1089+
});
1090+
1091+
/** The tree find's capture returns: "Save" sits at a distinctive point. */
1092+
function freshFindTree() {
1093+
return [
1094+
{ index: 0, type: 'Application', rect: { x: 0, y: 0, width: 390, height: 844 } },
1095+
{
1096+
index: 1,
1097+
parentIndex: 0,
1098+
type: 'XCUIElementTypeButton',
1099+
label: 'Save',
1100+
rect: { x: 300, y: 500, width: 20, height: 20 },
1101+
enabled: true,
1102+
hittable: true,
1103+
},
1104+
];
1105+
}
1106+
1107+
/**
1108+
* A tree in which the SAME ref body names a different element. Modelled on the
1109+
* real window this closes: `refreshAndroidRefSnapshotIfFreshnessActive` can
1110+
* re-capture between find's match and the leaf's resolution, so `session.snapshot`
1111+
* advances underneath the `@ref` find just minted.
1112+
*/
1113+
function divergedSessionTree() {
1114+
return {
1115+
nodes: [
1116+
{ index: 0, type: 'Application', rect: { x: 0, y: 0, width: 390, height: 844 }, ref: 'e1' },
1117+
{
1118+
index: 1,
1119+
parentIndex: 0,
1120+
type: 'XCUIElementTypeButton',
1121+
label: 'Delete',
1122+
rect: { x: 10, y: 700, width: 100, height: 40 },
1123+
enabled: true,
1124+
hittable: true,
1125+
ref: 'e2',
1126+
},
1127+
],
1128+
createdAt: Date.now(),
1129+
backend: 'xctest' as const,
1130+
};
1131+
}
1132+
1133+
async function runFindThroughLeaf(options: {
1134+
positionals: string[];
1135+
divergeBeforeDispatch: boolean;
1136+
}): Promise<{ response: DaemonResponse | null; invokeCalls: DaemonRequest[] }> {
1137+
const sessionStore = makeSessionStore();
1138+
const sessionName = 'default';
1139+
const session = makeSession(sessionName);
1140+
sessionStore.set(sessionName, session);
1141+
1142+
mockDispatch.mockImplementation(async (_device, command) =>
1143+
command === 'snapshot' ? { nodes: freshFindTree(), backend: 'xctest' } : {},
1144+
);
1145+
1146+
const invokeCalls: DaemonRequest[] = [];
1147+
const response = await handleFindCommands({
1148+
req: {
1149+
token: 't',
1150+
session: sessionName,
1151+
command: 'find',
1152+
positionals: options.positionals,
1153+
flags: {},
1154+
},
1155+
sessionName,
1156+
logPath: '/tmp/test.log',
1157+
sessionStore,
1158+
invoke: async (req) => {
1159+
invokeCalls.push(req);
1160+
if (options.divergeBeforeDispatch) {
1161+
const current = sessionStore.get(sessionName);
1162+
if (current) {
1163+
current.snapshot = divergedSessionTree();
1164+
sessionStore.set(sessionName, current);
1165+
}
1166+
}
1167+
// The real leaf, not a stub.
1168+
return (
1169+
(await handleInteractionCommands({
1170+
req,
1171+
sessionName,
1172+
sessionStore,
1173+
contextFromFlags: findRouteContextFromFlags,
1174+
})) ?? { ok: false, error: { code: 'COMMAND_FAILED', message: 'no interaction handler' } }
1175+
);
1176+
},
1177+
});
1178+
return { response, invokeCalls };
1179+
}
1180+
1181+
function dispatchedPoint(command: 'press' | 'fill'): string[] | undefined {
1182+
const call = mockDispatch.mock.calls.find((entry) => entry[1] === command);
1183+
return call?.[2] as string[] | undefined;
1184+
}
1185+
1186+
test('#1654 production route: find click carries its resolved node into the real interaction leaf', async () => {
1187+
const { response, invokeCalls } = await runFindThroughLeaf({
1188+
positionals: ['text', 'Save', 'click'],
1189+
divergeBeforeDispatch: false,
1190+
});
1191+
1192+
expect(response?.ok).toBe(true);
1193+
// The producer attaches the channel — fails if handleFindClick stops doing so.
1194+
expect(invokeCalls[0]?.internal?.findPreresolvedTarget?.node?.label).toBe('Save');
1195+
expect(dispatchedPoint('press')).toEqual(['310', '510']);
1196+
});
1197+
1198+
test('#1654 production route: find fill carries its resolved node into the real interaction leaf', async () => {
1199+
const { response, invokeCalls } = await runFindThroughLeaf({
1200+
positionals: ['text', 'Save', 'fill', 'hello'],
1201+
divergeBeforeDispatch: false,
1202+
});
1203+
1204+
expect(response?.ok).toBe(true);
1205+
// Fill has its own producer and its own forwarding hop; assert it separately.
1206+
expect(invokeCalls[0]?.internal?.findPreresolvedTarget?.node?.label).toBe('Save');
1207+
expect(dispatchedPoint('fill')).toEqual(['310', '510', 'hello']);
1208+
});
1209+
1210+
test('#1654 production route: a tree that advances mid-dispatch cannot retarget find click', async () => {
1211+
// The regression this closes. With the session tree advanced between find's
1212+
// match and the leaf's resolution, a second lookup of `@e2` finds "Delete" at
1213+
// (60, 720). Acting on find's node means (310, 510) regardless.
1214+
const { response } = await runFindThroughLeaf({
1215+
positionals: ['text', 'Save', 'click'],
1216+
divergeBeforeDispatch: true,
1217+
});
1218+
1219+
expect(response?.ok).toBe(true);
1220+
expect(dispatchedPoint('press')).toEqual(['310', '510']);
1221+
});
1222+
1223+
test('#1654 production route: a tree that advances mid-dispatch cannot retarget find fill', async () => {
1224+
const { response } = await runFindThroughLeaf({
1225+
positionals: ['text', 'Save', 'fill', 'hello'],
1226+
divergeBeforeDispatch: true,
1227+
});
1228+
1229+
expect(response?.ok).toBe(true);
1230+
expect(dispatchedPoint('fill')).toEqual(['310', '510', 'hello']);
1231+
});

0 commit comments

Comments
 (0)