-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaudeStatuslineUsage.test.js
More file actions
134 lines (115 loc) · 4.16 KB
/
Copy pathclaudeStatuslineUsage.test.js
File metadata and controls
134 lines (115 loc) · 4.16 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
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const test = require("node:test");
const {
CACHE_FILE_NAME,
readClaudeStatuslineUsage,
} = require("../src/claudeStatuslineUsage");
const { makeTempDir } = require("./testUtils");
// Write the statusline usage cache file the helper script produces.
function writeCache(claudeRoot, payload) {
const file = path.join(claudeRoot, CACHE_FILE_NAME);
fs.mkdirSync(claudeRoot, { recursive: true });
fs.writeFileSync(file, JSON.stringify(payload), "utf8");
return file;
}
test("maps five_hour to primary (300 min) and seven_day to secondary (10080 min)", () => {
const root = makeTempDir("codex-claude-monitor-statusline-");
writeCache(root, {
capturedAt: 1780000000,
rate_limits: {
five_hour: { used_percentage: 21, resets_at: 1780001111 },
seven_day: { used_percentage: 45, resets_at: 1780002222 },
},
});
const result = readClaudeStatuslineUsage(root);
assert.deepEqual(result.rateLimits.primary, {
used_percent: 21,
window_minutes: 300,
resets_at: 1780001111,
});
assert.deepEqual(result.rateLimits.secondary, {
used_percent: 45,
window_minutes: 10080,
resets_at: 1780002222,
});
assert.equal(result.capturedAt, 1780000000);
});
test("returns null when the cache file does not exist", () => {
const root = makeTempDir("codex-claude-monitor-statusline-");
assert.equal(readClaudeStatuslineUsage(root), null);
});
test("returns null when the cache file is not valid JSON", () => {
const root = makeTempDir("codex-claude-monitor-statusline-");
const file = path.join(root, CACHE_FILE_NAME);
fs.mkdirSync(root, { recursive: true });
fs.writeFileSync(file, "{ not json", "utf8");
assert.equal(readClaudeStatuslineUsage(root), null);
});
test("returns null when rate_limits is absent (subscriber before first API response)", () => {
const root = makeTempDir("codex-claude-monitor-statusline-");
writeCache(root, { capturedAt: 1780000000 });
assert.equal(readClaudeStatuslineUsage(root), null);
});
test("includes only the window that is present when the other is absent", () => {
const root = makeTempDir("codex-claude-monitor-statusline-");
writeCache(root, {
capturedAt: 1780000000,
rate_limits: {
five_hour: { used_percentage: 10, resets_at: 1780001111 },
},
});
const result = readClaudeStatuslineUsage(root);
assert.deepEqual(result.rateLimits.primary, {
used_percent: 10,
window_minutes: 300,
resets_at: 1780001111,
});
assert.equal(result.rateLimits.secondary, undefined);
});
test("omits resets_at when the window has no reset timestamp", () => {
const root = makeTempDir("codex-claude-monitor-statusline-");
writeCache(root, {
capturedAt: 1780000000,
rate_limits: {
five_hour: { used_percentage: 33 },
},
});
const result = readClaudeStatuslineUsage(root);
assert.deepEqual(result.rateLimits.primary, {
used_percent: 33,
window_minutes: 300,
});
});
test("returns null when a present window has a non-numeric used_percentage", () => {
const root = makeTempDir("codex-claude-monitor-statusline-");
writeCache(root, {
capturedAt: 1780000000,
rate_limits: {
five_hour: { used_percentage: "n/a", resets_at: 1780001111 },
},
});
assert.equal(readClaudeStatuslineUsage(root), null);
});
test("re-reads after the cache file changes (mtime+size cache invalidation)", () => {
const root = makeTempDir("codex-claude-monitor-statusline-");
writeCache(root, {
capturedAt: 1780000000,
rate_limits: { five_hour: { used_percentage: 10, resets_at: 1 } },
});
assert.equal(readClaudeStatuslineUsage(root).rateLimits.primary.used_percent, 10);
// Overwrite with a different percent and bump mtime so the cache key changes.
const file = path.join(root, CACHE_FILE_NAME);
fs.writeFileSync(
file,
JSON.stringify({
capturedAt: 1780000500,
rate_limits: { five_hour: { used_percentage: 88, resets_at: 1 } },
}),
"utf8",
);
const future = new Date(Date.now() + 60000);
fs.utimesSync(file, future, future);
assert.equal(readClaudeStatuslineUsage(root).rateLimits.primary.used_percent, 88);
});