Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { claudeRateLimitEventToUpdate, claudeUsageResponseToLimits } from "./claudeUsageLimits.ts";
4
5const checkedAt = "2026-07-18T10:00:00.000Z";
6const noNames = { overageIncluded: undefined } as const;
7
8describe("claudeUsageResponseToLimits", () => {
9 it("maps the session, weekly, and model-scoped weekly windows", () => {
10 expect(
11 claudeUsageResponseToLimits({
12 checkedAt,
13 response: {
14 rate_limits_available: true,
15 rate_limits: {
16 five_hour: { utilization: 54, resets_at: "2026-07-18T14:39:00Z" },
17 seven_day: { utilization: 18.4, resets_at: "2026-07-24T08:59:00+00:00" },
18 seven_day_opus: { utilization: 3, resets_at: null },
19 // Newer CLIs add this on top of the typed keys; the pinned SDK
20 // typings do not know it yet.
21 ...({
22 model_scoped: [
23 { display_name: "Fable", utilization: 73, resets_at: "2026-07-24T08:59:00Z" },
24 { display_name: "Ghost", utilization: null, resets_at: null },
25 ],
26 } as object),
27 extra_usage: {
28 is_enabled: false,
29 monthly_limit: null,
30 used_credits: null,
31 utilization: null,
32 },
33 },
34 },
35 }),
36 ).toEqual({
37 names: { overageIncluded: "Fable" },
38 limits: {
39 checkedAt,
40 windows: [
41 {
42 id: "five_hour",
43 kind: "session",
44 label: "Session",
45 usedPercent: 54,
46 windowDurationMins: 300,
47 resetsAt: "2026-07-18T14:39:00.000Z",
48 },
49 {
50 id: "seven_day",
51 kind: "weekly",
52 label: "Weekly",
53 usedPercent: 18.4,
54 windowDurationMins: 10080,
55 resetsAt: "2026-07-24T08:59:00.000Z",
56 },
57 {
58 id: "seven_day_fable",
59 kind: "weekly",
60 label: "Weekly · Fable",
61 usedPercent: 73,
62 windowDurationMins: 10080,
63 resetsAt: "2026-07-24T08:59:00.000Z",
64 },
65 ],
66 },
67 });
68 });
69
70 it("names the overage-included bucket only from a scoped entry that drew a row", () => {
71 expect(
72 claudeUsageResponseToLimits({
73 checkedAt,
74 response: {
75 rate_limits_available: true,
76 rate_limits: {
77 ...({
78 model_scoped: [
79 { display_name: "Ghost", utilization: null, resets_at: null },
80 { display_name: "Fable", utilization: 5, resets_at: null },
81 ],
82 } as object),
83 },
84 },
85 }).names,
86 ).toEqual({ overageIncluded: "Fable" });
87 });
88
89 it.each([
90 { rateLimitsAvailable: false, reason: "unsupported" },
91 { rateLimitsAvailable: true, reason: "probeFailed" },
92 ])(
93 "reports null usage as $reason when availability is $rateLimitsAvailable",
94 ({ rateLimitsAvailable, reason }) => {
95 expect(
96 claudeUsageResponseToLimits({
97 checkedAt,
98 response: { rate_limits_available: rateLimitsAvailable, rate_limits: null },
99 }),
100 ).toEqual({
101 limits: { checkedAt, windows: [], unavailable: { reason } },
102 names: noNames,
103 });
104 },
105 );
106
107 it("skips a window the endpoint reports without a utilization", () => {
108 expect(
109 claudeUsageResponseToLimits({
110 checkedAt,
111 response: {
112 rate_limits_available: true,
113 rate_limits: {
114 five_hour: { utilization: null, resets_at: null },
115 seven_day: { utilization: 250, resets_at: null },
116 },
117 },
118 }).limits.windows,
119 ).toEqual([
120 {
121 id: "seven_day",
122 kind: "weekly",
123 label: "Weekly",
124 usedPercent: 100,
125 windowDurationMins: 10080,
126 },
127 ]);
128 });
129
130 const enterpriseResponse = {
131 rate_limits_available: true,
132 rate_limits: {
133 five_hour: null,
134 seven_day: null,
135 spend: {
136 enabled: true,
137 used: { amount_minor: 4_631, currency: "USD", exponent: 2 },
138 limit: { amount_minor: 50_000, currency: "USD", exponent: 2 },
139 percent: 9,
140 },
141 extra_usage: {
142 is_enabled: true,
143 used_credits: 4_631,
144 monthly_limit: 50_000,
145 utilization: 9.261999999999999,
146 currency: "USD",
147 decimal_places: 2,
148 },
149 },
150 };
151 const monthlyWindow = {
152 id: "monthly_spend",
153 kind: "monthly",
154 label: "Monthly spend",
155 usedPercent: expect.closeTo(9.262, 10),
156 };
157
158 it("maps the captured Enterprise budget precisely, preferring spend beside rolling windows", () => {
159 expect(claudeUsageResponseToLimits({ checkedAt, response: enterpriseResponse }).limits).toEqual(
160 {
161 checkedAt,
162 windows: [monthlyWindow],
163 },
164 );
165 const response = {
166 ...enterpriseResponse,
167 rate_limits: {
168 ...enterpriseResponse.rate_limits,
169 five_hour: { utilization: 12, resets_at: null },
170 extra_usage: {
171 ...enterpriseResponse.rate_limits.extra_usage,
172 used_credits: 30_000,
173 utilization: 60,
174 },
175 },
176 };
177 expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([
178 {
179 id: "five_hour",
180 kind: "session",
181 label: "Session",
182 usedPercent: 12,
183 windowDurationMins: 300,
184 },
185 monthlyWindow,
186 ]);
187 });
188
189 it("falls back to enabled extra usage amounts or utilization when spend is unusable", () => {
190 const response = {
191 ...enterpriseResponse,
192 rate_limits: {
193 ...enterpriseResponse.rate_limits,
194 spend: {
195 ...enterpriseResponse.rate_limits.spend,
196 enabled: false,
197 used: { amount_minor: 30_000, currency: "USD", exponent: 2 },
198 },
199 extra_usage: { ...enterpriseResponse.rate_limits.extra_usage, utilization: 9 },
200 },
201 };
202 expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([
203 monthlyWindow,
204 ]);
205 const utilizationOnly = {
206 ...response,
207 rate_limits: {
208 ...response.rate_limits,
209 spend: { enabled: true, used: null, limit: null, percent: 9 },
210 extra_usage: {
211 ...enterpriseResponse.rate_limits.extra_usage,
212 used_credits: null,
213 monthly_limit: null,
214 },
215 },
216 };
217 expect(
218 claudeUsageResponseToLimits({ checkedAt, response: utilizationOnly }).limits.windows,
219 ).toEqual([monthlyWindow]);
220 });
221
222 it("omits disabled budgets and refuses invalid money without an enabled fallback", () => {
223 const rateLimits = enterpriseResponse.rate_limits;
224 const response = {
225 ...enterpriseResponse,
226 rate_limits: {
227 ...rateLimits,
228 spend: { ...rateLimits.spend, enabled: false },
229 extra_usage: { ...rateLimits.extra_usage, is_enabled: false },
230 },
231 };
232 expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([]);
233 response.rate_limits.spend = {
234 ...rateLimits.spend,
235 limit: { ...rateLimits.spend.limit, currency: "EUR" },
236 };
237 expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([]);
238 response.rate_limits.spend = {
239 ...rateLimits.spend,
240 limit: { ...rateLimits.spend.limit, amount_minor: 0 },
241 };
242 expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([]);
243 });
244});
245
246describe("claudeRateLimitEventToUpdate", () => {
247 it("scales the 0–1 utilization and epoch-second reset onto the probe's window id", () => {
248 expect(
249 claudeRateLimitEventToUpdate(
250 {
251 status: "allowed_warning",
252 rateLimitType: "seven_day",
253 utilization: 0.85,
254 resetsAt: 1_784_000_000,
255 },
256 noNames,
257 ),
258 ).toEqual({
259 windows: [
260 {
261 id: "seven_day",
262 kind: "weekly",
263 label: "Weekly",
264 usedPercent: 85,
265 windowDurationMins: 10080,
266 resetsAt: "2026-07-14T03:33:20.000Z",
267 },
268 ],
269 });
270 });
271
272 it("lands the streamed overage-included bucket on the row the probe named", () => {
273 const event = {
274 status: "allowed",
275 rateLimitType: "seven_day_overage_included" as never,
276 utilization: 0.4,
277 } as const;
278 expect(claudeRateLimitEventToUpdate(event, noNames)).toBeUndefined();
279 expect(claudeRateLimitEventToUpdate(event, { overageIncluded: "Fable" })).toEqual({
280 windows: [
281 {
282 id: "seven_day_fable",
283 kind: "weekly",
284 label: "Weekly · Fable",
285 usedPercent: 40,
286 windowDurationMins: 10080,
287 },
288 ],
289 });
290 });
291
292 it("ignores windows the page does not render and events without a utilization", () => {
293 expect(
294 claudeRateLimitEventToUpdate(
295 { status: "allowed", rateLimitType: "seven_day_opus", utilization: 0.1 },
296 noNames,
297 ),
298 ).toBeUndefined();
299 expect(
300 claudeRateLimitEventToUpdate({ status: "rejected", rateLimitType: "five_hour" }, noNames),
301 ).toBeUndefined();
302 });
303});