-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathruntime-interactions.test.ts
More file actions
941 lines (849 loc) · 27.6 KB
/
runtime-interactions.test.ts
File metadata and controls
941 lines (849 loc) · 27.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
import assert from 'node:assert/strict';
import { test } from 'vitest';
import type { AgentDeviceBackend } from '../backend.ts';
import { commands, ref, selector } from '../commands/index.ts';
import { resolveActionableTouchResolution } from '../commands/interaction-targeting.ts';
import { createLocalArtifactAdapter } from '../io.ts';
import { createAgentDevice, createMemorySessionStore, localCommandPolicy } from '../runtime.ts';
import { AppError } from '../utils/errors.ts';
import type { Point, SnapshotState } from '../utils/snapshot.ts';
import { makeSnapshotState } from './test-utils/index.ts';
test('runtime click taps an explicit point without requiring a snapshot', async () => {
const calls: Array<{ point: Point; count?: number }> = [];
const device = createInteractionDevice(selectorSnapshot(), {
tap: async (_context, point, options) => {
calls.push({ point, count: options?.count });
},
});
const result = await device.interactions.click({ kind: 'point', x: 10, y: 20 }, { count: 2 });
assert.deepEqual(calls, [{ point: { x: 10, y: 20 }, count: 2 }]);
assert.deepEqual(result, { kind: 'point', point: { x: 10, y: 20 } });
});
test('runtime interactions pass runtime signal to backend primitives', async () => {
const controller = new AbortController();
let signal: AbortSignal | undefined;
const device = createAgentDevice({
backend: {
platform: 'ios',
tap: async (context) => {
signal = context.signal;
},
typeText: async () => {},
} satisfies AgentDeviceBackend,
artifacts: createLocalArtifactAdapter(),
policy: localCommandPolicy(),
signal: controller.signal,
});
await device.interactions.click({ kind: 'point', x: 1, y: 2 });
assert.equal(signal, controller.signal);
});
test('runtime press resolves selector targets to the actionable node center', async () => {
const calls: Point[] = [];
const device = createInteractionDevice(selectorSnapshot(), {
tap: async (_context, point) => {
calls.push(point);
return { ok: true };
},
});
const result = await device.interactions.press(selector('label=Continue'), {
session: 'default',
});
assert.deepEqual(calls, [{ x: 60, y: 40 }]);
assert.equal(result.kind, 'selector');
assert.deepEqual(result.target, { kind: 'selector', selector: 'label=Continue' });
assert.equal(result.node?.label, 'Continue');
assert.deepEqual(result.selectorChain, [
'role="button" label="Continue"',
'label="Continue"',
'value="Continue"',
]);
assert.deepEqual(result.backendResult, { ok: true });
});
test('runtime click keeps distinct tab button centers when iOS reports the tab bar as hittable', async () => {
const calls: Point[] = [];
const device = createInteractionDevice(iosTabBarSnapshot(), {
tap: async (_context, point) => {
calls.push(point);
},
});
const refResult = await device.interactions.click(ref('@e4'), {
session: 'default',
});
const selectorResult = await device.interactions.click(selector('label=Settings'), {
session: 'default',
});
assert.deepEqual(calls, [
{ x: 166, y: 822 },
{ x: 257, y: 822 },
]);
assert.equal(refResult.kind, 'ref');
assert.equal(refResult.node?.label, 'Library');
assert.equal(selectorResult.kind, 'selector');
assert.equal(selectorResult.node?.label, 'Settings');
});
test('runtime click keeps non-button semantic targets at their own center', async () => {
const calls: Point[] = [];
const device = createInteractionDevice(nonHittableCellSnapshot(), {
tap: async (_context, point) => {
calls.push(point);
},
});
const result = await clickRefE2(device);
assert.deepEqual(calls, [{ x: 70, y: 30 }]);
assert.equal(result.kind, 'ref');
assert.equal(result.node?.label, 'Account');
});
test('runtime click still promotes non-touchable nodes to hittable ancestors', async () => {
const calls: Point[] = [];
const device = createInteractionDevice(nonTouchableGroupSnapshot(), {
tap: async (_context, point) => {
calls.push(point);
},
});
const result = await clickRefE2(device);
assert.deepEqual(calls, [{ x: 160, y: 60 }]);
assert.equal(result.kind, 'ref');
assert.equal(result.node?.label, 'Clickable group');
});
test('touch resolution promotes static text inside a hittable row to the row', () => {
const snapshot = makeSnapshotState([
{
index: 0,
depth: 0,
type: 'XCUIElementTypeCell',
label: 'Account row',
rect: { x: 10, y: 20, width: 300, height: 60 },
hittable: true,
},
{
index: 1,
depth: 1,
parentIndex: 0,
type: 'XCUIElementTypeStaticText',
label: 'Account',
rect: { x: 24, y: 32, width: 80, height: 20 },
hittable: false,
},
]);
const resolution = resolveActionableTouchResolution(snapshot.nodes, snapshot.nodes[1]!);
assert.equal(resolution.reason, 'hittable-ancestor');
assert.equal(resolution.node.label, 'Account row');
});
test('touch resolution prefers same-rect hittable descendants over semantic targets', () => {
const snapshot = makeSnapshotState([
{
index: 0,
depth: 0,
type: 'XCUIElementTypeButton',
label: 'Profile',
rect: { x: 30, y: 40, width: 120, height: 50 },
hittable: false,
},
{
index: 1,
depth: 1,
parentIndex: 0,
type: 'XCUIElementTypeImage',
identifier: 'profile-hit-area',
rect: { x: 30, y: 40, width: 120, height: 50 },
hittable: true,
},
]);
const resolution = resolveActionableTouchResolution(snapshot.nodes, snapshot.nodes[0]!);
assert.equal(resolution.reason, 'same-rect-descendant');
assert.equal(resolution.node.identifier, 'profile-hit-area');
});
test('touch resolution prevents full-screen window-like ancestors from stealing taps', () => {
const snapshot = makeSnapshotState([
{
index: 0,
depth: 0,
type: 'XCUIElementTypeApplication',
label: 'Example',
rect: { x: 0, y: 0, width: 390, height: 844 },
hittable: true,
},
{
index: 1,
depth: 1,
parentIndex: 0,
type: 'XCUIElementTypeStaticText',
label: 'Status',
rect: { x: 24, y: 72, width: 80, height: 24 },
hittable: false,
},
]);
const resolution = resolveActionableTouchResolution(snapshot.nodes, snapshot.nodes[1]!);
assert.equal(resolution.reason, 'overly-broad-ancestor');
assert.equal(resolution.node.label, 'Status');
});
test('touch resolution falls back to the original node when no usable touch target exists', () => {
const snapshot = makeSnapshotState([
{
index: 0,
depth: 0,
type: 'XCUIElementTypeOther',
label: 'Virtual item',
hittable: false,
},
]);
const resolution = resolveActionableTouchResolution(snapshot.nodes, snapshot.nodes[0]!);
assert.equal(resolution.reason, 'original');
assert.equal(resolution.node.label, 'Virtual item');
});
test('runtime fill resolves refs and forwards text to the backend primitive', async () => {
const calls: Array<{ point: Point; text: string; delayMs?: number }> = [];
const device = createInteractionDevice(fillableSnapshot(), {
captureSnapshot: async () => {
throw new Error('ref fill should use the stored session snapshot');
},
fill: async (_context, point, text, options) => {
calls.push({ point, text, delayMs: options?.delayMs });
},
});
const result = await device.interactions.fill(ref('@e1'), 'hello', {
session: 'default',
delayMs: 25,
});
assert.deepEqual(calls, [{ point: { x: 50, y: 30 }, text: 'hello', delayMs: 25 }]);
assert.equal(result.kind, 'ref');
assert.deepEqual(result.target, { kind: 'ref', ref: '@e1' });
assert.equal(result.text, 'hello');
assert.equal(result.warning, undefined);
});
test('runtime interactions reject unsupported macOS desktop and menubar surfaces', async () => {
const desktop = createInteractionDevice(selectorSnapshot(), {
platform: 'macos',
sessionMetadata: { surface: 'desktop' },
tap: async () => {
throw new Error('desktop click should be rejected before backend tap');
},
});
await assert.rejects(
() => desktop.interactions.click({ kind: 'point', x: 1, y: 2 }, { session: 'default' }),
/click is not supported on macOS desktop sessions yet/,
);
await assert.rejects(
() =>
desktop.interactions.click(
{ kind: 'point', x: 1, y: 2 },
{ session: 'default', metadata: { surface: 'app' } },
),
/click is not supported on macOS desktop sessions yet/,
);
const menubar = createInteractionDevice(fillableSnapshot(), {
platform: 'macos',
sessionMetadata: { surface: 'menubar' },
fill: async () => {
throw new Error('menubar fill should be rejected before backend fill');
},
});
await assert.rejects(
() => menubar.interactions.fill(ref('@e1'), 'hello', { session: 'default' }),
/fill is not supported on macOS menubar sessions yet/,
);
let pressed = false;
const menubarPress = createInteractionDevice(fillableSnapshot(), {
platform: 'macos',
sessionMetadata: { surface: 'menubar' },
tap: async () => {
pressed = true;
},
});
await menubarPress.interactions.press(ref('@e1'), { session: 'default' });
assert.equal(pressed, true);
});
test('runtime ref interactions refresh the snapshot when a stored ref has no usable rect', async () => {
const staleSnapshot = makeSnapshotState([
{
index: 0,
depth: 0,
type: 'Button',
label: 'Continue',
hittable: true,
},
]);
const freshSnapshot = selectorSnapshot();
const calls: Point[] = [];
let captures = 0;
const device = createInteractionDevice(staleSnapshot, {
captureSnapshot: async () => {
captures += 1;
return { snapshot: freshSnapshot };
},
tap: async (_context, point) => {
calls.push(point);
},
});
const result = await device.interactions.click(ref('@e1'), { session: 'default' });
assert.equal(captures, 1);
assert.deepEqual(calls, [{ x: 60, y: 40 }]);
assert.equal(result.kind, 'ref');
assert.equal(result.node?.rect?.width, 100);
});
test('runtime typeText validates refs and forwards text to the backend primitive', async () => {
const calls: Array<{ text: string; delayMs?: number }> = [];
const device = createInteractionDevice(selectorSnapshot(), {
typeText: async (_context, text, options) => {
calls.push({ text, delayMs: options?.delayMs });
},
});
const result = await device.interactions.typeText('hello', {
session: 'default',
delayMs: 25,
});
assert.deepEqual(calls, [{ text: 'hello', delayMs: 25 }]);
assert.equal(result.kind, 'text');
assert.equal(result.text, 'hello');
assert.equal(result.delayMs, 25);
assert.equal(result.message, 'Typed 5 chars');
await assert.rejects(
() => device.interactions.typeText('@e1 hello', { session: 'default' }),
/type does not accept a target ref/,
);
});
test('runtime focus and longPress share selector/ref target resolution', async () => {
const calls: unknown[] = [];
const device = createInteractionDevice(selectorSnapshot(), {
focus: async (_context, point) => {
calls.push({ command: 'focus', point });
return { focused: true };
},
longPress: async (_context, point, options) => {
calls.push({ command: 'longPress', point, durationMs: options?.durationMs });
},
});
const focused = await device.interactions.focus(selector('label=Continue'), {
session: 'default',
});
const longPressed = await device.interactions.longPress(ref('@e1'), {
session: 'default',
durationMs: 750,
});
assert.equal(focused.kind, 'selector');
assert.deepEqual(focused.backendResult, { focused: true });
assert.equal(longPressed.kind, 'ref');
assert.deepEqual(calls, [
{ command: 'focus', point: { x: 60, y: 40 } },
{ command: 'longPress', point: { x: 60, y: 40 }, durationMs: 750 },
]);
});
test('runtime scroll resolves selector targets before calling the backend primitive', async () => {
const calls: unknown[] = [];
const device = createInteractionDevice(selectorSnapshot(), {
scroll: async (_context, target, options) => {
calls.push({ target, options });
return { scrolled: true };
},
});
const selectorResult = await device.interactions.scroll({
session: 'default',
target: selector('label=Continue'),
direction: 'down',
pixels: 120,
});
const viewportResult = await device.interactions.scroll({
direction: 'up',
amount: 0.5,
});
assert.equal(selectorResult.kind, 'selector');
assert.equal(viewportResult.kind, 'viewport');
assert.deepEqual(calls, [
{
target: { kind: 'point', point: { x: 60, y: 40 } },
options: { direction: 'down', pixels: 120 },
},
{
target: { kind: 'viewport' },
options: { direction: 'up', amount: 0.5 },
},
]);
});
test('runtime scroll bottom rejects blind scrolling without snapshot support', async () => {
const calls: unknown[] = [];
const device = createInteractionDevice(selectorSnapshot(), {
captureSnapshot: async () => {
throw new Error('snapshot unavailable');
},
scroll: async (_context, target, options) => {
calls.push({ target, options });
return { pass: calls.length };
},
});
await assert.rejects(
() =>
device.interactions.scroll({
direction: 'bottom',
}),
/Failed to verify scroll bottom state/,
);
assert.equal(calls.length, 0);
});
test('runtime scroll bottom does not scroll when no hidden content is below', async () => {
const calls: unknown[] = [];
const device = createInteractionDevice(runtimeScrollSnapshot({ hiddenBelow: false }), {
scroll: async (_context, target, options) => {
calls.push({ target, options });
return { pass: calls.length };
},
});
const result = await device.interactions.scroll({
direction: 'bottom',
});
assert.equal(result.kind, 'viewport');
assert.equal(result.edge, 'bottom');
assert.equal(result.passes, 0);
assert.equal(calls.length, 0);
});
test('runtime scroll bottom scrolls only while scoped snapshot confirms hidden content', async () => {
const calls: unknown[] = [];
const snapshotScopes: unknown[] = [];
const snapshots = [
runtimeScrollSnapshot({ hiddenBelow: true, message: 'Middle message' }),
runtimeScrollSnapshot({ hiddenBelow: true, message: 'Middle message' }),
runtimeScrollSnapshot({ hiddenBelow: false, message: 'Latest message' }),
];
const device = createInteractionDevice(selectorSnapshot(), {
captureSnapshot: async (_context, options) => {
snapshotScopes.push(options?.scope);
return { snapshot: snapshots[Math.min(snapshotScopes.length - 1, snapshots.length - 1)] };
},
scroll: async (_context, target, options) => {
calls.push({ target, options });
return { pass: calls.length };
},
});
const result = await device.interactions.scroll({
direction: 'bottom',
});
assert.equal(result.kind, 'viewport');
assert.equal(result.edge, 'bottom');
assert.equal(result.passes, 1);
assert.equal(result.backendResult?.pass, 1);
assert.deepEqual(calls, [
{
target: { kind: 'viewport' },
options: { direction: 'down' },
},
]);
assert.deepEqual(snapshotScopes, [undefined, 'Messages', 'Messages']);
});
test('runtime scroll bottom tolerates unchanged signatures while hidden content advances', async () => {
const calls: unknown[] = [];
const snapshots = [
runtimeScrollSnapshot({ hiddenBelow: true, message: 'Repeated row' }),
runtimeScrollSnapshot({ hiddenBelow: true, message: 'Repeated row' }),
runtimeScrollSnapshot({ hiddenBelow: true, message: 'Repeated row' }),
runtimeScrollSnapshot({ hiddenBelow: false, message: 'Repeated row' }),
];
let snapshotIndex = 0;
const device = createInteractionDevice(selectorSnapshot(), {
captureSnapshot: async () => ({
snapshot: snapshots[Math.min(snapshotIndex++, snapshots.length - 1)],
}),
scroll: async (_context, target, options) => {
calls.push({ target, options });
return { pass: calls.length };
},
});
const result = await device.interactions.scroll({
direction: 'bottom',
});
assert.equal(result.passes, 2);
assert.equal(calls.length, 2);
});
test('runtime scroll bottom keeps scoped snapshot failures scoped', async () => {
let snapshotCount = 0;
const device = createInteractionDevice(selectorSnapshot(), {
captureSnapshot: async (_context, options) => {
snapshotCount += 1;
if (options?.scope) throw new Error('scoped snapshot failed');
return { snapshot: runtimeScrollSnapshot({ hiddenBelow: true, message: 'Middle message' }) };
},
scroll: async () => ({}),
});
await assert.rejects(
() =>
device.interactions.scroll({
direction: 'bottom',
}),
(error: unknown) =>
error instanceof AppError &&
error.code === 'COMMAND_FAILED' &&
/scoped container/i.test(error.message) &&
error.details?.scope === 'Messages',
);
assert.equal(snapshotCount, 2);
});
test('runtime swipe supports explicit and viewport-derived targets', async () => {
const calls: unknown[] = [];
const device = createInteractionDevice(selectorSnapshot(), {
swipe: async (_context, from, to, options) => {
calls.push({ from, to, durationMs: options?.durationMs });
},
});
const explicit = await device.interactions.swipe({
from: selector('label=Continue'),
to: { x: 200, y: 40 },
durationMs: 300,
session: 'default',
});
const directional = await device.interactions.swipe({
direction: 'left',
distance: 25,
session: 'default',
});
assert.deepEqual(explicit.from, { x: 60, y: 40 });
assert.deepEqual(directional.from, { x: 60, y: 40 });
assert.deepEqual(directional.to, { x: 35, y: 40 });
assert.deepEqual(calls, [
{ from: { x: 60, y: 40 }, to: { x: 200, y: 40 }, durationMs: 300 },
{ from: { x: 60, y: 40 }, to: { x: 35, y: 40 }, durationMs: undefined },
]);
});
test('runtime directional swipe uses the visible viewport instead of off-screen content bounds', async () => {
const calls: unknown[] = [];
const device = createInteractionDevice(snapshotWithOffscreenContent(), {
swipe: async (_context, from, to) => {
calls.push({ from, to });
},
});
const result = await device.interactions.swipe({
direction: 'left',
distance: 25,
session: 'default',
});
assert.deepEqual(result.from, { x: 50, y: 50 });
assert.deepEqual(result.to, { x: 25, y: 50 });
assert.deepEqual(calls, [{ from: { x: 50, y: 50 }, to: { x: 25, y: 50 } }]);
});
test('runtime gesture swipe presets use stable viewport lanes', async () => {
const calls: unknown[] = [];
const device = createInteractionDevice(snapshotWithOffscreenContent(), {
platform: 'android',
swipe: async (_context, from, to, options) => {
calls.push({ from, to, durationMs: options?.durationMs });
},
});
const pageSwipe = await device.interactions.swipe({
preset: 'left',
durationMs: 300,
session: 'default',
});
const edgeSwipe = await device.interactions.swipe({
preset: 'right-edge',
durationMs: 350,
session: 'default',
});
assert.deepEqual(pageSwipe.from, { x: 90, y: 65 });
assert.deepEqual(pageSwipe.to, { x: 10, y: 65 });
assert.deepEqual(edgeSwipe.from, { x: 8, y: 50 });
assert.deepEqual(edgeSwipe.to, { x: 85, y: 50 });
assert.deepEqual(calls, [
{ from: { x: 90, y: 65 }, to: { x: 10, y: 65 }, durationMs: 300 },
{ from: { x: 8, y: 50 }, to: { x: 85, y: 50 }, durationMs: 350 },
]);
});
test('runtime viewport gestures reject inspect-only macOS surfaces', async () => {
for (const surface of ['desktop', 'menubar'] as const) {
const device = createInteractionDevice(selectorSnapshot(), {
platform: 'macos',
sessionMetadata: { surface },
scroll: async () => {
throw new Error(`${surface} scroll should be rejected before backend call`);
},
swipe: async () => {
throw new Error(`${surface} swipe should be rejected before backend call`);
},
pinch: async () => {
throw new Error(`${surface} pinch should be rejected before backend call`);
},
});
await assert.rejects(
() =>
device.interactions.scroll({
direction: 'down',
target: { kind: 'viewport' },
session: 'default',
}),
new RegExp(`scroll is not supported on macOS ${surface}`),
);
await assert.rejects(
() =>
device.interactions.swipe({
direction: 'left',
session: 'default',
}),
new RegExp(`swipe is not supported on macOS ${surface}`),
);
await assert.rejects(
() =>
device.interactions.swipe({
from: { x: 10, y: 20 },
to: { x: 30, y: 20 },
session: 'default',
}),
new RegExp(`swipe is not supported on macOS ${surface}`),
);
await assert.rejects(
() =>
device.interactions.pinch({
scale: 1.2,
session: 'default',
}),
new RegExp(`pinch is not supported on macOS ${surface}`),
);
}
});
test('runtime pinch is backend-gated and resolves optional center targets', async () => {
const calls: unknown[] = [];
const unsupported = createInteractionDevice(selectorSnapshot());
await assert.rejects(
() => unsupported.interactions.pinch({ scale: 1.2 }),
/pinch is not supported/,
);
const device = createInteractionDevice(selectorSnapshot(), {
pinch: async (_context, options) => {
calls.push(options);
},
});
const result = await device.interactions.pinch({
scale: 0.8,
center: ref('@e1'),
session: 'default',
});
assert.equal(result.kind, 'pinch');
assert.deepEqual(result.center, { x: 60, y: 40 });
assert.deepEqual(calls, [{ scale: 0.8, center: { x: 60, y: 40 } }]);
});
test('runtime interaction commands are available from the command namespace', async () => {
const device = createInteractionDevice(selectorSnapshot(), {
tap: async () => {},
});
const result = await commands.interactions.click(device, {
session: 'default',
target: selector('label=Continue'),
});
assert.equal(result.kind, 'selector');
});
function selectorSnapshot(): SnapshotState {
return makeSnapshotState([
{
index: 0,
depth: 0,
type: 'Button',
label: 'Continue',
value: 'Continue',
rect: { x: 10, y: 20, width: 100, height: 40 },
hittable: true,
},
]);
}
function runtimeScrollSnapshot(options: { hiddenBelow: boolean; message?: string }): SnapshotState {
return makeSnapshotState([
{
index: 1,
depth: 0,
type: 'ScrollView',
label: 'Messages',
hiddenContentBelow: options.hiddenBelow ? true : undefined,
rect: { x: 0, y: 100, width: 400, height: 600 },
},
{
index: 2,
depth: 1,
parentIndex: 1,
type: 'Button',
label: options.message ?? 'Latest message',
rect: { x: 0, y: 640, width: 400, height: 56 },
hittable: true,
},
]);
}
function fillableSnapshot(): SnapshotState {
return makeSnapshotState([
{
index: 0,
depth: 0,
type: 'XCUIElementTypeTextField',
label: 'Email',
rect: { x: 20, y: 10, width: 60, height: 40 },
hittable: true,
},
]);
}
function iosTabBarSnapshot(): SnapshotState {
return makeSnapshotState([
{
index: 0,
depth: 0,
type: 'XCUIElementTypeApplication',
label: 'TabRepro',
rect: { x: 0, y: 0, width: 402, height: 874 },
hittable: false,
},
{
index: 1,
depth: 1,
parentIndex: 0,
type: 'XCUIElementTypeTabBar',
rect: { x: 0, y: 791, width: 402, height: 83 },
hittable: true,
},
{
index: 2,
depth: 2,
parentIndex: 1,
type: 'XCUIElementTypeButton',
label: 'Home',
rect: { x: 30, y: 800, width: 91, height: 44 },
hittable: false,
},
{
index: 3,
depth: 2,
parentIndex: 1,
type: 'XCUIElementTypeButton',
label: 'Library',
rect: { x: 120, y: 800, width: 92, height: 44 },
hittable: false,
},
{
index: 4,
depth: 2,
parentIndex: 1,
type: 'XCUIElementTypeButton',
label: 'Settings',
rect: { x: 211, y: 800, width: 91, height: 44 },
hittable: false,
},
{
index: 5,
depth: 2,
parentIndex: 1,
type: 'XCUIElementTypeButton',
label: 'Search',
rect: { x: 304, y: 800, width: 92, height: 44 },
hittable: false,
},
]);
}
function nonHittableCellSnapshot(): SnapshotState {
return makeSnapshotState([
{
index: 0,
depth: 0,
type: 'XCUIElementTypeOther',
label: 'Settings list',
rect: { x: 10, y: 20, width: 300, height: 80 },
hittable: true,
},
{
index: 1,
depth: 1,
parentIndex: 0,
type: 'XCUIElementTypeCell',
label: 'Account',
rect: { x: 20, y: 10, width: 100, height: 40 },
hittable: false,
},
]);
}
function nonTouchableGroupSnapshot(): SnapshotState {
return makeSnapshotState([
{
index: 0,
depth: 0,
type: 'XCUIElementTypeOther',
label: 'Clickable group',
rect: { x: 10, y: 20, width: 300, height: 80 },
hittable: true,
},
{
index: 1,
depth: 1,
parentIndex: 0,
type: 'XCUIElementTypeOther',
label: 'Decorative group',
rect: { x: 30, y: 40, width: 60, height: 20 },
hittable: false,
},
]);
}
function snapshotWithOffscreenContent(): SnapshotState {
return makeSnapshotState([
{
index: 0,
depth: 0,
type: 'Application',
label: 'Example',
rect: { x: 0, y: 0, width: 100, height: 100 },
},
{
index: 1,
depth: 1,
parentIndex: 0,
type: 'Button',
label: 'Visible',
rect: { x: 10, y: 10, width: 20, height: 20 },
hittable: true,
},
{
index: 2,
depth: 1,
parentIndex: 0,
type: 'Button',
label: 'Offscreen',
rect: { x: 10, y: 900, width: 20, height: 20 },
hittable: true,
},
]);
}
function createInteractionDevice(
snapshot: SnapshotState,
overrides: Partial<
Pick<
AgentDeviceBackend,
| 'captureSnapshot'
| 'tap'
| 'fill'
| 'typeText'
| 'focus'
| 'longPress'
| 'scroll'
| 'swipe'
| 'pinch'
>
> & {
platform?: AgentDeviceBackend['platform'];
sessionMetadata?: Record<string, unknown>;
} = {},
) {
return createAgentDevice({
backend: {
platform: overrides.platform ?? 'ios',
captureSnapshot: async (...args) =>
overrides.captureSnapshot ? await overrides.captureSnapshot(...args) : { snapshot },
tap: async (...args) => await overrides.tap?.(...args),
fill: async (...args) => await overrides.fill?.(...args),
typeText: async (...args) => await overrides.typeText?.(...args),
focus: overrides.focus ? async (...args) => await overrides.focus?.(...args) : undefined,
longPress: overrides.longPress
? async (...args) => await overrides.longPress?.(...args)
: undefined,
scroll: overrides.scroll ? async (...args) => await overrides.scroll?.(...args) : undefined,
swipe: overrides.swipe ? async (...args) => await overrides.swipe?.(...args) : undefined,
pinch: overrides.pinch ? async (...args) => await overrides.pinch?.(...args) : undefined,
} satisfies AgentDeviceBackend,
artifacts: createLocalArtifactAdapter(),
sessions: createMemorySessionStore([
{ name: 'default', snapshot, metadata: overrides.sessionMetadata },
]),
policy: localCommandPolicy(),
});
}
async function clickRefE2(device: ReturnType<typeof createInteractionDevice>) {
return await device.interactions.click(ref('@e2'), {
session: 'default',
});
}