-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathcommand-mapper.ts
More file actions
183 lines (172 loc) · 6.54 KB
/
Copy pathcommand-mapper.ts
File metadata and controls
183 lines (172 loc) · 6.54 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
import type { SessionAction } from '../../daemon/types.ts';
import { AppError } from '../../utils/errors.ts';
import { convertLaunchApp, convertStopApp } from './device-actions.ts';
import {
convertDoubleTapOn,
convertEraseText,
convertExtendedWaitUntil,
convertLongPressOn,
convertPressKey,
convertScroll,
convertScrollUntilVisible,
convertSwipe,
convertTapOn,
maestroSelector,
readInputText,
} from './interactions.ts';
import {
action,
assertOnlyKeys,
isPlainRecord,
readTimeoutMs,
requireAppId,
requireStringValue,
resolveMaestroString,
unsupportedCommand,
} from './support.ts';
import { convertRepeat, convertRetry, convertRunFlow } from './flow-control.ts';
import { convertRunScript } from './run-script.ts';
import { MAESTRO_RUNTIME_COMMAND } from './runtime-commands.ts';
import type {
MaestroCommand,
MaestroCommandMapperDeps,
MaestroFlowConfig,
MaestroParseContext,
} from './types.ts';
type MaestroCommandHandler = (params: {
value: unknown;
config: MaestroFlowConfig;
context: MaestroParseContext;
deps: MaestroCommandMapperDeps;
name: string;
}) => SessionAction[];
const MAP_COMMAND_HANDLERS: Record<string, MaestroCommandHandler> = {
launchApp: ({ value, config, context }) => [convertLaunchApp(value, config, context)],
tapOn: ({ value, context }) => [convertTapOn(value, context)],
doubleTapOn: ({ value, context }) => [convertDoubleTapOn(value, context)],
longPressOn: ({ value, context }) => [convertLongPressOn(value, context)],
inputText: ({ value, context }) => [
action('type', [resolveMaestroString(readInputText(value), context)]),
],
eraseText: ({ value }) => [convertEraseText(value)],
pasteText: ({ value, context, name }) => [
action('type', [resolveMaestroString(requireStringValue(name, value), context)]),
],
openLink: ({ value, config, context, name }) => [convertOpenLink(value, config, context, name)],
assertVisible: ({ value, context, name }) => [
action(MAESTRO_RUNTIME_COMMAND.assertVisible, [
maestroSelector(value, name, [], context),
'7000',
]),
],
assertNotVisible: ({ value, context, name }) => [
action(MAESTRO_RUNTIME_COMMAND.assertNotVisible, [maestroSelector(value, name, [], context)]),
],
extendedWaitUntil: ({ value, context }) => convertExtendedWaitUntil(value, context),
takeScreenshot: ({ value, context, name }) => [
action('screenshot', [resolveMaestroString(requireStringValue(name, value), context)]),
],
scroll: ({ value }) => [convertScroll(value)],
scrollUntilVisible: ({ value, context }) => convertScrollUntilVisible(value, context),
swipe: ({ value, context }) => [convertSwipe(value, context)],
hideKeyboard: () => [action('keyboard', ['dismiss'])],
pressKey: ({ value }) => [convertPressKey(value)],
back: () => [action('back')],
waitForAnimationToEnd: ({ value }) => [
action(MAESTRO_RUNTIME_COMMAND.waitForAnimationToEnd, [String(readTimeoutMs(value, 15000))]),
],
stopApp: ({ value, config, context }) => [convertStopApp(value, config, context)],
runScript: ({ value, context }) => [convertRunScript(value, context)],
runFlow: ({ value, config, context, deps }) =>
convertRunFlow(value, config, context, deps, convertCommandList),
repeat: ({ value, config, context, deps }) =>
convertRepeat(value, config, context, deps, convertCommandList),
retry: ({ value, config, context, deps }) =>
convertRetry(value, config, context, deps, convertCommandList),
};
const SCALAR_COMMAND_HANDLERS: Record<
string,
(config: MaestroFlowConfig, context: MaestroParseContext) => SessionAction[]
> = {
launchApp: (config, context) => [convertLaunchApp(undefined, config, context)],
scroll: () => [action('scroll', ['down'])],
hideKeyboard: () => [action('keyboard', ['dismiss'])],
eraseText: () => [convertEraseText(undefined)],
back: () => [action('back')],
waitForAnimationToEnd: () => [action(MAESTRO_RUNTIME_COMMAND.waitForAnimationToEnd, ['15000'])],
stopApp: (config, context) => [convertStopApp(undefined, config, context)],
};
export function convertMaestroCommandWithLine(
command: MaestroCommand,
config: MaestroFlowConfig,
line: number,
context: MaestroParseContext,
deps: MaestroCommandMapperDeps,
): SessionAction[] {
try {
return convertMaestroCommand(command, config, context, deps);
} catch (error) {
if (error instanceof AppError && !/\bline \d+\b/.test(error.message)) {
throw new AppError(error.code, `${error.message} (line ${line})`, error.details);
}
throw error;
}
}
function convertMaestroCommand(
command: MaestroCommand,
config: MaestroFlowConfig,
context: MaestroParseContext,
deps: MaestroCommandMapperDeps,
): SessionAction[] {
if (typeof command === 'string') return convertScalarCommand(command, config, context);
const entries = Object.entries(command);
if (entries.length !== 1) {
throw new AppError('INVALID_ARGS', 'Maestro command maps must contain exactly one command.');
}
const [name, value] = entries[0] as [string, unknown];
const handler = MAP_COMMAND_HANDLERS[name];
if (!handler) return unsupportedCommand(name);
return handler({ value, config, context, deps, name });
}
function convertScalarCommand(
command: string,
config: MaestroFlowConfig,
context: MaestroParseContext,
): SessionAction[] {
const handler = SCALAR_COMMAND_HANDLERS[command];
if (!handler) return unsupportedCommand(command);
return handler(config, context);
}
function convertOpenLink(
value: unknown,
config: MaestroFlowConfig,
context: MaestroParseContext,
name: string,
): SessionAction {
const rawLink = readOpenLink(value, name);
const url = resolveMaestroString(rawLink, context);
if ((context.platform === 'ios' || context.platform === 'android' || context.platform === 'harmonyos') && config.appId) {
return action(
'open',
[resolveMaestroString(requireAppId(config, name), context), url],
context.platform === 'ios' ? { maestro: { prewarmRunnerBeforeOpen: true } } : undefined,
);
}
return action('open', [url]);
}
function readOpenLink(value: unknown, name: string): string {
if (typeof value === 'string') return value;
if (!isPlainRecord(value)) return requireStringValue(name, value);
assertOnlyKeys(value, name, ['link']);
return requireStringValue(`${name}.link`, value.link);
}
function convertCommandList(
commands: MaestroCommand[],
config: MaestroFlowConfig,
context: MaestroParseContext,
deps: MaestroCommandMapperDeps,
): SessionAction[] {
return commands.flatMap((command, index) =>
convertMaestroCommandWithLine(command, config, index + 1, context, deps),
);
}