unbound-cli 1.22.1 → 1.24.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.
- package/README.md +26 -0
- package/package.json +1 -1
- package/src/commands/delivery.js +567 -0
- package/src/commands/setup.js +45 -7
- package/src/index.js +10 -0
- package/test/delivery.test.js +591 -0
- package/test/setup-wsl-task.test.js +123 -0
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
const { test } = require('node:test');
|
|
2
|
+
const assert = require('node:assert/strict');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const http = require('http');
|
|
5
|
+
const os = require('os');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const { spawn } = require('child_process');
|
|
8
|
+
|
|
9
|
+
const BASE = '/api/v1/analytics/delivery';
|
|
10
|
+
|
|
11
|
+
const SUMMARY = {
|
|
12
|
+
connections: { github: true, ticket_tracker: 'LINEAR' },
|
|
13
|
+
kpis: {
|
|
14
|
+
ai_branches: { value: 42, previous: 30 },
|
|
15
|
+
prs_opened: { value: 20, previous: 15 },
|
|
16
|
+
prs_merged: { value: 12, merge_rate_pct: 60, closed: 2 },
|
|
17
|
+
ai_spend: 321.5,
|
|
18
|
+
cost_per_pr: { value: 16.08 },
|
|
19
|
+
cost_per_merged_pr: { value: 26.79 },
|
|
20
|
+
time_to_first_pr: { median_hours: 3.5, p90_hours: 11.2 },
|
|
21
|
+
},
|
|
22
|
+
throughput_unit: 'week',
|
|
23
|
+
throughput: [],
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const BRANCH = {
|
|
27
|
+
id: 4821,
|
|
28
|
+
name: 'feat/login',
|
|
29
|
+
repository: 'acme/web',
|
|
30
|
+
users: [{ id: 1, name: 'Ada', email: 'ada@example.com' }],
|
|
31
|
+
tools: ['claude-code'],
|
|
32
|
+
ticket: { key: 'WEB-12', status: 'Done', category: 'done', url: 'https://linear.app/acme/issue/WEB-12' },
|
|
33
|
+
pr: { number: 123, status: 'merged', url: 'https://github.com/acme/web/pull/123' },
|
|
34
|
+
work_group: { group: 'build' },
|
|
35
|
+
category_pending: false,
|
|
36
|
+
sessions: 3,
|
|
37
|
+
ai_hours: 1.5,
|
|
38
|
+
cost: 12.345,
|
|
39
|
+
tokens: 98765,
|
|
40
|
+
first_seen: '2026-11-20T10:15:00Z',
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const BRANCHES = { count: 25, page: 1, page_size: 10, results: [BRANCH], category_counts: { all: 25 } };
|
|
44
|
+
|
|
45
|
+
const BRANCH_DETAIL = {
|
|
46
|
+
...BRANCH,
|
|
47
|
+
timeline: { first_session_at: '2026-11-20T10:15:00Z', pr_created_at: '2026-11-21T09:00:00Z', status_seen_at: null },
|
|
48
|
+
sessions: [{
|
|
49
|
+
task_id: 777, app_label: 'claude-code', user: { id: 1, email: 'ada@example.com' },
|
|
50
|
+
started_at: '2026-11-20T10:15:00Z', duration_minutes: 42, cost: 4.5, tokens: 1200,
|
|
51
|
+
}],
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const USERS = {
|
|
55
|
+
github: true,
|
|
56
|
+
columns: ['user', 'team', 'prs_raised', 'prs_merged', 'merge_rate_pct', 'ai_spend', 'cost_per_merged_pr', 'last_active'],
|
|
57
|
+
sort_by: 'ai_spend',
|
|
58
|
+
count: 1,
|
|
59
|
+
page: 1,
|
|
60
|
+
page_size: 10,
|
|
61
|
+
results: [{
|
|
62
|
+
user: { id: 1, name: 'Ada', email: 'ada@example.com' }, team: 'Platform', prs_raised: 9, prs_merged: 7,
|
|
63
|
+
merge_rate_pct: 78, ai_spend: 88.1, cost_per_merged_pr: 12.59, last_active: '2026-11-26T08:00:00Z', low_sample: false,
|
|
64
|
+
}],
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const TEAMS = {
|
|
68
|
+
github: false,
|
|
69
|
+
columns: ['team', 'active_ai_users', 'ai_branches', 'prs_raised', 'ai_spend', 'cost_per_pr', 'with_ticket_pct'],
|
|
70
|
+
sort_by: 'ai_spend',
|
|
71
|
+
count: 1,
|
|
72
|
+
page: 1,
|
|
73
|
+
page_size: 10,
|
|
74
|
+
results: [{
|
|
75
|
+
team: { name: 'Platform', manager: 'Grace' }, active_ai_users: 4, ai_branches: 11, prs_raised: 6,
|
|
76
|
+
ai_spend: 140.25, cost_per_pr: 23.38, with_ticket_pct: 82, low_sample: false,
|
|
77
|
+
}],
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const SPEND_OUTCOME = {
|
|
81
|
+
total_cost: 200,
|
|
82
|
+
pr_raised_cost: 150,
|
|
83
|
+
outcomes: [
|
|
84
|
+
{ key: 'merged', cost: 120, count: 8 },
|
|
85
|
+
{ key: 'in_review', cost: 30, count: 3 },
|
|
86
|
+
{ key: 'closed', cost: 0, count: 0 },
|
|
87
|
+
{ key: 'no_pr', cost: 50, count: 6 },
|
|
88
|
+
],
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const SPEND_WORK_GROUP = {
|
|
92
|
+
unit: 'week',
|
|
93
|
+
pr_state: 'merged',
|
|
94
|
+
buckets: [],
|
|
95
|
+
totals: {
|
|
96
|
+
build: { cost: 90, pull_requests: 5 },
|
|
97
|
+
fix: { cost: 40, pull_requests: 4 },
|
|
98
|
+
improve: { cost: 0, pull_requests: 0 },
|
|
99
|
+
infra_ops: { cost: 10, pull_requests: 1 },
|
|
100
|
+
unknown: { cost: 0, pull_requests: 0 },
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const REPOS = { repositories: ['acme/api', 'acme/web'] };
|
|
105
|
+
|
|
106
|
+
// An OSC 8 hyperlink and a red CSI colour, the two ways a server string could hijack the terminal.
|
|
107
|
+
const ESC = '\x1b';
|
|
108
|
+
const EVIL = `${ESC}]8;;https://evil.example${ESC}\\${ESC}[31m`;
|
|
109
|
+
|
|
110
|
+
function assertNoEscapes(text) {
|
|
111
|
+
assert.ok(!text.includes(ESC), `escape sequence reached the terminal:\n${JSON.stringify(text)}`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const ROUTES = {
|
|
115
|
+
[`${BASE}/summary/`]: SUMMARY,
|
|
116
|
+
[`${BASE}/branches/`]: BRANCHES,
|
|
117
|
+
[`${BASE}/branches/4821/`]: BRANCH_DETAIL,
|
|
118
|
+
[`${BASE}/users/`]: USERS,
|
|
119
|
+
[`${BASE}/teams/`]: TEAMS,
|
|
120
|
+
[`${BASE}/spend-by-outcome/`]: SPEND_OUTCOME,
|
|
121
|
+
[`${BASE}/spend-by-work-group/`]: SPEND_WORK_GROUP,
|
|
122
|
+
[`${BASE}/filters/`]: REPOS,
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const allRequests = [];
|
|
126
|
+
|
|
127
|
+
function startServer(routes) {
|
|
128
|
+
const seen = [];
|
|
129
|
+
const server = http.createServer((req, res) => {
|
|
130
|
+
req.resume();
|
|
131
|
+
req.on('end', () => {
|
|
132
|
+
const url = new URL(req.url, 'http://x');
|
|
133
|
+
const request = { method: req.method, path: url.pathname, query: url.searchParams };
|
|
134
|
+
seen.push(request);
|
|
135
|
+
allRequests.push(request);
|
|
136
|
+
const [status, payload] = routes(req.method, url) || [404, { error: 'no route' }];
|
|
137
|
+
res.writeHead(status, { 'Content-Type': 'application/json' });
|
|
138
|
+
res.end(JSON.stringify(payload));
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
return new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve({ server, seen, port: server.address().port })));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function mockBackend() {
|
|
145
|
+
return startServer((method, url) => (ROUTES[url.pathname] ? [200, ROUTES[url.pathname]] : null));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function runCli(port, args) {
|
|
149
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'delivery-test-'));
|
|
150
|
+
fs.mkdirSync(path.join(home, '.unbound'));
|
|
151
|
+
fs.writeFileSync(path.join(home, '.unbound', 'config.json'),
|
|
152
|
+
JSON.stringify({ api_key: 'test-key', base_url: `http://127.0.0.1:${port}` }));
|
|
153
|
+
return new Promise((resolve) => {
|
|
154
|
+
const child = spawn('node', [path.join(__dirname, '..', 'src', 'index.js'), ...args], {
|
|
155
|
+
env: {
|
|
156
|
+
...process.env, HOME: home, UNBOUND_TOKEN: 'test-key', UNBOUND_API_URL: '', UNBOUND_TELEMETRY: '0',
|
|
157
|
+
UNBOUND_NO_UPDATE_CHECK: '1', COLUMNS: '400',
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
let stdout = '';
|
|
161
|
+
let stderr = '';
|
|
162
|
+
child.stdout.on('data', (d) => { stdout += d; });
|
|
163
|
+
child.stderr.on('data', (d) => { stderr += d; });
|
|
164
|
+
child.on('close', (code) => {
|
|
165
|
+
fs.rmSync(home, { recursive: true, force: true });
|
|
166
|
+
resolve({ code, stdout, stderr });
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function withBackend(fn, backend = mockBackend) {
|
|
172
|
+
const { server, seen, port } = await backend();
|
|
173
|
+
try {
|
|
174
|
+
await fn({ seen, run: (args) => runCli(port, args) });
|
|
175
|
+
} finally {
|
|
176
|
+
server.close();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
test('summary prints the KPIs with the default 30-day window, and --json prints the raw body', async () => {
|
|
181
|
+
await withBackend(async ({ seen, run }) => {
|
|
182
|
+
const table = await run(['delivery', 'summary']);
|
|
183
|
+
assert.equal(table.code, 0, table.stderr);
|
|
184
|
+
assert.equal(seen[0].path, `${BASE}/summary/`);
|
|
185
|
+
assert.equal(seen[0].query.get('time_window'), 'THIRTY_DAYS');
|
|
186
|
+
for (const text of ['GitHub connected', 'yes', 'LINEAR', '42 (previous 30)', 'merge rate 60%', '$321.50',
|
|
187
|
+
'Cost per merged PR', '$26.79', 'median 3.5h, p90 11.2h']) {
|
|
188
|
+
assert.ok(table.stdout.includes(text), `${text} missing from:\n${table.stdout}`);
|
|
189
|
+
}
|
|
190
|
+
const json = await run(['delivery', 'summary', '--json']);
|
|
191
|
+
assert.deepEqual(JSON.parse(json.stdout), SUMMARY);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test('summary without GitHub warns and marks merges and cost per merged PR as needing GitHub', async () => {
|
|
196
|
+
const noGithub = { ...SUMMARY, connections: { github: false, ticket_tracker: null } };
|
|
197
|
+
await withBackend(async ({ run }) => {
|
|
198
|
+
const r = await run(['delivery', 'summary']);
|
|
199
|
+
assert.equal(r.code, 0, r.stderr);
|
|
200
|
+
assert.match(r.stderr, /GitHub is not connected, so merges and cost per merged PR are unavailable\./);
|
|
201
|
+
assert.ok(r.stdout.includes('none'), r.stdout);
|
|
202
|
+
assert.match(r.stdout, /PRs merged\s+needs GitHub/);
|
|
203
|
+
assert.match(r.stdout, /Cost per merged PR\s+needs GitHub/);
|
|
204
|
+
assert.ok(!r.stdout.includes('$26.79'), r.stdout);
|
|
205
|
+
}, () => startServer(() => [200, noGithub]));
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test('summary prints - for null KPIs and strips escapes from the ticket tracker', async () => {
|
|
209
|
+
const nullKpis = {
|
|
210
|
+
connections: { github: true, ticket_tracker: `${EVIL}JIRA` },
|
|
211
|
+
kpis: {
|
|
212
|
+
ai_branches: { value: null, previous: null },
|
|
213
|
+
prs_opened: { value: null, previous: null },
|
|
214
|
+
prs_merged: { value: null, merge_rate_pct: null },
|
|
215
|
+
ai_spend: null,
|
|
216
|
+
cost_per_pr: { value: null },
|
|
217
|
+
cost_per_merged_pr: { value: null },
|
|
218
|
+
time_to_first_pr: { median_hours: null, p90_hours: null },
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
await withBackend(async ({ run }) => {
|
|
222
|
+
const r = await run(['delivery', 'summary']);
|
|
223
|
+
assert.equal(r.code, 0, r.stderr);
|
|
224
|
+
assertNoEscapes(r.stdout);
|
|
225
|
+
assert.ok(r.stdout.includes('JIRA'), r.stdout);
|
|
226
|
+
assert.match(r.stdout, /AI branches\s+- \(previous -\)/);
|
|
227
|
+
assert.match(r.stdout, /PRs merged\s+- \(merge rate -\)/);
|
|
228
|
+
assert.match(r.stdout, /AI spend\s+-\n/);
|
|
229
|
+
assert.match(r.stdout, /Cost per merged PR\s+-\n/);
|
|
230
|
+
assert.match(r.stdout, /Time to first PR\s+median -, p90 -/);
|
|
231
|
+
assert.ok(!r.stdout.includes('null') && !r.stdout.includes('undefined'), r.stdout);
|
|
232
|
+
}, () => startServer(() => [200, nullKpis]));
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test('summary sends the window and every people, tool and repo filter', async () => {
|
|
236
|
+
await withBackend(async ({ seen, run }) => {
|
|
237
|
+
const r = await run(['delivery', 'summary', '--start', '2026-11-01', '--end', '2026-11-20',
|
|
238
|
+
'--user', '7,3f2b8c1e-9a4d-4e2f-8b6a-1c2d3e4f5a6b', '--team', 'Platform', '--manager', 'Grace',
|
|
239
|
+
'--tool', 'claude-code,cursor', '--repo', 'acme/web,acme/api']);
|
|
240
|
+
assert.equal(r.code, 0, r.stderr);
|
|
241
|
+
const { query } = seen[0];
|
|
242
|
+
assert.equal(query.get('start_date'), '2026-11-01');
|
|
243
|
+
assert.equal(query.get('end_date'), '2026-11-20');
|
|
244
|
+
assert.equal(query.get('time_window'), null);
|
|
245
|
+
assert.deepEqual(query.getAll('user_ids'), ['7', '3f2b8c1e-9a4d-4e2f-8b6a-1c2d3e4f5a6b']);
|
|
246
|
+
assert.deepEqual(query.getAll('department_names'), ['Platform']);
|
|
247
|
+
assert.deepEqual(query.getAll('manager_names'), ['Grace']);
|
|
248
|
+
assert.deepEqual(query.getAll('tool_types'), ['CLAUDE_CODE', 'CURSOR']);
|
|
249
|
+
assert.deepEqual(query.getAll('git_repos'), ['acme/web', 'acme/api']);
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test('branches sends its filters as repeated keys and prints rows with a page footer', async () => {
|
|
254
|
+
await withBackend(async ({ seen, run }) => {
|
|
255
|
+
const r = await run(['delivery', 'branches', '--outcome', 'merged', '--ticket-status', 'done,in-progress',
|
|
256
|
+
'--work-group', 'build,infra_ops', '--search', 'login', '--sort', 'cost', '--order', 'asc',
|
|
257
|
+
'--page-size', '10', '--time-window', 'seven_days']);
|
|
258
|
+
assert.equal(r.code, 0, r.stderr);
|
|
259
|
+
const { path: requestPath, query } = seen[0];
|
|
260
|
+
assert.equal(requestPath, `${BASE}/branches/`);
|
|
261
|
+
assert.equal(query.get('outcome'), 'merged');
|
|
262
|
+
assert.deepEqual(query.getAll('status'), ['done', 'in_progress']);
|
|
263
|
+
assert.deepEqual(query.getAll('work_group'), ['build', 'infra_ops']);
|
|
264
|
+
assert.equal(query.get('search'), 'login');
|
|
265
|
+
assert.equal(query.get('sort_by'), 'cost');
|
|
266
|
+
assert.equal(query.get('sort_order'), 'asc');
|
|
267
|
+
assert.equal(query.get('page'), '1');
|
|
268
|
+
assert.equal(query.get('page_size'), '10');
|
|
269
|
+
assert.equal(query.get('time_window'), 'SEVEN_DAYS');
|
|
270
|
+
assert.equal(query.get('pr_state'), null);
|
|
271
|
+
for (const text of ['4821', 'feat/login', 'acme/web', '#123 merged', 'WEB-12 Done', 'Build', 'ada@example.com',
|
|
272
|
+
'$12.35', '2026-11-20 10:15']) {
|
|
273
|
+
assert.ok(r.stdout.includes(text), `${text} missing from:\n${r.stdout}`);
|
|
274
|
+
}
|
|
275
|
+
assert.match(r.stderr, /Page 1 of 3 \(25 branches\)/);
|
|
276
|
+
assert.match(r.stderr, /Next page: --page 2/);
|
|
277
|
+
assert.match(r.stderr, /unbound delivery branch <ID>/);
|
|
278
|
+
const json = await run(['delivery', 'branches', '--json']);
|
|
279
|
+
assert.deepEqual(JSON.parse(json.stdout), BRANCHES);
|
|
280
|
+
assert.equal(seen[1].query.get('page'), '1');
|
|
281
|
+
assert.equal(seen[1].query.get('time_window'), 'THIRTY_DAYS');
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test('branches shows a message when nothing matches', async () => {
|
|
286
|
+
await withBackend(async ({ run }) => {
|
|
287
|
+
const r = await run(['delivery', 'branches']);
|
|
288
|
+
assert.equal(r.code, 0, r.stderr);
|
|
289
|
+
assert.match(r.stderr, /No AI branches/);
|
|
290
|
+
}, () => startServer(() => [200, { count: 0, page: 1, page_size: 10, results: [] }]));
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
test('branches prints - for a branch with no PR, ticket, users or work group, and pending while categorizing', async () => {
|
|
294
|
+
const bare = { ...BRANCH, pr: null, ticket: null, work_group: { group: null }, category_pending: true, users: [] };
|
|
295
|
+
await withBackend(async ({ run }) => {
|
|
296
|
+
const r = await run(['delivery', 'branches']);
|
|
297
|
+
assert.equal(r.code, 0, r.stderr);
|
|
298
|
+
const row = r.stdout.split('\n').find((line) => line.includes('feat/login'));
|
|
299
|
+
assert.ok(row, r.stdout);
|
|
300
|
+
assert.match(row, /4821\s+feat\/login\s+acme\/web\s+-\s+-\s+pending\s+-\s+\$12\.35/);
|
|
301
|
+
}, () => startServer(() => [200, { ...BRANCHES, results: [bare] }]));
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
test('branches strips terminal escapes from server strings', async () => {
|
|
305
|
+
const evil = { ...BRANCH, name: `${EVIL}feat/evil`, first_seen: `${EVIL}2026-11-20T10:15:00Z` };
|
|
306
|
+
await withBackend(async ({ run }) => {
|
|
307
|
+
const r = await run(['delivery', 'branches']);
|
|
308
|
+
assert.equal(r.code, 0, r.stderr);
|
|
309
|
+
assertNoEscapes(r.stdout);
|
|
310
|
+
assert.ok(r.stdout.includes('feat/evil'), r.stdout);
|
|
311
|
+
}, () => startServer(() => [200, { ...BRANCHES, results: [evil] }]));
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test('a page past the last one says so instead of reporting no data', async () => {
|
|
315
|
+
const pastEnd = { count: 25, page: 9, page_size: 10, results: [] };
|
|
316
|
+
await withBackend(async ({ run }) => {
|
|
317
|
+
const branches = await run(['delivery', 'branches', '--page', '9']);
|
|
318
|
+
assert.equal(branches.code, 0, branches.stderr);
|
|
319
|
+
assert.match(branches.stderr, /Page 9 is past the last page \(3 pages, 25 branches\)\./);
|
|
320
|
+
assert.doesNotMatch(branches.stderr, /No AI branches/);
|
|
321
|
+
const users = await run(['delivery', 'users', '--page', '9']);
|
|
322
|
+
assert.equal(users.code, 0, users.stderr);
|
|
323
|
+
assert.match(users.stderr, /Page 9 is past the last page \(3 pages, 25 users\)\./);
|
|
324
|
+
const teams = await run(['delivery', 'teams', '--page', '9']);
|
|
325
|
+
assert.match(teams.stderr, /Page 9 is past the last page \(3 pages, 25 teams\)\./);
|
|
326
|
+
}, () => startServer(() => [200, { ...pastEnd, columns: ['user'] }]));
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test('branch shows the detail and its sessions, and --json prints the raw body', async () => {
|
|
330
|
+
await withBackend(async ({ seen, run }) => {
|
|
331
|
+
const r = await run(['delivery', 'branch', '4821']);
|
|
332
|
+
assert.equal(r.code, 0, r.stderr);
|
|
333
|
+
assert.equal(seen[0].path, `${BASE}/branches/4821/`);
|
|
334
|
+
for (const text of ['feat/login', '#123 merged https://github.com/acme/web/pull/123',
|
|
335
|
+
'WEB-12 Done https://linear.app/acme/issue/WEB-12', '98,765', '1.5h', '777', 'claude-code', '42', '$4.50']) {
|
|
336
|
+
assert.ok(r.stdout.includes(text), `${text} missing from:\n${r.stdout}`);
|
|
337
|
+
}
|
|
338
|
+
assert.match(r.stdout, /Work group\s+Build/);
|
|
339
|
+
assert.doesNotMatch(r.stderr, /Showing the newest/);
|
|
340
|
+
const json = await run(['delivery', 'branch', '4821', '--json']);
|
|
341
|
+
assert.deepEqual(JSON.parse(json.stdout), BRANCH_DETAIL);
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
test('branch detail strips terminal escapes from names, links and times', async () => {
|
|
346
|
+
const evil = {
|
|
347
|
+
...BRANCH_DETAIL,
|
|
348
|
+
name: `${EVIL}feat/evil`,
|
|
349
|
+
repository: `${EVIL}acme/web`,
|
|
350
|
+
ticket: { ...BRANCH.ticket, url: `${EVIL}https://linear.app/x` },
|
|
351
|
+
timeline: { first_session_at: `${EVIL}2026-11-20`, pr_created_at: null, status_seen_at: null },
|
|
352
|
+
};
|
|
353
|
+
await withBackend(async ({ run }) => {
|
|
354
|
+
const r = await run(['delivery', 'branch', '4821']);
|
|
355
|
+
assert.equal(r.code, 0, r.stderr);
|
|
356
|
+
assertNoEscapes(r.stdout);
|
|
357
|
+
assert.ok(r.stdout.includes('feat/evil') && r.stdout.includes('https://linear.app/x'), r.stdout);
|
|
358
|
+
assert.match(r.stdout, /First session\s+-/);
|
|
359
|
+
}, () => startServer(() => [200, evil]));
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
test('branch detail with no sessions, null tokens and an empty ticket URL', async () => {
|
|
363
|
+
const empty = { ...BRANCH_DETAIL, sessions: [], tokens: null, ticket: { ...BRANCH.ticket, url: '' } };
|
|
364
|
+
await withBackend(async ({ run }) => {
|
|
365
|
+
const r = await run(['delivery', 'branch', '4821']);
|
|
366
|
+
assert.equal(r.code, 0, r.stderr);
|
|
367
|
+
assert.match(r.stdout, /Ticket\s+WEB-12 Done\n/);
|
|
368
|
+
assert.match(r.stdout, /Tokens\s+-\n/);
|
|
369
|
+
assert.match(r.stderr, /No sessions on this branch/);
|
|
370
|
+
}, () => startServer(() => [200, empty]));
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
test('branch detail says when only the newest 100 sessions are shown', async () => {
|
|
374
|
+
const sessions = Array.from({ length: 100 }, (_, index) => ({ ...BRANCH_DETAIL.sessions[0], task_id: index + 1 }));
|
|
375
|
+
await withBackend(async ({ run }) => {
|
|
376
|
+
const r = await run(['delivery', 'branch', '4821']);
|
|
377
|
+
assert.equal(r.code, 0, r.stderr);
|
|
378
|
+
assert.match(r.stderr, /Showing the newest 100 sessions\./);
|
|
379
|
+
}, () => startServer(() => [200, { ...BRANCH_DETAIL, sessions }]));
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
test('users and teams send sort, page and ticket filters and print the columns the server lists', async () => {
|
|
383
|
+
await withBackend(async ({ seen, run }) => {
|
|
384
|
+
const users = await run(['delivery', 'users', '--sort', 'merge_rate_pct', '--page', '2', '--ticket-status', 'todo',
|
|
385
|
+
'--work-group', 'fix', '--team', 'Platform']);
|
|
386
|
+
assert.equal(users.code, 0, users.stderr);
|
|
387
|
+
assert.equal(seen[0].path, `${BASE}/users/`);
|
|
388
|
+
assert.equal(seen[0].query.get('sort_by'), 'merge_rate_pct');
|
|
389
|
+
assert.equal(seen[0].query.get('page'), '2');
|
|
390
|
+
assert.deepEqual(seen[0].query.getAll('status'), ['todo']);
|
|
391
|
+
assert.deepEqual(seen[0].query.getAll('work_group'), ['fix']);
|
|
392
|
+
assert.deepEqual(seen[0].query.getAll('department_names'), ['Platform']);
|
|
393
|
+
for (const text of ['User', 'Merge rate', 'Cost per merged PR', 'ada@example.com', 'Platform', '78%', '$88.10']) {
|
|
394
|
+
assert.ok(users.stdout.includes(text), `${text} missing from:\n${users.stdout}`);
|
|
395
|
+
}
|
|
396
|
+
assert.match(users.stderr, /Page 1 of 1 \(1 users\)/);
|
|
397
|
+
|
|
398
|
+
const teams = await run(['delivery', 'teams']);
|
|
399
|
+
assert.equal(teams.code, 0, teams.stderr);
|
|
400
|
+
assert.equal(seen[1].path, `${BASE}/teams/`);
|
|
401
|
+
assert.equal(seen[1].query.get('page'), '1');
|
|
402
|
+
for (const text of ['Active AI users', 'With ticket', 'Platform (Grace)', '$140.25', '82%', 'Cost per PR']) {
|
|
403
|
+
assert.ok(teams.stdout.includes(text), `${text} missing from:\n${teams.stdout}`);
|
|
404
|
+
}
|
|
405
|
+
assert.ok(!teams.stdout.includes('Cost per merged PR'), 'teams without GitHub show no merge columns');
|
|
406
|
+
|
|
407
|
+
assert.deepEqual(JSON.parse((await run(['delivery', 'users', '--json'])).stdout), USERS);
|
|
408
|
+
assert.deepEqual(JSON.parse((await run(['delivery', 'teams', '--json'])).stdout), TEAMS);
|
|
409
|
+
});
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
test('users without GitHub print the cost per PR column and no merge columns', async () => {
|
|
413
|
+
const noGithub = {
|
|
414
|
+
...USERS,
|
|
415
|
+
github: false,
|
|
416
|
+
columns: ['user', 'team', 'prs_raised', 'ai_spend', 'cost_per_pr', 'last_active'],
|
|
417
|
+
results: [{
|
|
418
|
+
user: { id: 1, name: 'Ada', email: 'ada@example.com' }, team: 'Platform', prs_raised: 9, ai_spend: 88.1,
|
|
419
|
+
cost_per_pr: 9.79, last_active: '2026-11-26T08:00:00Z', low_sample: false,
|
|
420
|
+
}],
|
|
421
|
+
};
|
|
422
|
+
await withBackend(async ({ run }) => {
|
|
423
|
+
const r = await run(['delivery', 'users']);
|
|
424
|
+
assert.equal(r.code, 0, r.stderr);
|
|
425
|
+
for (const text of ['Cost per PR', '$9.79', 'PRs raised', '2026-11-26 08:00']) {
|
|
426
|
+
assert.ok(r.stdout.includes(text), `${text} missing from:\n${r.stdout}`);
|
|
427
|
+
}
|
|
428
|
+
assert.ok(!r.stdout.includes('Merge rate') && !r.stdout.includes('Cost per merged PR'), r.stdout);
|
|
429
|
+
}, () => startServer(() => [200, noGithub]));
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
test('users and teams warn when the server sorted by another column', async () => {
|
|
433
|
+
const fellBack = { ...TEAMS, sort_by: 'ai_spend' };
|
|
434
|
+
await withBackend(async ({ run }) => {
|
|
435
|
+
const r = await run(['delivery', 'teams', '--sort', 'merge_rate_pct']);
|
|
436
|
+
assert.equal(r.code, 0, r.stderr);
|
|
437
|
+
assert.match(r.stderr, /Sorted by ai_spend: merge_rate_pct needs GitHub connected\./);
|
|
438
|
+
const asked = await run(['delivery', 'teams', '--sort', 'ai_spend']);
|
|
439
|
+
assert.doesNotMatch(asked.stderr, /Sorted by/);
|
|
440
|
+
const unsorted = await run(['delivery', 'teams']);
|
|
441
|
+
assert.doesNotMatch(unsorted.stderr, /Sorted by/);
|
|
442
|
+
}, () => startServer(() => [200, fellBack]));
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
test('teams strips terminal escapes from team names', async () => {
|
|
446
|
+
const evil = { ...TEAMS, results: [{ ...TEAMS.results[0], team: { name: `${EVIL}Platform`, manager: null } }] };
|
|
447
|
+
await withBackend(async ({ run }) => {
|
|
448
|
+
const r = await run(['delivery', 'teams']);
|
|
449
|
+
assert.equal(r.code, 0, r.stderr);
|
|
450
|
+
assertNoEscapes(r.stdout);
|
|
451
|
+
assert.ok(r.stdout.includes('Platform'), r.stdout);
|
|
452
|
+
}, () => startServer(() => [200, evil]));
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
test('spend defaults to outcome and switches to work group with a PR state', async () => {
|
|
456
|
+
await withBackend(async ({ seen, run }) => {
|
|
457
|
+
const outcome = await run(['delivery', 'spend', '--repo', 'acme/web']);
|
|
458
|
+
assert.equal(outcome.code, 0, outcome.stderr);
|
|
459
|
+
assert.equal(seen[0].path, `${BASE}/spend-by-outcome/`);
|
|
460
|
+
assert.equal(seen[0].query.get('pr_state'), null);
|
|
461
|
+
assert.deepEqual(seen[0].query.getAll('git_repos'), ['acme/web']);
|
|
462
|
+
for (const text of ['Merged', 'In review', 'No PR', '$120.00', '$50.00']) {
|
|
463
|
+
assert.ok(outcome.stdout.includes(text), `${text} missing from:\n${outcome.stdout}`);
|
|
464
|
+
}
|
|
465
|
+
assert.match(outcome.stderr, /Total spend \$200\.00 \(\$150\.00 on branches with a PR\)/);
|
|
466
|
+
|
|
467
|
+
const workGroup = await run(['delivery', 'spend', '--by', 'work-group', '--pr-state', 'merged']);
|
|
468
|
+
assert.equal(workGroup.code, 0, workGroup.stderr);
|
|
469
|
+
assert.equal(seen[1].path, `${BASE}/spend-by-work-group/`);
|
|
470
|
+
assert.equal(seen[1].query.get('pr_state'), 'merged');
|
|
471
|
+
for (const text of ['Build', 'Fix', 'Infra / ops', '$90.00']) {
|
|
472
|
+
assert.ok(workGroup.stdout.includes(text), `${text} missing from:\n${workGroup.stdout}`);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
assert.deepEqual(JSON.parse((await run(['delivery', 'spend', '--json'])).stdout), SPEND_OUTCOME);
|
|
476
|
+
assert.deepEqual(JSON.parse((await run(['delivery', 'spend', '--by', 'work_group', '--json'])).stdout),
|
|
477
|
+
SPEND_WORK_GROUP);
|
|
478
|
+
});
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
test('repos lists the window\'s repositories and sends only the window', async () => {
|
|
482
|
+
await withBackend(async ({ seen, run }) => {
|
|
483
|
+
const r = await run(['delivery', 'repos', '--time-window', 'FOURTEEN_DAYS']);
|
|
484
|
+
assert.equal(r.code, 0, r.stderr);
|
|
485
|
+
assert.equal(seen[0].path, `${BASE}/filters/`);
|
|
486
|
+
assert.deepEqual([...seen[0].query.keys()], ['time_window']);
|
|
487
|
+
assert.equal(seen[0].query.get('time_window'), 'FOURTEEN_DAYS');
|
|
488
|
+
assert.ok(r.stdout.includes('acme/api') && r.stdout.includes('acme/web'), r.stdout);
|
|
489
|
+
assert.deepEqual(JSON.parse((await run(['delivery', 'repos', '--json'])).stdout), REPOS);
|
|
490
|
+
});
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
test('repos strips terminal escapes from repository names', async () => {
|
|
494
|
+
await withBackend(async ({ run }) => {
|
|
495
|
+
const r = await run(['delivery', 'repos']);
|
|
496
|
+
assert.equal(r.code, 0, r.stderr);
|
|
497
|
+
assertNoEscapes(r.stdout);
|
|
498
|
+
assert.ok(r.stdout.includes('acme/evil'), r.stdout);
|
|
499
|
+
}, () => startServer(() => [200, { repositories: [`${EVIL}acme/evil`] }]));
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
test('bad input fails before any request is sent', async () => {
|
|
503
|
+
const cases = [
|
|
504
|
+
[['delivery', 'branches', '--outcome', 'shipped'], /Invalid --outcome "shipped"/],
|
|
505
|
+
[['delivery', 'branches', '--page-size', '101'], /--page-size cannot exceed 100/],
|
|
506
|
+
[['delivery', 'summary', '--user', 'priya@acme.dev'], /Invalid --user "priya@acme.dev"/],
|
|
507
|
+
[['delivery', 'branches', '--page', '0'], /Invalid --page "0"/],
|
|
508
|
+
[['delivery', 'branches', '--ticket-status', 'blocked'], /Invalid --ticket-status "blocked"/],
|
|
509
|
+
[['delivery', 'users', '--work-group', 'docs'], /Invalid --work-group "docs"/],
|
|
510
|
+
[['delivery', 'users', '--sort', 'name'], /Invalid --sort "name"/],
|
|
511
|
+
[['delivery', 'users', '--sort', 'active_ai_users'], /Invalid --sort "active_ai_users"/],
|
|
512
|
+
[['delivery', 'teams', '--sort', 'last_active'], /Invalid --sort "last_active"/],
|
|
513
|
+
[['delivery', 'teams', '--order', 'up'], /Invalid --order "up"/],
|
|
514
|
+
[['delivery', 'branch', 'abc'], /Invalid branch ID "abc"/],
|
|
515
|
+
[['delivery', 'spend', '--by', 'tool'], /Invalid --by "tool"/],
|
|
516
|
+
[['delivery', 'spend', '--pr-state', 'merged'], /--pr-state applies only to --by work-group/],
|
|
517
|
+
[['delivery', 'summary', '--time-window', 'THREE_MONTHS'], /Invalid --time-window "THREE_MONTHS"/],
|
|
518
|
+
[['delivery', 'repos', '--start', '2026-11-01'], /--start and --end must be used together/],
|
|
519
|
+
[['delivery', 'summary', '--start', '2026-09-01', '--end', '2026-11-20'],
|
|
520
|
+
/Custom range cannot exceed 31 days \(requested 81 days\)/],
|
|
521
|
+
[['delivery', 'branches', '--start', '2026-10-01', '--end', '2026-11-01'],
|
|
522
|
+
/Custom range cannot exceed 31 days \(requested 32 days\)/],
|
|
523
|
+
[['delivery', 'summary', '--start', '2026-07-01', '--end', '2026-11-20'],
|
|
524
|
+
/Custom range cannot exceed 31 days \(requested 143 days\)/],
|
|
525
|
+
];
|
|
526
|
+
await withBackend(async ({ seen, run }) => {
|
|
527
|
+
for (const [args, message] of cases) {
|
|
528
|
+
const r = await run(args);
|
|
529
|
+
assert.equal(r.code, 1, args.join(' '));
|
|
530
|
+
assert.match(r.stderr, message);
|
|
531
|
+
}
|
|
532
|
+
assert.equal(seen.length, 0);
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
test('a 403 explains the role needed and a 404 explains what is missing', async () => {
|
|
537
|
+
await withBackend(async ({ run }) => {
|
|
538
|
+
const r = await run(['delivery', 'summary']);
|
|
539
|
+
assert.equal(r.code, 1);
|
|
540
|
+
assert.match(r.stderr, /needs Admin or Analytics Viewer over the whole organization/);
|
|
541
|
+
}, () => startServer(() => [403, { error: 'Forbidden' }]));
|
|
542
|
+
|
|
543
|
+
await withBackend(async ({ run }) => {
|
|
544
|
+
const branch = await run(['delivery', 'branch', '99']);
|
|
545
|
+
assert.equal(branch.code, 1);
|
|
546
|
+
assert.match(branch.stderr, /Branch 99 not found in your organization/);
|
|
547
|
+
}, () => startServer(() => [404, { error: 'Branch not found' }]));
|
|
548
|
+
|
|
549
|
+
await withBackend(async ({ run }) => {
|
|
550
|
+
const branch = await run(['delivery', 'branch', '99']);
|
|
551
|
+
assert.equal(branch.code, 1);
|
|
552
|
+
assert.match(branch.stderr, /does not serve Delivery analytics yet/);
|
|
553
|
+
const list = await run(['delivery', 'branches']);
|
|
554
|
+
assert.equal(list.code, 1);
|
|
555
|
+
assert.match(list.stderr, /does not serve Delivery analytics yet/);
|
|
556
|
+
}, () => startServer(() => [404, '<html>Not Found</html>']));
|
|
557
|
+
|
|
558
|
+
await withBackend(async ({ run }) => {
|
|
559
|
+
const branch = await run(['delivery', 'branch', '99']);
|
|
560
|
+
assert.equal(branch.code, 1);
|
|
561
|
+
assert.match(branch.stderr, /Organization not found/);
|
|
562
|
+
assert.doesNotMatch(branch.stderr, /Branch 99|does not serve/);
|
|
563
|
+
const summary = await run(['delivery', 'summary']);
|
|
564
|
+
assert.match(summary.stderr, /Organization not found/);
|
|
565
|
+
}, () => startServer(() => [404, { error: 'Organization not found' }]));
|
|
566
|
+
});
|
|
567
|
+
|
|
568
|
+
test('a server 400 message is passed through with escapes stripped', async () => {
|
|
569
|
+
const message = 'start_date cannot be earlier than 30 days ago (2026-10-28).';
|
|
570
|
+
await withBackend(async ({ run }) => {
|
|
571
|
+
const r = await run(['delivery', 'summary', '--start', '2026-10-01', '--end', '2026-10-20']);
|
|
572
|
+
assert.equal(r.code, 1);
|
|
573
|
+
assert.ok(r.stderr.includes(message), r.stderr);
|
|
574
|
+
assert.ok(!r.stderr.includes(`${ESC}]8`), JSON.stringify(r.stderr));
|
|
575
|
+
}, () => startServer(() => [400, { error: `${EVIL}${message}` }]));
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
test('a 31-day custom range is sent with both dates', async () => {
|
|
579
|
+
await withBackend(async ({ run, seen }) => {
|
|
580
|
+
const r = await run(['delivery', 'summary', '--start', '2026-10-01', '--end', '2026-10-31', '--json']);
|
|
581
|
+
assert.equal(r.code, 0, r.stderr);
|
|
582
|
+
assert.equal(seen.length, 1);
|
|
583
|
+
assert.equal(seen[0].query.get('start_date'), '2026-10-01');
|
|
584
|
+
assert.equal(seen[0].query.get('end_date'), '2026-10-31');
|
|
585
|
+
}, mockBackend);
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
test('every request the suite sent was a GET', () => {
|
|
589
|
+
assert.ok(allRequests.length > 0);
|
|
590
|
+
assert.deepEqual([...new Set(allRequests.map((request) => request.method))], ['GET']);
|
|
591
|
+
});
|