-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathcli-diagnostics.test.ts
More file actions
311 lines (288 loc) · 11 KB
/
Copy pathcli-diagnostics.test.ts
File metadata and controls
311 lines (288 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { test } from 'vitest';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { runCli } from '../cli.ts';
import type { DaemonRequest, DaemonResponse } from '../daemon-client.ts';
import { resolveDaemonPaths } from '../daemon/config.ts';
class ExitSignal extends Error {
public readonly code: number;
constructor(code: number) {
super(`EXIT_${code}`);
this.code = code;
}
}
type RunResult = {
code: number | null;
stdout: string;
stderr: string;
calls: Omit<DaemonRequest, 'token'>[];
};
async function runCliCapture(
argv: string[],
responder: (req: Omit<DaemonRequest, 'token'>) => Promise<DaemonResponse>,
): Promise<RunResult> {
let stdout = '';
let stderr = '';
let code: number | null = null;
const calls: Array<Omit<DaemonRequest, 'token'>> = [];
const originalExit = process.exit;
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
const originalStderrWrite = process.stderr.write.bind(process.stderr);
const originalStateDir = process.env.AGENT_DEVICE_STATE_DIR;
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-cli-diagnostics-'));
process.env.AGENT_DEVICE_STATE_DIR = stateDir;
(process as any).exit = ((nextCode?: number) => {
throw new ExitSignal(nextCode ?? 0);
}) as typeof process.exit;
(process.stdout as any).write = ((chunk: unknown) => {
stdout += String(chunk);
return true;
}) as typeof process.stdout.write;
(process.stderr as any).write = ((chunk: unknown) => {
stderr += String(chunk);
return true;
}) as typeof process.stderr.write;
const sendToDaemon = async (req: Omit<DaemonRequest, 'token'>): Promise<DaemonResponse> => {
calls.push(req);
return await responder(req);
};
try {
await runCli(argv, { sendToDaemon });
} catch (error) {
if (error instanceof ExitSignal) code = error.code;
else throw error;
} finally {
if (originalStateDir === undefined) delete process.env.AGENT_DEVICE_STATE_DIR;
else process.env.AGENT_DEVICE_STATE_DIR = originalStateDir;
fs.rmSync(stateDir, { recursive: true, force: true });
process.exit = originalExit;
process.stdout.write = originalStdoutWrite;
process.stderr.write = originalStderrWrite;
}
return { code, stdout, stderr, calls };
}
test('cli forwards --debug as verbose/debug metadata', async () => {
const result = await runCliCapture(['open', 'settings', '--debug', '--json'], async () => ({
ok: true,
data: {
app: 'settings',
platform: 'ios',
target: 'mobile',
device: 'iPhone 16',
id: 'SIM-001',
},
}));
assert.equal(result.code, null);
assert.equal(result.calls.length, 1);
assert.equal(result.calls[0]?.command, 'open');
assert.equal(result.calls[0]?.flags?.verbose, true);
assert.equal(result.calls[0]?.meta?.debug, true);
assert.equal(result.calls[0]?.meta?.cwd, process.cwd());
assert.equal(typeof result.calls[0]?.meta?.requestId, 'string');
});
test('cli does not tail local daemon log when remote daemon base URL is set', async () => {
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-cli-remote-'));
const daemonPaths = resolveDaemonPaths(stateDir);
fs.mkdirSync(path.dirname(daemonPaths.logPath), { recursive: true });
fs.writeFileSync(daemonPaths.logPath, 'REMOTE_TAIL_SENTINEL\n', 'utf8');
const previousBaseUrl = process.env.AGENT_DEVICE_DAEMON_BASE_URL;
const previousAuthToken = process.env.AGENT_DEVICE_DAEMON_AUTH_TOKEN;
process.env.AGENT_DEVICE_DAEMON_BASE_URL = 'http://remote-mac.example.test:7777/agent-device';
process.env.AGENT_DEVICE_DAEMON_AUTH_TOKEN = 'remote-secret';
try {
const result = await runCliCapture(
['clipboard', 'write', 'hello', '--debug', '--state-dir', stateDir],
async () => {
await new Promise((resolve) => setTimeout(resolve, 300));
return {
ok: true,
data: { action: 'write', message: 'Clipboard updated' },
};
},
);
assert.equal(result.code, null);
assert.equal(result.stdout.includes('REMOTE_TAIL_SENTINEL'), false);
assert.match(result.stdout, /Clipboard updated/);
} finally {
if (previousBaseUrl === undefined) delete process.env.AGENT_DEVICE_DAEMON_BASE_URL;
else process.env.AGENT_DEVICE_DAEMON_BASE_URL = previousBaseUrl;
if (previousAuthToken === undefined) delete process.env.AGENT_DEVICE_DAEMON_AUTH_TOKEN;
else process.env.AGENT_DEVICE_DAEMON_AUTH_TOKEN = previousAuthToken;
fs.rmSync(stateDir, { recursive: true, force: true });
}
});
test('cli returns normalized JSON failures with diagnostics fields', async () => {
const result = await runCliCapture(['open', 'settings', '--json'], async () => ({
ok: false,
error: {
code: 'COMMAND_FAILED',
message: 'boom',
hint: 'retry later',
diagnosticId: 'diag-123',
logPath: '/tmp/diag.ndjson',
details: { token: 'secret', safe: 'ok' },
},
}));
assert.equal(result.code, 1);
const payload = JSON.parse(result.stdout);
assert.equal(payload.success, false);
assert.equal(payload.error.code, 'COMMAND_FAILED');
assert.equal(payload.error.hint, 'retry later');
assert.equal(payload.error.diagnosticId, 'diag-123');
assert.equal(payload.error.logPath, '/tmp/diag.ndjson');
assert.equal(payload.error.details.token, '[REDACTED]');
assert.equal(payload.error.details.safe, 'ok');
});
test('cli parse failures include diagnostic references in JSON mode', async () => {
const previousHome = process.env.HOME;
process.env.HOME = '/tmp';
try {
const result = await runCliCapture(['open', '--unknown-flag', '--json'], async () => ({
ok: true,
data: {},
}));
assert.equal(result.code, 1);
assert.equal(result.calls.length, 0);
const payload = JSON.parse(result.stdout);
assert.equal(payload.success, false);
assert.equal(payload.error.code, 'INVALID_ARGS');
assert.equal(typeof payload.error.diagnosticId, 'string');
assert.equal(typeof payload.error.logPath, 'string');
} finally {
process.env.HOME = previousHome;
}
});
test('cli forwards save-script and no-record flags for client-backed open', async () => {
const result = await runCliCapture(
['open', 'settings', '--save-script', '--no-record', '--json'],
async () => ({
ok: true,
data: {
app: 'settings',
platform: 'ios',
target: 'mobile',
device: 'iPhone 16',
id: 'SIM-001',
},
}),
);
assert.equal(result.code, null);
assert.equal(result.calls.length, 1);
assert.equal(result.calls[0]?.command, 'open');
assert.equal(result.calls[0]?.flags?.saveScript, true);
assert.equal(result.calls[0]?.flags?.noRecord, true);
});
test('cli preserves --out for client-backed screenshot', async () => {
const result = await runCliCapture(
['screenshot', '--out', '/tmp/shot.png', '--json'],
async () => ({
ok: true,
data: { path: '/tmp/shot.png' },
}),
);
assert.equal(result.code, null);
assert.equal(result.calls.length, 1);
assert.equal(result.calls[0]?.command, 'screenshot');
assert.deepEqual(result.calls[0]?.positionals, ['/tmp/shot.png']);
});
test('cli applies AGENT_DEVICE_PLATFORM to client-backed commands', async () => {
const previousPlatform = process.env.AGENT_DEVICE_PLATFORM;
process.env.AGENT_DEVICE_PLATFORM = 'android';
try {
const result = await runCliCapture(['open', 'com.example.app', '--json'], async () => ({
ok: true,
data: {
app: 'com.example.app',
platform: 'android',
target: 'mobile',
device: 'Pixel 9',
id: 'emulator-5554',
},
}));
assert.equal(result.code, null);
assert.equal(result.calls[0]?.flags?.platform, 'android');
} finally {
if (previousPlatform === undefined) delete process.env.AGENT_DEVICE_PLATFORM;
else process.env.AGENT_DEVICE_PLATFORM = previousPlatform;
}
});
test('cli prints success acknowledgment for client-backed open in human mode', async () => {
const result = await runCliCapture(['open', 'settings'], async () => ({
ok: true,
data: {
session: 'default',
appName: 'Settings',
message: 'Opened: Settings',
platform: 'ios',
target: 'mobile',
device: 'iPhone 16',
id: 'SIM-001',
},
}));
assert.equal(result.code, null);
assert.match(result.stdout, /Opened: Settings/);
});
test('cli prints success acknowledgment for client-backed close in human mode', async () => {
const result = await runCliCapture(['close'], async () => ({
ok: true,
data: { session: 'default', message: 'Closed: default' },
}));
assert.equal(result.code, null);
assert.match(result.stdout, /Closed: default/);
});
test('cli prints success acknowledgment for daemon-backed mutating commands in human mode', async () => {
const result = await runCliCapture(['scroll', 'down'], async () => ({
ok: true,
data: { direction: 'down', message: 'Scrolled down' },
}));
assert.equal(result.code, null);
assert.match(result.stdout, /Scrolled down/);
});
test('cli forwards bound-session lock policy when session defaults are configured', async () => {
const previousSession = process.env.AGENT_DEVICE_SESSION;
const previousPlatform = process.env.AGENT_DEVICE_PLATFORM;
process.env.AGENT_DEVICE_SESSION = 'qa-ios';
process.env.AGENT_DEVICE_PLATFORM = 'ios';
try {
const result = await runCliCapture(['snapshot', '--device', 'Pixel 9', '--json'], async () => ({
ok: true,
data: {},
}));
assert.equal(result.code, null);
assert.equal(result.calls.length, 1);
assert.equal(result.calls[0]?.meta?.lockPolicy, 'reject');
assert.equal(result.calls[0]?.meta?.lockPlatform, 'ios');
assert.equal(result.calls[0]?.flags?.platform, 'ios');
assert.equal(result.calls[0]?.flags?.device, 'Pixel 9');
} finally {
if (previousSession === undefined) delete process.env.AGENT_DEVICE_SESSION;
else process.env.AGENT_DEVICE_SESSION = previousSession;
if (previousPlatform === undefined) delete process.env.AGENT_DEVICE_PLATFORM;
else process.env.AGENT_DEVICE_PLATFORM = previousPlatform;
}
});
test('cli session lock flag overrides environment for a single invocation', async () => {
const previousPlatform = process.env.AGENT_DEVICE_PLATFORM;
const previousLocked = process.env.AGENT_DEVICE_SESSION_LOCKED;
process.env.AGENT_DEVICE_PLATFORM = 'ios';
process.env.AGENT_DEVICE_SESSION_LOCKED = '0';
try {
const result = await runCliCapture(
['snapshot', '--session-lock', 'reject', '--device', 'Pixel 9', '--json'],
async () => ({
ok: true,
data: {},
}),
);
assert.equal(result.code, null);
assert.equal(result.calls.length, 1);
assert.equal(result.calls[0]?.meta?.lockPolicy, 'reject');
} finally {
if (previousPlatform === undefined) delete process.env.AGENT_DEVICE_PLATFORM;
else process.env.AGENT_DEVICE_PLATFORM = previousPlatform;
if (previousLocked === undefined) delete process.env.AGENT_DEVICE_SESSION_LOCKED;
else process.env.AGENT_DEVICE_SESSION_LOCKED = previousLocked;
}
});