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

claudeUsageLimits.test.ts

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