agent-device 0.4.1 → 0.4.2

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 (41) hide show
  1. package/README.md +18 -12
  2. package/dist/src/bin.js +32 -32
  3. package/dist/src/daemon.js +18 -14
  4. package/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift +8 -2
  5. package/package.json +1 -1
  6. package/skills/agent-device/SKILL.md +19 -13
  7. package/skills/agent-device/references/permissions.md +7 -2
  8. package/skills/agent-device/references/session-management.md +3 -1
  9. package/src/__tests__/cli-close.test.ts +155 -0
  10. package/src/cli.ts +32 -16
  11. package/src/core/__tests__/capabilities.test.ts +2 -1
  12. package/src/core/__tests__/dispatch-open.test.ts +25 -0
  13. package/src/core/__tests__/open-target.test.ts +40 -1
  14. package/src/core/capabilities.ts +1 -1
  15. package/src/core/dispatch.ts +22 -0
  16. package/src/core/open-target.ts +14 -0
  17. package/src/daemon/__tests__/device-ready.test.ts +52 -0
  18. package/src/daemon/device-ready.ts +146 -4
  19. package/src/daemon/handlers/__tests__/session.test.ts +477 -0
  20. package/src/daemon/handlers/session.ts +196 -91
  21. package/src/daemon/session-store.ts +0 -2
  22. package/src/daemon-client.ts +118 -18
  23. package/src/platforms/android/__tests__/index.test.ts +118 -1
  24. package/src/platforms/android/index.ts +77 -47
  25. package/src/platforms/ios/__tests__/index.test.ts +292 -4
  26. package/src/platforms/ios/__tests__/runner-client.test.ts +42 -0
  27. package/src/platforms/ios/apps.ts +358 -0
  28. package/src/platforms/ios/config.ts +28 -0
  29. package/src/platforms/ios/devicectl.ts +134 -0
  30. package/src/platforms/ios/devices.ts +15 -2
  31. package/src/platforms/ios/index.ts +20 -455
  32. package/src/platforms/ios/runner-client.ts +72 -16
  33. package/src/platforms/ios/simulator.ts +164 -0
  34. package/src/utils/__tests__/args.test.ts +20 -2
  35. package/src/utils/__tests__/daemon-client.test.ts +21 -4
  36. package/src/utils/args.ts +6 -1
  37. package/src/utils/command-schema.ts +7 -14
  38. package/src/utils/interactors.ts +2 -2
  39. package/src/utils/timeouts.ts +9 -0
  40. package/src/daemon/__tests__/app-state.test.ts +0 -138
  41. package/src/daemon/app-state.ts +0 -65
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict';
3
3
  import { promises as fs } from 'node:fs';
4
4
  import os from 'node:os';
5
5
  import path from 'node:path';
6
- import { openAndroidApp, parseAndroidLaunchComponent, swipeAndroid } from '../index.ts';
6
+ import { inferAndroidAppName, listAndroidApps, openAndroidApp, parseAndroidLaunchComponent, swipeAndroid } from '../index.ts';
7
7
  import type { DeviceInfo } from '../../../utils/device.ts';
8
8
  import { AppError } from '../../../utils/errors.ts';
9
9
  import { findBounds, parseUiHierarchy } from '../ui-hierarchy.ts';
@@ -95,6 +95,123 @@ test('parseAndroidLaunchComponent returns null when no component is present', ()
95
95
  assert.equal(parseAndroidLaunchComponent(stdout), null);
96
96
  });
97
97
 
98
+ test('inferAndroidAppName derives readable names from package ids', () => {
99
+ assert.equal(inferAndroidAppName('com.android.settings'), 'Settings');
100
+ assert.equal(inferAndroidAppName('com.google.android.apps.maps'), 'Maps');
101
+ assert.equal(inferAndroidAppName('org.mozilla.firefox'), 'Firefox');
102
+ assert.equal(inferAndroidAppName('com.facebook.katana'), 'Katana');
103
+ assert.equal(inferAndroidAppName('single'), 'Single');
104
+ assert.equal(inferAndroidAppName('com.android.app.services'), 'Services');
105
+ });
106
+
107
+ test('listAndroidApps returns launchable apps with inferred names', async () => {
108
+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-android-apps-all-'));
109
+ const adbPath = path.join(tmpDir, 'adb');
110
+ await fs.writeFile(
111
+ adbPath,
112
+ [
113
+ '#!/bin/sh',
114
+ 'if [ "$1" = "-s" ]; then',
115
+ ' shift',
116
+ ' shift',
117
+ 'fi',
118
+ 'if [ "$1" = "shell" ] && [ "$2" = "cmd" ] && [ "$3" = "package" ] && [ "$4" = "query-activities" ]; then',
119
+ ' echo "com.google.android.apps.maps/.MainActivity"',
120
+ ' echo "org.mozilla.firefox/.App"',
121
+ ' echo "com.android.settings/.Settings"',
122
+ ' exit 0',
123
+ 'fi',
124
+ 'if [ "$1" = "shell" ] && [ "$2" = "pm" ] && [ "$3" = "list" ] && [ "$4" = "packages" ] && [ "$5" = "-3" ]; then',
125
+ ' echo "package:com.google.android.apps.maps"',
126
+ ' echo "package:com.example.serviceonly"',
127
+ ' echo "package:org.mozilla.firefox"',
128
+ ' exit 0',
129
+ 'fi',
130
+ 'echo "unexpected args: $@" >&2',
131
+ 'exit 1',
132
+ '',
133
+ ].join('\n'),
134
+ 'utf8',
135
+ );
136
+ await fs.chmod(adbPath, 0o755);
137
+
138
+ const previousPath = process.env.PATH;
139
+ process.env.PATH = `${tmpDir}${path.delimiter}${previousPath ?? ''}`;
140
+
141
+ const device: DeviceInfo = {
142
+ platform: 'android',
143
+ id: 'emulator-5554',
144
+ name: 'Pixel',
145
+ kind: 'emulator',
146
+ booted: true,
147
+ };
148
+
149
+ try {
150
+ const apps = await listAndroidApps(device, 'all');
151
+ assert.deepEqual(apps, [
152
+ { package: 'com.android.settings', name: 'Settings' },
153
+ { package: 'com.google.android.apps.maps', name: 'Maps' },
154
+ { package: 'org.mozilla.firefox', name: 'Firefox' },
155
+ ]);
156
+ } finally {
157
+ process.env.PATH = previousPath;
158
+ await fs.rm(tmpDir, { recursive: true, force: true });
159
+ }
160
+ });
161
+
162
+ test('listAndroidApps user-installed excludes non-launchable packages', async () => {
163
+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-android-apps-user-'));
164
+ const adbPath = path.join(tmpDir, 'adb');
165
+ await fs.writeFile(
166
+ adbPath,
167
+ [
168
+ '#!/bin/sh',
169
+ 'if [ "$1" = "-s" ]; then',
170
+ ' shift',
171
+ ' shift',
172
+ 'fi',
173
+ 'if [ "$1" = "shell" ] && [ "$2" = "cmd" ] && [ "$3" = "package" ] && [ "$4" = "query-activities" ]; then',
174
+ ' echo "com.google.android.apps.maps/.MainActivity"',
175
+ ' echo "org.mozilla.firefox/.App"',
176
+ ' exit 0',
177
+ 'fi',
178
+ 'if [ "$1" = "shell" ] && [ "$2" = "pm" ] && [ "$3" = "list" ] && [ "$4" = "packages" ] && [ "$5" = "-3" ]; then',
179
+ ' echo "package:com.google.android.apps.maps"',
180
+ ' echo "package:com.example.serviceonly"',
181
+ ' echo "package:org.mozilla.firefox"',
182
+ ' exit 0',
183
+ 'fi',
184
+ 'echo "unexpected args: $@" >&2',
185
+ 'exit 1',
186
+ '',
187
+ ].join('\n'),
188
+ 'utf8',
189
+ );
190
+ await fs.chmod(adbPath, 0o755);
191
+
192
+ const previousPath = process.env.PATH;
193
+ process.env.PATH = `${tmpDir}${path.delimiter}${previousPath ?? ''}`;
194
+
195
+ const device: DeviceInfo = {
196
+ platform: 'android',
197
+ id: 'emulator-5554',
198
+ name: 'Pixel',
199
+ kind: 'emulator',
200
+ booted: true,
201
+ };
202
+
203
+ try {
204
+ const apps = await listAndroidApps(device, 'user-installed');
205
+ assert.deepEqual(apps, [
206
+ { package: 'com.google.android.apps.maps', name: 'Maps' },
207
+ { package: 'org.mozilla.firefox', name: 'Firefox' },
208
+ ]);
209
+ } finally {
210
+ process.env.PATH = previousPath;
211
+ await fs.rm(tmpDir, { recursive: true, force: true });
212
+ }
213
+ });
214
+
98
215
  test('openAndroidApp rejects activity override for deep link URLs', async () => {
99
216
  const device: DeviceInfo = {
100
217
  platform: 'android',
@@ -48,60 +48,87 @@ export async function resolveAndroidApp(
48
48
 
49
49
  export async function listAndroidApps(
50
50
  device: DeviceInfo,
51
- filter: 'launchable' | 'user-installed' | 'all' = 'launchable',
52
- ): Promise<string[]> {
53
- if (filter === 'launchable') {
54
- const result = await runCmd(
55
- 'adb',
56
- adbArgs(device, [
57
- 'shell',
58
- 'cmd',
59
- 'package',
60
- 'query-activities',
61
- '--brief',
62
- '-a',
63
- 'android.intent.action.MAIN',
64
- '-c',
65
- 'android.intent.category.LAUNCHER',
66
- ]),
67
- { allowFailure: true },
68
- );
69
- if (result.exitCode === 0 && result.stdout.trim().length > 0) {
70
- const packages = new Set<string>();
71
- for (const line of result.stdout.split('\n')) {
72
- const trimmed = line.trim();
73
- if (!trimmed) continue;
74
- const firstToken = trimmed.split(/\s+/)[0];
75
- const pkg = firstToken.includes('/') ? firstToken.split('/')[0] : firstToken;
76
- if (pkg) packages.add(pkg);
77
- }
78
- if (packages.size > 0) {
79
- return Array.from(packages);
80
- }
81
- }
82
- // fallback: list all if query-activities not available
51
+ filter: 'user-installed' | 'all' = 'all',
52
+ ): Promise<Array<{ package: string; name: string }>> {
53
+ const launchable = await listAndroidLaunchablePackages(device);
54
+ const packageIds =
55
+ filter === 'user-installed'
56
+ ? (await listAndroidUserInstalledPackages(device)).filter((pkg) => launchable.has(pkg))
57
+ : Array.from(launchable);
58
+ return packageIds
59
+ .sort((a, b) => a.localeCompare(b))
60
+ .map((pkg) => ({ package: pkg, name: inferAndroidAppName(pkg) }));
61
+ }
62
+
63
+ async function listAndroidLaunchablePackages(device: DeviceInfo): Promise<Set<string>> {
64
+ const result = await runCmd(
65
+ 'adb',
66
+ adbArgs(device, [
67
+ 'shell',
68
+ 'cmd',
69
+ 'package',
70
+ 'query-activities',
71
+ '--brief',
72
+ '-a',
73
+ 'android.intent.action.MAIN',
74
+ '-c',
75
+ 'android.intent.category.LAUNCHER',
76
+ ]),
77
+ { allowFailure: true },
78
+ );
79
+ if (result.exitCode !== 0 || result.stdout.trim().length === 0) {
80
+ return new Set<string>();
81
+ }
82
+ const packages = new Set<string>();
83
+ for (const line of result.stdout.split('\n')) {
84
+ const trimmed = line.trim();
85
+ if (!trimmed) continue;
86
+ const firstToken = trimmed.split(/\s+/)[0];
87
+ const pkg = firstToken.includes('/') ? firstToken.split('/')[0] : firstToken;
88
+ if (pkg) packages.add(pkg);
83
89
  }
90
+ return packages;
91
+ }
84
92
 
85
- const args =
86
- filter === 'user-installed'
87
- ? ['shell', 'pm', 'list', 'packages', '-3']
88
- : ['shell', 'pm', 'list', 'packages'];
89
- const result = await runCmd('adb', adbArgs(device, args));
93
+ async function listAndroidUserInstalledPackages(device: DeviceInfo): Promise<string[]> {
94
+ const result = await runCmd('adb', adbArgs(device, ['shell', 'pm', 'list', 'packages', '-3']));
90
95
  return result.stdout
91
96
  .split('\n')
92
97
  .map((line: string) => line.replace('package:', '').trim())
93
98
  .filter(Boolean);
94
99
  }
95
100
 
96
- export async function listAndroidAppsMetadata(
97
- device: DeviceInfo,
98
- filter: 'launchable' | 'user-installed' | 'all' = 'launchable',
99
- ): Promise<Array<{ package: string; launchable: boolean }>> {
100
- const apps = await listAndroidApps(device, filter);
101
- const launchable = filter === 'launchable'
102
- ? new Set(apps)
103
- : new Set(await listAndroidApps(device, 'launchable'));
104
- return apps.map((pkg) => ({ package: pkg, launchable: launchable.has(pkg) }));
101
+ export function inferAndroidAppName(packageName: string): string {
102
+ const ignoredTokens = new Set([
103
+ 'com',
104
+ 'android',
105
+ 'google',
106
+ 'app',
107
+ 'apps',
108
+ 'service',
109
+ 'services',
110
+ 'mobile',
111
+ 'client',
112
+ ]);
113
+ const tokens = packageName
114
+ .split('.')
115
+ .flatMap((segment) => segment.split(/[_-]+/))
116
+ .map((token) => token.trim().toLowerCase())
117
+ .filter((token) => token.length > 0);
118
+ // Fallback to last token if every token is ignored (e.g. "com.android.app.services" → "Services").
119
+ let chosen = tokens[tokens.length - 1] ?? packageName;
120
+ for (let index = tokens.length - 1; index >= 0; index -= 1) {
121
+ const token = tokens[index];
122
+ if (!ignoredTokens.has(token)) {
123
+ chosen = token;
124
+ break;
125
+ }
126
+ }
127
+ return chosen
128
+ .split(/[^a-z0-9]+/i)
129
+ .filter(Boolean)
130
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
131
+ .join(' ');
105
132
  }
106
133
 
107
134
  export async function getAndroidAppState(
@@ -180,7 +207,7 @@ export async function openAndroidApp(
180
207
  if (activity) {
181
208
  throw new AppError('INVALID_ARGS', 'Activity override requires a package name, not an intent');
182
209
  }
183
- await runCmd('adb', adbArgs(device, ['shell', 'am', 'start', '-a', resolved.value]));
210
+ await runCmd('adb', adbArgs(device, ['shell', 'am', 'start', '-W', '-a', resolved.value]));
184
211
  return;
185
212
  }
186
213
  if (activity) {
@@ -193,6 +220,7 @@ export async function openAndroidApp(
193
220
  'shell',
194
221
  'am',
195
222
  'start',
223
+ '-W',
196
224
  '-a',
197
225
  'android.intent.action.MAIN',
198
226
  '-c',
@@ -212,6 +240,7 @@ export async function openAndroidApp(
212
240
  'shell',
213
241
  'am',
214
242
  'start',
243
+ '-W',
215
244
  '-a',
216
245
  'android.intent.action.MAIN',
217
246
  '-c',
@@ -232,6 +261,7 @@ export async function openAndroidApp(
232
261
  'shell',
233
262
  'am',
234
263
  'start',
264
+ '-W',
235
265
  '-a',
236
266
  'android.intent.action.MAIN',
237
267
  '-c',
@@ -1,10 +1,13 @@
1
1
  import test from 'node:test';
2
2
  import assert from 'node:assert/strict';
3
- import { openIosApp } from '../index.ts';
3
+ import { promises as fs } from 'node:fs';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import { listIosApps, openIosApp, parseIosDeviceAppsPayload, resolveIosApp } from '../index.ts';
4
7
  import type { DeviceInfo } from '../../../utils/device.ts';
5
8
  import { AppError } from '../../../utils/errors.ts';
6
9
 
7
- test('openIosApp rejects deep links on iOS physical devices', async () => {
10
+ test('openIosApp custom scheme deep links on iOS devices require app bundle context', async () => {
8
11
  const device: DeviceInfo = {
9
12
  platform: 'ios',
10
13
  id: 'ios-device-1',
@@ -14,11 +17,296 @@ test('openIosApp rejects deep links on iOS physical devices', async () => {
14
17
  };
15
18
 
16
19
  await assert.rejects(
17
- () => openIosApp(device, 'https://example.com/path'),
20
+ () => openIosApp(device, 'myapp://home'),
18
21
  (error: unknown) => {
19
22
  assert.equal(error instanceof AppError, true);
20
- assert.equal((error as AppError).code, 'UNSUPPORTED_OPERATION');
23
+ assert.equal((error as AppError).code, 'INVALID_ARGS');
21
24
  return true;
22
25
  },
23
26
  );
24
27
  });
28
+
29
+ test('openIosApp web URL on iOS device without app falls back to Safari', async () => {
30
+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-ios-safari-test-'));
31
+ const xcrunPath = path.join(tmpDir, 'xcrun');
32
+ const argsLogPath = path.join(tmpDir, 'args.log');
33
+ await fs.writeFile(
34
+ xcrunPath,
35
+ '#!/bin/sh\nprintf "%s\\n" "$@" > "$AGENT_DEVICE_TEST_ARGS_FILE"\nexit 0\n',
36
+ 'utf8',
37
+ );
38
+ await fs.chmod(xcrunPath, 0o755);
39
+
40
+ const previousPath = process.env.PATH;
41
+ const previousArgsFile = process.env.AGENT_DEVICE_TEST_ARGS_FILE;
42
+ process.env.PATH = `${tmpDir}${path.delimiter}${previousPath ?? ''}`;
43
+ process.env.AGENT_DEVICE_TEST_ARGS_FILE = argsLogPath;
44
+
45
+ const device: DeviceInfo = {
46
+ platform: 'ios',
47
+ id: 'ios-device-1',
48
+ name: 'iPhone Device',
49
+ kind: 'device',
50
+ booted: true,
51
+ };
52
+
53
+ try {
54
+ await openIosApp(device, 'https://example.com/path');
55
+ const args = (await fs.readFile(argsLogPath, 'utf8'))
56
+ .trim()
57
+ .split('\n')
58
+ .filter(Boolean);
59
+ assert.deepEqual(args, [
60
+ 'devicectl',
61
+ 'device',
62
+ 'process',
63
+ 'launch',
64
+ '--device',
65
+ 'ios-device-1',
66
+ 'com.apple.mobilesafari',
67
+ '--payload-url',
68
+ 'https://example.com/path',
69
+ ]);
70
+ } finally {
71
+ process.env.PATH = previousPath;
72
+ if (previousArgsFile === undefined) {
73
+ delete process.env.AGENT_DEVICE_TEST_ARGS_FILE;
74
+ } else {
75
+ process.env.AGENT_DEVICE_TEST_ARGS_FILE = previousArgsFile;
76
+ }
77
+ await fs.rm(tmpDir, { recursive: true, force: true });
78
+ }
79
+ });
80
+
81
+ test('openIosApp custom scheme on iOS device uses active app context', async () => {
82
+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-ios-openurl-test-'));
83
+ const xcrunPath = path.join(tmpDir, 'xcrun');
84
+ const argsLogPath = path.join(tmpDir, 'args.log');
85
+ await fs.writeFile(
86
+ xcrunPath,
87
+ '#!/bin/sh\nprintf "%s\\n" "$@" > "$AGENT_DEVICE_TEST_ARGS_FILE"\nexit 0\n',
88
+ 'utf8',
89
+ );
90
+ await fs.chmod(xcrunPath, 0o755);
91
+
92
+ const previousPath = process.env.PATH;
93
+ const previousArgsFile = process.env.AGENT_DEVICE_TEST_ARGS_FILE;
94
+ process.env.PATH = `${tmpDir}${path.delimiter}${previousPath ?? ''}`;
95
+ process.env.AGENT_DEVICE_TEST_ARGS_FILE = argsLogPath;
96
+
97
+ const device: DeviceInfo = {
98
+ platform: 'ios',
99
+ id: 'ios-device-1',
100
+ name: 'iPhone Device',
101
+ kind: 'device',
102
+ booted: true,
103
+ };
104
+
105
+ try {
106
+ await openIosApp(device, 'myapp://item/42', { appBundleId: 'com.example.app' });
107
+ const args = (await fs.readFile(argsLogPath, 'utf8'))
108
+ .trim()
109
+ .split('\n')
110
+ .filter(Boolean);
111
+ assert.deepEqual(args, [
112
+ 'devicectl',
113
+ 'device',
114
+ 'process',
115
+ 'launch',
116
+ '--device',
117
+ 'ios-device-1',
118
+ 'com.example.app',
119
+ '--payload-url',
120
+ 'myapp://item/42',
121
+ ]);
122
+ } finally {
123
+ process.env.PATH = previousPath;
124
+ if (previousArgsFile === undefined) {
125
+ delete process.env.AGENT_DEVICE_TEST_ARGS_FILE;
126
+ } else {
127
+ process.env.AGENT_DEVICE_TEST_ARGS_FILE = previousArgsFile;
128
+ }
129
+ await fs.rm(tmpDir, { recursive: true, force: true });
130
+ }
131
+ });
132
+
133
+ test('openIosApp with app and URL on iOS device launches app bundle with payload URL', async () => {
134
+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-ios-open-app-url-test-'));
135
+ const xcrunPath = path.join(tmpDir, 'xcrun');
136
+ const argsLogPath = path.join(tmpDir, 'args.log');
137
+ await fs.writeFile(
138
+ xcrunPath,
139
+ '#!/bin/sh\nprintf "%s\\n" "$@" > "$AGENT_DEVICE_TEST_ARGS_FILE"\nexit 0\n',
140
+ 'utf8',
141
+ );
142
+ await fs.chmod(xcrunPath, 0o755);
143
+
144
+ const previousPath = process.env.PATH;
145
+ const previousArgsFile = process.env.AGENT_DEVICE_TEST_ARGS_FILE;
146
+ process.env.PATH = `${tmpDir}${path.delimiter}${previousPath ?? ''}`;
147
+ process.env.AGENT_DEVICE_TEST_ARGS_FILE = argsLogPath;
148
+
149
+ const device: DeviceInfo = {
150
+ platform: 'ios',
151
+ id: 'ios-device-1',
152
+ name: 'iPhone Device',
153
+ kind: 'device',
154
+ booted: true,
155
+ };
156
+
157
+ try {
158
+ await openIosApp(device, 'MyApp', { appBundleId: 'com.example.app', url: 'myapp://screen/to' });
159
+ const args = (await fs.readFile(argsLogPath, 'utf8'))
160
+ .trim()
161
+ .split('\n')
162
+ .filter(Boolean);
163
+ assert.deepEqual(args, [
164
+ 'devicectl',
165
+ 'device',
166
+ 'process',
167
+ 'launch',
168
+ '--device',
169
+ 'ios-device-1',
170
+ 'com.example.app',
171
+ '--payload-url',
172
+ 'myapp://screen/to',
173
+ ]);
174
+ } finally {
175
+ process.env.PATH = previousPath;
176
+ if (previousArgsFile === undefined) {
177
+ delete process.env.AGENT_DEVICE_TEST_ARGS_FILE;
178
+ } else {
179
+ process.env.AGENT_DEVICE_TEST_ARGS_FILE = previousArgsFile;
180
+ }
181
+ await fs.rm(tmpDir, { recursive: true, force: true });
182
+ }
183
+ });
184
+
185
+ test('parseIosDeviceAppsPayload maps devicectl app entries', () => {
186
+ const apps = parseIosDeviceAppsPayload({
187
+ result: {
188
+ apps: [
189
+ {
190
+ bundleIdentifier: 'com.apple.Maps',
191
+ name: 'Maps',
192
+ },
193
+ {
194
+ bundleIdentifier: 'com.example.NoName',
195
+ },
196
+ ],
197
+ },
198
+ });
199
+
200
+ assert.equal(apps.length, 2);
201
+ assert.deepEqual(apps[0], {
202
+ bundleId: 'com.apple.Maps',
203
+ name: 'Maps',
204
+ });
205
+ assert.equal(apps[1].bundleId, 'com.example.NoName');
206
+ assert.equal(apps[1].name, 'com.example.NoName');
207
+ });
208
+
209
+ test('parseIosDeviceAppsPayload ignores malformed entries', () => {
210
+ const apps = parseIosDeviceAppsPayload({
211
+ result: {
212
+ apps: [
213
+ null,
214
+ {},
215
+ { name: 'Missing bundle id' },
216
+ { bundleIdentifier: '' },
217
+ ],
218
+ },
219
+ });
220
+ assert.deepEqual(apps, []);
221
+ });
222
+
223
+ test('resolveIosApp resolves app display name on iOS physical devices', async () => {
224
+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-ios-app-resolve-'));
225
+ const xcrunPath = path.join(tmpDir, 'xcrun');
226
+ await fs.writeFile(
227
+ xcrunPath,
228
+ [
229
+ '#!/bin/sh',
230
+ 'if [ "$1" = "devicectl" ] && [ "$2" = "device" ] && [ "$3" = "info" ] && [ "$4" = "apps" ]; then',
231
+ ' out=""',
232
+ ' while [ "$#" -gt 0 ]; do',
233
+ ' if [ "$1" = "--json-output" ]; then',
234
+ ' out="$2"',
235
+ ' shift 2',
236
+ ' continue',
237
+ ' fi',
238
+ ' shift',
239
+ ' done',
240
+ " cat > \"$out\" <<'JSON'",
241
+ '{"result":{"apps":[{"bundleIdentifier":"com.apple.Maps","name":"Maps"},{"bundleIdentifier":"com.example.demo","name":"Demo"}]}}',
242
+ 'JSON',
243
+ ' exit 0',
244
+ 'fi',
245
+ 'echo "unexpected xcrun args: $@" >&2',
246
+ 'exit 1',
247
+ '',
248
+ ].join('\n'),
249
+ 'utf8',
250
+ );
251
+ await fs.chmod(xcrunPath, 0o755);
252
+
253
+ const previousPath = process.env.PATH;
254
+ process.env.PATH = `${tmpDir}${path.delimiter}${previousPath ?? ''}`;
255
+
256
+ const device: DeviceInfo = {
257
+ platform: 'ios',
258
+ id: 'ios-device-1',
259
+ name: 'iPhone Device',
260
+ kind: 'device',
261
+ booted: true,
262
+ };
263
+
264
+ try {
265
+ const bundleId = await resolveIosApp(device, 'Maps');
266
+ assert.equal(bundleId, 'com.apple.Maps');
267
+ } finally {
268
+ process.env.PATH = previousPath;
269
+ await fs.rm(tmpDir, { recursive: true, force: true });
270
+ }
271
+ });
272
+
273
+ test('listIosApps applies user-installed filter on simulator', async () => {
274
+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-ios-list-sim-'));
275
+ const xcrunPath = path.join(tmpDir, 'xcrun');
276
+ await fs.writeFile(
277
+ xcrunPath,
278
+ [
279
+ '#!/bin/sh',
280
+ 'if [ "$1" = "simctl" ] && [ "$2" = "listapps" ]; then',
281
+ " cat <<'JSON'",
282
+ '{"com.apple.Maps":{"CFBundleDisplayName":"Maps"},"com.example.demo":{"CFBundleDisplayName":"Demo"}}',
283
+ 'JSON',
284
+ ' exit 0',
285
+ 'fi',
286
+ 'echo "unexpected xcrun args: $@" >&2',
287
+ 'exit 1',
288
+ '',
289
+ ].join('\n'),
290
+ 'utf8',
291
+ );
292
+ await fs.chmod(xcrunPath, 0o755);
293
+
294
+ const previousPath = process.env.PATH;
295
+ process.env.PATH = `${tmpDir}${path.delimiter}${previousPath ?? ''}`;
296
+
297
+ const device: DeviceInfo = {
298
+ platform: 'ios',
299
+ id: 'sim-1',
300
+ name: 'iPhone Sim',
301
+ kind: 'simulator',
302
+ booted: true,
303
+ };
304
+
305
+ try {
306
+ const apps = await listIosApps(device, 'user-installed');
307
+ assert.deepEqual(apps, [{ bundleId: 'com.example.demo', name: 'Demo' }]);
308
+ } finally {
309
+ process.env.PATH = previousPath;
310
+ await fs.rm(tmpDir, { recursive: true, force: true });
311
+ }
312
+ });
@@ -1,12 +1,16 @@
1
1
  import test from 'node:test';
2
2
  import assert from 'node:assert/strict';
3
3
  import type { DeviceInfo } from '../../../utils/device.ts';
4
+ import { AppError } from '../../../utils/errors.ts';
4
5
  import {
5
6
  assertSafeDerivedCleanup,
7
+ isRetryableRunnerError,
8
+ resolveRunnerEarlyExitHint,
6
9
  resolveRunnerBuildDestination,
7
10
  resolveRunnerDestination,
8
11
  resolveRunnerMaxConcurrentDestinationsFlag,
9
12
  resolveRunnerSigningBuildSettings,
13
+ shouldRetryRunnerConnectError,
10
14
  } from '../runner-client.ts';
11
15
 
12
16
  const iosSimulator: DeviceInfo = {
@@ -111,3 +115,41 @@ test('assertSafeDerivedCleanup allows cleaning override path with explicit opt-i
111
115
  });
112
116
  });
113
117
  });
118
+
119
+ test('resolveRunnerEarlyExitHint surfaces busy-connecting guidance', () => {
120
+ const hint = resolveRunnerEarlyExitHint(
121
+ 'Runner did not accept connection (xcodebuild exited early)',
122
+ 'Ineligible destinations for the "AgentDeviceRunner" scheme:\n{ error:Device is busy (Connecting to iPhone) }',
123
+ '',
124
+ );
125
+ assert.match(hint, /still connecting/i);
126
+ });
127
+
128
+ test('resolveRunnerEarlyExitHint falls back to runner connect timeout hint', () => {
129
+ const hint = resolveRunnerEarlyExitHint(
130
+ 'Runner did not accept connection (xcodebuild exited early)',
131
+ '',
132
+ 'xcodebuild failed unexpectedly',
133
+ );
134
+ assert.match(hint, /retry runner startup/i);
135
+ });
136
+
137
+ test('shouldRetryRunnerConnectError does not retry xcodebuild early-exit errors', () => {
138
+ const err = new AppError('COMMAND_FAILED', 'Runner did not accept connection (xcodebuild exited early)');
139
+ assert.equal(shouldRetryRunnerConnectError(err), false);
140
+ });
141
+
142
+ test('shouldRetryRunnerConnectError retries transient connect errors', () => {
143
+ const err = new AppError('COMMAND_FAILED', 'Runner endpoint probe failed');
144
+ assert.equal(shouldRetryRunnerConnectError(err), true);
145
+ });
146
+
147
+ test('isRetryableRunnerError does not retry xcodebuild early-exit errors', () => {
148
+ const err = new AppError('COMMAND_FAILED', 'Runner did not accept connection (xcodebuild exited early)');
149
+ assert.equal(isRetryableRunnerError(err), false);
150
+ });
151
+
152
+ test('isRetryableRunnerError does not retry busy-connecting errors', () => {
153
+ const err = new AppError('COMMAND_FAILED', 'Device is busy (Connecting to iPhone)');
154
+ assert.equal(isRetryableRunnerError(err), false);
155
+ });