Repository navigation
Expand file tree
/
Copy pathalert.ts
More file actions
84 lines (74 loc) · 3.4 KB
/
Copy pathalert.ts
File metadata and controls
84 lines (74 loc) · 3.4 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
import { ALERT_ACTIONS, type AlertAction } from '@agent-device/contracts/alert-contract';
import { PUBLIC_COMMANDS } from '@agent-device/command-registry/catalog';
import type { AlertCommandOptions } from '@agent-device/contracts/client';
import { enumField, integerField } from '../command-input.ts';
import { compactRecord } from '../input-readers.ts';
import {
commonInputFromFlags,
direct,
optionalNumber,
readFiniteNumber,
} from '../cli-grammar/common.ts';
import type { CliReader, DaemonWriter } from '../cli-grammar/types.ts';
import { defineCommandFacet } from '../family/types.ts';
import { defineFieldCommandMetadata } from '../field-command-contract.ts';
import { messageOutput } from '../output-common.ts';
import { AppError } from '@agent-device/kernel/errors';
const ALERT_COMMAND_NAME = 'alert';
const alertCommandDescription =
'Examine, wait for, accept, or dismiss a platform alert. If the alert content is important, use get before you act. accept and dismiss change the active alert state. Each iOS XCTest execution activates once, and then only observes. On the Apple XCUITest runner, dismiss taps only a recognized decline label (Cancel, Don\'t Allow, Ask App Not to Track, and similar) or the close element of a popover whose accessibility marker reads exactly "dismiss popup"; when no button fits the action, accept and dismiss answer ALERT_BUTTON_NOT_FOUND having tapped nothing — read labels with alert get, then press the button by label. After an unconfirmed action, examine the alert again. Never assume that a timeout means that nothing occurred.';
const alertCommandMetadata = defineFieldCommandMetadata(
ALERT_COMMAND_NAME,
alertCommandDescription,
{
action: enumField(ALERT_ACTIONS),
timeoutMs: integerField(),
},
);
const alertCliSchema = {
usageOverride: 'alert [get|accept|dismiss|wait] [timeout]',
positionalArgs: ['action?', 'timeout?'],
} as const;
export const alertCliReader: CliReader = (positionals, flags) => ({
...commonInputFromFlags(flags),
...readAlertInput(positionals),
});
export const alertDaemonWriter: DaemonWriter = direct(PUBLIC_COMMANDS.alert, (input) =>
alertPositionals(input as AlertCommandOptions),
);
export const alertCommandFacet = defineCommandFacet({
name: ALERT_COMMAND_NAME,
text: {
summary: 'Examine, accept, or dismiss a platform alert',
},
metadata: alertCommandMetadata,
run: (client, input) => client.command.alert(input),
cliSchema: alertCliSchema,
cliReader: alertCliReader,
daemonWriter: alertDaemonWriter,
cliOutputFormatter: messageOutput,
});
function alertPositionals(input: AlertCommandOptions): string[] {
return [input.action ?? 'get', ...optionalNumber(input.timeoutMs)];
}
function readAlertInput(positionals: string[]): Record<string, unknown> {
if (positionals.length > 2) {
throw new AppError('INVALID_ARGS', 'alert accepts at most action and timeout arguments.');
}
const action = readAlertAction(positionals[0]);
const timeoutMs = readFiniteNumber(positionals[1], 'alert timeout');
return compactRecord({ action, timeoutMs });
}
function readAlertAction(value: string | undefined): AlertAction | undefined {
const action = value?.toLowerCase();
if (
action === undefined ||
action === 'get' ||
action === 'accept' ||
action === 'dismiss' ||
action === 'wait'
) {
return action;
}
throw new AppError('INVALID_ARGS', 'alert action must be get, accept, dismiss, or wait.');
}