@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 287 lines 8.42 KB
Raw
1 import { describe, expect, it } from "vite-plus/test";
2
3 import { claudeRateLimitEventToUpdate, claudeUsageResponseToLimits } from "./claudeUsageLimits.ts";
4
5 const checkedAt = "2026-07-18T10:00:00.000Z";
6 const noNames = { overageIncluded: undefined } as const;
7
8 describe("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("flags a $0 cap instead of reporting a monthly window", () => {
203 const response = {
204 rate_limits_available: true,
205 rate_limits: {
206 five_hour: null,
207 seven_day: null,
208 extra_usage: { is_enabled: true, used_credits: 0, monthly_limit: 0, utilization: null },
209 },
210 };
211 expect(claudeUsageResponseToLimits({ checkedAt, response }).limits).toEqual({
212 checkedAt,
213 windows: [],
214 zeroSpendCap: true,
215 });
216 });
217
218 it("omits disabled budgets and refuses invalid money without an enabled fallback", () => {
219 const rateLimits = enterpriseResponse.rate_limits;
220 const response = {
221 ...enterpriseResponse,
222 rate_limits: {
223 ...rateLimits,
224 spend: { ...rateLimits.spend, enabled: false },
225 extra_usage: { ...rateLimits.extra_usage, is_enabled: false },
226 },
227 };
228 expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([]);
229 response.rate_limits.spend = {
230 ...rateLimits.spend,
231 limit: { ...rateLimits.spend.limit, currency: "EUR" },
232 };
233 expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([]);
234 response.rate_limits.spend = {
235 ...rateLimits.spend,
236 limit: { ...rateLimits.spend.limit, amount_minor: 0 },
237 };
238 expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([]);
239 });
240 });
241
242 describe("claudeRateLimitEventToUpdate", () => {
243 it("scales the 0–1 utilization and epoch-second reset onto the probe's window id", () => {
244 expect(
245 claudeRateLimitEventToUpdate(
246 {
247 status: "allowed_warning",
248 rateLimitType: "seven_day",
249 utilization: 0.85,
250 resetsAt: 1_784_000_000,
251 },
252 noNames,
253 ),
254 ).toEqual({
255 windows: [
256 {
257 id: "seven_day",
258 kind: "weekly",
259 label: "Weekly",
260 usedPercent: 85,
261 windowDurationMins: 10080,
262 resetsAt: "2026-07-14T03:33:20.000Z",
263 },
264 ],
265 });
266 });
267
268 it("lands the streamed overage-included bucket on the row the probe named", () => {
269 const event = {
270 status: "allowed",
271 rateLimitType: "seven_day_overage_included" as never,
272 utilization: 0.4,
273 } as const;
274 expect(claudeRateLimitEventToUpdate(event, noNames)).toBeUndefined();
275 expect(claudeRateLimitEventToUpdate(event, { overageIncluded: "Fable" })).toEqual({
276 windows: [
277 {
278 id: "seven_day_fable",
279 kind: "weekly",
280 label: "Weekly · Fable",
281 usedPercent: 40,
282 windowDurationMins: 10080,
283 },
284 ],
285 });
286 });
287 });