agent-device 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/README.md +2 -9
  2. package/dist/src/797.js +1 -1
  3. package/dist/src/bin.js +5 -5
  4. package/dist/src/daemon.js +16 -20
  5. package/package.json +7 -9
  6. package/skills/agent-device/SKILL.md +3 -6
  7. package/skills/agent-device/references/permissions.md +3 -15
  8. package/skills/agent-device/references/snapshot-refs.md +1 -4
  9. package/dist/bin/axsnapshot +0 -0
  10. package/ios-runner/AXSnapshot/Package.swift +0 -18
  11. package/ios-runner/AXSnapshot/Sources/AXSnapshot/main.swift +0 -444
  12. package/src/__tests__/cli-close.test.ts +0 -155
  13. package/src/__tests__/cli-help.test.ts +0 -102
  14. package/src/bin.ts +0 -3
  15. package/src/cli.ts +0 -305
  16. package/src/core/__tests__/capabilities.test.ts +0 -75
  17. package/src/core/__tests__/dispatch-open.test.ts +0 -25
  18. package/src/core/__tests__/open-target.test.ts +0 -55
  19. package/src/core/capabilities.ts +0 -57
  20. package/src/core/dispatch.ts +0 -382
  21. package/src/core/open-target.ts +0 -27
  22. package/src/daemon/__tests__/device-ready.test.ts +0 -52
  23. package/src/daemon/__tests__/is-predicates.test.ts +0 -68
  24. package/src/daemon/__tests__/selectors.test.ts +0 -261
  25. package/src/daemon/__tests__/session-routing.test.ts +0 -108
  26. package/src/daemon/__tests__/session-selector.test.ts +0 -64
  27. package/src/daemon/__tests__/session-store.test.ts +0 -142
  28. package/src/daemon/__tests__/snapshot-processing.test.ts +0 -47
  29. package/src/daemon/action-utils.ts +0 -29
  30. package/src/daemon/context.ts +0 -48
  31. package/src/daemon/device-ready.ts +0 -155
  32. package/src/daemon/handlers/__tests__/find.test.ts +0 -99
  33. package/src/daemon/handlers/__tests__/interaction.test.ts +0 -22
  34. package/src/daemon/handlers/__tests__/replay-heal.test.ts +0 -509
  35. package/src/daemon/handlers/__tests__/session-reinstall.test.ts +0 -219
  36. package/src/daemon/handlers/__tests__/session.test.ts +0 -820
  37. package/src/daemon/handlers/__tests__/snapshot-handler.test.ts +0 -92
  38. package/src/daemon/handlers/__tests__/snapshot.test.ts +0 -128
  39. package/src/daemon/handlers/find.ts +0 -324
  40. package/src/daemon/handlers/interaction.ts +0 -550
  41. package/src/daemon/handlers/parse-utils.ts +0 -8
  42. package/src/daemon/handlers/record-trace.ts +0 -154
  43. package/src/daemon/handlers/session.ts +0 -1137
  44. package/src/daemon/handlers/snapshot.ts +0 -439
  45. package/src/daemon/is-predicates.ts +0 -46
  46. package/src/daemon/selectors.ts +0 -540
  47. package/src/daemon/session-routing.ts +0 -22
  48. package/src/daemon/session-selector.ts +0 -39
  49. package/src/daemon/session-store.ts +0 -296
  50. package/src/daemon/snapshot-processing.ts +0 -131
  51. package/src/daemon/types.ts +0 -56
  52. package/src/daemon-client.ts +0 -272
  53. package/src/daemon.ts +0 -295
  54. package/src/platforms/__tests__/boot-diagnostics.test.ts +0 -59
  55. package/src/platforms/android/__tests__/index.test.ts +0 -274
  56. package/src/platforms/android/devices.ts +0 -196
  57. package/src/platforms/android/index.ts +0 -784
  58. package/src/platforms/android/ui-hierarchy.ts +0 -312
  59. package/src/platforms/boot-diagnostics.ts +0 -128
  60. package/src/platforms/ios/__tests__/index.test.ts +0 -312
  61. package/src/platforms/ios/__tests__/runner-client.test.ts +0 -155
  62. package/src/platforms/ios/apps.ts +0 -358
  63. package/src/platforms/ios/ax-snapshot.ts +0 -207
  64. package/src/platforms/ios/config.ts +0 -28
  65. package/src/platforms/ios/devicectl.ts +0 -134
  66. package/src/platforms/ios/devices.ts +0 -100
  67. package/src/platforms/ios/index.ts +0 -20
  68. package/src/platforms/ios/runner-client.ts +0 -994
  69. package/src/platforms/ios/simulator.ts +0 -164
  70. package/src/utils/__tests__/args.test.ts +0 -239
  71. package/src/utils/__tests__/daemon-client.test.ts +0 -95
  72. package/src/utils/__tests__/exec.test.ts +0 -16
  73. package/src/utils/__tests__/finders.test.ts +0 -34
  74. package/src/utils/__tests__/keyed-lock.test.ts +0 -55
  75. package/src/utils/__tests__/process-identity.test.ts +0 -33
  76. package/src/utils/__tests__/retry.test.ts +0 -44
  77. package/src/utils/args.ts +0 -239
  78. package/src/utils/command-schema.ts +0 -622
  79. package/src/utils/device.ts +0 -84
  80. package/src/utils/errors.ts +0 -35
  81. package/src/utils/exec.ts +0 -339
  82. package/src/utils/finders.ts +0 -101
  83. package/src/utils/interactive.ts +0 -4
  84. package/src/utils/interactors.ts +0 -173
  85. package/src/utils/keyed-lock.ts +0 -14
  86. package/src/utils/output.ts +0 -204
  87. package/src/utils/process-identity.ts +0 -100
  88. package/src/utils/retry.ts +0 -180
  89. package/src/utils/snapshot.ts +0 -64
  90. package/src/utils/timeouts.ts +0 -9
  91. package/src/utils/version.ts +0 -26
@@ -1,35 +0,0 @@
1
- export type ErrorCode =
2
- | 'INVALID_ARGS'
3
- | 'DEVICE_NOT_FOUND'
4
- | 'TOOL_MISSING'
5
- | 'APP_NOT_INSTALLED'
6
- | 'UNSUPPORTED_PLATFORM'
7
- | 'UNSUPPORTED_OPERATION'
8
- | 'COMMAND_FAILED'
9
- | 'UNKNOWN';
10
-
11
- export class AppError extends Error {
12
- code: ErrorCode;
13
- details?: Record<string, unknown>;
14
- cause?: unknown;
15
-
16
- constructor(
17
- code: ErrorCode,
18
- message: string,
19
- details?: Record<string, unknown>,
20
- cause?: unknown,
21
- ) {
22
- super(message);
23
- this.code = code;
24
- this.details = details;
25
- this.cause = cause;
26
- }
27
- }
28
-
29
- export function asAppError(err: unknown): AppError {
30
- if (err instanceof AppError) return err;
31
- if (err instanceof Error) {
32
- return new AppError('UNKNOWN', err.message, undefined, err);
33
- }
34
- return new AppError('UNKNOWN', 'Unknown error', { err });
35
- }
package/src/utils/exec.ts DELETED
@@ -1,339 +0,0 @@
1
- import { spawn, spawnSync } from 'node:child_process';
2
- import { AppError } from './errors.ts';
3
-
4
- export type ExecResult = {
5
- stdout: string;
6
- stderr: string;
7
- exitCode: number;
8
- stdoutBuffer?: Buffer;
9
- };
10
-
11
- export type ExecOptions = {
12
- cwd?: string;
13
- env?: NodeJS.ProcessEnv;
14
- allowFailure?: boolean;
15
- binaryStdout?: boolean;
16
- stdin?: string | Buffer;
17
- timeoutMs?: number;
18
- };
19
-
20
- export type ExecStreamOptions = ExecOptions & {
21
- onStdoutChunk?: (chunk: string) => void;
22
- onStderrChunk?: (chunk: string) => void;
23
- };
24
-
25
- export type ExecBackgroundResult = {
26
- child: ReturnType<typeof spawn>;
27
- wait: Promise<ExecResult>;
28
- };
29
-
30
- export async function runCmd(
31
- cmd: string,
32
- args: string[],
33
- options: ExecOptions = {},
34
- ): Promise<ExecResult> {
35
- return new Promise((resolve, reject) => {
36
- const child = spawn(cmd, args, {
37
- cwd: options.cwd,
38
- env: options.env,
39
- stdio: ['pipe', 'pipe', 'pipe'],
40
- });
41
-
42
- let stdout = '';
43
- let stdoutBuffer: Buffer | undefined = options.binaryStdout ? Buffer.alloc(0) : undefined;
44
- let stderr = '';
45
- let didTimeout = false;
46
- const timeoutMs = normalizeTimeoutMs(options.timeoutMs);
47
- const timeoutHandle = timeoutMs
48
- ? setTimeout(() => {
49
- didTimeout = true;
50
- child.kill('SIGKILL');
51
- }, timeoutMs)
52
- : null;
53
-
54
- if (!options.binaryStdout) child.stdout.setEncoding('utf8');
55
- child.stderr.setEncoding('utf8');
56
-
57
- if (options.stdin !== undefined) {
58
- child.stdin.write(options.stdin);
59
- }
60
- child.stdin.end();
61
-
62
- child.stdout.on('data', (chunk) => {
63
- if (options.binaryStdout) {
64
- stdoutBuffer = Buffer.concat([
65
- stdoutBuffer ?? Buffer.alloc(0),
66
- Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk),
67
- ]);
68
- } else {
69
- stdout += chunk;
70
- }
71
- });
72
-
73
- child.stderr.on('data', (chunk) => {
74
- stderr += chunk;
75
- });
76
-
77
- child.on('error', (err) => {
78
- if (timeoutHandle) clearTimeout(timeoutHandle);
79
- const code = (err as NodeJS.ErrnoException).code;
80
- if (code === 'ENOENT') {
81
- reject(new AppError('TOOL_MISSING', `${cmd} not found in PATH`, { cmd }, err));
82
- return;
83
- }
84
- reject(new AppError('COMMAND_FAILED', `Failed to run ${cmd}`, { cmd, args }, err));
85
- });
86
-
87
- child.on('close', (code) => {
88
- if (timeoutHandle) clearTimeout(timeoutHandle);
89
- const exitCode = code ?? 1;
90
- if (didTimeout && timeoutMs) {
91
- reject(
92
- new AppError('COMMAND_FAILED', `${cmd} timed out after ${timeoutMs}ms`, {
93
- cmd,
94
- args,
95
- stdout,
96
- stderr,
97
- exitCode,
98
- timeoutMs,
99
- }),
100
- );
101
- return;
102
- }
103
- if (exitCode !== 0 && !options.allowFailure) {
104
- reject(
105
- new AppError('COMMAND_FAILED', `${cmd} exited with code ${exitCode}`, {
106
- cmd,
107
- args,
108
- stdout,
109
- stderr,
110
- exitCode,
111
- }),
112
- );
113
- return;
114
- }
115
- resolve({ stdout, stderr, exitCode, stdoutBuffer });
116
- });
117
- });
118
- }
119
-
120
- export async function whichCmd(cmd: string): Promise<boolean> {
121
- try {
122
- const { shell, args } = resolveWhichArgs(cmd);
123
- const result = await runCmd(shell, args, { allowFailure: true });
124
- return result.exitCode === 0 && result.stdout.trim().length > 0;
125
- } catch {
126
- return false;
127
- }
128
- }
129
-
130
- export function runCmdSync(cmd: string, args: string[], options: ExecOptions = {}): ExecResult {
131
- const result = spawnSync(cmd, args, {
132
- cwd: options.cwd,
133
- env: options.env,
134
- stdio: ['pipe', 'pipe', 'pipe'],
135
- encoding: options.binaryStdout ? undefined : 'utf8',
136
- input: options.stdin,
137
- timeout: normalizeTimeoutMs(options.timeoutMs),
138
- });
139
-
140
- if (result.error) {
141
- const code = (result.error as NodeJS.ErrnoException).code;
142
- if (code === 'ETIMEDOUT') {
143
- throw new AppError('COMMAND_FAILED', `${cmd} timed out after ${normalizeTimeoutMs(options.timeoutMs)}ms`, {
144
- cmd,
145
- args,
146
- timeoutMs: normalizeTimeoutMs(options.timeoutMs),
147
- }, result.error);
148
- }
149
- if (code === 'ENOENT') {
150
- throw new AppError('TOOL_MISSING', `${cmd} not found in PATH`, { cmd }, result.error);
151
- }
152
- throw new AppError('COMMAND_FAILED', `Failed to run ${cmd}`, { cmd, args }, result.error);
153
- }
154
-
155
- const stdoutBuffer = options.binaryStdout
156
- ? Buffer.isBuffer(result.stdout)
157
- ? result.stdout
158
- : Buffer.from(result.stdout ?? '')
159
- : undefined;
160
- const stdout = options.binaryStdout
161
- ? ''
162
- : typeof result.stdout === 'string'
163
- ? result.stdout
164
- : (result.stdout ?? '').toString();
165
- const stderr =
166
- typeof result.stderr === 'string' ? result.stderr : (result.stderr ?? '').toString();
167
- const exitCode = result.status ?? 1;
168
-
169
- if (exitCode !== 0 && !options.allowFailure) {
170
- throw new AppError('COMMAND_FAILED', `${cmd} exited with code ${exitCode}`, {
171
- cmd,
172
- args,
173
- stdout,
174
- stderr,
175
- exitCode,
176
- });
177
- }
178
-
179
- return { stdout, stderr, exitCode, stdoutBuffer };
180
- }
181
-
182
- export function runCmdDetached(cmd: string, args: string[], options: ExecOptions = {}): void {
183
- const child = spawn(cmd, args, {
184
- cwd: options.cwd,
185
- env: options.env,
186
- stdio: 'ignore',
187
- detached: true,
188
- });
189
- child.unref();
190
- }
191
-
192
- export async function runCmdStreaming(
193
- cmd: string,
194
- args: string[],
195
- options: ExecStreamOptions = {},
196
- ): Promise<ExecResult> {
197
- return new Promise((resolve, reject) => {
198
- const child = spawn(cmd, args, {
199
- cwd: options.cwd,
200
- env: options.env,
201
- stdio: ['pipe', 'pipe', 'pipe'],
202
- });
203
-
204
- let stdout = '';
205
- let stderr = '';
206
- let stdoutBuffer: Buffer | undefined = options.binaryStdout ? Buffer.alloc(0) : undefined;
207
-
208
- if (!options.binaryStdout) child.stdout.setEncoding('utf8');
209
- child.stderr.setEncoding('utf8');
210
-
211
- if (options.stdin !== undefined) {
212
- child.stdin.write(options.stdin);
213
- }
214
- child.stdin.end();
215
-
216
- child.stdout.on('data', (chunk) => {
217
- if (options.binaryStdout) {
218
- stdoutBuffer = Buffer.concat([
219
- stdoutBuffer ?? Buffer.alloc(0),
220
- Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk),
221
- ]);
222
- return;
223
- }
224
- const text = String(chunk);
225
- stdout += text;
226
- options.onStdoutChunk?.(text);
227
- });
228
-
229
- child.stderr.on('data', (chunk) => {
230
- const text = String(chunk);
231
- stderr += text;
232
- options.onStderrChunk?.(text);
233
- });
234
-
235
- child.on('error', (err) => {
236
- const code = (err as NodeJS.ErrnoException).code;
237
- if (code === 'ENOENT') {
238
- reject(new AppError('TOOL_MISSING', `${cmd} not found in PATH`, { cmd }, err));
239
- return;
240
- }
241
- reject(new AppError('COMMAND_FAILED', `Failed to run ${cmd}`, { cmd, args }, err));
242
- });
243
-
244
- child.on('close', (code) => {
245
- const exitCode = code ?? 1;
246
- if (exitCode !== 0 && !options.allowFailure) {
247
- reject(
248
- new AppError('COMMAND_FAILED', `${cmd} exited with code ${exitCode}`, {
249
- cmd,
250
- args,
251
- stdout,
252
- stderr,
253
- exitCode,
254
- }),
255
- );
256
- return;
257
- }
258
- resolve({ stdout, stderr, exitCode, stdoutBuffer });
259
- });
260
- });
261
- }
262
-
263
- export function runCmdBackground(
264
- cmd: string,
265
- args: string[],
266
- options: ExecOptions = {},
267
- ): ExecBackgroundResult {
268
- const child = spawn(cmd, args, {
269
- cwd: options.cwd,
270
- env: options.env,
271
- stdio: ['ignore', 'pipe', 'pipe'],
272
- });
273
-
274
- let stdout = '';
275
- let stderr = '';
276
-
277
- child.stdout.setEncoding('utf8');
278
- child.stderr.setEncoding('utf8');
279
-
280
- child.stdout.on('data', (chunk) => {
281
- stdout += chunk;
282
- });
283
- child.stderr.on('data', (chunk) => {
284
- stderr += chunk;
285
- });
286
-
287
- const wait = new Promise<ExecResult>((resolve, reject) => {
288
- child.on('error', (err) => {
289
- const code = (err as NodeJS.ErrnoException).code;
290
- if (code === 'ENOENT') {
291
- reject(new AppError('TOOL_MISSING', `${cmd} not found in PATH`, { cmd }, err));
292
- return;
293
- }
294
- reject(new AppError('COMMAND_FAILED', `Failed to run ${cmd}`, { cmd, args }, err));
295
- });
296
- child.on('close', (code) => {
297
- const exitCode = code ?? 1;
298
- if (exitCode !== 0 && !options.allowFailure) {
299
- reject(
300
- new AppError('COMMAND_FAILED', `${cmd} exited with code ${exitCode}`, {
301
- cmd,
302
- args,
303
- stdout,
304
- stderr,
305
- exitCode,
306
- }),
307
- );
308
- return;
309
- }
310
- resolve({ stdout, stderr, exitCode });
311
- });
312
- });
313
-
314
- return { child, wait };
315
- }
316
-
317
- export function whichCmdSync(cmd: string): boolean {
318
- try {
319
- const { shell, args } = resolveWhichArgs(cmd);
320
- const result = runCmdSync(shell, args, { allowFailure: true });
321
- return result.exitCode === 0 && result.stdout.trim().length > 0;
322
- } catch {
323
- return false;
324
- }
325
- }
326
-
327
- function resolveWhichArgs(cmd: string): { shell: string; args: string[] } {
328
- if (process.platform === 'win32') {
329
- return { shell: 'cmd.exe', args: ['/c', 'where', cmd] };
330
- }
331
- return { shell: 'bash', args: ['-lc', `command -v ${cmd}`] };
332
- }
333
-
334
- function normalizeTimeoutMs(value: number | undefined): number | undefined {
335
- if (!Number.isFinite(value)) return undefined;
336
- const timeout = Math.floor(value as number);
337
- if (timeout <= 0) return undefined;
338
- return timeout;
339
- }
@@ -1,101 +0,0 @@
1
- import type { SnapshotNode } from './snapshot.ts';
2
-
3
- export type FindLocator = 'any' | 'text' | 'label' | 'value' | 'role' | 'id';
4
-
5
- export type FindMatchOptions = {
6
- requireRect?: boolean;
7
- };
8
-
9
- export type FindBestMatches = {
10
- matches: SnapshotNode[];
11
- score: number;
12
- };
13
-
14
- export function findNodeByLocator(
15
- nodes: SnapshotNode[],
16
- locator: FindLocator,
17
- query: string,
18
- options: FindMatchOptions = {},
19
- ): SnapshotNode | null {
20
- const best = findBestMatchesByLocator(nodes, locator, query, options);
21
- return best.matches[0] ?? null;
22
- }
23
-
24
- export function findBestMatchesByLocator(
25
- nodes: SnapshotNode[],
26
- locator: FindLocator,
27
- query: string,
28
- options: FindMatchOptions = {},
29
- ): FindBestMatches {
30
- const normalizedQuery = normalizeText(query);
31
- if (!normalizedQuery) return { matches: [], score: 0 };
32
- let bestScore = 0;
33
- const matches: SnapshotNode[] = [];
34
- for (const node of nodes) {
35
- if (options.requireRect && !node.rect) continue;
36
- const score = matchNode(node, locator, normalizedQuery);
37
- if (score <= 0) continue;
38
- if (score > bestScore) {
39
- bestScore = score;
40
- matches.length = 0;
41
- matches.push(node);
42
- continue;
43
- }
44
- if (score === bestScore) {
45
- matches.push(node);
46
- }
47
- }
48
- return { matches, score: bestScore };
49
- }
50
-
51
- function matchNode(node: SnapshotNode, locator: FindLocator, query: string): number {
52
- switch (locator) {
53
- case 'role':
54
- return matchRole(node.type, query);
55
- case 'label':
56
- return matchText(node.label, query);
57
- case 'value':
58
- return matchText(node.value, query);
59
- case 'id':
60
- return matchText(node.identifier, query);
61
- case 'text':
62
- case 'any':
63
- default:
64
- return Math.max(
65
- matchText(node.label, query),
66
- matchText(node.value, query),
67
- matchText(node.identifier, query),
68
- );
69
- }
70
- }
71
-
72
- function matchText(value: string | undefined, query: string): number {
73
- const normalized = normalizeText(value ?? '');
74
- if (!normalized) return 0;
75
- if (normalized === query) return 2;
76
- if (normalized.includes(query)) return 1;
77
- return 0;
78
- }
79
-
80
- function matchRole(value: string | undefined, query: string): number {
81
- const normalized = normalizeRole(value ?? '');
82
- if (!normalized) return 0;
83
- if (normalized === query) return 2;
84
- if (normalized.includes(query)) return 1;
85
- return 0;
86
- }
87
-
88
- function normalizeText(value: string): string {
89
- return value.trim().toLowerCase().replace(/\s+/g, ' ');
90
- }
91
-
92
- function normalizeRole(value: string): string {
93
- let normalized = value.trim();
94
- if (!normalized) return '';
95
- const lastSegment = normalized.split('.').pop() ?? normalized;
96
- normalized = lastSegment.replace(/XCUIElementType/gi, '').toLowerCase();
97
- if (normalized.startsWith('ax')) {
98
- normalized = normalized.replace(/^ax/, '');
99
- }
100
- return normalized;
101
- }
@@ -1,4 +0,0 @@
1
- export function isInteractive(): boolean {
2
- if (process.env.CI) return false;
3
- return Boolean(process.stdin.isTTY && process.stdout.isTTY);
4
- }
@@ -1,173 +0,0 @@
1
- import { AppError } from './errors.ts';
2
- import type { DeviceInfo } from './device.ts';
3
- import {
4
- closeAndroidApp,
5
- fillAndroid,
6
- focusAndroid,
7
- longPressAndroid,
8
- openAndroidApp,
9
- openAndroidDevice,
10
- pressAndroid,
11
- swipeAndroid,
12
- scrollAndroid,
13
- scrollIntoViewAndroid,
14
- screenshotAndroid,
15
- typeAndroid,
16
- } from '../platforms/android/index.ts';
17
- import {
18
- closeIosApp,
19
- openIosApp,
20
- openIosDevice,
21
- screenshotIos,
22
- } from '../platforms/ios/index.ts';
23
- import { runIosRunnerCommand } from '../platforms/ios/runner-client.ts';
24
-
25
- export type RunnerContext = {
26
- appBundleId?: string;
27
- verbose?: boolean;
28
- logPath?: string;
29
- traceLogPath?: string;
30
- };
31
-
32
- export type Interactor = {
33
- open(app: string, options?: { activity?: string; appBundleId?: string; url?: string }): Promise<void>;
34
- openDevice(): Promise<void>;
35
- close(app: string): Promise<void>;
36
- tap(x: number, y: number): Promise<void>;
37
- swipe(x1: number, y1: number, x2: number, y2: number, durationMs?: number): Promise<void>;
38
- longPress(x: number, y: number, durationMs?: number): Promise<void>;
39
- focus(x: number, y: number): Promise<void>;
40
- type(text: string): Promise<void>;
41
- fill(x: number, y: number, text: string): Promise<void>;
42
- scroll(direction: string, amount?: number): Promise<void>;
43
- scrollIntoView(text: string): Promise<{ attempts?: number } | void>;
44
- screenshot(outPath: string): Promise<void>;
45
- };
46
-
47
- export function getInteractor(device: DeviceInfo, runnerContext: RunnerContext): Interactor {
48
- switch (device.platform) {
49
- case 'android':
50
- return {
51
- open: (app, options) => openAndroidApp(device, app, options?.activity),
52
- openDevice: () => openAndroidDevice(device),
53
- close: (app) => closeAndroidApp(device, app),
54
- tap: (x, y) => pressAndroid(device, x, y),
55
- swipe: (x1, y1, x2, y2, durationMs) => swipeAndroid(device, x1, y1, x2, y2, durationMs),
56
- longPress: (x, y, durationMs) => longPressAndroid(device, x, y, durationMs),
57
- focus: (x, y) => focusAndroid(device, x, y),
58
- type: (text) => typeAndroid(device, text),
59
- fill: (x, y, text) => fillAndroid(device, x, y, text),
60
- scroll: (direction, amount) => scrollAndroid(device, direction, amount),
61
- scrollIntoView: (text) => scrollIntoViewAndroid(device, text),
62
- screenshot: (outPath) => screenshotAndroid(device, outPath),
63
- };
64
- case 'ios':
65
- return {
66
- open: (app, options) => openIosApp(device, app, { appBundleId: options?.appBundleId, url: options?.url }),
67
- openDevice: () => openIosDevice(device),
68
- close: (app) => closeIosApp(device, app),
69
- screenshot: (outPath) => screenshotIos(device, outPath),
70
- ...iosRunnerOverrides(device, runnerContext),
71
- };
72
- default:
73
- throw new AppError('UNSUPPORTED_PLATFORM', `Unsupported platform: ${device.platform}`);
74
- }
75
- }
76
-
77
- type IoRunnerOverrides = Pick<Interactor, 'tap' | 'swipe' | 'longPress' | 'focus' | 'type' | 'fill' | 'scroll' | 'scrollIntoView'>;
78
-
79
- function iosRunnerOverrides(device: DeviceInfo, ctx: RunnerContext): IoRunnerOverrides {
80
- const runnerOpts = { verbose: ctx.verbose, logPath: ctx.logPath, traceLogPath: ctx.traceLogPath };
81
-
82
- return {
83
- tap: async (x, y) => {
84
- await runIosRunnerCommand(
85
- device,
86
- { command: 'tap', x, y, appBundleId: ctx.appBundleId },
87
- runnerOpts,
88
- );
89
- },
90
- swipe: async (x1, y1, x2, y2, durationMs) => {
91
- await runIosRunnerCommand(
92
- device,
93
- { command: 'drag', x: x1, y: y1, x2, y2, durationMs, appBundleId: ctx.appBundleId },
94
- runnerOpts,
95
- );
96
- },
97
- longPress: async (x, y, durationMs) => {
98
- await runIosRunnerCommand(
99
- device,
100
- { command: 'longPress', x, y, durationMs, appBundleId: ctx.appBundleId },
101
- runnerOpts,
102
- );
103
- },
104
- focus: async (x, y) => {
105
- await runIosRunnerCommand(
106
- device,
107
- { command: 'tap', x, y, appBundleId: ctx.appBundleId },
108
- runnerOpts,
109
- );
110
- },
111
- type: async (text) => {
112
- await runIosRunnerCommand(
113
- device,
114
- { command: 'type', text, appBundleId: ctx.appBundleId },
115
- runnerOpts,
116
- );
117
- },
118
- fill: async (x, y, text) => {
119
- await runIosRunnerCommand(
120
- device,
121
- { command: 'tap', x, y, appBundleId: ctx.appBundleId },
122
- runnerOpts,
123
- );
124
- await runIosRunnerCommand(
125
- device,
126
- { command: 'type', text, clearFirst: true, appBundleId: ctx.appBundleId },
127
- runnerOpts,
128
- );
129
- },
130
- scroll: async (direction, _amount) => {
131
- if (!['up', 'down', 'left', 'right'].includes(direction)) {
132
- throw new AppError('INVALID_ARGS', `Unknown direction: ${direction}`);
133
- }
134
- const inverted = invertScrollDirection(direction as 'up' | 'down' | 'left' | 'right');
135
- await runIosRunnerCommand(
136
- device,
137
- { command: 'swipe', direction: inverted, appBundleId: ctx.appBundleId },
138
- runnerOpts,
139
- );
140
- },
141
- scrollIntoView: async (text) => {
142
- const maxAttempts = 8;
143
- for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
144
- const found = (await runIosRunnerCommand(
145
- device,
146
- { command: 'findText', text, appBundleId: ctx.appBundleId },
147
- runnerOpts,
148
- )) as { found?: boolean };
149
- if (found?.found) return { attempts: attempt + 1 };
150
- await runIosRunnerCommand(
151
- device,
152
- { command: 'swipe', direction: 'up', appBundleId: ctx.appBundleId },
153
- runnerOpts,
154
- );
155
- await new Promise((resolve) => setTimeout(resolve, 300));
156
- }
157
- throw new AppError('COMMAND_FAILED', `scrollintoview could not find text: ${text}`);
158
- },
159
- };
160
- }
161
-
162
- function invertScrollDirection(direction: 'up' | 'down' | 'left' | 'right'): 'up' | 'down' | 'left' | 'right' {
163
- switch (direction) {
164
- case 'up':
165
- return 'down';
166
- case 'down':
167
- return 'up';
168
- case 'left':
169
- return 'right';
170
- case 'right':
171
- return 'left';
172
- }
173
- }
@@ -1,14 +0,0 @@
1
- export async function withKeyedLock<T>(
2
- locks: Map<string, Promise<unknown>>,
3
- key: string,
4
- task: () => Promise<T>,
5
- ): Promise<T> {
6
- const previous = locks.get(key) ?? Promise.resolve();
7
- const current = previous.catch(() => {}).then(task);
8
- locks.set(key, current);
9
- return current.finally(() => {
10
- if (locks.get(key) === current) {
11
- locks.delete(key);
12
- }
13
- });
14
- }