-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathcli-network.test.ts
More file actions
292 lines (275 loc) · 9.15 KB
/
Copy pathcli-network.test.ts
File metadata and controls
292 lines (275 loc) · 9.15 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
import { test } from 'vitest';
import assert from 'node:assert/strict';
import { promises as fs } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { runCliCapture } from './cli-capture.ts';
function makeFailedReplayResult() {
return {
file: '/tmp/02-fail.ad',
session: 'default:test:suite:2',
status: 'failed',
durationMs: 5,
attempts: 2,
artifactsDir: '/tmp/test-artifacts/02-fail',
error: { message: 'Replay failed at step 1 (open Demo): boom' },
};
}
function makeReplaySuiteResponse() {
const failedReplayResult = makeFailedReplayResult();
return {
ok: true as const,
data: {
total: 3,
executed: 2,
passed: 1,
failed: 1,
skipped: 1,
notRun: 0,
durationMs: 25,
failures: [failedReplayResult],
tests: [
{
file: '/tmp/01-pass.ad',
session: 'default:test:suite:1',
status: 'passed',
durationMs: 10,
attempts: 1,
},
failedReplayResult,
{
file: '/tmp/03-skip.ad',
status: 'skipped',
durationMs: 0,
message: 'missing platform metadata for --platform android',
},
],
},
};
}
test('network dump prints parsed entries and metadata', async () => {
const result = await runCliCapture(['network', 'dump', '10', '--include', 'all'], async () => ({
ok: true,
data: {
path: '/tmp/app.log',
include: 'all',
active: true,
state: 'active',
backend: 'android',
scannedLines: 120,
matchedLines: 2,
entries: [
{
timestamp: '2026-02-24T10:00:01Z',
method: 'POST',
url: 'https://api.example.com/v1/login',
status: 401,
durationMs: 377,
headers: '{"x-id":"abc"}',
requestBody: '{"email":"u@example.com"}',
responseBody: '{"error":"denied"}',
},
],
notes: ['best-effort parser'],
},
}));
assert.equal(result.code, null);
assert.equal(result.calls.length, 1);
const request = result.calls[0];
assert.ok(request);
assert.deepEqual(request.positionals, ['dump', '10']);
assert.equal(request.flags?.networkInclude, 'all');
assert.match(result.stdout, /\/tmp\/app\.log/);
assert.match(
result.stdout,
/2026-02-24T10:00:01Z POST https:\/\/api\.example\.com\/v1\/login status=401 durationMs=377/,
);
assert.match(result.stdout, /headers:/);
assert.match(result.stdout, /request:/);
assert.match(result.stdout, /response:/);
assert.match(result.stderr, /active=true/);
assert.match(result.stderr, /include=all/);
assert.match(result.stderr, /matchedLines=2/);
assert.match(result.stderr, /best-effort parser/);
});
test('test command prints suite summary and exits non-zero on failures', async () => {
const result = await runCliCapture(['test', './suite'], async () => makeReplaySuiteResponse());
assert.equal(result.code, 1);
assert.equal(result.calls.length, 1);
assert.match(result.stderr, /Running replay suite\.\.\./);
assert.doesNotMatch(result.stdout, /PASS \/tmp\/01-pass\.ad/);
assert.match(result.stdout, /FAIL \/tmp\/02-fail\.ad after 2 attempts \(5ms\)/);
assert.match(result.stdout, /Replay failed at step 1 \(open Demo\): boom/);
assert.match(result.stdout, /artifacts: \/tmp\/test-artifacts\/02-fail/);
assert.doesNotMatch(result.stdout, /SKIP \/tmp\/03-skip\.ad/);
assert.match(result.stdout, /Test summary: 1 passed, 1 failed in 25ms/);
});
test('test command --verbose prints all test statuses', async () => {
const result = await runCliCapture(['test', './suite', '--verbose'], async () =>
makeReplaySuiteResponse(),
);
assert.equal(result.code, 1);
assert.match(result.stderr, /Running replay suite\.\.\./);
assert.match(result.stdout, /PASS \/tmp\/01-pass\.ad \(10ms\)/);
assert.match(result.stdout, /SKIP \/tmp\/03-skip\.ad/);
});
test('test command reports flaky passed-on-retry cases in the default summary', async () => {
const result = await runCliCapture(['test', './suite'], async () => ({
ok: true,
data: {
total: 1,
executed: 1,
passed: 1,
failed: 0,
skipped: 0,
notRun: 0,
durationMs: 25,
failures: [],
tests: [
{
file: '/tmp/01-flaky.ad',
session: 'default:test:suite:1',
status: 'passed',
durationMs: 10,
attempts: 2,
},
],
},
}));
assert.equal(result.code, null);
assert.match(result.stderr, /Running replay suite\.\.\./);
assert.match(result.stdout, /FLAKY \/tmp\/01-flaky\.ad after 2 attempts \(10ms\)/);
assert.match(result.stdout, /Test summary: 1 passed, 0 failed, 1 flaky in 25ms/);
});
test('test --maestro forwards Maestro backend and platform for directory suites', async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-cli-maestro-suite-'));
await fs.writeFile(
path.join(tmpDir, 'auth-flow.yml'),
['appId: demo.app', '---', '- launchApp', ''].join('\n'),
);
try {
const result = await runCliCapture(
['test', '--maestro', '--platform', 'android', tmpDir],
async () => ({
ok: true,
data: {
total: 1,
executed: 1,
passed: 1,
failed: 0,
skipped: 0,
notRun: 0,
durationMs: 5,
failures: [],
tests: [],
},
}),
);
assert.equal(result.code, null);
assert.equal(result.calls.length, 1);
assert.equal(result.calls[0]?.command, 'test');
assert.deepEqual(result.calls[0]?.positionals, [tmpDir]);
assert.equal(result.calls[0]?.flags?.replayBackend, 'maestro');
assert.equal(result.calls[0]?.flags?.platform, 'android');
assert.match(result.stderr, /Running replay suite\.\.\./);
} finally {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
test('test command writes JUnit report with failure metadata', async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'agent-device-junit-test-'));
const reportPath = path.join(tmpDir, 'replays.junit.xml');
try {
const result = await runCliCapture(
['test', './suite', '--report-junit', reportPath],
async () => ({
ok: true,
data: {
total: 3,
executed: 3,
passed: 1,
failed: 1,
skipped: 1,
notRun: 0,
durationMs: 25,
failures: [
{
file: '/tmp/02-fail.ad',
session: 'default:test:suite:2',
status: 'failed',
durationMs: 5,
attempts: 2,
artifactsDir: '/tmp/test-artifacts/02-fail',
error: {
code: 'COMMAND_FAILED',
message: 'Replay failed at step 1 (open Demo): boom',
hint: 'retry me',
diagnosticId: 'diag-123',
logPath: '/tmp/diag.ndjson',
details: { command: 'open', reason: 'selector_not_found' },
},
},
],
tests: [
{
file: '/tmp/01-flaky.ad',
session: 'default:test:suite:1',
status: 'passed',
durationMs: 10,
attempts: 2,
replayed: 1,
healed: 0,
},
{
file: '/tmp/02-fail.ad',
session: 'default:test:suite:2',
status: 'failed',
durationMs: 5,
attempts: 2,
artifactsDir: '/tmp/test-artifacts/02-fail',
error: {
code: 'COMMAND_FAILED',
message: 'Replay failed at step 1 (open Demo): boom',
hint: 'retry me',
diagnosticId: 'diag-123',
logPath: '/tmp/diag.ndjson',
details: { command: 'open', reason: 'selector_not_found' },
},
},
{
file: '/tmp/03-skip.ad',
status: 'skipped',
durationMs: 0,
message: 'not runnable',
reason: 'skipped-by-filter',
},
],
},
}),
);
assert.equal(result.code, 1);
const xml = await fs.readFile(reportPath, 'utf8');
assert.match(
xml,
/<testsuite name="agent-device replay suite" tests="3" failures="1" skipped="1" time="0\.025">/,
);
assert.match(
xml,
/<testcase classname="\/tmp" name="02-fail\.ad" file="\/tmp\/02-fail\.ad" time="0\.005">/,
);
assert.match(xml, /<failure message="Replay failed at step 1 \(open Demo\): boom">/);
assert.match(xml, /diagnosticId: diag-123/);
assert.match(xml, /logPath: \/tmp\/diag\.ndjson/);
assert.match(xml, /artifactsDir: \/tmp\/test-artifacts\/02-fail/);
assert.match(xml, /errorCode: COMMAND_FAILED/);
assert.match(xml, /errorMessage: Replay failed at step 1 \(open Demo\): boom/);
assert.match(
xml,
/details: \{"command":"open","reason":"selector_not_found"\}/,
);
assert.match(xml, /flaky: true/);
assert.match(xml, /<skipped message="not runnable" \/>/);
} finally {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});