agent-device 0.3.4 → 0.4.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 (42) hide show
  1. package/README.md +58 -16
  2. package/dist/src/bin.js +35 -96
  3. package/dist/src/daemon.js +16 -15
  4. package/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift +24 -0
  5. package/ios-runner/README.md +1 -1
  6. package/package.json +1 -1
  7. package/skills/agent-device/SKILL.md +32 -14
  8. package/skills/agent-device/references/permissions.md +15 -1
  9. package/skills/agent-device/references/session-management.md +2 -0
  10. package/skills/agent-device/references/snapshot-refs.md +2 -0
  11. package/skills/agent-device/references/video-recording.md +2 -0
  12. package/src/cli.ts +7 -3
  13. package/src/core/__tests__/capabilities.test.ts +11 -6
  14. package/src/core/__tests__/open-target.test.ts +16 -0
  15. package/src/core/capabilities.ts +26 -20
  16. package/src/core/dispatch.ts +110 -31
  17. package/src/core/open-target.ts +13 -0
  18. package/src/daemon/__tests__/app-state.test.ts +138 -0
  19. package/src/daemon/__tests__/session-store.test.ts +24 -0
  20. package/src/daemon/app-state.ts +37 -38
  21. package/src/daemon/context.ts +12 -0
  22. package/src/daemon/handlers/__tests__/interaction.test.ts +22 -0
  23. package/src/daemon/handlers/__tests__/session.test.ts +226 -5
  24. package/src/daemon/handlers/__tests__/snapshot-handler.test.ts +92 -0
  25. package/src/daemon/handlers/interaction.ts +37 -0
  26. package/src/daemon/handlers/record-trace.ts +1 -1
  27. package/src/daemon/handlers/session.ts +96 -26
  28. package/src/daemon/handlers/snapshot.ts +21 -3
  29. package/src/daemon/session-store.ts +11 -0
  30. package/src/daemon-client.ts +14 -6
  31. package/src/daemon.ts +1 -1
  32. package/src/platforms/android/__tests__/index.test.ts +67 -1
  33. package/src/platforms/android/index.ts +41 -0
  34. package/src/platforms/ios/__tests__/index.test.ts +24 -0
  35. package/src/platforms/ios/__tests__/runner-client.test.ts +113 -0
  36. package/src/platforms/ios/devices.ts +40 -18
  37. package/src/platforms/ios/index.ts +70 -5
  38. package/src/platforms/ios/runner-client.ts +329 -42
  39. package/src/utils/__tests__/args.test.ts +175 -0
  40. package/src/utils/args.ts +174 -212
  41. package/src/utils/command-schema.ts +591 -0
  42. package/src/utils/interactors.ts +13 -3
@@ -0,0 +1,138 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import type { DeviceInfo } from '../../utils/device.ts';
4
+ import { resolveIosAppStateFromSnapshots } from '../app-state.ts';
5
+ import type { CommandFlags, dispatchCommand } from '../../core/dispatch.ts';
6
+
7
+ const iosSimulator: DeviceInfo = {
8
+ platform: 'ios',
9
+ id: 'sim-1',
10
+ name: 'iPhone Simulator',
11
+ kind: 'simulator',
12
+ booted: true,
13
+ };
14
+
15
+ const iosDevice: DeviceInfo = {
16
+ platform: 'ios',
17
+ id: '00008110-000E12341234002E',
18
+ name: 'iPhone',
19
+ kind: 'device',
20
+ booted: true,
21
+ };
22
+
23
+ test('appstate uses xctest first on iOS simulator', async () => {
24
+ const backends: string[] = [];
25
+ const fakeDispatch: typeof dispatchCommand = async (_device, _command, _positionals, _outPath, context) => {
26
+ backends.push(context?.snapshotBackend ?? 'unknown');
27
+ return {
28
+ nodes: [
29
+ {
30
+ type: 'XCUIElementTypeApplication',
31
+ label: 'Settings',
32
+ identifier: 'com.apple.Preferences',
33
+ },
34
+ ],
35
+ };
36
+ };
37
+
38
+ const result = await resolveIosAppStateFromSnapshots(
39
+ iosSimulator,
40
+ '/tmp/daemon.log',
41
+ undefined,
42
+ {} as CommandFlags,
43
+ fakeDispatch,
44
+ );
45
+
46
+ assert.deepEqual(backends, ['xctest']);
47
+ assert.equal(result.source, 'snapshot-xctest');
48
+ assert.equal(result.appBundleId, 'com.apple.Preferences');
49
+ });
50
+
51
+ test('appstate does not try ax on iOS device when xctest succeeds', async () => {
52
+ const backends: string[] = [];
53
+ const fakeDispatch: typeof dispatchCommand = async (_device, _command, _positionals, _outPath, context) => {
54
+ backends.push(context?.snapshotBackend ?? 'unknown');
55
+ return {
56
+ nodes: [
57
+ {
58
+ type: 'XCUIElementTypeApplication',
59
+ label: 'Settings',
60
+ identifier: 'com.apple.Preferences',
61
+ },
62
+ ],
63
+ };
64
+ };
65
+
66
+ const result = await resolveIosAppStateFromSnapshots(
67
+ iosDevice,
68
+ '/tmp/daemon.log',
69
+ undefined,
70
+ {} as CommandFlags,
71
+ fakeDispatch,
72
+ );
73
+
74
+ assert.deepEqual(backends, ['xctest']);
75
+ assert.equal(result.source, 'snapshot-xctest');
76
+ assert.equal(result.appName, 'Settings');
77
+ });
78
+
79
+ test('appstate fails on simulator when xctest is empty', async () => {
80
+ const backends: string[] = [];
81
+ const fakeDispatch: typeof dispatchCommand = async (_device, _command, _positionals, _outPath, context) => {
82
+ backends.push(context?.snapshotBackend ?? 'unknown');
83
+ return { nodes: [] };
84
+ };
85
+
86
+ await assert.rejects(
87
+ resolveIosAppStateFromSnapshots(
88
+ iosSimulator,
89
+ '/tmp/daemon.log',
90
+ undefined,
91
+ {} as CommandFlags,
92
+ fakeDispatch,
93
+ ),
94
+ /not recommended/,
95
+ );
96
+ assert.deepEqual(backends, ['xctest']);
97
+ });
98
+
99
+ test('appstate fails on simulator when xctest throws', async () => {
100
+ const backends: string[] = [];
101
+ const fakeDispatch: typeof dispatchCommand = async (_device, _command, _positionals, _outPath, context) => {
102
+ const backend = context?.snapshotBackend ?? 'unknown';
103
+ backends.push(backend);
104
+ throw new Error('xctest failed');
105
+ };
106
+
107
+ await assert.rejects(
108
+ resolveIosAppStateFromSnapshots(
109
+ iosSimulator,
110
+ '/tmp/daemon.log',
111
+ undefined,
112
+ {} as CommandFlags,
113
+ fakeDispatch,
114
+ ),
115
+ /not recommended/,
116
+ );
117
+ assert.deepEqual(backends, ['xctest']);
118
+ });
119
+
120
+ test('appstate fails on device when xctest throws', async () => {
121
+ const backends: string[] = [];
122
+ const fakeDispatch: typeof dispatchCommand = async (_device, _command, _positionals, _outPath, context) => {
123
+ backends.push(context?.snapshotBackend ?? 'unknown');
124
+ throw new Error('xctest failed');
125
+ };
126
+
127
+ await assert.rejects(
128
+ resolveIosAppStateFromSnapshots(
129
+ iosDevice,
130
+ '/tmp/daemon.log',
131
+ undefined,
132
+ {} as CommandFlags,
133
+ fakeDispatch,
134
+ ),
135
+ /not recommended/,
136
+ );
137
+ assert.deepEqual(backends, ['xctest']);
138
+ });
@@ -93,3 +93,27 @@ test('saveScript flag enables .ad session log writing', () => {
93
93
  const files = fs.readdirSync(root);
94
94
  assert.equal(files.filter((file) => file.endsWith('.ad')).length, 1);
95
95
  });
96
+
97
+ test('writeSessionLog persists open --relaunch in script output', () => {
98
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-session-log-relaunch-'));
99
+ const store = new SessionStore(root);
100
+ const session = makeSession('default');
101
+ store.recordAction(session, {
102
+ command: 'open',
103
+ positionals: ['Settings'],
104
+ flags: { platform: 'ios', saveScript: true, relaunch: true },
105
+ result: {},
106
+ });
107
+ store.recordAction(session, {
108
+ command: 'close',
109
+ positionals: [],
110
+ flags: { platform: 'ios' },
111
+ result: {},
112
+ });
113
+
114
+ store.writeSessionLog(session);
115
+ const scriptFile = fs.readdirSync(root).find((file) => file.endsWith('.ad'));
116
+ assert.ok(scriptFile);
117
+ const script = fs.readFileSync(path.join(root, scriptFile!), 'utf8');
118
+ assert.match(script, /open "Settings" --relaunch/);
119
+ });
@@ -1,6 +1,7 @@
1
1
  import { dispatchCommand, type CommandFlags } from '../core/dispatch.ts';
2
2
  import type { DeviceInfo } from '../utils/device.ts';
3
3
  import { attachRefs, type RawSnapshotNode } from '../utils/snapshot.ts';
4
+ import { AppError } from '../utils/errors.ts';
4
5
  import { contextFromFlags } from './context.ts';
5
6
  import { normalizeType } from './snapshot-processing.ts';
6
7
 
@@ -9,47 +10,45 @@ export async function resolveIosAppStateFromSnapshots(
9
10
  logPath: string,
10
11
  traceLogPath: string | undefined,
11
12
  flags: CommandFlags | undefined,
12
- ): Promise<{ appName: string; appBundleId?: string; source: 'snapshot-ax' | 'snapshot-xctest' }> {
13
- const axResult = await dispatchCommand(device, 'snapshot', [], flags?.out, {
14
- ...contextFromFlags(
15
- logPath,
16
- {
17
- ...flags,
18
- snapshotDepth: 1,
19
- snapshotCompact: true,
20
- snapshotBackend: 'ax',
21
- },
22
- undefined,
23
- traceLogPath,
24
- ),
25
- });
26
- const axNode = extractAppNodeFromSnapshot(axResult as { nodes?: RawSnapshotNode[] });
27
- if (axNode?.appName || axNode?.appBundleId) {
13
+ dispatch: typeof dispatchCommand = dispatchCommand,
14
+ ): Promise<{ appName: string; appBundleId?: string; source: 'snapshot-xctest' }> {
15
+ let xctestResult: { nodes?: RawSnapshotNode[] } | undefined;
16
+ try {
17
+ xctestResult = (await dispatch(device, 'snapshot', [], flags?.out, {
18
+ ...contextFromFlags(
19
+ logPath,
20
+ {
21
+ ...flags,
22
+ snapshotDepth: 1,
23
+ snapshotCompact: true,
24
+ snapshotBackend: 'xctest',
25
+ },
26
+ undefined,
27
+ traceLogPath,
28
+ ),
29
+ })) as { nodes?: RawSnapshotNode[] };
30
+ } catch (error) {
31
+ const cause = error instanceof Error ? error.message : String(error);
32
+ throw new AppError(
33
+ 'COMMAND_FAILED',
34
+ 'Unable to resolve iOS app state from XCTest snapshot. You can try snapshot --backend ax for diagnostics, but AX snapshots are not recommended.',
35
+ { cause },
36
+ );
37
+ }
38
+
39
+ const xcNode = extractAppNodeFromSnapshot(xctestResult);
40
+ if (xcNode?.appName || xcNode?.appBundleId) {
28
41
  return {
29
- appName: axNode.appName ?? axNode.appBundleId ?? 'unknown',
30
- appBundleId: axNode.appBundleId,
31
- source: 'snapshot-ax',
42
+ appName: xcNode.appName ?? xcNode.appBundleId ?? 'unknown',
43
+ appBundleId: xcNode.appBundleId,
44
+ source: 'snapshot-xctest',
32
45
  };
33
46
  }
34
- const xctestResult = await dispatchCommand(device, 'snapshot', [], flags?.out, {
35
- ...contextFromFlags(
36
- logPath,
37
- {
38
- ...flags,
39
- snapshotDepth: 1,
40
- snapshotCompact: true,
41
- snapshotBackend: 'xctest',
42
- },
43
- undefined,
44
- traceLogPath,
45
- ),
46
- });
47
- const xcNode = extractAppNodeFromSnapshot(xctestResult as { nodes?: RawSnapshotNode[] });
48
- return {
49
- appName: xcNode?.appName ?? xcNode?.appBundleId ?? 'unknown',
50
- appBundleId: xcNode?.appBundleId,
51
- source: 'snapshot-xctest',
52
- };
47
+
48
+ throw new AppError(
49
+ 'COMMAND_FAILED',
50
+ 'Unable to resolve iOS app state from XCTest snapshot (0 nodes or missing application node). You can try snapshot --backend ax for diagnostics, but AX snapshots are not recommended.',
51
+ );
53
52
  }
54
53
 
55
54
  function extractAppNodeFromSnapshot(
@@ -12,6 +12,12 @@ export type DaemonCommandContext = {
12
12
  snapshotScope?: string;
13
13
  snapshotBackend?: 'ax' | 'xctest';
14
14
  snapshotRaw?: boolean;
15
+ count?: number;
16
+ intervalMs?: number;
17
+ holdMs?: number;
18
+ jitterPx?: number;
19
+ pauseMs?: number;
20
+ pattern?: 'one-way' | 'ping-pong';
15
21
  };
16
22
 
17
23
  export function contextFromFlags(
@@ -32,5 +38,11 @@ export function contextFromFlags(
32
38
  snapshotScope: flags?.snapshotScope,
33
39
  snapshotRaw: flags?.snapshotRaw,
34
40
  snapshotBackend: flags?.snapshotBackend,
41
+ count: flags?.count,
42
+ intervalMs: flags?.intervalMs,
43
+ holdMs: flags?.holdMs,
44
+ jitterPx: flags?.jitterPx,
45
+ pauseMs: flags?.pauseMs,
46
+ pattern: flags?.pattern,
35
47
  };
36
48
  }
@@ -0,0 +1,22 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { unsupportedRefSnapshotFlags } from '../interaction.ts';
4
+
5
+ test('unsupportedRefSnapshotFlags returns unsupported snapshot flags for @ref flows', () => {
6
+ const unsupported = unsupportedRefSnapshotFlags({
7
+ snapshotDepth: 2,
8
+ snapshotScope: 'Login',
9
+ snapshotRaw: true,
10
+ snapshotBackend: 'ax',
11
+ });
12
+ assert.deepEqual(unsupported, ['--depth', '--scope', '--raw', '--backend']);
13
+ });
14
+
15
+ test('unsupportedRefSnapshotFlags returns empty when no ref-unsupported flags are present', () => {
16
+ const unsupported = unsupportedRefSnapshotFlags({
17
+ platform: 'ios',
18
+ session: 'default',
19
+ verbose: true,
20
+ });
21
+ assert.deepEqual(unsupported, []);
22
+ });
@@ -46,7 +46,7 @@ test('boot requires session or explicit selector', async () => {
46
46
  }
47
47
  });
48
48
 
49
- test('boot rejects unsupported iOS device kind', async () => {
49
+ test('boot succeeds for iOS physical devices', async () => {
50
50
  const sessionStore = makeSessionStore();
51
51
  const sessionName = 'ios-device-session';
52
52
  sessionStore.set(
@@ -59,6 +59,7 @@ test('boot rejects unsupported iOS device kind', async () => {
59
59
  booted: true,
60
60
  }),
61
61
  );
62
+ let ensureCalls = 0;
62
63
  const response = await handleSessionCommands({
63
64
  req: {
64
65
  token: 't',
@@ -72,13 +73,15 @@ test('boot rejects unsupported iOS device kind', async () => {
72
73
  sessionStore,
73
74
  invoke: noopInvoke,
74
75
  ensureReady: async () => {
75
- throw new Error('ensureReady should not be called for unsupported boot');
76
+ ensureCalls += 1;
76
77
  },
77
78
  });
78
79
  assert.ok(response);
79
- assert.equal(response?.ok, false);
80
- if (response && !response.ok) {
81
- assert.equal(response.error.code, 'UNSUPPORTED_OPERATION');
80
+ assert.equal(response?.ok, true);
81
+ assert.equal(ensureCalls, 1);
82
+ if (response && response.ok) {
83
+ assert.equal(response.data?.platform, 'ios');
84
+ assert.equal(response.data?.booted, true);
82
85
  }
83
86
  });
84
87
 
@@ -120,3 +123,221 @@ test('boot succeeds for supported device in session', async () => {
120
123
  assert.equal(response.data?.booted, true);
121
124
  }
122
125
  });
126
+
127
+ test('open URL on existing iOS session clears stale app bundle id', async () => {
128
+ const sessionStore = makeSessionStore();
129
+ const sessionName = 'ios-session';
130
+ sessionStore.set(
131
+ sessionName,
132
+ {
133
+ ...makeSession(sessionName, {
134
+ platform: 'ios',
135
+ id: 'sim-1',
136
+ name: 'iPhone 15',
137
+ kind: 'simulator',
138
+ booted: true,
139
+ }),
140
+ appBundleId: 'com.example.old',
141
+ appName: 'Old App',
142
+ },
143
+ );
144
+
145
+ let dispatchedContext: Record<string, unknown> | undefined;
146
+ const response = await handleSessionCommands({
147
+ req: {
148
+ token: 't',
149
+ session: sessionName,
150
+ command: 'open',
151
+ positionals: ['https://example.com/path'],
152
+ flags: {},
153
+ },
154
+ sessionName,
155
+ logPath: path.join(os.tmpdir(), 'daemon.log'),
156
+ sessionStore,
157
+ invoke: noopInvoke,
158
+ dispatch: async (_device, _command, _positionals, _out, context) => {
159
+ dispatchedContext = context as Record<string, unknown> | undefined;
160
+ return {};
161
+ },
162
+ ensureReady: async () => {},
163
+ });
164
+
165
+ assert.ok(response);
166
+ assert.equal(response?.ok, true);
167
+ const updated = sessionStore.get(sessionName);
168
+ assert.equal(updated?.appBundleId, undefined);
169
+ assert.equal(updated?.appName, 'https://example.com/path');
170
+ assert.equal(dispatchedContext?.appBundleId, undefined);
171
+ });
172
+
173
+ test('open app on existing iOS session resolves and stores bundle id', async () => {
174
+ const sessionStore = makeSessionStore();
175
+ const sessionName = 'ios-session';
176
+ sessionStore.set(
177
+ sessionName,
178
+ {
179
+ ...makeSession(sessionName, {
180
+ platform: 'ios',
181
+ id: 'sim-1',
182
+ name: 'iPhone 15',
183
+ kind: 'simulator',
184
+ booted: true,
185
+ }),
186
+ appBundleId: 'com.example.old',
187
+ appName: 'Old App',
188
+ },
189
+ );
190
+
191
+ let dispatchedContext: Record<string, unknown> | undefined;
192
+ const response = await handleSessionCommands({
193
+ req: {
194
+ token: 't',
195
+ session: sessionName,
196
+ command: 'open',
197
+ positionals: ['settings'],
198
+ flags: {},
199
+ },
200
+ sessionName,
201
+ logPath: path.join(os.tmpdir(), 'daemon.log'),
202
+ sessionStore,
203
+ invoke: noopInvoke,
204
+ dispatch: async (_device, _command, _positionals, _out, context) => {
205
+ dispatchedContext = context as Record<string, unknown> | undefined;
206
+ return {};
207
+ },
208
+ ensureReady: async () => {},
209
+ });
210
+
211
+ assert.ok(response);
212
+ assert.equal(response?.ok, true);
213
+ const updated = sessionStore.get(sessionName);
214
+ assert.equal(updated?.appBundleId, 'com.apple.Preferences');
215
+ assert.equal(updated?.appName, 'settings');
216
+ assert.equal(dispatchedContext?.appBundleId, 'com.apple.Preferences');
217
+ });
218
+
219
+ test('open --relaunch closes and reopens active session app', async () => {
220
+ const sessionStore = makeSessionStore();
221
+ const sessionName = 'android-session';
222
+ sessionStore.set(
223
+ sessionName,
224
+ {
225
+ ...makeSession(sessionName, {
226
+ platform: 'android',
227
+ id: 'emulator-5554',
228
+ name: 'Pixel Emulator',
229
+ kind: 'emulator',
230
+ booted: true,
231
+ }),
232
+ appName: 'com.example.app',
233
+ },
234
+ );
235
+
236
+ const calls: Array<{ command: string; positionals: string[] }> = [];
237
+ const response = await handleSessionCommands({
238
+ req: {
239
+ token: 't',
240
+ session: sessionName,
241
+ command: 'open',
242
+ positionals: [],
243
+ flags: { relaunch: true },
244
+ },
245
+ sessionName,
246
+ logPath: path.join(os.tmpdir(), 'daemon.log'),
247
+ sessionStore,
248
+ invoke: noopInvoke,
249
+ dispatch: async (_device, command, positionals) => {
250
+ calls.push({ command, positionals });
251
+ return {};
252
+ },
253
+ });
254
+
255
+ assert.ok(response);
256
+ assert.equal(response?.ok, true);
257
+ assert.equal(calls.length, 2);
258
+ assert.deepEqual(calls[0], { command: 'close', positionals: ['com.example.app'] });
259
+ assert.deepEqual(calls[1], { command: 'open', positionals: ['com.example.app'] });
260
+ });
261
+
262
+ test('open --relaunch rejects URL targets', async () => {
263
+ const sessionStore = makeSessionStore();
264
+ const response = await handleSessionCommands({
265
+ req: {
266
+ token: 't',
267
+ session: 'default',
268
+ command: 'open',
269
+ positionals: ['https://example.com/path'],
270
+ flags: { relaunch: true },
271
+ },
272
+ sessionName: 'default',
273
+ logPath: path.join(os.tmpdir(), 'daemon.log'),
274
+ sessionStore,
275
+ invoke: noopInvoke,
276
+ });
277
+
278
+ assert.ok(response);
279
+ assert.equal(response?.ok, false);
280
+ if (response && !response.ok) {
281
+ assert.equal(response.error.code, 'INVALID_ARGS');
282
+ assert.match(response.error.message, /does not support URL targets/i);
283
+ }
284
+ });
285
+
286
+ test('open --relaunch fails without app when no session exists', async () => {
287
+ const sessionStore = makeSessionStore();
288
+ const response = await handleSessionCommands({
289
+ req: {
290
+ token: 't',
291
+ session: 'default',
292
+ command: 'open',
293
+ positionals: [],
294
+ flags: { relaunch: true },
295
+ },
296
+ sessionName: 'default',
297
+ logPath: path.join(os.tmpdir(), 'daemon.log'),
298
+ sessionStore,
299
+ invoke: noopInvoke,
300
+ });
301
+
302
+ assert.ok(response);
303
+ assert.equal(response?.ok, false);
304
+ if (response && !response.ok) {
305
+ assert.equal(response.error.code, 'INVALID_ARGS');
306
+ assert.match(response.error.message, /requires an app argument/i);
307
+ }
308
+ });
309
+
310
+ test('replay parses open --relaunch flag and replays open with relaunch semantics', async () => {
311
+ const sessionStore = makeSessionStore();
312
+ const replayRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-replay-relaunch-'));
313
+ const replayPath = path.join(replayRoot, 'relaunch.ad');
314
+ fs.writeFileSync(replayPath, 'open "Settings" --relaunch\n');
315
+
316
+ const invoked: DaemonRequest[] = [];
317
+ const response = await handleSessionCommands({
318
+ req: {
319
+ token: 't',
320
+ session: 'default',
321
+ command: 'replay',
322
+ positionals: [replayPath],
323
+ flags: {},
324
+ },
325
+ sessionName: 'default',
326
+ logPath: path.join(os.tmpdir(), 'daemon.log'),
327
+ sessionStore,
328
+ invoke: async (req) => {
329
+ invoked.push(req);
330
+ return { ok: true, data: {} };
331
+ },
332
+ });
333
+
334
+ assert.ok(response);
335
+ assert.equal(response?.ok, true);
336
+ if (response && response.ok) {
337
+ assert.equal(response.data?.replayed, 1);
338
+ }
339
+ assert.equal(invoked.length, 1);
340
+ assert.equal(invoked[0]?.command, 'open');
341
+ assert.deepEqual(invoked[0]?.positionals, ['Settings']);
342
+ assert.equal(invoked[0]?.flags?.relaunch, true);
343
+ });
@@ -0,0 +1,92 @@
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 { handleSnapshotCommands } from '../snapshot.ts';
7
+ import { SessionStore } from '../../session-store.ts';
8
+ import type { SessionState } from '../../types.ts';
9
+
10
+ function makeSessionStore(): SessionStore {
11
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-snapshot-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
+ test('snapshot rejects AX backend on iOS physical devices', async () => {
25
+ const sessionStore = makeSessionStore();
26
+ const sessionName = 'ios-device';
27
+ sessionStore.set(
28
+ sessionName,
29
+ makeSession(sessionName, {
30
+ platform: 'ios',
31
+ id: 'ios-device-1',
32
+ name: 'My iPhone',
33
+ kind: 'device',
34
+ booted: true,
35
+ }),
36
+ );
37
+
38
+ const response = await handleSnapshotCommands({
39
+ req: {
40
+ token: 't',
41
+ session: sessionName,
42
+ command: 'snapshot',
43
+ positionals: [],
44
+ flags: { snapshotBackend: 'ax' },
45
+ },
46
+ sessionName,
47
+ logPath: '/tmp/daemon.log',
48
+ sessionStore,
49
+ });
50
+
51
+ assert.ok(response);
52
+ assert.equal(response?.ok, false);
53
+ if (response && !response.ok) {
54
+ assert.equal(response.error.code, 'UNSUPPORTED_OPERATION');
55
+ assert.match(response.error.message, /AX snapshot backend is not supported/i);
56
+ }
57
+ });
58
+
59
+ test('settings rejects unsupported iOS physical devices', async () => {
60
+ const sessionStore = makeSessionStore();
61
+ const sessionName = 'ios-device';
62
+ sessionStore.set(
63
+ sessionName,
64
+ makeSession(sessionName, {
65
+ platform: 'ios',
66
+ id: 'ios-device-1',
67
+ name: 'My iPhone',
68
+ kind: 'device',
69
+ booted: true,
70
+ }),
71
+ );
72
+
73
+ const response = await handleSnapshotCommands({
74
+ req: {
75
+ token: 't',
76
+ session: sessionName,
77
+ command: 'settings',
78
+ positionals: ['wifi', 'on'],
79
+ flags: {},
80
+ },
81
+ sessionName,
82
+ logPath: '/tmp/daemon.log',
83
+ sessionStore,
84
+ });
85
+
86
+ assert.ok(response);
87
+ assert.equal(response?.ok, false);
88
+ if (response && !response.ok) {
89
+ assert.equal(response.error.code, 'UNSUPPORTED_OPERATION');
90
+ assert.match(response.error.message, /settings is not supported/i);
91
+ }
92
+ });