agent-device 0.3.1 → 0.3.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.
@@ -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 } },
@@ -225,6 +225,11 @@ 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
  });
@@ -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
  }