-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathargs.test.ts
More file actions
1450 lines (1332 loc) · 57.1 KB
/
args.test.ts
File metadata and controls
1450 lines (1332 loc) · 57.1 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
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { test } from 'vitest';
import assert from 'node:assert/strict';
import { parseArgs, usage, usageForCommand } from '../args.ts';
import { AppError } from '../errors.ts';
import { getCliCommandNames, getSchemaCapabilityKeys } from '../command-schema.ts';
import { listCapabilityCommands } from '../../core/capabilities.ts';
test('parseArgs recognizes command-specific flag combinations', async () => {
const scenarios: Array<{
label: string;
argv: string[];
strictFlags?: boolean;
assertParsed: (parsed: ReturnType<typeof parseArgs>) => void;
}> = [
{
label: 'open --relaunch',
argv: ['open', 'settings', '--relaunch'],
assertParsed: (parsed) => {
assert.equal(parsed.command, 'open');
assert.deepEqual(parsed.positionals, ['settings']);
assert.equal(parsed.flags.relaunch, true);
},
},
{
label: 'open --platform ios --target tv',
argv: ['open', 'Settings', '--platform', 'ios', '--target', 'tv'],
strictFlags: true,
assertParsed: (parsed) => {
assert.equal(parsed.command, 'open');
assert.equal(parsed.flags.platform, 'ios');
assert.equal(parsed.flags.target, 'tv');
},
},
{
label: 'boot --headless on android',
argv: ['boot', '--platform', 'android', '--device', 'Pixel_9_Pro_XL', '--headless'],
strictFlags: true,
assertParsed: (parsed) => {
assert.equal(parsed.command, 'boot');
assert.equal(parsed.flags.platform, 'android');
assert.equal(parsed.flags.device, 'Pixel_9_Pro_XL');
assert.equal(parsed.flags.headless, true);
},
},
{
label: 'back --in-app',
argv: ['back', '--in-app'],
strictFlags: true,
assertParsed: (parsed) => {
assert.equal(parsed.command, 'back');
assert.equal(parsed.flags.backMode, 'in-app');
},
},
{
label: 'back --system',
argv: ['back', '--system'],
strictFlags: true,
assertParsed: (parsed) => {
assert.equal(parsed.command, 'back');
assert.equal(parsed.flags.backMode, 'system');
},
},
{
label: 'react-native dismiss-overlay',
argv: ['react-native', 'dismiss-overlay', '--platform', 'ios'],
strictFlags: true,
assertParsed: (parsed) => {
assert.equal(parsed.command, 'react-native');
assert.deepEqual(parsed.positionals, ['dismiss-overlay']);
assert.equal(parsed.flags.platform, 'ios');
},
},
{
label: 'open --platform apple alias',
argv: ['open', 'Settings', '--platform', 'apple', '--target', 'tv'],
strictFlags: true,
assertParsed: (parsed) => {
assert.equal(parsed.command, 'open');
assert.equal(parsed.flags.platform, 'apple');
assert.equal(parsed.flags.target, 'tv');
},
},
{
label: 'open --surface frontmost-app',
argv: ['open', '--platform', 'macos', '--surface', 'frontmost-app'],
strictFlags: true,
assertParsed: (parsed) => {
assert.equal(parsed.command, 'open');
assert.equal(parsed.flags.platform, 'macos');
assert.equal(parsed.flags.surface, 'frontmost-app');
},
},
{
label: 'test suite with retries, timeout, artifacts, fail-fast, and replay update',
argv: [
'test',
'./suite',
'--platform',
'android',
'--fail-fast',
'--update',
'--timeout',
'60000',
'--retries',
'2',
'--artifacts-dir',
'.agent-device/test-artifacts',
],
strictFlags: true,
assertParsed: (parsed) => {
assert.equal(parsed.command, 'test');
assert.deepEqual(parsed.positionals, ['./suite']);
assert.equal(parsed.flags.platform, 'android');
assert.equal(parsed.flags.failFast, true);
assert.equal(parsed.flags.replayUpdate, true);
assert.equal(parsed.flags.timeoutMs, 60000);
assert.equal(parsed.flags.retries, 2);
assert.equal(parsed.flags.artifactsDir, '.agent-device/test-artifacts');
},
},
{
label: 'replay maestro flow',
argv: ['replay', './flow.yaml', '--maestro', '--env', 'USER=Ada'],
strictFlags: true,
assertParsed: (parsed) => {
assert.equal(parsed.command, 'replay');
assert.deepEqual(parsed.positionals, ['./flow.yaml']);
assert.equal(parsed.flags.replayMaestro, true);
assert.deepEqual(parsed.flags.replayEnv, ['USER=Ada']);
},
},
];
for (const scenario of scenarios) {
scenario.assertParsed(parseArgs(scenario.argv, { strictFlags: scenario.strictFlags }));
}
});
test('parseArgs recognizes device isolation flags', () => {
const parsed = parseArgs(
[
'devices',
'--platform',
'ios',
'--ios-simulator-device-set',
'/tmp/tenant-a/simulators',
'--android-device-allowlist',
'emulator-5554,device-1234',
],
{ strictFlags: true },
);
assert.equal(parsed.command, 'devices');
assert.equal(parsed.flags.platform, 'ios');
assert.equal(parsed.flags.iosSimulatorDeviceSet, '/tmp/tenant-a/simulators');
assert.equal(parsed.flags.androidDeviceAllowlist, 'emulator-5554,device-1234');
});
test('parseArgs rejects test retries above the supported ceiling', () => {
assert.throws(
() => parseArgs(['test', './suite', '--retries', '4'], { strictFlags: true }),
(error: unknown) =>
error instanceof AppError &&
error.code === 'INVALID_ARGS' &&
/Invalid retries: 4/.test(error.message),
);
});
test('parseArgs recognizes logs clear --restart', () => {
const parsed = parseArgs(['logs', 'clear', '--restart'], { strictFlags: true });
assert.equal(parsed.command, 'logs');
assert.deepEqual(parsed.positionals, ['clear']);
assert.equal(parsed.flags.restart, true);
});
test('parseArgs recognizes network dump arguments', () => {
const parsed = parseArgs(['network', 'dump', '20', 'headers'], { strictFlags: true });
assert.equal(parsed.command, 'network');
assert.deepEqual(parsed.positionals, ['dump', '20', 'headers']);
});
test('parseArgs recognizes network include flag', () => {
const parsed = parseArgs(['network', 'dump', '20', '--include', 'headers'], {
strictFlags: true,
});
assert.equal(parsed.command, 'network');
assert.deepEqual(parsed.positionals, ['dump', '20']);
assert.equal(parsed.flags.networkInclude, 'headers');
});
test('parseArgs preserves react-devtools arguments as passthrough positionals', () => {
const parsed = parseArgs(
[
'react-devtools',
'profile',
'diff',
'--threshold',
'10',
'--limit=5',
'--json',
'--session',
'rn',
],
{ strictFlags: true },
);
assert.equal(parsed.command, 'react-devtools');
assert.equal(parsed.flags.json, true);
assert.equal(parsed.flags.session, 'rn');
assert.deepEqual(parsed.positionals, ['profile', 'diff', '--threshold', '10', '--limit=5']);
});
test('parseArgs supports explicit passthrough boundary for react-devtools global flag names', () => {
const parsed = parseArgs(['react-devtools', '--', 'status', '--json'], { strictFlags: true });
assert.equal(parsed.command, 'react-devtools');
assert.equal(parsed.flags.json, false);
assert.deepEqual(parsed.positionals, ['status', '--json']);
});
test('parseArgs accepts push with payload file', () => {
const parsed = parseArgs(['push', 'com.example.app', './payload.json'], { strictFlags: true });
assert.equal(parsed.command, 'push');
assert.deepEqual(parsed.positionals, ['com.example.app', './payload.json']);
});
test('parseArgs accepts install command args', () => {
const parsed = parseArgs(['install', 'com.example.app', './build/app.apk'], {
strictFlags: true,
});
assert.equal(parsed.command, 'install');
assert.deepEqual(parsed.positionals, ['com.example.app', './build/app.apk']);
});
test('parseArgs accepts install-from-source url and repeated headers', () => {
const parsed = parseArgs(
[
'install-from-source',
'https://example.com/builds/app.apk',
'--header',
'authorization: Bearer token',
'--header',
'x-build-id: 42',
'--retain-paths',
'--retention-ms',
'60000',
],
{ strictFlags: true },
);
assert.equal(parsed.command, 'install-from-source');
assert.deepEqual(parsed.positionals, ['https://example.com/builds/app.apk']);
assert.deepEqual(parsed.flags.header, ['authorization: Bearer token', 'x-build-id: 42']);
assert.equal(parsed.flags.retainPaths, true);
assert.equal(parsed.flags.retentionMs, 60000);
});
test('parseArgs accepts install-from-source GitHub Actions artifact flag', () => {
const parsed = parseArgs(
[
'install-from-source',
'--github-actions-artifact',
'thymikee/RNCLI83:6635342232',
'--platform',
'android',
],
{ strictFlags: true },
);
assert.equal(parsed.command, 'install-from-source');
assert.deepEqual(parsed.positionals, []);
assert.equal(parsed.flags.githubActionsArtifact, 'thymikee/RNCLI83:6635342232');
assert.equal(parsed.flags.platform, 'android');
});
test('parseArgs accepts metro prepare arguments', () => {
const parsed = parseArgs(
[
'metro',
'prepare',
'--project-root',
'./apps/demo',
'--public-base-url',
'https://sandbox.example.test',
'--proxy-base-url',
'https://proxy.example.test',
'--bearer-token',
'secret',
'--port',
'9090',
'--kind',
'expo',
'--runtime-file',
'./.agent-device/metro-runtime.json',
'--no-reuse-existing',
'--no-install-deps',
],
{ strictFlags: true },
);
assert.equal(parsed.command, 'metro');
assert.deepEqual(parsed.positionals, ['prepare']);
assert.equal(parsed.flags.metroProjectRoot, './apps/demo');
assert.equal(parsed.flags.metroPublicBaseUrl, 'https://sandbox.example.test');
assert.equal(parsed.flags.metroProxyBaseUrl, 'https://proxy.example.test');
assert.equal(parsed.flags.metroBearerToken, 'secret');
assert.equal(parsed.flags.metroPreparePort, 9090);
assert.equal(parsed.flags.metroKind, 'expo');
assert.equal(parsed.flags.metroRuntimeFile, './.agent-device/metro-runtime.json');
assert.equal(parsed.flags.metroNoReuseExisting, true);
assert.equal(parsed.flags.metroNoInstallDeps, true);
});
test('parseArgs accepts metro reload arguments', () => {
const parsed = parseArgs(
[
'metro',
'reload',
'--metro-host',
'127.0.0.1',
'--metro-port',
'9090',
'--bundle-url',
'http://127.0.0.1:9090/index.bundle?platform=ios',
'--probe-timeout-ms',
'1500',
],
{ strictFlags: true },
);
assert.equal(parsed.command, 'metro');
assert.deepEqual(parsed.positionals, ['reload']);
assert.equal(parsed.flags.metroHost, '127.0.0.1');
assert.equal(parsed.flags.metroPort, 9090);
assert.equal(parsed.flags.bundleUrl, 'http://127.0.0.1:9090/index.bundle?platform=ios');
assert.equal(parsed.flags.metroProbeTimeoutMs, 1500);
});
test('parseArgs accepts remote workflow profile flag', () => {
const parsed = parseArgs(
[
'connect',
'--remote-config',
'./agent-device.remote.json',
'--tenant',
'acme',
'--run-id',
'run-1',
],
{
strictFlags: true,
},
);
assert.equal(parsed.command, 'connect');
assert.deepEqual(parsed.positionals, []);
assert.equal(parsed.flags.remoteConfig, './agent-device.remote.json');
});
test('parseArgs accepts clipboard subcommands', () => {
const read = parseArgs(['clipboard', 'read'], { strictFlags: true });
assert.equal(read.command, 'clipboard');
assert.deepEqual(read.positionals, ['read']);
const write = parseArgs(['clipboard', 'write', 'otp', '123456'], { strictFlags: true });
assert.equal(write.command, 'clipboard');
assert.deepEqual(write.positionals, ['write', 'otp', '123456']);
});
test('parseArgs accepts keyboard subcommands', () => {
const status = parseArgs(['keyboard', 'status'], { strictFlags: true });
assert.equal(status.command, 'keyboard');
assert.deepEqual(status.positionals, ['status']);
const dismiss = parseArgs(['keyboard', 'dismiss'], { strictFlags: true });
assert.equal(dismiss.command, 'keyboard');
assert.deepEqual(dismiss.positionals, ['dismiss']);
});
test('parseArgs accepts scroll pixel distance flag', () => {
const parsed = parseArgs(['scroll', 'down', '--pixels', '240'], { strictFlags: true });
assert.equal(parsed.command, 'scroll');
assert.deepEqual(parsed.positionals, ['down']);
assert.equal(parsed.flags.pixels, 240);
});
test('parseArgs recognizes --debug alias for verbose mode', () => {
const parsed = parseArgs(['open', 'settings', '--debug']);
assert.equal(parsed.command, 'open');
assert.deepEqual(parsed.positionals, ['settings']);
assert.equal(parsed.flags.verbose, true);
});
test('parseArgs recognizes daemon transport/state/tenant isolation flags', () => {
const parsed = parseArgs(
[
'open',
'settings',
'--state-dir',
'./tmp/ad-state',
'--daemon-base-url',
'https://remote-mac.example.test:7777/agent-device',
'--daemon-auth-token',
'remote-secret',
'--daemon-transport',
'http',
'--daemon-server-mode',
'dual',
'--tenant',
'team_alpha',
'--session-isolation',
'tenant',
'--run-id',
'run_42',
'--lease-id',
'abcd1234ef567890',
],
{ strictFlags: true },
);
assert.equal(parsed.flags.stateDir, './tmp/ad-state');
assert.equal(parsed.flags.daemonBaseUrl, 'https://remote-mac.example.test:7777/agent-device');
assert.equal(parsed.flags.daemonAuthToken, 'remote-secret');
assert.equal(parsed.flags.daemonTransport, 'http');
assert.equal(parsed.flags.daemonServerMode, 'dual');
assert.equal(parsed.flags.tenant, 'team_alpha');
assert.equal(parsed.flags.sessionIsolation, 'tenant');
assert.equal(parsed.flags.runId, 'run_42');
assert.equal(parsed.flags.leaseId, 'abcd1234ef567890');
});
test('parseArgs recognizes connect lease backend force and no-login flags', () => {
const parsed = parseArgs(
[
'connect',
'--remote-config',
'./remote.json',
'--tenant',
'acme',
'--run-id',
'run-123',
'--lease-backend',
'android-instance',
'--force',
'--no-login',
],
{ strictFlags: true },
);
assert.equal(parsed.command, 'connect');
assert.equal(parsed.flags.remoteConfig, './remote.json');
assert.equal(parsed.flags.leaseBackend, 'android-instance');
assert.equal(parsed.flags.force, true);
assert.equal(parsed.flags.noLogin, true);
});
test('parseArgs accepts auth management subcommands', () => {
const status = parseArgs(['auth', 'status'], { strictFlags: true });
assert.equal(status.command, 'auth');
assert.deepEqual(status.positionals, ['status']);
const login = parseArgs(['auth', 'login', '--remote-config', './remote.json'], {
strictFlags: true,
});
assert.equal(login.command, 'auth');
assert.deepEqual(login.positionals, ['login']);
assert.equal(login.flags.remoteConfig, './remote.json');
});
test('parseArgs recognizes explicit config file flag', () => {
const parsed = parseArgs(['open', 'settings', '--config', './agent-device.json'], {
strictFlags: true,
});
assert.equal(parsed.command, 'open');
assert.equal(parsed.flags.config, './agent-device.json');
});
test('parseArgs recognizes session lock policy flag', () => {
const parsed = parseArgs(['snapshot', '--session-lock', 'strip'], { strictFlags: true });
assert.equal(parsed.command, 'snapshot');
assert.equal(parsed.flags.sessionLock, 'strip');
});
test('parseArgs keeps deprecated session lock aliases for compatibility', () => {
const parsed = parseArgs(['snapshot', '--session-locked', '--session-lock-conflicts', 'strip'], {
strictFlags: true,
});
assert.equal(parsed.command, 'snapshot');
assert.equal(parsed.flags.sessionLocked, true);
assert.equal(parsed.flags.sessionLockConflicts, 'strip');
});
test('batch requires exactly one step source', () => {
assert.throws(
() => parseArgs(['batch'], { strictFlags: true }),
/requires exactly one step source/,
);
assert.throws(
() =>
parseArgs(['batch', '--steps', '[]', '--steps-file', './steps.json'], { strictFlags: true }),
/requires exactly one step source/,
);
const inline = parseArgs(['batch', '--steps', '[]'], { strictFlags: true });
assert.equal(inline.command, 'batch');
assert.equal(inline.flags.steps, '[]');
assert.throws(
() => parseArgs(['batch', '--steps', '[]', '--on-error', 'continue'], { strictFlags: true }),
/Invalid on-error: continue/,
);
});
test('parseArgs accepts --save-script with optional path value', () => {
const withoutPath = parseArgs(['open', 'settings', '--save-script']);
assert.equal(withoutPath.command, 'open');
assert.deepEqual(withoutPath.positionals, ['settings']);
assert.equal(withoutPath.flags.saveScript, true);
const withPath = parseArgs(['open', 'settings', '--save-script', './workflows/my-flow.ad']);
assert.equal(withPath.command, 'open');
assert.deepEqual(withPath.positionals, ['settings']);
assert.equal(withPath.flags.saveScript, './workflows/my-flow.ad');
const nonPathPositional = parseArgs(['open', '--save-script', 'settings']);
assert.equal(nonPathPositional.command, 'open');
assert.deepEqual(nonPathPositional.positionals, ['settings']);
assert.equal(nonPathPositional.flags.saveScript, true);
const inlineValue = parseArgs(['open', 'settings', '--save-script=my-flow.ad']);
assert.equal(inlineValue.command, 'open');
assert.deepEqual(inlineValue.positionals, ['settings']);
assert.equal(inlineValue.flags.saveScript, 'my-flow.ad');
const ambiguousBareValue = parseArgs(['open', '--save-script', 'my-flow.ad']);
assert.equal(ambiguousBareValue.command, 'open');
assert.deepEqual(ambiguousBareValue.positionals, ['my-flow.ad']);
assert.equal(ambiguousBareValue.flags.saveScript, true);
});
test('parseArgs recognizes press series flags', () => {
const parsed = parseArgs([
'press',
'300',
'500',
'--count',
'12',
'--interval-ms=45',
'--hold-ms',
'120',
'--jitter-px',
'3',
]);
assert.equal(parsed.command, 'press');
assert.deepEqual(parsed.positionals, ['300', '500']);
assert.equal(parsed.flags.count, 12);
assert.equal(parsed.flags.intervalMs, 45);
assert.equal(parsed.flags.holdMs, 120);
assert.equal(parsed.flags.jitterPx, 3);
});
test('parseArgs recognizes press selector + snapshot flags', () => {
const parsed = parseArgs(['press', '@e2', '--depth', '3', '--scope', 'Sign In', '--raw'], {
strictFlags: true,
});
assert.equal(parsed.command, 'press');
assert.deepEqual(parsed.positionals, ['@e2']);
assert.equal(parsed.flags.snapshotDepth, 3);
assert.equal(parsed.flags.snapshotScope, 'Sign In');
assert.equal(parsed.flags.snapshotRaw, true);
});
test('parseArgs recognizes click series flags', () => {
const parsed = parseArgs(['click', '@e5', '--count', '4', '--interval-ms', '10'], {
strictFlags: true,
});
assert.equal(parsed.command, 'click');
assert.deepEqual(parsed.positionals, ['@e5']);
assert.equal(parsed.flags.count, 4);
assert.equal(parsed.flags.intervalMs, 10);
});
test('parseArgs recognizes click button flag', () => {
const parsed = parseArgs(['click', '@e5', '--button', 'secondary'], {
strictFlags: true,
});
assert.equal(parsed.command, 'click');
assert.deepEqual(parsed.positionals, ['@e5']);
assert.equal(parsed.flags.clickButton, 'secondary');
});
test('parseArgs recognizes double-tap flag for repeated press', () => {
const parsed = parseArgs(['press', '201', '545', '--count', '5', '--double-tap'], {
strictFlags: true,
});
assert.equal(parsed.command, 'press');
assert.deepEqual(parsed.positionals, ['201', '545']);
assert.equal(parsed.flags.count, 5);
assert.equal(parsed.flags.doubleTap, true);
});
test('parseArgs recognizes swipe positional + pattern flags', () => {
const parsed = parseArgs([
'swipe',
'540',
'1500',
'540',
'500',
'120',
'--count',
'8',
'--pause-ms',
'30',
'--pattern',
'ping-pong',
]);
assert.equal(parsed.command, 'swipe');
assert.deepEqual(parsed.positionals, ['540', '1500', '540', '500', '120']);
assert.equal(parsed.flags.count, 8);
assert.equal(parsed.flags.pauseMs, 30);
assert.equal(parsed.flags.pattern, 'ping-pong');
});
test('parseArgs recognizes gesture subcommand positionals', () => {
const pan = parseArgs(['gesture', 'pan', '200', '420', '0', '-80', '500'], {
strictFlags: true,
});
assert.equal(pan.command, 'gesture');
assert.deepEqual(pan.positionals, ['pan', '200', '420', '0', '-80', '500']);
const fling = parseArgs(['gesture', 'fling', 'right', '200', '420', '180'], {
strictFlags: true,
});
assert.equal(fling.command, 'gesture');
assert.deepEqual(fling.positionals, ['fling', 'right', '200', '420', '180']);
const rotate = parseArgs(['gesture', 'rotate', '35', '200', '420'], {
strictFlags: true,
});
assert.equal(rotate.command, 'gesture');
assert.deepEqual(rotate.positionals, ['rotate', '35', '200', '420']);
const transform = parseArgs(['gesture', 'transform', '200', '420', '80', '-40', '2', '35'], {
strictFlags: true,
});
assert.equal(transform.command, 'gesture');
assert.deepEqual(transform.positionals, ['transform', '200', '420', '80', '-40', '2', '35']);
});
test('parseArgs recognizes type and fill delay flags', () => {
const typeParsed = parseArgs(['type', 'hello', '--delay-ms', '75'], {
strictFlags: true,
});
assert.equal(typeParsed.command, 'type');
assert.deepEqual(typeParsed.positionals, ['hello']);
assert.equal(typeParsed.flags.delayMs, 75);
const fillParsed = parseArgs(['fill', '@e5', 'search', '--delay-ms', '40'], {
strictFlags: true,
});
assert.equal(fillParsed.command, 'fill');
assert.deepEqual(fillParsed.positionals, ['@e5', 'search']);
assert.equal(fillParsed.flags.delayMs, 40);
});
test('parseArgs recognizes record --fps flag', () => {
const parsed = parseArgs(['record', 'start', './capture.mp4', '--fps', '30'], {
strictFlags: true,
});
assert.equal(parsed.command, 'record');
assert.deepEqual(parsed.positionals, ['start', './capture.mp4']);
assert.equal(parsed.flags.fps, 30);
});
test('parseArgs recognizes record --quality flag', () => {
const parsed = parseArgs(['record', 'start', './capture.mp4', '--quality', '7'], {
strictFlags: true,
});
assert.equal(parsed.command, 'record');
assert.deepEqual(parsed.positionals, ['start', './capture.mp4']);
assert.equal(parsed.flags.quality, 7);
});
test('parseArgs accepts record --quality boundaries', () => {
const parsedMin = parseArgs(['record', 'start', './capture.mp4', '--quality', '5'], {
strictFlags: true,
});
assert.equal(parsedMin.flags.quality, 5);
const parsedMax = parseArgs(['record', 'start', './capture.mp4', '--quality', '10'], {
strictFlags: true,
});
assert.equal(parsedMax.flags.quality, 10);
});
test('parseArgs recognizes record --hide-touches flag', () => {
const parsed = parseArgs(['record', 'start', './capture.mp4', '--hide-touches'], {
strictFlags: true,
});
assert.equal(parsed.command, 'record');
assert.deepEqual(parsed.positionals, ['start', './capture.mp4']);
assert.equal(parsed.flags.hideTouches, true);
});
test('parseArgs recognizes screenshot flags', () => {
const parsed = parseArgs(
['screenshot', 'page.png', '--fullscreen', '--max-size', '1024', '--no-stabilize'],
{
strictFlags: true,
},
);
assert.equal(parsed.command, 'screenshot');
assert.deepEqual(parsed.positionals, ['page.png']);
assert.equal(parsed.flags.screenshotFullscreen, true);
assert.equal(parsed.flags.screenshotMaxSize, 1024);
assert.equal(parsed.flags.screenshotNoStabilize, true);
});
test('usageForCommand documents screenshot stabilization tradeoff', () => {
const help = usageForCommand('screenshot');
if (help === null) throw new Error('Expected screenshot help text');
assert.match(help, /--no-stabilize/);
assert.match(help, /low-latency Android capture loops/);
});
test('parseArgs rejects invalid record --fps range', () => {
assert.throws(
() => parseArgs(['record', 'start', './capture.mp4', '--fps', '0'], { strictFlags: true }),
(error) =>
error instanceof AppError &&
error.code === 'INVALID_ARGS' &&
error.message === 'Invalid fps: 0',
);
});
test('parseArgs rejects invalid record --quality range', () => {
assert.throws(
() => parseArgs(['record', 'start', './capture.mp4', '--quality', '4'], { strictFlags: true }),
(error) =>
error instanceof AppError &&
error.code === 'INVALID_ARGS' &&
error.message === 'Invalid quality: 4',
);
});
test('parseArgs recognizes longpress command', () => {
const parsed = parseArgs(['longpress', '300', '500', '800'], { strictFlags: true });
assert.equal(parsed.command, 'longpress');
assert.deepEqual(parsed.positionals, ['300', '500', '800']);
});
test('parseArgs supports legacy long-press alias', () => {
const parsed = parseArgs(['long-press', '300', '500', '800'], { strictFlags: true });
assert.equal(parsed.command, 'longpress');
assert.deepEqual(parsed.positionals, ['300', '500', '800']);
});
test('parseArgs supports metrics alias for perf', () => {
const parsed = parseArgs(['metrics'], { strictFlags: true });
assert.equal(parsed.command, 'perf');
assert.deepEqual(parsed.positionals, []);
});
test('parseArgs supports trigger-app-event payload argument', () => {
const parsed = parseArgs(['trigger-app-event', 'screenshot_taken', '{"source":"qa"}'], {
strictFlags: true,
});
assert.equal(parsed.command, 'trigger-app-event');
assert.deepEqual(parsed.positionals, ['screenshot_taken', '{"source":"qa"}']);
});
test('parseArgs accepts rotate orientation aliases', () => {
const parsed = parseArgs(['rotate', 'left'], { strictFlags: true });
assert.equal(parsed.command, 'rotate');
assert.deepEqual(parsed.positionals, ['left']);
});
test('usageForCommand resolves longpress help', () => {
const help = usageForCommand('longpress');
assert.equal(help === null, false);
assert.match(help ?? '', /agent-device longpress <x y\|@ref\|selector> \[durationMs\]/);
});
test('usageForCommand supports legacy long-press alias', () => {
const help = usageForCommand('long-press');
assert.equal(help === null, false);
assert.match(help ?? '', /agent-device longpress <x y\|@ref\|selector> \[durationMs\]/);
assert.doesNotMatch(help ?? '', /agent-device long-press/);
});
test('usageForCommand supports metrics alias', () => {
const help = usageForCommand('metrics');
assert.equal(help === null, false);
assert.match(help ?? '', /agent-device perf/);
});
test('parseArgs rejects invalid swipe pattern', () => {
assert.throws(
() => parseArgs(['swipe', '0', '0', '10', '10', '--pattern', 'diagonal']),
/Invalid pattern/,
);
});
test('parseArgs rejects conflicting back mode flags', () => {
assert.throws(
() => parseArgs(['back', '--in-app', '--system'], { strictFlags: true }),
(error) =>
error instanceof AppError &&
error.code === 'INVALID_ARGS' &&
error.message ===
'back accepts only one explicit mode flag: use either --in-app or --system.',
);
});
test('usage includes concise top-level commands', () => {
const usageText = usage();
assert.match(
usageText,
/install-from-source <url> \| install-from-source --github-actions-artifact/,
);
assert.match(usageText, /metro prepare --public-base-url <url>/);
assert.match(usageText, /batch --steps <json> \| --steps-file <path>/);
assert.match(usageText, /network dump/);
assert.match(usageText, /clipboard read \| clipboard write <text>/);
assert.match(usageText, /keyboard \[action\]/);
assert.match(usageText, /trigger-app-event <event> \[payloadJson\]/);
assert.match(usageText, /gesture <pan\|fling\|pinch\|rotate\|transform> \.\.\./);
assert.doesNotMatch(usageText, /^ pan <x> <y> <dx> <dy> \[durationMs\]/m);
assert.doesNotMatch(usageText, /^ fling <up\|down\|left\|right>/m);
assert.doesNotMatch(usageText, /^ pinch <scale> \[x\] \[y\]/m);
assert.doesNotMatch(usageText, /^ rotate-gesture <degrees>/m);
assert.match(usageText, /rotate <orientation>/);
assert.match(usageText, /record start \[path\] \| record stop/);
assert.match(usageText, /trace start <path> \| trace stop <path>/);
});
test('usage includes only global flags in the top-level flags section', () => {
const usageText = usage();
const flagsSection = usageText.slice(
usageText.indexOf('Flags:'),
usageText.indexOf('Agent Quickstart:'),
);
assert.match(flagsSection, /--target mobile\|tv/);
assert.match(flagsSection, /--ios-simulator-device-set <path>/);
assert.match(flagsSection, /--android-device-allowlist <serials>/);
assert.match(flagsSection, /--state-dir <path>/);
assert.match(flagsSection, /--daemon-transport auto\|socket\|http/);
assert.match(flagsSection, /--daemon-server-mode socket\|http\|dual/);
assert.match(flagsSection, /--tenant <id>/);
assert.match(flagsSection, /--session-isolation none\|tenant/);
assert.match(flagsSection, /--run-id <id>/);
assert.match(flagsSection, /--lease-id <id>/);
assert.match(flagsSection, /--lease-backend ios-simulator\|ios-instance\|android-instance/);
assert.doesNotMatch(flagsSection, /--relaunch/);
assert.doesNotMatch(flagsSection, /--header <name:value>/);
assert.doesNotMatch(flagsSection, /--restart/);
assert.doesNotMatch(flagsSection, /--fps <n>/);
assert.doesNotMatch(flagsSection, /--quality <5-10>/);
assert.doesNotMatch(flagsSection, /--save-script \[path\]/);
assert.doesNotMatch(flagsSection, /--metadata/);
});
test('usage includes agent workflows, config, environment, and examples footers', () => {
const usageText = usage();
assert.match(usageText, /Agent Quickstart:/);
assert.match(usageText, /Default loop: devices\/apps -> open -> snapshot -i/);
assert.match(usageText, /Use selectors or refs as positional targets/);
assert.match(
usageText,
/Plain snapshot reads state; snapshot -i refreshes current interactive refs only/,
);
assert.match(usageText, /agent-facing, token-efficient view for planning and targeting actions/);
assert.match(usageText, /Truncated text\/input preview: expand first with snapshot -s @e12/);
assert.match(usageText, /React Native apps: read help react-native/);
assert.match(usageText, /adb reverse tcp:<port> tcp:<port> is harmless/);
assert.match(usageText, /Expo Go\/dev clients: use the provided URL when given/);
assert.match(usageText, /on iOS prefer open "Expo Go" <url>/);
assert.match(usageText, /Install flows: install\/install-from-source first/);
assert.match(usageText, /fill 'id="field-email"' "qa@example\.com" replaces/);
assert.match(usageText, /do not use fill <target> ""/);
assert.match(usageText, /Android IME capture: if fill says input was captured/);
assert.match(usageText, /Run mutating commands serially against one session/);
assert.match(usageText, /run session list and reuse the active session name/);
assert.match(usageText, /After mutation: refs are stale/);
assert.match(usageText, /use its selector directly; otherwise refresh with snapshot -i/);
assert.match(usageText, /app-owned back uses back/);
assert.match(usageText, /logs clear --restart\/mark\/path/);
assert.match(usageText, /trace start \.\/path; trace stop \.\/path/);
assert.match(usageText, /network dump --include headers/);
assert.match(usageText, /Full operating guide: agent-device help workflow/);
assert.match(usageText, /Exploratory QA: agent-device help dogfood/);
assert.match(usageText, /Agent Workflows:/);
assert.match(usageText, /help workflow\s+Normal bootstrap, exploration, and validation loop/);
assert.match(usageText, /help debugging\s+Logs, network, alerts, diagnostics, and traces/);
assert.match(
usageText,
/help react-devtools\s+React Native performance, profiling, component tree, and renders/,
);
assert.match(
usageText,
/help react-native\s+React Native app automation hazards, overlays, Metro, and routing/,
);
assert.match(usageText, /Configuration:/);
assert.match(
usageText,
/Default config files: ~\/\.agent-device\/config\.json, \.\/agent-device\.json/,
);
assert.match(
usageText,
/Use --config <path> or AGENT_DEVICE_CONFIG to load one explicit config file\./,
);
assert.match(usageText, /Environment:/);
assert.match(usageText, /AGENT_DEVICE_SESSION\s+Default session name/);
assert.match(usageText, /AGENT_DEVICE_PLATFORM\s+Default platform binding/);
assert.match(usageText, /AGENT_DEVICE_SESSION_LOCK\s+Bound-session conflict mode/);
assert.match(usageText, /AGENT_DEVICE_DAEMON_BASE_URL\s+Connect to remote daemon/);
assert.match(usageText, /Examples:/);
assert.match(usageText, /agent-device open Settings --platform ios/);
assert.match(usageText, /agent-device snapshot -i/);
assert.match(usageText, /agent-device fill @e3 "test@example\.com"/);
assert.match(usageText, /agent-device replay \.\/session\.ad/);
assert.match(usageText, /agent-device test \.\/suite --platform android/);
});
test('usageForCommand includes Maestro replay flag', () => {
const help = usageForCommand('replay');
if (help === null) throw new Error('Expected replay help text');
assert.match(help, /--maestro/);
assert.match(help, /doubleTapOn/);
assert.match(help, /pasteText/);
assert.match(help, /setPermissions/);
assert.match(help, /startRecording\/stopRecording/);
assert.match(help, /runFlow file\/inline/);
assert.match(help, /repeat\.times/);
assert.match(help, /Unsupported syntax fails loudly/);
assert.match(help, /issues\/558/);
});
test('usageForCommand resolves workflow help topic', () => {
const help = usageForCommand('workflow');
if (help === null) throw new Error('Expected workflow help text');
assert.match(help, /agent-device help workflow/);
assert.match(help, /Use selectors as positional targets/);
assert.match(help, /Do not use CSS selectors/);
assert.match(help, /Snapshot legend:/);
assert.match(help, /@e12 \[button\] label="Add to cart"/);
assert.match(help, /Truncated text\/input previews: do not use get text first/);
assert.match(help, /snapshot -s @e7/);
assert.match(help, /Read-only visible\/state question: use snapshot\/get\/is\/find/);
assert.match(help, /Use snapshot -i only when refs are needed/);
assert.match(help, /install-from-source --github-actions-artifact org\/repo:app-debug/);
assert.match(help, /Discovery is not enough when the task asks to open\/start/);
assert.match(help, /If the task says install, use install/);
assert.match(help, /Do not open artifact paths or invent package ids/);
assert.match(help, /agent-device get attrs @e4/);
assert.match(help, /Ambiguous find: add --first or --last/);
assert.match(help, /report that gap instead of typing\/searching\/navigating/);
assert.match(help, /App-owned action sheets, menus, and camera\/scan screens are normal UI/);
assert.match(help, /wait for a concrete result before returning to chat\/form state/);
assert.match(help, /choose a point near the center of the intended app-owned target/);
assert.match(help, /Avoid screen edges, tab bars, navigation bars, and home indicators/);
assert.match(help, /Android transform injects a geometric two-finger path/);
assert.match(help, /verify qualitative state such as "pan changed yes"/);
assert.match(help, /prefer isolated gesture pan, gesture pinch, or gesture rotate/);
assert.match(help, /longpress accepts coordinates, @refs, or selectors/);
assert.match(help, /use help react-native for Metro\/Fast Refresh/);
assert.match(help, /iOS Allow Paste prompt cannot be exercised under XCUITest/);
assert.match(help, /Empty replacement is not a supported clear-field command/);
assert.match(help, /do not plan fill <target> ""/);
assert.match(help, /prefer keyboard dismiss before manually pressing visible Done/);
assert.match(help, /UNSUPPORTED_OPERATION/);
assert.match(help, /Stateful commands against one --session must run serially/);
assert.match(
help,
/Do not run open\/press\/fill\/type\/scroll\/back\/alert\/replay\/batch\/close commands in parallel/,
);
assert.match(help, /agent-device clipboard write "some text"/);
assert.match(help, /For gesture-heavy iOS simulator proof videos, prefer --hide-touches/);
assert.match(help, /Android Gboard handwriting\/stylus UI can capture text/);
assert.match(help, /targetInput\/actualInput details/);
assert.match(help, /Do not keep retrying fill\/type against the same field/);
assert.match(help, /provider-native text injection when available/);
assert.match(help, /Do not switch to raw adb, clipboard, or paste as an agent fallback/);
assert.match(help, /if no URL is provided but a target\/app name is provided, open that target/);
assert.match(help, /adb reverse tcp:<port> tcp:<port> before opening the app or URL/);
assert.match(help, /do not split clear\/restart/);
assert.match(help, /do not write network log headers/);
assert.match(help, /agent-device open exp:\/\/127\.0\.0\.1:8081 --platform ios/);
assert.match(help, /agent-device open "Expo Go" exp:\/\/127\.0\.0\.1:8081 --platform ios/);
assert.match(help, /There is no open-url command/);
assert.match(help, /direct URL open can report success while leaving the runner\/shell focused/);
assert.match(help, /verify with snapshot -i after opening/);
assert.match(help, /agent-device open exp:\/\/127\.0\.0\.1:8081 --platform android/);
assert.match(help, /apps lookup misses the project but shows Expo Go\/dev-client/);
assert.match(help, /metro prepare --kind expo/);
assert.match(help, /help react-devtools/);
assert.match(help, /help react-native/);
assert.doesNotMatch(help, /agent-device react-devtools profile/);
});
test('workflow help keeps common copyable command forms', () => {
const help = usageForCommand('workflow');
if (help === null) throw new Error('Expected workflow help text');
assert.match(help, /network dump --include headers/);
assert.match(help, /settings animations off/);
assert.match(help, /connect --remote-config/);
assert.match(help, /metro reload/);
assert.match(help, /screenshot --overlay-refs/);
assert.match(help, /snapshot -s @e7/);
assert.match(help, /clipboard write "some text"/);
});