Skip to content

Commit f8c60ca

Browse files
authored
refactor(gesture): pass structured input through client dispatch (#3852)
1 parent aed7a91 commit f8c60ca

10 files changed

Lines changed: 114 additions & 259 deletions

File tree

‎packages/contracts/src/client-gesture.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import type { ClickButton } from './click-button.ts';
44
import type { GesturePointerCount } from './gesture-plan-types.ts';
5+
import type { GesturePayload } from './gesture-input.ts';
56
import type {
67
ScrollDirection,
78
ScrollInputDirection,
@@ -78,6 +79,8 @@ export type SwipeOptions = DeviceCommandBaseOptions & {
7879
pattern?: SwipePattern;
7980
};
8081

82+
export type GestureOptions = DeviceCommandBaseOptions & GesturePayload;
83+
8184
export type PanOptions = DeviceCommandBaseOptions & {
8285
x: number;
8386
y: number;

‎packages/contracts/src/facades/client.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export type {
5656
FillOptions,
5757
FlingOptions,
5858
FocusOptions,
59+
GestureOptions,
5960
HoverOptions,
6061
LongPressOptions,
6162
PanOptions,

‎src/__tests__/agent-device-client-gesture-tap.test.ts‎

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,42 @@ import { test } from 'vitest';
33
import { createAgentDeviceClient } from '../agent-device-client.ts';
44
import { createTransport } from './client-transport-fixture.ts';
55

6-
test('interactions.tapGesture sends its contacts and series as structured gesture input', async () => {
7-
const setup = createTransport(async () => ({ ok: true, data: { message: 'Tapped' } }));
8-
const client = createAgentDeviceClient(setup.config, { transport: setup.transport });
6+
test.for(['named', 'structured'] as const)(
7+
'%s tap sends its contacts and series as structured gesture input',
8+
async (surface) => {
9+
const setup = createTransport(async () => ({ ok: true, data: { message: 'Tapped' } }));
10+
const client = createAgentDeviceClient(setup.config, { transport: setup.transport });
911

10-
await client.interactions.tapGesture({
11-
points: [
12-
{ x: 80, y: 300 },
13-
{ x: 200, y: 450 },
14-
],
15-
holdMs: 40,
16-
count: 3,
17-
pauseMs: 100,
18-
});
12+
const input = {
13+
points: [
14+
{ x: 80, y: 300 },
15+
{ x: 200, y: 450 },
16+
],
17+
holdMs: 40,
18+
count: 3,
19+
pauseMs: 100,
20+
};
21+
if (surface === 'named') await client.interactions.tapGesture(input);
22+
else await client.interactions.gesture({ kind: 'tap', ...input });
1923

20-
assert.deepEqual(
21-
setup.calls.map(({ command, positionals, input }) => ({ command, positionals, input })),
22-
[
23-
{
24-
command: 'gesture',
25-
positionals: [],
26-
input: {
27-
kind: 'tap',
28-
points: [
29-
{ x: 80, y: 300 },
30-
{ x: 200, y: 450 },
31-
],
32-
holdMs: 40,
33-
count: 3,
34-
pauseMs: 100,
24+
assert.deepEqual(
25+
setup.calls.map(({ command, positionals, input }) => ({ command, positionals, input })),
26+
[
27+
{
28+
command: 'gesture',
29+
positionals: [],
30+
input: {
31+
kind: 'tap',
32+
points: [
33+
{ x: 80, y: 300 },
34+
{ x: 200, y: 450 },
35+
],
36+
holdMs: 40,
37+
count: 3,
38+
pauseMs: 100,
39+
},
3540
},
36-
},
37-
],
38-
);
39-
});
41+
],
42+
);
43+
},
44+
);

‎src/agent-device-client.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ export function createAgentDeviceClient(
452452
diff: async (options) => await executeCommand<CommandResult<'diff'>>('diff', options),
453453
},
454454
interactions: {
455+
gesture: async (options) => await executeCommand('gesture', options),
455456
click: async (options) => await executeCommand('click', options),
456457
press: async (options) => await executeCommand('press', options),
457458
longPress: async (options) => await executeCommand('longpress', options),

‎src/client/client-types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ import type {
9494
FindOptions,
9595
FlingOptions,
9696
FocusOptions,
97+
GestureOptions,
9798
GetOptions,
9899
HomeCommandOptions,
99100
HoverOptions,
@@ -259,6 +260,7 @@ export type AgentDeviceClient = {
259260
diff: (options: CaptureDiffOptions) => Promise<CommandResult<'diff'>>;
260261
};
261262
interactions: {
263+
gesture: (options: GestureOptions) => Promise<CommandRequestResult>;
262264
click: (options: ClickOptions) => Promise<CommandResult<'click'>>;
263265
press: (options: PressOptions) => Promise<CommandResult<'press'>>;
264266
longPress: (options: LongPressOptions) => Promise<CommandResult<'longpress'>>;
Lines changed: 46 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,83 @@
11
import { describe, expect, test } from 'vitest';
22
import type { CliFlags } from '@agent-device/contracts/command';
3-
import type { AgentDeviceClient } from '../../client/client-types.ts';
3+
import { createAgentDeviceClient } from '../../agent-device-client.ts';
4+
import { createTransport } from '../../__tests__/client-transport-fixture.ts';
45
import { gestureCliReaders } from './gesture.ts';
56
import { interactionCommandFamily } from './index.ts';
67

78
const NO_FLAGS = {} as CliFlags;
89

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-
2810
function gestureDefinition() {
2911
const definition = interactionCommandFamily.definitions.find((entry) => entry.name === 'gesture');
3012
if (!definition) throw new Error('missing gesture definition');
3113
return definition;
3214
}
3315

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).
16+
describe('gesture command transport', () => {
3917
test.for([
4018
{
4119
argv: ['pan', '10', '20', '5', '6', '300'],
42-
method: 'pan',
43-
options: { x: 10, y: 20, dx: 5, dy: 6, durationMs: 300 },
20+
input: { kind: 'pan', origin: { x: 10, y: 20 }, delta: { x: 5, y: 6 }, durationMs: 300 },
4421
},
4522
{
4623
argv: ['fling', 'up', '10', '20', '400'],
47-
method: 'fling',
48-
options: { direction: 'up', x: 10, y: 20, distance: 400 },
24+
input: { kind: 'fling', direction: 'up', origin: { x: 10, y: 20 }, distance: 400 },
4925
},
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 } },
26+
{ argv: ['swipe', 'left-edge'], input: { kind: 'swipe', preset: 'left-edge' } },
27+
{
28+
argv: ['pinch', '2', '30', '40'],
29+
input: { kind: 'pinch', scale: 2, origin: { x: 30, y: 40 } },
30+
},
31+
{ argv: ['pinch', '2'], input: { kind: 'pinch', scale: 2 } },
5232
{
5333
argv: ['rotate', '45', '10', '20'],
54-
method: 'rotateGesture',
55-
options: { degrees: 45, x: 10, y: 20 },
34+
input: { kind: 'rotate', degrees: 45, origin: { x: 10, y: 20 } },
5635
},
5736
{
5837
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 },
38+
input: {
39+
kind: 'transform',
40+
origin: { x: 10, y: 20 },
41+
delta: { x: 5, y: 6 },
42+
scale: 1.5,
43+
degrees: 45,
44+
durationMs: 250,
45+
},
6146
},
6247
{
6348
argv: ['drag', 'id="drag-source"', '@e2~s42', '700', '600', '200'],
64-
method: 'drag',
65-
options: {
49+
input: {
50+
kind: 'drag',
6651
source: 'id="drag-source"',
6752
destination: '@e2~s42',
6853
sourceHoldMs: 700,
6954
moveMs: 600,
7055
destinationHoldMs: 200,
7156
},
7257
},
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();
58+
])('$argv sends structured input through the client transport', async ({ argv, input }) => {
59+
const setup = createTransport(() => ({ ok: true, data: { message: 'Performed gesture' } }));
60+
const client = createAgentDeviceClient(setup.config, { transport: setup.transport });
61+
const parsed = gestureCliReaders.gesture(argv, NO_FLAGS);
62+
const result = await gestureDefinition().invoke(client, {
63+
...parsed,
64+
session: 'gesture-test',
65+
deviceTarget: 'mobile',
66+
udid: 'gesture-device',
67+
});
68+
expect(result).toEqual({ message: 'Performed gesture' });
69+
expect(setup.calls).toHaveLength(1);
70+
expect(setup.calls[0]).toMatchObject({
71+
command: 'gesture',
72+
positionals: [],
73+
session: 'gesture-test',
74+
flags: { target: 'mobile', udid: 'gesture-device' },
75+
});
76+
expect(setup.calls[0]?.input).toEqual({
77+
...input,
78+
session: 'gesture-test',
79+
deviceTarget: 'mobile',
80+
udid: 'gesture-device',
81+
});
9282
});
9383
});

0 commit comments

Comments
 (0)