Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

claudeUsageLimits.test.ts

273 lines8.1 KB

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("skips a window the endpoint reports without a utilization", () => {
90 expect(
91 claudeUsageResponseToLimits({
92 checkedAt,
93 response: {
94 rate_limits_available: true,
95 rate_limits: {
96 five_hour: { utilization: null, resets_at: null },
97 seven_day: { utilization: 250, resets_at: null },
98 },
99 },
100 }).limits.windows,
101 ).toEqual([
102 {
103 id: "seven_day",
104 kind: "weekly",
105 label: "Weekly",
106 usedPercent: 100,
107 windowDurationMins: 10080,
108 },
109 ]);
110 });
111​
112 const enterpriseResponse = {
113 rate_limits_available: true,
114 rate_limits: {
115 five_hour: null,
116 seven_day: null,
117 spend: {
118 enabled: true,
119 used: { amount_minor: 4_631, currency: "USD", exponent: 2 },
120 limit: { amount_minor: 50_000, currency: "USD", exponent: 2 },
121 percent: 9,
122 },
123 extra_usage: {
124 is_enabled: true,
125 used_credits: 4_631,
126 monthly_limit: 50_000,
127 utilization: 9.261999999999999,
128 currency: "USD",
129 decimal_places: 2,
130 },
131 },
132 };
133 const monthlyWindow = {
134 id: "monthly_spend",
135 kind: "monthly",
136 label: "Monthly spend",
137 usedPercent: expect.closeTo(9.262, 10),
138 };
139​
140 it("maps the captured Enterprise budget precisely, preferring spend beside rolling windows", () => {
141 expect(claudeUsageResponseToLimits({ checkedAt, response: enterpriseResponse }).limits).toEqual(
142 {
143 checkedAt,
144 windows: [monthlyWindow],
145 },
146 );
147 const response = {
148 ...enterpriseResponse,
149 rate_limits: {
150 ...enterpriseResponse.rate_limits,
151 five_hour: { utilization: 12, resets_at: null },
152 extra_usage: {
153 ...enterpriseResponse.rate_limits.extra_usage,
154 used_credits: 30_000,
155 utilization: 60,
156 },
157 },
158 };
159 expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([
160 {
161 id: "five_hour",
162 kind: "session",
163 label: "Session",
164 usedPercent: 12,
165 windowDurationMins: 300,
166 },
167 monthlyWindow,
168 ]);
169 });
170​
171 it("falls back to enabled extra usage amounts or utilization when spend is unusable", () => {
172 const response = {
173 ...enterpriseResponse,
174 rate_limits: {
175 ...enterpriseResponse.rate_limits,
176 spend: {
177 ...enterpriseResponse.rate_limits.spend,
178 enabled: false,
179 used: { amount_minor: 30_000, currency: "USD", exponent: 2 },
180 },
181 extra_usage: { ...enterpriseResponse.rate_limits.extra_usage, utilization: 9 },
182 },
183 };
184 expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([
185 monthlyWindow,
186 ]);
187 const utilizationOnly = {
188 ...response,
189 rate_limits: {
190 ...response.rate_limits,
191 spend: { enabled: true, used: null, limit: null, percent: 9 },
192 extra_usage: {
193 ...enterpriseResponse.rate_limits.extra_usage,
194 used_credits: null,
195 monthly_limit: null,
196 },
197 },
198 };
199 expect(
200 claudeUsageResponseToLimits({ checkedAt, response: utilizationOnly }).limits.windows,
201 ).toEqual([monthlyWindow]);
202 });
203​
204 it("omits disabled budgets and refuses invalid money without an enabled fallback", () => {
205 const rateLimits = enterpriseResponse.rate_limits;
206 const response = {
207 ...enterpriseResponse,
208 rate_limits: {
209 ...rateLimits,
210 spend: { ...rateLimits.spend, enabled: false },
211 extra_usage: { ...rateLimits.extra_usage, is_enabled: false },
212 },
213 };
214 expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([]);
215 response.rate_limits.spend = {
216 ...rateLimits.spend,
217 limit: { ...rateLimits.spend.limit, currency: "EUR" },
218 };
219 expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([]);
220 response.rate_limits.spend = {
221 ...rateLimits.spend,
222 limit: { ...rateLimits.spend.limit, amount_minor: 0 },
223 };
224 expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([]);
225 });
226});
227​
228describe("claudeRateLimitEventToUpdate", () => {
229 it("scales the 0–1 utilization and epoch-second reset onto the probe's window id", () => {
230 expect(
231 claudeRateLimitEventToUpdate(
232 {
233 status: "allowed_warning",
234 rateLimitType: "seven_day",
235 utilization: 0.85,
236 resetsAt: 1_784_000_000,
237 },
238 noNames,
239 ),
240 ).toEqual({
241 windows: [
242 {
243 id: "seven_day",
244 kind: "weekly",
245 label: "Weekly",
246 usedPercent: 85,
247 windowDurationMins: 10080,
248 resetsAt: "2026-07-14T03:33:20.000Z",
249 },
250 ],
251 });
252 });
253​
254 it("lands the streamed overage-included bucket on the row the probe named", () => {
255 const event = {
256 status: "allowed",
257 rateLimitType: "seven_day_overage_included" as never,
258 utilization: 0.4,
259 } as const;
260 expect(claudeRateLimitEventToUpdate(event, noNames)).toBeUndefined();
261 expect(claudeRateLimitEventToUpdate(event, { overageIncluded: "Fable" })).toEqual({
262 windows: [
263 {
264 id: "seven_day_fable",
265 kind: "weekly",
266 label: "Weekly · Fable",
267 usedPercent: 40,
268 windowDurationMins: 10080,
269 },
270 ],
271 });
272 });
273});