|
| 1 | +import { test, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import type { DaemonResponse } from '../../types.ts'; |
| 3 | +import { makeSessionStore } from '../../../__tests__/test-utils/store-factory.ts'; |
| 4 | +import { makeIosSession } from '../../../__tests__/test-utils/session-factories.ts'; |
| 5 | +import { |
| 6 | + mockFocusPoint, |
| 7 | + mockTypeText, |
| 8 | + resetGetRuntimeFixture, |
| 9 | + runtimeBindingSpies, |
| 10 | +} from './interaction-get-runtime-fixture.ts'; |
| 11 | +import { invokeFindHandler } from './find-handler-fixture.ts'; |
| 12 | + |
| 13 | +const { mockDispatch } = vi.hoisted(() => ({ mockDispatch: vi.fn() })); |
| 14 | + |
| 15 | +vi.mock('../../../core/dispatch.ts', async (importOriginal) => { |
| 16 | + const actual = await importOriginal<typeof import('../../../core/dispatch.ts')>(); |
| 17 | + return { |
| 18 | + ...actual, |
| 19 | + dispatchCommand: mockDispatch, |
| 20 | + resolveTargetDevice: actual.resolveTargetDevice, |
| 21 | + }; |
| 22 | +}); |
| 23 | + |
| 24 | +vi.mock('../snapshot-interactor-capture.ts', async () => { |
| 25 | + const fixture = await import('../../__tests__/legacy-snapshot-capture-fixture.ts'); |
| 26 | + return { captureSnapshotWithInteractor: fixture.captureSnapshotThroughLegacyDispatchFixture }; |
| 27 | +}); |
| 28 | + |
| 29 | +beforeEach(() => { |
| 30 | + resetGetRuntimeFixture(); |
| 31 | + mockDispatch.mockReset(); |
| 32 | +}); |
| 33 | + |
| 34 | +async function runMutatingFind(positionals: string[], node: Record<string, unknown>) { |
| 35 | + const sessionStore = makeSessionStore(); |
| 36 | + const sessionName = 'default'; |
| 37 | + const session = makeIosSession(sessionName); |
| 38 | + sessionStore.set(sessionName, session); |
| 39 | + mockDispatch.mockImplementation(async (_device: unknown, command: string) => |
| 40 | + command === 'snapshot' ? { nodes: [node] } : {}, |
| 41 | + ); |
| 42 | + return await invokeFindHandler({ |
| 43 | + sessionName, |
| 44 | + sessionStore, |
| 45 | + positionals, |
| 46 | + invoke: async () => ({ ok: true, data: {} }) as DaemonResponse, |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +test('find focus performs exactly one facts inspection and one bind (ADR 0019 §9)', async () => { |
| 51 | + const response = await runMutatingFind(['Save', 'focus'], { |
| 52 | + index: 0, |
| 53 | + ref: 'e1', |
| 54 | + type: 'Button', |
| 55 | + label: 'Save', |
| 56 | + hittable: true, |
| 57 | + rect: { x: 10, y: 20, width: 100, height: 40 }, |
| 58 | + }); |
| 59 | + |
| 60 | + expect(response?.ok).toBe(true); |
| 61 | + expect(mockFocusPoint).toHaveBeenCalledTimes(1); |
| 62 | + const spies = runtimeBindingSpies(); |
| 63 | + expect(spies.inspectFacts).toHaveBeenCalledTimes(1); |
| 64 | + expect(spies.bindDevice).toHaveBeenCalledTimes(1); |
| 65 | +}); |
| 66 | + |
| 67 | +test('find type performs exactly one facts inspection and one bind (ADR 0019 §9)', async () => { |
| 68 | + const response = await runMutatingFind(['Email', 'type', '[email protected]'], { |
| 69 | + index: 0, |
| 70 | + ref: 'e1', |
| 71 | + type: 'TextField', |
| 72 | + label: 'Email', |
| 73 | + hittable: true, |
| 74 | + rect: { x: 10, y: 20, width: 200, height: 40 }, |
| 75 | + }); |
| 76 | + |
| 77 | + expect(response?.ok).toBe(true); |
| 78 | + // The one bind carried both legs: the focus tap and the text entry. |
| 79 | + expect(mockFocusPoint).toHaveBeenCalledTimes(1); |
| 80 | + expect(mockTypeText).toHaveBeenCalledTimes(1); |
| 81 | + expect(mockTypeText).toHaveBeenCalledWith(expect.objectContaining({ text: '[email protected]' })); |
| 82 | + const spies = runtimeBindingSpies(); |
| 83 | + expect(spies.inspectFacts).toHaveBeenCalledTimes(1); |
| 84 | + expect(spies.bindDevice).toHaveBeenCalledTimes(1); |
| 85 | +}); |
0 commit comments