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

claudeUsageLimits.test.ts

188 lines5.5 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.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​
131describe("claudeRateLimitEventToUpdate", () => {
132 it("scales the 0–1 utilization and epoch-second reset onto the probe's window id", () => {
133 expect(
134 claudeRateLimitEventToUpdate(
135 {
136 status: "allowed_warning",
137 rateLimitType: "seven_day",
138 utilization: 0.85,
139 resetsAt: 1_784_000_000,
140 },
141 noNames,
142 ),
143 ).toEqual({
144 windows: [
145 {
146 id: "seven_day",
147 kind: "weekly",
148 label: "Weekly",
149 usedPercent: 85,
150 windowDurationMins: 10080,
151 resetsAt: "2026-07-14T03:33:20.000Z",
152 },
153 ],
154 });
155 });
156​
157 it("lands the streamed overage-included bucket on the row the probe named", () => {
158 const event = {
159 status: "allowed",
160 rateLimitType: "seven_day_overage_included" as never,
161 utilization: 0.4,
162 } as const;
163 expect(claudeRateLimitEventToUpdate(event, noNames)).toBeUndefined();
164 expect(claudeRateLimitEventToUpdate(event, { overageIncluded: "Fable" })).toEqual({
165 windows: [
166 {
167 id: "seven_day_fable",
168 kind: "weekly",
169 label: "Weekly · Fable",
170 usedPercent: 40,
171 windowDurationMins: 10080,
172 },
173 ],
174 });
175 });
176​
177 it("ignores windows the page does not render and events without a utilization", () => {
178 expect(
179 claudeRateLimitEventToUpdate(
180 { status: "allowed", rateLimitType: "seven_day_opus", utilization: 0.1 },
181 noNames,
182 ),
183 ).toBeUndefined();
184 expect(
185 claudeRateLimitEventToUpdate({ status: "rejected", rateLimitType: "five_hour" }, noNames),
186 ).toBeUndefined();
187 });
188});