-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathargs.test.ts
More file actions
290 lines (256 loc) · 11 KB
/
args.test.ts
File metadata and controls
290 lines (256 loc) · 11 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
import test from 'node:test';
import assert from 'node:assert/strict';
import { parseArgs, toDaemonFlags, 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 --relaunch', () => {
const parsed = parseArgs(['open', 'settings', '--relaunch']);
assert.equal(parsed.command, 'open');
assert.deepEqual(parsed.positionals, ['settings']);
assert.equal(parsed.flags.relaunch, true);
});
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('toDaemonFlags strips CLI-only flags', () => {
const parsed = parseArgs(['open', 'settings', '--json']);
const daemonFlags = toDaemonFlags(parsed.flags);
assert.equal(Object.hasOwn(daemonFlags, 'json'), false);
assert.equal(Object.hasOwn(daemonFlags, 'help'), false);
assert.equal(Object.hasOwn(daemonFlags, 'version'), false);
});
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 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 rejects invalid swipe pattern', () => {
assert.throws(
() => parseArgs(['swipe', '0', '0', '10', '10', '--pattern', 'diagonal']),
/Invalid pattern/,
);
});
test('usage includes --relaunch flag', () => {
assert.match(usage(), /--relaunch/);
assert.match(usage(), /--save-script \[path\]/);
assert.match(usage(), /pinch <scale> \[x\] \[y\]/);
assert.doesNotMatch(usage(), /--metadata/);
});
test('apps defaults to --all filter and allows overrides', () => {
const defaultFilter = parseArgs(['apps'], { strictFlags: true });
assert.equal(defaultFilter.command, 'apps');
assert.equal(defaultFilter.flags.appsFilter, 'all');
const userInstalled = parseArgs(['apps', '--user-installed'], { strictFlags: true });
assert.equal(userInstalled.command, 'apps');
assert.equal(userInstalled.flags.appsFilter, 'user-installed');
});
test('every capability command has a parser schema entry', () => {
const schemaCommands = new Set(getCliCommandNames());
for (const command of listCapabilityCommands()) {
assert.equal(schemaCommands.has(command), true, `Missing schema for command: ${command}`);
}
});
test('schema capability mappings match capability source-of-truth', () => {
assert.deepEqual(getSchemaCapabilityKeys(), listCapabilityCommands());
});
test('compat mode warns and strips unsupported command flags', () => {
const parsed = parseArgs(['press', '10', '20', '--pause-ms', '2'], { strictFlags: false });
assert.equal(parsed.command, 'press');
assert.equal(parsed.flags.pauseMs, undefined);
assert.equal(parsed.warnings.length, 1);
assert.match(parsed.warnings[0], /not supported for command press/);
});
test('strict mode rejects unsupported pilot-command flags', () => {
assert.throws(
() => parseArgs(['press', '10', '20', '--pause-ms', '2'], { strictFlags: true }),
(error) =>
error instanceof AppError &&
error.code === 'INVALID_ARGS' &&
error.message.includes('not supported for command press'),
);
});
test('snapshot command accepts command-specific flags', () => {
const parsed = parseArgs(['snapshot', '-i', '-c', '--depth', '3', '-s', 'Login'], { strictFlags: true });
assert.equal(parsed.command, 'snapshot');
assert.equal(parsed.flags.snapshotInteractiveOnly, true);
assert.equal(parsed.flags.snapshotCompact, true);
assert.equal(parsed.flags.snapshotDepth, 3);
assert.equal(parsed.flags.snapshotScope, 'Login');
});
test('unknown short flags are rejected', () => {
assert.throws(
() => parseArgs(['press', '10', '20', '-x'], { strictFlags: true }),
(error) => error instanceof AppError && error.code === 'INVALID_ARGS' && error.message === 'Unknown flag: -x',
);
});
test('negative numeric positionals are accepted without -- separator', () => {
const typed = parseArgs(['type', '-123'], { strictFlags: true });
assert.equal(typed.command, 'type');
assert.deepEqual(typed.positionals, ['-123']);
const typedMulti = parseArgs(['type', '-123', '-456'], { strictFlags: true });
assert.equal(typedMulti.command, 'type');
assert.deepEqual(typedMulti.positionals, ['-123', '-456']);
const pressed = parseArgs(['press', '-10', '20'], { strictFlags: true });
assert.equal(pressed.command, 'press');
assert.deepEqual(pressed.positionals, ['-10', '20']);
});
test('command-specific flags without command fail in strict mode', () => {
assert.throws(
() => parseArgs(['--depth', '3'], { strictFlags: true }),
(error) =>
error instanceof AppError &&
error.code === 'INVALID_ARGS' &&
error.message.includes('requires a command that supports it'),
);
});
test('command-specific flags without command warn and strip in compat mode', () => {
const parsed = parseArgs(['--depth', '3'], { strictFlags: false });
assert.equal(parsed.command, null);
assert.equal(parsed.flags.snapshotDepth, undefined);
assert.equal(parsed.warnings.length, 1);
assert.match(parsed.warnings[0], /requires a command that supports/);
});
test('all commands participate in strict command-flag validation', () => {
assert.throws(
() => parseArgs(['open', 'Settings', '--depth', '1'], { strictFlags: true }),
(error) =>
error instanceof AppError &&
error.code === 'INVALID_ARGS' &&
error.message.includes('not supported for command open'),
);
});
test('invalid range errors are deterministic', () => {
assert.throws(
() => parseArgs(['snapshot', '--backend', 'xctest'], { strictFlags: true }),
(error) =>
error instanceof AppError && error.code === 'INVALID_ARGS' && error.message === 'Unknown flag: --backend',
);
assert.throws(
() => parseArgs(['snapshot', '--depth', '-1'], { strictFlags: true }),
(error) => error instanceof AppError && error.code === 'INVALID_ARGS' && error.message === 'Invalid depth: -1',
);
});
test('usage includes swipe and press series options', () => {
const help = usage();
assert.match(help, /swipe <x1> <y1> <x2> <y2>/);
assert.match(help, /--pattern one-way\|ping-pong/);
assert.match(help, /--interval-ms/);
assert.match(help, /settings <wifi\|airplane\|location\|faceid>/);
});
test('command usage shows command and global flags separately', () => {
const help = usageForCommand('swipe');
if (help === null) throw new Error('Expected command help text');
assert.match(help, /Swipe coordinates with optional repeat pattern/);
assert.match(help, /Command flags:/);
assert.match(help, /--pattern one-way\|ping-pong/);
assert.match(help, /Global flags:/);
assert.match(help, /--platform ios\|android/);
});
test('command usage shows no command flags when unsupported', () => {
const help = usageForCommand('appstate');
if (help === null) throw new Error('Expected command help text');
assert.match(help, /Show foreground app\/activity/);
assert.doesNotMatch(help, /Command flags:/);
assert.match(help, /Global flags:/);
});
test('settings usage documents canonical faceid states', () => {
const help = usageForCommand('settings');
if (help === null) throw new Error('Expected command help text');
assert.match(help, /match\|nonmatch\|enroll\|unenroll/);
assert.doesNotMatch(help, /validate\|unvalidate/);
});