|
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | +import type { CliFlags } from '@agent-device/contracts/command'; |
| 3 | +import type { AgentDeviceClient } from '../../client/client-types.ts'; |
| 4 | +import { gestureCliReaders } from './gesture.ts'; |
| 5 | +import { interactionCommandFamily } from './index.ts'; |
| 6 | + |
| 7 | +const NO_FLAGS = {} as CliFlags; |
| 8 | + |
| 9 | +/** |
| 10 | + * The gesture `run` projection is exercised through the family's executable |
| 11 | + * definition — the same `invoke` path the CLI/daemon/MCP surfaces dispatch |
| 12 | + * through — with a client that records which interaction method each gesture |
| 13 | + * kind lands on and with which options. |
| 14 | + */ |
| 15 | +function recordingGestureClient(calls: Array<{ method: string; options: unknown }>) { |
| 16 | + const interactions = new Proxy( |
| 17 | + {}, |
| 18 | + { |
| 19 | + get: (_target, method: string) => async (options: unknown) => { |
| 20 | + calls.push({ method, options }); |
| 21 | + return { ok: true }; |
| 22 | + }, |
| 23 | + }, |
| 24 | + ); |
| 25 | + return { interactions } as unknown as AgentDeviceClient; |
| 26 | +} |
| 27 | + |
| 28 | +function gestureDefinition() { |
| 29 | + const definition = interactionCommandFamily.definitions.find((entry) => entry.name === 'gesture'); |
| 30 | + if (!definition) throw new Error('missing gesture definition'); |
| 31 | + return definition; |
| 32 | +} |
| 33 | + |
| 34 | +describe('gesture run dispatch', () => { |
| 35 | + // Each entry pins both halves of the run projection: which |
| 36 | + // `client.interactions.*` method the kind routes to and how its payload |
| 37 | + // lands on that method's options (`gesture.test.ts` covers the reader and |
| 38 | + // daemon writer, not this client-side projection). |
| 39 | + test.for([ |
| 40 | + { |
| 41 | + argv: ['pan', '10', '20', '5', '6', '300'], |
| 42 | + method: 'pan', |
| 43 | + options: { x: 10, y: 20, dx: 5, dy: 6, durationMs: 300 }, |
| 44 | + }, |
| 45 | + { |
| 46 | + argv: ['fling', 'up', '10', '20', '400'], |
| 47 | + method: 'fling', |
| 48 | + options: { direction: 'up', x: 10, y: 20, distance: 400 }, |
| 49 | + }, |
| 50 | + { argv: ['swipe', 'left-edge'], method: 'swipeGesture', options: { preset: 'left-edge' } }, |
| 51 | + { argv: ['pinch', '2', '30', '40'], method: 'pinch', options: { scale: 2, x: 30, y: 40 } }, |
| 52 | + { |
| 53 | + argv: ['rotate', '45', '10', '20'], |
| 54 | + method: 'rotateGesture', |
| 55 | + options: { degrees: 45, x: 10, y: 20 }, |
| 56 | + }, |
| 57 | + { |
| 58 | + argv: ['transform', '10', '20', '5', '6', '1.5', '45', '250'], |
| 59 | + method: 'transformGesture', |
| 60 | + options: { x: 10, y: 20, dx: 5, dy: 6, scale: 1.5, degrees: 45, durationMs: 250 }, |
| 61 | + }, |
| 62 | + { |
| 63 | + argv: ['drag', 'id="drag-source"', '@e2~s42', '700', '600', '200'], |
| 64 | + method: 'drag', |
| 65 | + options: { |
| 66 | + source: 'id="drag-source"', |
| 67 | + destination: '@e2~s42', |
| 68 | + sourceHoldMs: 700, |
| 69 | + moveMs: 600, |
| 70 | + destinationHoldMs: 200, |
| 71 | + }, |
| 72 | + }, |
| 73 | + ])( |
| 74 | + '%argv routes to client.interactions.%method with projected options', |
| 75 | + async ({ argv, method, options }) => { |
| 76 | + const calls: Array<{ method: string; options: unknown }> = []; |
| 77 | + const input = gestureCliReaders.gesture(argv, NO_FLAGS); |
| 78 | + await gestureDefinition().invoke(recordingGestureClient(calls), input); |
| 79 | + expect(calls).toHaveLength(1); |
| 80 | + expect(calls[0]?.method).toBe(method); |
| 81 | + expect(calls[0]?.options).toMatchObject(options); |
| 82 | + }, |
| 83 | + ); |
| 84 | + |
| 85 | + test('pinch without an origin still projects the scale', async () => { |
| 86 | + const calls: Array<{ method: string; options: unknown }> = []; |
| 87 | + const input = gestureCliReaders.gesture(['pinch', '2'], NO_FLAGS); |
| 88 | + await gestureDefinition().invoke(recordingGestureClient(calls), input); |
| 89 | + const options = calls[0]?.options as { scale?: number; x?: unknown } | undefined; |
| 90 | + expect(options).toMatchObject({ scale: 2 }); |
| 91 | + expect(options?.x).toBeUndefined(); |
| 92 | + }); |
| 93 | +}); |
0 commit comments