-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentUsage.test.js
More file actions
512 lines (456 loc) · 16.9 KB
/
Copy pathagentUsage.test.js
File metadata and controls
512 lines (456 loc) · 16.9 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
const assert = require("node:assert/strict");
const path = require("node:path");
const test = require("node:test");
const {
formatAgentUsage,
formatClaudeTokenDetail,
formatModelName,
formatRateLimits,
formatRateLimitsStatusBar,
formatTimeAgo,
formatTimeLeft,
getUsageSeverity,
readLatestAgentUsage,
} = require("../src/agentUsage");
const { makeTempDir, setMtime, writeJsonl } = require("./testUtils");
test("readLatestAgentUsage selects Codex when Codex session is newer", () => {
const codexRoot = makeTempDir();
const claudeRoot = makeTempDir();
const codexFile = path.join(codexRoot, "2026", "06", "03", "rollout-new.jsonl");
const claudeFile = path.join(claudeRoot, "projects", "-workspace", "old.jsonl");
writeJsonl(codexFile, [
{
type: "event_msg",
payload: {
type: "token_count",
info: {
total_token_usage: { total_tokens: 34000 },
last_token_usage: { input_tokens: 8200, total_tokens: 9000 },
model_context_window: 258400,
},
},
},
]);
writeJsonl(claudeFile, [
{
type: "assistant",
message: {
model: "claude-opus-4-8",
usage: { input_tokens: 1, cache_read_input_tokens: 1000, cache_creation_input_tokens: 0 },
},
},
]);
setMtime(codexFile, new Date("2026-06-03T00:00:00Z"));
setMtime(claudeFile, new Date("2026-06-02T00:00:00Z"));
const usage = readLatestAgentUsage({ codexSessionsRoot: codexRoot, claudeRoot });
const formatted = formatAgentUsage(usage);
assert.equal(usage.provider, "Codex");
assert.equal(formatted.text, "Codex $(comment) 3%");
assert.equal(formatted.tooltip, "Codex: ctx 8k / 258k (3%)");
assert.equal(formatted.severity, "low");
});
test("getUsageSeverity maps percent to low/medium/high thresholds", () => {
assert.equal(getUsageSeverity(0), "low");
assert.equal(getUsageSeverity(49), "low");
assert.equal(getUsageSeverity(50), "medium");
assert.equal(getUsageSeverity(79), "medium");
assert.equal(getUsageSeverity(80), "high");
assert.equal(getUsageSeverity(100), "high");
});
test("getUsageSeverity returns null for non-finite values", () => {
assert.equal(getUsageSeverity(NaN), null);
assert.equal(getUsageSeverity(undefined), null);
});
test("formatAgentUsage returns null severity when usage is missing", () => {
assert.equal(formatAgentUsage(null).severity, null);
});
test("readLatestAgentUsage selects Claude when Claude session is newer", () => {
const codexRoot = makeTempDir();
const claudeRoot = makeTempDir();
const codexFile = path.join(codexRoot, "2026", "06", "02", "rollout-old.jsonl");
const claudeFile = path.join(claudeRoot, "projects", "-workspace", "new.jsonl");
writeJsonl(codexFile, [
{
type: "event_msg",
payload: {
type: "token_count",
info: {
total_token_usage: { total_tokens: 34000 },
last_token_usage: { input_tokens: 8200, total_tokens: 9000 },
model_context_window: 258400,
},
},
},
]);
writeJsonl(claudeFile, [
{
type: "assistant",
message: {
model: "claude-opus-4-8",
usage: { input_tokens: 2, cache_read_input_tokens: 184794, cache_creation_input_tokens: 155 },
},
},
]);
setMtime(codexFile, new Date("2026-06-02T00:00:00Z"));
setMtime(claudeFile, new Date("2026-06-03T00:00:00Z"));
const usage = readLatestAgentUsage({ codexSessionsRoot: codexRoot, claudeRoot });
const formatted = formatAgentUsage(usage);
assert.equal(usage.provider, "Claude");
assert.equal(formatted.text, "Claude $(comment) 18%");
assert.equal(
formatted.tooltip,
[
"Claude: ctx 185k / 1m (18%)",
"Model: Opus 4.8 (1M context)",
"Tokens: input 2 · cache read 185k · cache create 155",
"Cache hit: 100%",
].join("\n"),
);
assert.equal(formatted.severity, "low");
});
test("formatRateLimits formats 5h and weekly windows with reset times", () => {
// Use local time construction so the assertions stay deterministic across time zones.
const now = new Date(2026, 5, 3, 12, 0).getTime();
const sameDayReset = Math.floor(new Date(2026, 5, 3, 14, 32).getTime() / 1000);
const nextWeekReset = Math.floor(new Date(2026, 5, 8, 9, 24).getTime() / 1000);
const lines = formatRateLimits(
{
primary: { used_percent: 21.0, window_minutes: 300, resets_at: sameDayReset },
secondary: { used_percent: 10.0, window_minutes: 10080, resets_at: nextWeekReset },
},
now,
);
assert.deepEqual(lines, [
"5h usage: 21% · Reset at 14:32 (in 2h 32m)",
"Weekly usage: 10% · Reset at 6/8 09:24 (in 4d 21h)",
]);
});
// The fixture dates sit in early June / early July, away from any DST switch, so
// the relative durations stay deterministic across time zones.
test("formatTimeLeft renders compact countdowns and rejects past times", () => {
const now = new Date(2026, 5, 3, 12, 0).getTime();
const at = (seconds) => now / 1000 + seconds;
assert.equal(formatTimeLeft(at(30), now), "1m");
assert.equal(formatTimeLeft(at(59 * 60 + 30), now), "1h");
assert.equal(formatTimeLeft(at(60 * 60), now), "1h");
assert.equal(formatTimeLeft(at(2 * 3600 + 13 * 60), now), "2h 13m");
assert.equal(formatTimeLeft(at(24 * 3600), now), "1d");
// The day scale keeps at most two units: leftover minutes are dropped.
assert.equal(formatTimeLeft(at(24 * 3600 + 25 * 60), now), "1d");
assert.equal(formatTimeLeft(at(4 * 86400 + 21 * 3600 + 24 * 60), now), "4d 21h");
assert.equal(formatTimeLeft(at(7 * 86400), now), "7d");
assert.equal(formatTimeLeft(at(0), now), null);
assert.equal(formatTimeLeft(at(-60), now), null);
assert.equal(formatTimeLeft(undefined, now), null);
assert.equal(formatTimeLeft(NaN, now), null);
assert.equal(formatTimeLeft("soon", now), null);
});
test("formatRateLimits keeps the absolute reset time without countdown once passed", () => {
const now = new Date(2026, 5, 3, 12, 0).getTime();
const pastReset = Math.floor(new Date(2026, 5, 3, 11, 59).getTime() / 1000);
assert.deepEqual(
formatRateLimits(
{ primary: { used_percent: 21, window_minutes: 300, resets_at: pastReset } },
now,
),
["5h usage: 21% · Reset at 11:59"],
);
// A reset exactly at `now` also omits the countdown.
assert.deepEqual(
formatRateLimits(
{ primary: { used_percent: 21, window_minutes: 300, resets_at: now / 1000 } },
now,
),
["5h usage: 21% · Reset at 12:00"],
);
});
test("formatRateLimits omits invalid windows and missing reset times", () => {
const now = new Date(2026, 5, 3, 12, 0).getTime();
assert.deepEqual(formatRateLimits(null, now), []);
assert.deepEqual(formatRateLimits({}, now), []);
// Missing used_percent omits the entire row.
assert.deepEqual(
formatRateLimits({ primary: { window_minutes: 300, resets_at: 1780492366 } }, now),
[],
);
// Missing window_minutes omits the entire row.
assert.deepEqual(formatRateLimits({ primary: { used_percent: 21 } }, now), []);
// Missing resets_at omits the reset-time suffix.
assert.deepEqual(
formatRateLimits({ primary: { used_percent: 21, window_minutes: 300 } }, now),
["5h usage: 21%"],
);
});
test("formatRateLimits falls back to day label for mid-length windows", () => {
const now = new Date(2026, 5, 3, 12, 0).getTime();
const lines = formatRateLimits(
{ primary: { used_percent: 55.6, window_minutes: 2880 } },
now,
);
assert.deepEqual(lines, ["2d usage: 56%"]);
});
test("formatRateLimitsStatusBar renders compact segments for both windows", () => {
const segments = formatRateLimitsStatusBar({
primary: { used_percent: 45.4, window_minutes: 300 },
secondary: { used_percent: 23.0, window_minutes: 10080 },
});
assert.deepEqual(segments, ["$(history) 45%", "$(calendar) 23%"]);
});
test("formatRateLimitsStatusBar maps mid-length windows to the calendar icon", () => {
const segments = formatRateLimitsStatusBar({
primary: { used_percent: 55.6, window_minutes: 2880 },
});
assert.deepEqual(segments, ["$(calendar) 56%"]);
});
test("formatRateLimitsStatusBar omits missing or invalid windows", () => {
assert.deepEqual(formatRateLimitsStatusBar(null), []);
assert.deepEqual(formatRateLimitsStatusBar({}), []);
assert.deepEqual(
formatRateLimitsStatusBar({
primary: { used_percent: NaN, window_minutes: 300 },
secondary: { used_percent: 10, window_minutes: undefined },
}),
[],
);
});
test("formatAgentUsage shows compact Codex rate limits in the status bar", () => {
const now = new Date(2026, 5, 3, 12, 0).getTime();
const sameDayReset = Math.floor(new Date(2026, 5, 3, 14, 32).getTime() / 1000);
const nextWeekReset = Math.floor(new Date(2026, 5, 8, 9, 24).getTime() / 1000);
const formatted = formatAgentUsage(
{
provider: "Codex",
contextTokens: 8200,
contextWindow: 258400,
contextPercent: 3,
rateLimits: {
primary: { used_percent: 21.0, window_minutes: 300, resets_at: sameDayReset },
secondary: { used_percent: 10.0, window_minutes: 10080, resets_at: nextWeekReset },
},
},
now,
);
assert.equal(formatted.text, "Codex $(comment) 3% · $(history) 21% · $(calendar) 10%");
assert.equal(
formatted.tooltip,
[
"Codex: ctx 8k / 258k (3%)",
"5h usage: 21% · Reset at 14:32 (in 2h 32m)",
"Weekly usage: 10% · Reset at 6/8 09:24 (in 4d 21h)",
].join("\n"),
);
});
test("formatAgentUsage appends countdown rows for Claude probe rate limits", () => {
const now = new Date(2026, 6, 7, 12, 0).getTime();
const sessionReset = Math.floor(new Date(2026, 6, 7, 20, 20).getTime() / 1000);
const weekReset = Math.floor(new Date(2026, 6, 14, 1, 0).getTime() / 1000);
const formatted = formatAgentUsage(
{
provider: "Claude",
model: "claude-opus-4-8",
contextTokens: 185000,
contextWindow: 1000000,
contextPercent: 18,
usage: {
input_tokens: 2,
cache_read_input_tokens: 185000,
cache_creation_input_tokens: 155,
},
rateLimits: {
primary: { used_percent: 25, window_minutes: 300, resets_at: sessionReset },
secondary: { used_percent: 8, window_minutes: 10080, resets_at: weekReset },
},
},
now,
);
assert.equal(
formatted.tooltip,
[
"Claude: ctx 185k / 1m (18%)",
"Model: Opus 4.8 (1M context)",
"Tokens: input 2 · cache read 185k · cache create 155",
"Cache hit: 100%",
"5h usage: 25% · Reset at 20:20 (in 8h 20m)",
"Weekly usage: 8% · Reset at 7/14 01:00 (in 6d 13h)",
].join("\n"),
);
});
test("formatAgentUsage keeps Claude model details in the tooltip only", () => {
const formatted = formatAgentUsage({
provider: "Claude",
model: "claude-opus-4-8",
contextTokens: 185000,
contextWindow: 1000000,
contextPercent: 18,
});
assert.equal(
formatted.tooltip,
["Claude: ctx 185k / 1m (18%)", "Model: Opus 4.8 (1M context)"].join("\n"),
);
assert.equal(formatted.text, "Claude $(comment) 18%");
});
test("formatTimeAgo renders compact elapsed time and rejects future/invalid values", () => {
const now = new Date(2026, 5, 3, 12, 0).getTime();
const ago = (seconds) => now / 1000 - seconds;
assert.equal(formatTimeAgo(ago(0), now), "just now");
assert.equal(formatTimeAgo(ago(30), now), "just now");
assert.equal(formatTimeAgo(ago(60), now), "1m ago");
assert.equal(formatTimeAgo(ago(2 * 60 + 30), now), "2m ago");
assert.equal(formatTimeAgo(ago(59 * 60), now), "59m ago");
assert.equal(formatTimeAgo(ago(60 * 60), now), "1h ago");
assert.equal(formatTimeAgo(ago(2 * 3600 + 40 * 60), now), "2h ago");
assert.equal(formatTimeAgo(ago(24 * 3600), now), "1d ago");
assert.equal(formatTimeAgo(ago(3 * 86400), now), "3d ago");
// A future timestamp (clock skew) reads as just now rather than negative.
assert.equal(formatTimeAgo(ago(-60), now), "just now");
assert.equal(formatTimeAgo(undefined, now), null);
assert.equal(formatTimeAgo(NaN, now), null);
});
test("formatAgentUsage appends a capture-time row for statusline rate limits", () => {
const now = new Date(2026, 6, 7, 12, 0).getTime();
const sessionReset = Math.floor(new Date(2026, 6, 7, 20, 20).getTime() / 1000);
const capturedAt = Math.floor(new Date(2026, 6, 7, 11, 58).getTime() / 1000);
const formatted = formatAgentUsage(
{
provider: "Claude",
model: "claude-opus-4-8",
contextTokens: 185000,
contextWindow: 1000000,
contextPercent: 18,
rateLimits: {
primary: { used_percent: 25, window_minutes: 300, resets_at: sessionReset },
},
rateLimitsCapturedAt: capturedAt,
},
now,
);
assert.equal(
formatted.tooltip,
[
"Claude: ctx 185k / 1m (18%)",
"Model: Opus 4.8 (1M context)",
"5h usage: 25% · Reset at 20:20 (in 8h 20m)",
"Usage updated 2m ago (11:58)",
].join("\n"),
);
});
test("formatAgentUsage omits the capture-time row when there are no rate limits", () => {
const formatted = formatAgentUsage({
provider: "Claude",
model: "claude-opus-4-8",
contextTokens: 185000,
contextWindow: 1000000,
contextPercent: 18,
rateLimitsCapturedAt: 1780000000,
});
assert.equal(
formatted.tooltip,
["Claude: ctx 185k / 1m (18%)", "Model: Opus 4.8 (1M context)"].join("\n"),
);
});
test("readLatestAgentUsage attaches capturedAt alongside Claude rate limits", () => {
const codexRoot = makeTempDir();
const claudeRoot = makeTempDir();
const claudeFile = path.join(claudeRoot, "projects", "-workspace", "new.jsonl");
writeJsonl(claudeFile, [
{
type: "assistant",
timestamp: "2026-07-07T12:00:00Z",
message: {
model: "claude-opus-4-8",
usage: { input_tokens: 100, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 },
},
},
]);
const usage = readLatestAgentUsage({
codexSessionsRoot: codexRoot,
claudeRoot,
claudeRateLimits: { primary: { used_percent: 25, window_minutes: 300 } },
claudeRateLimitsCapturedAt: 1780000000,
});
assert.equal(usage.provider, "Claude");
assert.equal(usage.rateLimitsCapturedAt, 1780000000);
});
test("formatModelName maps Claude model ids to friendly names and ignores others", () => {
assert.equal(formatModelName("claude-opus-4-8"), "Opus 4.8");
assert.equal(formatModelName("claude-sonnet-4-6"), "Sonnet 4.6");
assert.equal(formatModelName("claude-haiku-4-5-20251001"), "Haiku 4.5");
assert.equal(formatModelName("gpt-5"), null);
assert.equal(formatModelName(undefined), null);
});
test("formatAgentUsage drops the 1M marker for 200k Claude models", () => {
const formatted = formatAgentUsage({
provider: "Claude",
model: "claude-sonnet-4-6",
contextTokens: 100000,
contextWindow: 200000,
contextPercent: 50,
});
assert.equal(formatted.text, "Claude $(comment) 50%");
assert.match(formatted.tooltip, /Model: Sonnet 4\.6\n?/);
});
test("formatClaudeTokenDetail renders composition and cache-hit rows", () => {
const rows = formatClaudeTokenDetail({
input_tokens: 2,
cache_read_input_tokens: 48913,
cache_creation_input_tokens: 361,
});
assert.deepEqual(rows, [
"Tokens: input 2 · cache read 49k · cache create 361",
"Cache hit: 99%",
]);
assert.deepEqual(formatClaudeTokenDetail(undefined), []);
assert.deepEqual(formatClaudeTokenDetail({ output_tokens: 5 }), []);
});
test("readLatestAgentUsage attaches claudeRateLimits when Claude wins", () => {
const codexRoot = makeTempDir();
const claudeRoot = makeTempDir();
const claudeFile = path.join(claudeRoot, "projects", "-workspace", "new.jsonl");
writeJsonl(claudeFile, [
{
type: "assistant",
timestamp: "2026-07-07T12:00:00Z",
message: {
model: "claude-opus-4-8",
usage: { input_tokens: 100, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 },
},
},
]);
const claudeRateLimits = {
primary: { used_percent: 25, window_minutes: 300 },
secondary: { used_percent: 8, window_minutes: 10080 },
};
const usage = readLatestAgentUsage({
codexSessionsRoot: codexRoot,
claudeRoot,
claudeRateLimits,
});
assert.equal(usage.provider, "Claude");
assert.deepEqual(usage.rateLimits, claudeRateLimits);
assert.match(formatAgentUsage(usage).text, /^Claude \$\(comment\) \d+% · \$\(history\) 25% · \$\(calendar\) 8%$/);
});
test("readLatestAgentUsage does not attach claudeRateLimits to Codex", () => {
const codexRoot = makeTempDir();
const claudeRoot = makeTempDir();
const codexFile = path.join(codexRoot, "2026", "07", "07", "rollout-x.jsonl");
writeJsonl(codexFile, [
{
type: "event_msg",
payload: {
type: "token_count",
info: {
total_token_usage: { total_tokens: 1000 },
last_token_usage: { input_tokens: 500 },
model_context_window: 10000,
},
},
},
]);
const usage = readLatestAgentUsage({
codexSessionsRoot: codexRoot,
claudeRoot,
claudeRateLimits: { primary: { used_percent: 25, window_minutes: 300 } },
});
assert.equal(usage.provider, "Codex");
assert.deepEqual(usage.rateLimits, {});
});