From 737c5a6b65fada5dfd23986771dd722029d72492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Tue, 3 Mar 2026 20:21:52 +0100 Subject: [PATCH] test: lock find click JSON response to exact deterministic key set Add strict key-set assertions to find click handler tests to prevent accidental schema drift in the machine-consumed JSON response. Closes #182 Co-Authored-By: Claude Opus 4.6 --- src/daemon/handlers/__tests__/find.test.ts | 49 ++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/src/daemon/handlers/__tests__/find.test.ts b/src/daemon/handlers/__tests__/find.test.ts index 1557241d27..6b3e635b3b 100644 --- a/src/daemon/handlers/__tests__/find.test.ts +++ b/src/daemon/handlers/__tests__/find.test.ts @@ -7,7 +7,7 @@ import { parseFindArgs, handleFindCommands } from '../find.ts'; import { AppError } from '../../../utils/errors.ts'; import { SessionStore } from '../../session-store.ts'; import type { SessionState } from '../../types.ts'; -import type { DaemonRequest, DaemonResponse } from '../../types.ts'; +import type { DaemonRequest } from '../../types.ts'; function makeSessionStore(): SessionStore { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-find-handler-')); @@ -173,14 +173,56 @@ test('handleFindCommands click returns deterministic matched-target metadata', a assert.equal(data.x, 100, 'x must be derived from the matched node rect center'); assert.equal(data.y, 50, 'y must be derived from the matched node rect center'); - // Non-deterministic platform data must not leak through - assert.equal(data.platformSpecificRef, undefined, 'platform runner data must not appear in response'); + // Strict key set — no platform-specific fields may leak through + assert.deepEqual(Object.keys(data).sort(), ['locator', 'query', 'ref', 'x', 'y']); // invoke was called with the resolved ref assert.equal(invokeCalls.length, 1); assert.equal(invokeCalls[0].positionals?.[0], '@e1'); }); +test('handleFindCommands click response contains exactly the deterministic key set (fallback: no rect on resolved node)', async () => { + const sessionStore = makeSessionStore(); + const sessionName = 'default'; + sessionStore.set(sessionName, makeSession(sessionName)); + + // Parent is hittable but has no rect — resolving through it loses coordinates. + // Child has a rect (satisfies requireRect) but is not hittable, so findNearestHittableAncestor + // walks up to the parent, which has no rect → fallback path: x/y are absent from response. + const hittableParentNoRect = { index: 0, type: 'View', hittable: true, depth: 0 }; + const nonHittableChildWithRect = { + index: 1, + type: 'StaticText', + label: 'Increment', + hittable: false, + rect: { x: 50, y: 0, width: 100, height: 100 }, + depth: 1, + parentIndex: 0, + }; + + const response = await handleFindCommands({ + req: { + token: 't', + session: sessionName, + command: 'find', + positionals: ['Increment', 'click'], + flags: {}, + }, + sessionName, + logPath: '/tmp/test.log', + sessionStore, + invoke: async () => ({ ok: true, data: { platformSpecificRef: 'XCUIElementTypeView' } }), + dispatch: async (_device, command) => { + if (command === 'snapshot') return { nodes: [hittableParentNoRect, nonHittableChildWithRect] }; + return {}; + }, + }); + + assert.ok(response?.ok); + const data = response!.data as Record; + assert.deepEqual(Object.keys(data).sort(), ['locator', 'query', 'ref']); +}); + test('handleFindCommands click with explicit label locator returns locator in metadata', async () => { const sessionStore = makeSessionStore(); const sessionName = 'default'; @@ -206,6 +248,7 @@ test('handleFindCommands click with explicit label locator returns locator in me assert.ok(response?.ok); const data = response!.data as Record; + assert.deepEqual(Object.keys(data).sort(), ['locator', 'query', 'ref', 'x', 'y']); assert.equal(data.locator, 'label'); assert.equal(data.query, 'Increment'); });