Skip to content

Commit ffd2f05

Browse files
committed
test(system,interaction): type the alert backend double and pin gesture option projection
Review follow-up on #3438: - the alert-wait backend double returns BackendAlertResult values, dropping the `as never` casts, and the wrong-kind refusal asserts the typed COMMAND_FAILED code alongside the message - the gesture routing table now asserts the projected client options for every kind (fling, swipe, rotate, and transform had no toXOptions mapping test in gesture.test.ts)
1 parent 06d2bb9 commit ffd2f05

2 files changed

Lines changed: 69 additions & 44 deletions

File tree

‎src/commands/interaction/index.test.ts‎

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,55 @@ function gestureDefinition() {
3232
}
3333

3434
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).
3539
test.for([
36-
{ argv: ['pan', '10', '20', '5', '6', '300'], method: 'pan' },
37-
{ argv: ['fling', 'up', '10', '20', '400'], method: 'fling' },
38-
{ argv: ['swipe', 'left-edge'], method: 'swipeGesture' },
39-
{ argv: ['pinch', '2', '30', '40'], method: 'pinch' },
40-
{ argv: ['rotate', '45'], method: 'rotateGesture' },
41-
{ argv: ['transform', '10', '20', '5', '6', '1.5', '45', '250'], method: 'transformGesture' },
42-
{ argv: ['drag', 'id="drag-source"', '@e2~s42', '700', '600', '200'], method: 'drag' },
43-
])('%argv routes to client.interactions.%method', async ({ argv, method }) => {
44-
const calls: Array<{ method: string; options: unknown }> = [];
45-
const input = gestureCliReaders.gesture(argv, NO_FLAGS);
46-
await gestureDefinition().invoke(recordingGestureClient(calls), input);
47-
expect(calls).toHaveLength(1);
48-
expect(calls[0]?.method).toBe(method);
49-
});
50-
51-
test('pan projects origin/delta and the timed movement onto client options', async () => {
52-
const calls: Array<{ method: string; options: unknown }> = [];
53-
const input = gestureCliReaders.gesture(['pan', '10', '20', '5', '6', '300'], NO_FLAGS);
54-
await gestureDefinition().invoke(recordingGestureClient(calls), input);
55-
expect(calls[0]?.options).toMatchObject({ x: 10, y: 20, dx: 5, dy: 6, durationMs: 300 });
56-
});
57-
58-
test('drag projects selectors and hold durations onto client options', async () => {
59-
const calls: Array<{ method: string; options: unknown }> = [];
60-
const input = gestureCliReaders.gesture(
61-
['drag', 'id="drag-source"', '@e2~s42', '700', '600', '200'],
62-
NO_FLAGS,
63-
);
64-
await gestureDefinition().invoke(recordingGestureClient(calls), input);
65-
expect(calls[0]?.options).toMatchObject({
66-
source: 'id="drag-source"',
67-
destination: '@e2~s42',
68-
sourceHoldMs: 700,
69-
moveMs: 600,
70-
destinationHoldMs: 200,
71-
});
72-
});
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+
);
7384

7485
test('pinch without an origin still projects the scale', async () => {
7586
const calls: Array<{ method: string; options: unknown }> = [];

‎src/commands/system/runtime/system.test.ts‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { test } from 'vitest';
33
import type {
44
AgentDeviceBackend,
55
BackendAlertAction,
6+
BackendAlertResult,
67
BackendDeviceOrientation,
78
BackendKeyboardOptions,
89
BackendTvRemoteOptions,
@@ -116,16 +117,20 @@ test('runtime system commands validate options before backend calls', async () =
116117

117118
test('runtime alert wait projects the backend alert, timing, and timeout verdict', async () => {
118119
const alert = { title: 'Location', message: 'Allow access?', buttons: ['Allow', 'Deny'] };
119-
const outcomes: Array<{ alert: unknown; waitedMs?: number; timedOut?: boolean }> = [
120-
{ alert, waitedMs: 250 },
121-
{ alert: null, waitedMs: 5_000, timedOut: true },
122-
{ alert },
120+
const outcomes: BackendAlertResult[] = [
121+
{ kind: 'alertWait', alert, waitedMs: 250 },
122+
{ kind: 'alertWait', alert: null, waitedMs: 5_000, timedOut: true },
123+
{ kind: 'alertWait', alert },
123124
];
124125
let next = 0;
125126
const device = createAgentDevice({
126127
backend: {
127128
platform: 'ios',
128-
handleAlert: async () => ({ kind: 'alertWait', ...outcomes[next++] }) as never,
129+
handleAlert: async (): Promise<BackendAlertResult> => {
130+
const result = outcomes[next++];
131+
if (!result) throw new Error('alert backend double exhausted');
132+
return result;
133+
},
129134
},
130135
artifacts: createLocalArtifactAdapter(),
131136
policy: localCommandPolicy(),
@@ -155,18 +160,27 @@ test('runtime alert wait projects the backend alert, timing, and timeout verdict
155160
});
156161

157162
test('runtime alert wait refuses a backend result of the wrong kind', async () => {
163+
// Typed as the union the backend contract allows: a wrong `kind` is a legal
164+
// backend result the normalizer must refuse with the typed COMMAND_FAILED
165+
// code, not just the message.
166+
const wrongKind: BackendAlertResult = { kind: 'alertStatus', alert: null };
158167
const device = createAgentDevice({
159168
backend: {
160169
platform: 'ios',
161-
handleAlert: async () => ({ kind: 'alertStatus', alert: null }) as never,
170+
handleAlert: async () => wrongKind,
162171
},
163172
artifacts: createLocalArtifactAdapter(),
164173
policy: localCommandPolicy(),
165174
});
166175

167176
await assert.rejects(
168177
() => device.system.alert({ action: 'wait' }),
169-
/system\.alert wait returned an invalid backend result/,
178+
(error: unknown) => {
179+
const appError = error as { code?: string; message?: string };
180+
assert.equal(appError.code, 'COMMAND_FAILED');
181+
assert.match(appError.message ?? '', /system\.alert wait returned an invalid backend result/);
182+
return true;
183+
},
170184
);
171185
});
172186

0 commit comments

Comments
 (0)