agent-device 0.3.1 → 0.3.3

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.
@@ -27,7 +27,7 @@ npx -y agent-device
27
27
 
28
28
  ## Core workflow
29
29
 
30
- 1. Open app or just boot device: `open [app]`
30
+ 1. Open app: `open [app]` (`open` handles target selection + boot/activation in the normal flow)
31
31
  2. Snapshot: `snapshot` to get refs from accessibility tree
32
32
  3. Interact using refs (`click @ref`, `fill @ref "text"`)
33
33
  4. Re-snapshot after navigation/UI changes
@@ -38,12 +38,19 @@ npx -y agent-device
38
38
  ### Navigation
39
39
 
40
40
  ```bash
41
+ agent-device boot # Ensure target is booted/ready without opening app
42
+ agent-device boot --platform ios # Boot iOS simulator
43
+ agent-device boot --platform android # Boot Android emulator/device target
41
44
  agent-device open [app] # Boot device/simulator; optionally launch app
42
45
  agent-device open [app] --activity com.example/.MainActivity # Android: open specific activity
43
46
  agent-device close [app] # Close app or just end session
47
+ agent-device reinstall <app> <path> # Uninstall + install app in one command
44
48
  agent-device session list # List active sessions
45
49
  ```
46
50
 
51
+ `boot` requires either an active session or an explicit selector (`--platform`, `--device`, `--udid`, or `--serial`).
52
+ `boot` is a fallback, not a regular step: use it when starting a new session only if `open` cannot find/connect to an available target.
53
+
47
54
  ### Snapshot (page analysis)
48
55
 
49
56
  ```bash
package/src/cli.ts CHANGED
@@ -105,6 +105,13 @@ export async function runCli(argv: string[]): Promise<void> {
105
105
  if (logTailStopper) logTailStopper();
106
106
  return;
107
107
  }
108
+ if (command === 'boot') {
109
+ const platform = (response.data as any)?.platform ?? 'unknown';
110
+ const device = (response.data as any)?.device ?? (response.data as any)?.id ?? 'unknown';
111
+ process.stdout.write(`Boot ready: ${device} (${platform})\n`);
112
+ if (logTailStopper) logTailStopper();
113
+ return;
114
+ }
108
115
  if (command === 'click') {
109
116
  const ref = (response.data as any)?.ref ?? '';
110
117
  const x = (response.data as any)?.x;
@@ -37,6 +37,7 @@ test('iOS simulator + Android commands reject iOS devices', () => {
37
37
  'app-switcher',
38
38
  'apps',
39
39
  'back',
40
+ 'boot',
40
41
  'click',
41
42
  'close',
42
43
  'fill',
@@ -46,6 +47,7 @@ test('iOS simulator + Android commands reject iOS devices', () => {
46
47
  'home',
47
48
  'long-press',
48
49
  'open',
50
+ 'reinstall',
49
51
  'press',
50
52
  'record',
51
53
  'screenshot',
@@ -19,6 +19,7 @@ const COMMAND_CAPABILITY_MATRIX: Record<string, CommandCapability> = {
19
19
  'app-switcher': { ios: { simulator: true }, android: { emulator: true, device: true, unknown: true } },
20
20
  apps: { ios: { simulator: true }, android: { emulator: true, device: true, unknown: true } },
21
21
  back: { ios: { simulator: true }, android: { emulator: true, device: true, unknown: true } },
22
+ boot: { ios: { simulator: true }, android: { emulator: true, device: true, unknown: true } },
22
23
  click: { ios: { simulator: true }, android: { emulator: true, device: true, unknown: true } },
23
24
  close: { ios: { simulator: true }, android: { emulator: true, device: true, unknown: true } },
24
25
  fill: { ios: { simulator: true }, android: { emulator: true, device: true, unknown: true } },
@@ -29,6 +30,7 @@ const COMMAND_CAPABILITY_MATRIX: Record<string, CommandCapability> = {
29
30
  home: { ios: { simulator: true }, android: { emulator: true, device: true, unknown: true } },
30
31
  'long-press': { ios: { simulator: true }, android: { emulator: true, device: true, unknown: true } },
31
32
  open: { ios: { simulator: true }, android: { emulator: true, device: true, unknown: true } },
33
+ reinstall: { ios: { simulator: true }, android: { emulator: true, device: true, unknown: true } },
32
34
  press: { ios: { simulator: true }, android: { emulator: true, device: true, unknown: true } },
33
35
  record: { ios: { simulator: true }, android: { emulator: true, device: true, unknown: true } },
34
36
  screenshot: { ios: { simulator: true }, android: { emulator: true, device: true, unknown: true } },
@@ -74,6 +74,97 @@ test('resolveSelectorChain falls back when first selector is ambiguous', () => {
74
74
  assert.equal(resolved.node.ref, 'e2');
75
75
  });
76
76
 
77
+ test('resolveSelectorChain keeps strict ambiguity behavior by default', () => {
78
+ const chain = parseSelectorChain('label="Continue"');
79
+ const resolved = resolveSelectorChain(nodes, chain, {
80
+ platform: 'ios',
81
+ requireRect: true,
82
+ requireUnique: true,
83
+ });
84
+ assert.equal(resolved, null);
85
+ });
86
+
87
+ test('resolveSelectorChain disambiguates to deeper/smaller matching node when enabled', () => {
88
+ const disambiguationNodes: SnapshotState['nodes'] = [
89
+ {
90
+ ref: 'e1',
91
+ index: 0,
92
+ type: 'Other',
93
+ label: 'Press me',
94
+ rect: { x: 0, y: 0, width: 300, height: 300 },
95
+ depth: 1,
96
+ enabled: true,
97
+ hittable: true,
98
+ },
99
+ {
100
+ ref: 'e2',
101
+ index: 1,
102
+ type: 'Other',
103
+ label: 'Press me',
104
+ rect: { x: 10, y: 10, width: 100, height: 20 },
105
+ depth: 2,
106
+ enabled: true,
107
+ hittable: true,
108
+ },
109
+ ];
110
+ const chain = parseSelectorChain('role="other" label="Press me" || label="Press me"');
111
+ const resolved = resolveSelectorChain(disambiguationNodes, chain, {
112
+ platform: 'ios',
113
+ requireRect: true,
114
+ requireUnique: true,
115
+ disambiguateAmbiguous: true,
116
+ });
117
+ assert.ok(resolved);
118
+ assert.equal(resolved.node.ref, 'e2');
119
+ assert.equal(resolved.matches, 2);
120
+ });
121
+
122
+ test('resolveSelectorChain disambiguation tie falls back to next selector', () => {
123
+ const tieNodes: SnapshotState['nodes'] = [
124
+ {
125
+ ref: 'e1',
126
+ index: 0,
127
+ type: 'Other',
128
+ label: 'Press me',
129
+ rect: { x: 0, y: 0, width: 100, height: 20 },
130
+ depth: 2,
131
+ enabled: true,
132
+ hittable: true,
133
+ },
134
+ {
135
+ ref: 'e2',
136
+ index: 1,
137
+ type: 'Other',
138
+ label: 'Press me',
139
+ rect: { x: 0, y: 40, width: 100, height: 20 },
140
+ depth: 2,
141
+ enabled: true,
142
+ hittable: true,
143
+ },
144
+ {
145
+ ref: 'e3',
146
+ index: 2,
147
+ type: 'Other',
148
+ label: 'Press me',
149
+ identifier: 'press_me_unique',
150
+ rect: { x: 0, y: 80, width: 100, height: 20 },
151
+ depth: 2,
152
+ enabled: true,
153
+ hittable: true,
154
+ },
155
+ ];
156
+ const chain = parseSelectorChain('label="Press me" || id="press_me_unique"');
157
+ const resolved = resolveSelectorChain(tieNodes, chain, {
158
+ platform: 'ios',
159
+ requireRect: true,
160
+ requireUnique: true,
161
+ disambiguateAmbiguous: true,
162
+ });
163
+ assert.ok(resolved);
164
+ assert.equal(resolved.selectorIndex, 1);
165
+ assert.equal(resolved.node.ref, 'e3');
166
+ });
167
+
77
168
  test('findSelectorChainMatch returns first matching selector for existence checks', () => {
78
169
  const chain = parseSelectorChain('label="Continue" || id=auth_continue');
79
170
  const match = findSelectorChainMatch(nodes, chain, {
@@ -91,12 +182,31 @@ test('splitSelectorFromArgs extracts selector prefix and trailing value', () =>
91
182
  assert.deepEqual(split.rest, ['qa@example.com']);
92
183
  });
93
184
 
185
+ test('splitSelectorFromArgs prefers trailing token for value when requested', () => {
186
+ const split = splitSelectorFromArgs(['label="Filter"', 'visible=true'], { preferTrailingValue: true });
187
+ assert.ok(split);
188
+ assert.equal(split.selectorExpression, 'label="Filter"');
189
+ assert.deepEqual(split.rest, ['visible=true']);
190
+ });
191
+
192
+ test('splitSelectorFromArgs keeps full selector when trailing value preference is disabled', () => {
193
+ const split = splitSelectorFromArgs(['label="Filter"', 'visible=true']);
194
+ assert.ok(split);
195
+ assert.equal(split.selectorExpression, 'label="Filter" visible=true');
196
+ assert.deepEqual(split.rest, []);
197
+ });
198
+
94
199
  test('parseSelectorChain rejects unknown keys and malformed quotes', () => {
95
200
  assert.throws(() => parseSelectorChain('foo=bar'), /Unknown selector key/i);
96
201
  assert.throws(() => parseSelectorChain('label="unclosed'), /Unclosed quote/i);
97
202
  assert.throws(() => parseSelectorChain(''), /cannot be empty/i);
98
203
  });
99
204
 
205
+ test('parseSelectorChain handles quoted values ending in escaped backslashes', () => {
206
+ const chain = parseSelectorChain('label="path\\\\" || id=auth_continue');
207
+ assert.equal(chain.selectors.length, 2);
208
+ });
209
+
100
210
  test('isSelectorToken only accepts known keys for key=value tokens', () => {
101
211
  assert.equal(isSelectorToken('id=foo'), true);
102
212
  assert.equal(isSelectorToken('editable=true'), true);
@@ -126,3 +236,26 @@ test('buildSelectorChainForNode prefers id and adds editable for fill action', (
126
236
  assert.ok(chain.some((entry) => entry.includes('id=')));
127
237
  assert.ok(chain.some((entry) => entry.includes('editable=true')));
128
238
  });
239
+
240
+ test('role selector normalization matches Android class names by leaf type', () => {
241
+ const androidNodes: SnapshotState['nodes'] = [
242
+ {
243
+ ref: 'a1',
244
+ index: 0,
245
+ type: 'android.widget.Button',
246
+ label: 'Continue',
247
+ identifier: 'auth_continue',
248
+ rect: { x: 0, y: 0, width: 120, height: 44 },
249
+ enabled: true,
250
+ hittable: true,
251
+ },
252
+ ];
253
+ const chain = parseSelectorChain('role=button label="Continue"');
254
+ const resolved = resolveSelectorChain(androidNodes, chain, {
255
+ platform: 'android',
256
+ requireRect: true,
257
+ requireUnique: true,
258
+ });
259
+ assert.ok(resolved);
260
+ assert.equal(resolved.node.ref, 'a1');
261
+ });
@@ -225,10 +225,74 @@ test('replay without --update does not heal or rewrite', async () => {
225
225
 
226
226
  assert.ok(response);
227
227
  assert.equal(response.ok, false);
228
+ if (!response.ok) {
229
+ assert.match(response.error.message, /Replay failed at step 1/);
230
+ assert.equal(response.error.details?.step, 1);
231
+ assert.equal(response.error.details?.action, 'click');
232
+ }
228
233
  assert.equal(snapshotDispatchCalls, 0);
229
234
  assert.equal(fs.readFileSync(replayPath, 'utf8'), originalPayload);
230
235
  });
231
236
 
237
+ test('replay --update skips malformed selector candidates and preserves replay error context', async () => {
238
+ const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-replay-malformed-candidate-'));
239
+ const sessionsDir = path.join(tempRoot, 'sessions');
240
+ const replayPath = path.join(tempRoot, 'replay.ad');
241
+ const sessionStore = new SessionStore(sessionsDir);
242
+ const sessionName = 'malformed-candidate-session';
243
+ sessionStore.set(sessionName, makeSession(sessionName));
244
+
245
+ writeReplayFile(replayPath, {
246
+ ts: Date.now(),
247
+ command: 'click',
248
+ positionals: ['id="old_continue" ||'],
249
+ flags: {},
250
+ result: {},
251
+ });
252
+
253
+ const dispatch = async (): Promise<Record<string, unknown> | void> => {
254
+ return {
255
+ nodes: [
256
+ {
257
+ index: 0,
258
+ type: 'XCUIElementTypeButton',
259
+ label: 'Continue',
260
+ identifier: 'auth_continue',
261
+ rect: { x: 10, y: 10, width: 100, height: 44 },
262
+ enabled: true,
263
+ hittable: true,
264
+ },
265
+ ],
266
+ truncated: false,
267
+ backend: 'xctest',
268
+ };
269
+ };
270
+
271
+ const response = await handleSessionCommands({
272
+ req: {
273
+ token: 't',
274
+ session: sessionName,
275
+ command: 'replay',
276
+ positionals: [replayPath],
277
+ flags: { replayUpdate: true },
278
+ },
279
+ sessionName,
280
+ logPath: path.join(tempRoot, 'daemon.log'),
281
+ sessionStore,
282
+ invoke: async () => ({ ok: false, error: { code: 'COMMAND_FAILED', message: 'selector stale' } }),
283
+ dispatch,
284
+ });
285
+
286
+ assert.ok(response);
287
+ assert.equal(response.ok, false);
288
+ if (!response.ok) {
289
+ assert.equal(response.error.code, 'COMMAND_FAILED');
290
+ assert.match(response.error.message, /Replay failed at step 1/);
291
+ assert.equal(response.error.details?.step, 1);
292
+ assert.equal(response.error.details?.action, 'click');
293
+ }
294
+ });
295
+
232
296
  test('replay --update heals selector in is command', async () => {
233
297
  const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-replay-heal-is-'));
234
298
  const sessionsDir = path.join(tempRoot, 'sessions');
@@ -0,0 +1,219 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import fs from 'node:fs';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import { handleSessionCommands } from '../session.ts';
7
+ import { SessionStore } from '../../session-store.ts';
8
+ import type { DaemonRequest, DaemonResponse, SessionState } from '../../types.ts';
9
+
10
+ function makeStore(): SessionStore {
11
+ const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-session-reinstall-'));
12
+ return new SessionStore(path.join(tempRoot, 'sessions'));
13
+ }
14
+
15
+ function makeSession(name: string, device: SessionState['device']): SessionState {
16
+ return {
17
+ name,
18
+ device,
19
+ createdAt: Date.now(),
20
+ actions: [],
21
+ };
22
+ }
23
+
24
+ const invoke = async (_req: DaemonRequest): Promise<DaemonResponse> => {
25
+ return { ok: false, error: { code: 'INVALID_ARGS', message: 'invoke should not be called in reinstall tests' } };
26
+ };
27
+
28
+ test('reinstall requires active session or explicit device selector', async () => {
29
+ const sessionStore = makeStore();
30
+ const response = await handleSessionCommands({
31
+ req: {
32
+ token: 't',
33
+ session: 'default',
34
+ command: 'reinstall',
35
+ positionals: ['com.example.app', '/tmp/app.apk'],
36
+ flags: {},
37
+ },
38
+ sessionName: 'default',
39
+ logPath: '/tmp/daemon.log',
40
+ sessionStore,
41
+ invoke,
42
+ });
43
+ assert.ok(response);
44
+ assert.equal(response.ok, false);
45
+ if (!response.ok) {
46
+ assert.equal(response.error.code, 'INVALID_ARGS');
47
+ assert.match(response.error.message, /active session or an explicit device selector/i);
48
+ }
49
+ });
50
+
51
+ test('reinstall validates required args before device operations', async () => {
52
+ const sessionStore = makeStore();
53
+ sessionStore.set(
54
+ 'default',
55
+ makeSession('default', {
56
+ platform: 'ios',
57
+ id: 'sim-1',
58
+ name: 'iPhone',
59
+ kind: 'simulator',
60
+ booted: true,
61
+ }),
62
+ );
63
+ const response = await handleSessionCommands({
64
+ req: {
65
+ token: 't',
66
+ session: 'default',
67
+ command: 'reinstall',
68
+ positionals: ['com.example.app'],
69
+ flags: {},
70
+ },
71
+ sessionName: 'default',
72
+ logPath: '/tmp/daemon.log',
73
+ sessionStore,
74
+ invoke,
75
+ });
76
+ assert.ok(response);
77
+ assert.equal(response.ok, false);
78
+ if (!response.ok) {
79
+ assert.equal(response.error.code, 'INVALID_ARGS');
80
+ assert.match(response.error.message, /reinstall <app> <path-to-app-binary>/i);
81
+ }
82
+ });
83
+
84
+ test('reinstall reports unsupported operation on iOS physical devices', async () => {
85
+ const sessionStore = makeStore();
86
+ sessionStore.set(
87
+ 'default',
88
+ makeSession('default', {
89
+ platform: 'ios',
90
+ id: 'device-1',
91
+ name: 'iPhone Device',
92
+ kind: 'device',
93
+ booted: true,
94
+ }),
95
+ );
96
+ const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-reinstall-binary-'));
97
+ const appPath = path.join(tempRoot, 'Sample.app');
98
+ fs.writeFileSync(appPath, 'placeholder');
99
+
100
+ const response = await handleSessionCommands({
101
+ req: {
102
+ token: 't',
103
+ session: 'default',
104
+ command: 'reinstall',
105
+ positionals: ['com.example.app', appPath],
106
+ flags: {},
107
+ },
108
+ sessionName: 'default',
109
+ logPath: '/tmp/daemon.log',
110
+ sessionStore,
111
+ invoke,
112
+ });
113
+ assert.ok(response);
114
+ assert.equal(response.ok, false);
115
+ if (!response.ok) {
116
+ assert.equal(response.error.code, 'UNSUPPORTED_OPERATION');
117
+ assert.match(response.error.message, /reinstall is not supported/i);
118
+ }
119
+ });
120
+
121
+ test('reinstall succeeds on active iOS simulator session and records action', async () => {
122
+ const sessionStore = makeStore();
123
+ const session = makeSession('default', {
124
+ platform: 'ios',
125
+ id: 'sim-1',
126
+ name: 'iPhone',
127
+ kind: 'simulator',
128
+ booted: true,
129
+ });
130
+ sessionStore.set('default', session);
131
+ const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-reinstall-success-ios-'));
132
+ const appPath = path.join(tempRoot, 'Sample.app');
133
+ fs.writeFileSync(appPath, 'placeholder');
134
+
135
+ const response = await handleSessionCommands({
136
+ req: {
137
+ token: 't',
138
+ session: 'default',
139
+ command: 'reinstall',
140
+ positionals: ['com.example.app', appPath],
141
+ flags: {},
142
+ },
143
+ sessionName: 'default',
144
+ logPath: '/tmp/daemon.log',
145
+ sessionStore,
146
+ invoke,
147
+ reinstallOps: {
148
+ ios: async (_device, app, pathToBinary) => {
149
+ assert.equal(app, 'com.example.app');
150
+ assert.equal(pathToBinary, appPath);
151
+ return { bundleId: 'com.example.app' };
152
+ },
153
+ android: async () => {
154
+ throw new Error('unexpected android reinstall');
155
+ },
156
+ },
157
+ });
158
+
159
+ assert.ok(response);
160
+ assert.equal(response.ok, true);
161
+ if (response.ok) {
162
+ assert.equal(response.data?.platform, 'ios');
163
+ assert.equal(response.data?.appId, 'com.example.app');
164
+ assert.equal(response.data?.bundleId, 'com.example.app');
165
+ assert.equal(response.data?.appPath, appPath);
166
+ }
167
+ assert.equal(session.actions.length, 1);
168
+ assert.equal(session.actions[0]?.command, 'reinstall');
169
+ });
170
+
171
+ test('reinstall succeeds on active Android session with normalized appId', async () => {
172
+ const sessionStore = makeStore();
173
+ sessionStore.set(
174
+ 'default',
175
+ makeSession('default', {
176
+ platform: 'android',
177
+ id: 'emulator-5554',
178
+ name: 'Pixel',
179
+ kind: 'emulator',
180
+ booted: true,
181
+ }),
182
+ );
183
+ const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-reinstall-success-android-'));
184
+ const appPath = path.join(tempRoot, 'Sample.apk');
185
+ fs.writeFileSync(appPath, 'placeholder');
186
+
187
+ const response = await handleSessionCommands({
188
+ req: {
189
+ token: 't',
190
+ session: 'default',
191
+ command: 'reinstall',
192
+ positionals: ['com.example.app', appPath],
193
+ flags: {},
194
+ },
195
+ sessionName: 'default',
196
+ logPath: '/tmp/daemon.log',
197
+ sessionStore,
198
+ invoke,
199
+ reinstallOps: {
200
+ ios: async () => {
201
+ throw new Error('unexpected ios reinstall');
202
+ },
203
+ android: async (_device, app, pathToBinary) => {
204
+ assert.equal(app, 'com.example.app');
205
+ assert.equal(pathToBinary, appPath);
206
+ return { package: 'com.example.app' };
207
+ },
208
+ },
209
+ });
210
+
211
+ assert.ok(response);
212
+ assert.equal(response.ok, true);
213
+ if (response.ok) {
214
+ assert.equal(response.data?.platform, 'android');
215
+ assert.equal(response.data?.appId, 'com.example.app');
216
+ assert.equal(response.data?.package, 'com.example.app');
217
+ assert.equal(response.data?.appPath, appPath);
218
+ }
219
+ });
@@ -0,0 +1,122 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import fs from 'node:fs';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import { handleSessionCommands } from '../session.ts';
7
+ import { SessionStore } from '../../session-store.ts';
8
+ import type { DaemonRequest, DaemonResponse, SessionState } from '../../types.ts';
9
+
10
+ function makeSessionStore(): SessionStore {
11
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-session-handler-'));
12
+ return new SessionStore(path.join(root, 'sessions'));
13
+ }
14
+
15
+ function makeSession(name: string, device: SessionState['device']): SessionState {
16
+ return {
17
+ name,
18
+ device,
19
+ createdAt: Date.now(),
20
+ actions: [],
21
+ };
22
+ }
23
+
24
+ const noopInvoke = async (_req: DaemonRequest): Promise<DaemonResponse> => ({ ok: true, data: {} });
25
+
26
+ test('boot requires session or explicit selector', async () => {
27
+ const sessionStore = makeSessionStore();
28
+ const response = await handleSessionCommands({
29
+ req: {
30
+ token: 't',
31
+ session: 'default',
32
+ command: 'boot',
33
+ positionals: [],
34
+ flags: {},
35
+ },
36
+ sessionName: 'default',
37
+ logPath: path.join(os.tmpdir(), 'daemon.log'),
38
+ sessionStore,
39
+ invoke: noopInvoke,
40
+ ensureReady: async () => {},
41
+ });
42
+ assert.ok(response);
43
+ assert.equal(response?.ok, false);
44
+ if (response && !response.ok) {
45
+ assert.equal(response.error.code, 'INVALID_ARGS');
46
+ }
47
+ });
48
+
49
+ test('boot rejects unsupported iOS device kind', async () => {
50
+ const sessionStore = makeSessionStore();
51
+ const sessionName = 'ios-device-session';
52
+ sessionStore.set(
53
+ sessionName,
54
+ makeSession(sessionName, {
55
+ platform: 'ios',
56
+ id: 'ios-device-1',
57
+ name: 'iPhone Device',
58
+ kind: 'device',
59
+ booted: true,
60
+ }),
61
+ );
62
+ const response = await handleSessionCommands({
63
+ req: {
64
+ token: 't',
65
+ session: sessionName,
66
+ command: 'boot',
67
+ positionals: [],
68
+ flags: {},
69
+ },
70
+ sessionName,
71
+ logPath: path.join(os.tmpdir(), 'daemon.log'),
72
+ sessionStore,
73
+ invoke: noopInvoke,
74
+ ensureReady: async () => {
75
+ throw new Error('ensureReady should not be called for unsupported boot');
76
+ },
77
+ });
78
+ assert.ok(response);
79
+ assert.equal(response?.ok, false);
80
+ if (response && !response.ok) {
81
+ assert.equal(response.error.code, 'UNSUPPORTED_OPERATION');
82
+ }
83
+ });
84
+
85
+ test('boot succeeds for supported device in session', async () => {
86
+ const sessionStore = makeSessionStore();
87
+ const sessionName = 'android-session';
88
+ sessionStore.set(
89
+ sessionName,
90
+ makeSession(sessionName, {
91
+ platform: 'android',
92
+ id: 'emulator-5554',
93
+ name: 'Pixel Emulator',
94
+ kind: 'emulator',
95
+ booted: true,
96
+ }),
97
+ );
98
+ let ensureCalls = 0;
99
+ const response = await handleSessionCommands({
100
+ req: {
101
+ token: 't',
102
+ session: sessionName,
103
+ command: 'boot',
104
+ positionals: [],
105
+ flags: {},
106
+ },
107
+ sessionName,
108
+ logPath: path.join(os.tmpdir(), 'daemon.log'),
109
+ sessionStore,
110
+ invoke: noopInvoke,
111
+ ensureReady: async () => {
112
+ ensureCalls += 1;
113
+ },
114
+ });
115
+ assert.ok(response);
116
+ assert.equal(response?.ok, true);
117
+ assert.equal(ensureCalls, 1);
118
+ if (response && response.ok) {
119
+ assert.equal(response.data?.platform, 'android');
120
+ assert.equal(response.data?.booted, true);
121
+ }
122
+ });
@@ -1,5 +1,5 @@
1
1
  import { dispatchCommand, resolveTargetDevice } from '../../core/dispatch.ts';
2
- import { findNodeByLocator, type FindLocator } from '../../utils/finders.ts';
2
+ import { findBestMatchesByLocator, type FindLocator } from '../../utils/finders.ts';
3
3
  import { attachRefs, centerOfRect, type RawSnapshotNode, type SnapshotState } from '../../utils/snapshot.ts';
4
4
  import { AppError } from '../../utils/errors.ts';
5
5
  import type { DaemonRequest, DaemonResponse } from '../types.ts';
@@ -97,7 +97,7 @@ export async function handleFindCommands(params: {
97
97
  const start = Date.now();
98
98
  while (Date.now() - start < timeout) {
99
99
  const { nodes } = await fetchNodes();
100
- const match = findNodeByLocator(nodes, locator, query, { requireRect: false });
100
+ const match = findBestMatchesByLocator(nodes, locator, query, { requireRect: false }).matches[0];
101
101
  if (match) {
102
102
  if (session) {
103
103
  sessionStore.recordAction(session, {
@@ -114,7 +114,27 @@ export async function handleFindCommands(params: {
114
114
  return { ok: false, error: { code: 'COMMAND_FAILED', message: 'find wait timed out' } };
115
115
  }
116
116
  const { nodes } = await fetchNodes();
117
- const node = findNodeByLocator(nodes, locator, query, { requireRect: requiresRect });
117
+ const bestMatches = findBestMatchesByLocator(nodes, locator, query, { requireRect: requiresRect });
118
+ if (requiresRect && bestMatches.matches.length > 1) {
119
+ const candidates = bestMatches.matches.slice(0, 8).map((candidate) => {
120
+ const label = extractNodeText(candidate) || candidate.label || candidate.identifier || candidate.type || '';
121
+ return `@${candidate.ref}${label ? `(${label})` : ''}`;
122
+ });
123
+ return {
124
+ ok: false,
125
+ error: {
126
+ code: 'AMBIGUOUS_MATCH',
127
+ message: `find matched ${bestMatches.matches.length} elements for ${locator} "${query}". Use a more specific locator or selector.`,
128
+ details: {
129
+ locator,
130
+ query,
131
+ matches: bestMatches.matches.length,
132
+ candidates,
133
+ },
134
+ },
135
+ };
136
+ }
137
+ const node = bestMatches.matches[0] ?? null;
118
138
  if (!node) {
119
139
  return { ok: false, error: { code: 'COMMAND_FAILED', message: 'find did not match any element' } };
120
140
  }