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

codexUsageLimits.test.ts

383 lines12.0 KB

1import * as CodexErrors from "effect-codex-app-server/errors";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 codexRateLimitsFailureMessage,
6 codexRateLimitsToLimits,
7 codexRateLimitsToUpdate,
8 codexResetCreditsToContract,
9 codexUsageLimitMessage,
10 mergeCodexRateLimits,
11} from "./codexUsageLimits.ts";
12​
13const checkedAt = "2026-07-18T10:00:00.000Z";
14​
15describe("codexRateLimitsToLimits", () => {
16 it("shows an Enterprise monthly credit allowance in reads and notifications without a duration", () => {
17 const snapshot = {
18 limitId: "codex",
19 planType: "enterprise_cbp_usage_based",
20 primary: null,
21 secondary: null,
22 individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
23 };
24 const windows = [
25 {
26 id: "individual",
27 kind: "monthly",
28 label: "Monthly credit limit",
29 usedPercent: 1,
30 resetsAt: "2026-07-19T22:26:40.000Z",
31 },
32 ];
33​
34 expect(
35 codexRateLimitsToLimits({
36 checkedAt,
37 snapshot: { limitId: "spark" },
38 rateLimitsByLimitId: { codex: snapshot },
39 }),
40 ).toEqual({ checkedAt, windows });
41 expect(codexRateLimitsToUpdate(snapshot)).toEqual({ windows });
42 });
43​
44 it("maps primary and secondary onto the session and weekly windows", () => {
45 expect(
46 codexRateLimitsToLimits({
47 checkedAt,
48 snapshot: {
49 planType: "plus",
50 primary: { usedPercent: 12, resetsAt: 1_784_000_000, windowDurationMins: 300 },
51 secondary: { usedPercent: 47, resetsAt: 1_784_500_000, windowDurationMins: 10080 },
52 },
53 }),
54 ).toEqual({
55 checkedAt,
56 windows: [
57 {
58 id: "primary",
59 kind: "session",
60 label: "Session",
61 usedPercent: 12,
62 windowDurationMins: 300,
63 resetsAt: "2026-07-14T03:33:20.000Z",
64 },
65 {
66 id: "secondary",
67 kind: "weekly",
68 label: "Weekly",
69 usedPercent: 47,
70 windowDurationMins: 10080,
71 resetsAt: "2026-07-19T22:26:40.000Z",
72 },
73 ],
74 });
75 });
76​
77 it("treats a lone duration-less primary as monthly on Free and Go", () => {
78 expect(
79 codexRateLimitsToLimits({
80 checkedAt,
81 snapshot: { planType: "free", primary: { usedPercent: 80, resetsAt: null } },
82 }).windows,
83 ).toEqual([
84 {
85 id: "primary",
86 kind: "monthly",
87 label: "Monthly",
88 usedPercent: 80,
89 windowDurationMins: 43_200,
90 },
91 ]);
92 });
93​
94 it("selects the main Codex allowance and leaves Spark out", () => {
95 const spark = {
96 limitId: "codex_bengalfox",
97 individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
98 primary: { usedPercent: 0, windowDurationMins: 300 },
99 secondary: { usedPercent: 90, windowDurationMins: 10080 },
100 };
101 expect(
102 codexRateLimitsToLimits({
103 checkedAt,
104 snapshot: spark,
105 rateLimitsByLimitId: {
106 codex_bengalfox: spark,
107 codex: { secondary: { usedPercent: 42, windowDurationMins: 10080 } },
108 },
109 }).windows,
110 ).toEqual([
111 {
112 id: "secondary",
113 kind: "weekly",
114 label: "Weekly",
115 usedPercent: 42,
116 windowDurationMins: 10080,
117 },
118 ]);
119 });
120​
121 it.each([undefined, null, {}])(
122 "supports legacy reads with no bucket map: %j",
123 (rateLimitsByLimitId) => {
124 const snapshot = { primary: { usedPercent: 12, windowDurationMins: 300 } };
125 expect(codexRateLimitsToLimits({ checkedAt, snapshot, rateLimitsByLimitId })).toEqual(
126 codexRateLimitsToLimits({ checkedAt, snapshot }),
127 );
128 },
129 );
130​
131 it("does not show a model-specific legacy snapshot as the main allowance", () => {
132 expect(
133 codexRateLimitsToLimits({
134 checkedAt,
135 snapshot: {
136 limitId: "codex_bengalfox",
137 individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
138 secondary: { usedPercent: 90 },
139 },
140 }).windows,
141 ).toEqual([]);
142 });
143});
144​
145describe("codexRateLimitsToUpdate", () => {
146 it("carries only the windows the notification names", () => {
147 expect(
148 codexRateLimitsToUpdate({
149 secondary: { usedPercent: 51, windowDurationMins: 10080 },
150 }),
151 ).toEqual({
152 windows: [
153 {
154 id: "secondary",
155 kind: "weekly",
156 label: "Weekly",
157 usedPercent: 51,
158 windowDurationMins: 10080,
159 },
160 ],
161 });
162 expect(codexRateLimitsToUpdate({ planType: "plus" })).toBeUndefined();
163 });
164​
165 it("ignores Spark notifications so they cannot overwrite the main allowance", () => {
166 expect(
167 codexRateLimitsToUpdate({
168 limitId: "codex_bengalfox",
169 individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
170 primary: { usedPercent: 0, windowDurationMins: 300 },
171 secondary: { usedPercent: 90, windowDurationMins: 10080 },
172 }),
173 ).toBeUndefined();
174 expect(
175 codexRateLimitsToUpdate({
176 limitId: "codex",
177 secondary: { usedPercent: 42, windowDurationMins: 10080 },
178 })?.windows,
179 ).toEqual([
180 {
181 id: "secondary",
182 kind: "weekly",
183 label: "Weekly",
184 usedPercent: 42,
185 windowDurationMins: 10080,
186 },
187 ]);
188 });
189});
190​
191describe("codexRateLimitsFailureMessage", () => {
192 it("keeps the JSON-RPC code and nothing else from a request failure", () => {
193 expect(
194 codexRateLimitsFailureMessage(
195 new CodexErrors.CodexAppServerRequestError({
196 code: -32603,
197 errorMessage:
198 "failed to fetch codex rate limits: GET https://chatgpt.com/backend-api/wham/usage failed: 401 Unauthorized",
199 }),
200 ),
201 ).toBe("Codex could not read usage (JSON-RPC -32603).");
202 });
203​
204 it("phrases a dead process differently from a bad answer", () => {
205 expect(
206 codexRateLimitsFailureMessage(new CodexErrors.CodexAppServerProcessExitedError({ code: 1 })),
207 ).toBe("Codex exited before it could report usage.");
208 });
209});
210​
211describe("codexResetCreditsToContract", () => {
212 it("counts available credits and reports the soonest expiry", () => {
213 expect(
214 codexResetCreditsToContract({
215 availableCount: 2,
216 credits: [
217 { status: "available", expiresAt: 1_784_500_000 },
218 { status: "redeemed", expiresAt: 1_700_000_000 },
219 { status: "available", expiresAt: 1_784_000_000 },
220 ],
221 }),
222 ).toEqual({ availableCount: 2, nextExpiresAt: "2026-07-14T03:33:20.000Z" });
223 expect(codexResetCreditsToContract({ availableCount: 0 })).toEqual({ availableCount: 0 });
224 expect(codexResetCreditsToContract(null)).toBeUndefined();
225 });
226​
227 it("rides along on the probe's limits", () => {
228 expect(
229 codexRateLimitsToLimits({
230 checkedAt,
231 snapshot: { primary: { usedPercent: 5, windowDurationMins: 300 } },
232 resetCredits: { availableCount: 1 },
233 }).resetCredits,
234 ).toEqual({ availableCount: 1 });
235 });
236});
237​
238describe("codexUsageLimitMessage", () => {
239 const at = "2026-01-01T00:00:00.000Z";
240 const atSeconds = Date.parse(at) / 1000;
241​
242 it("names the exhausted window and the workspace's missing credits", () => {
243 expect(
244 codexUsageLimitMessage(
245 {
246 limitId: "codex",
247 rateLimitReachedType: "workspace_owner_credits_depleted",
248 primary: { usedPercent: 40, resetsAt: atSeconds + 3_600, windowDurationMins: 300 },
249 secondary: {
250 usedPercent: 100,
251 resetsAt: atSeconds + 5 * 86_400 + 5 * 3_600,
252 windowDurationMins: 10_080,
253 },
254 },
255 at,
256 ),
257 ).toBe(
258 "Codex usage limit reached. The weekly limit resets in 5d 5h. The workspace has no credits to continue sooner: ask your workspace owner to add credits, or send the message again once the limit resets.",
259 );
260 });
261​
262 it("points a reached spend cap at the workspace owner", () => {
263 expect(
264 codexUsageLimitMessage(
265 {
266 limitId: "codex",
267 rateLimitReachedType: "workspace_member_usage_limit_reached",
268 primary: {
269 usedPercent: 100,
270 resetsAt: atSeconds + 3 * 3_600 + 20 * 60,
271 windowDurationMins: 300,
272 },
273 },
274 at,
275 ),
276 ).toBe(
277 "Codex usage limit reached. The session limit resets in 3h 20m. The workspace spend limit is reached: ask your workspace owner to raise it, or send the message again once the limit resets.",
278 );
279 });
280​
281 it("names no window when credits run out without one", () => {
282 expect(
283 codexUsageLimitMessage(
284 { limitId: "codex", rateLimitReachedType: "workspace_member_credits_depleted" },
285 at,
286 ),
287 ).toBe(
288 "Codex usage limit reached. The workspace has no credits to continue sooner: ask your workspace owner to add credits, or send the message again once the limit resets.",
289 );
290 });
291​
292 it("says only what it knows without a snapshot", () => {
293 expect(codexUsageLimitMessage(undefined, at)).toBe(
294 "Codex usage limit reached. Send the message again once the limit resets.",
295 );
296 });
297});
298​
299describe("mergeCodexRateLimits", () => {
300 it("updates monthly credit usage, retains it when omitted, and clears an explicit null", () => {
301 const snapshot = {
302 limitId: "codex",
303 primary: { usedPercent: 12 },
304 individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
305 };
306 const individualLimit = { remainingPercent: 75, resetsAt: 1_787_000_000 };
307 const changed = mergeCodexRateLimits(snapshot, { individualLimit });
308 expect(changed).toEqual({ ...snapshot, individualLimit });
309 expect(codexRateLimitsToUpdate(changed!)).toEqual({
310 windows: [
311 {
312 id: "primary",
313 kind: "session",
314 label: "Session",
315 usedPercent: 12,
316 windowDurationMins: 300,
317 },
318 {
319 id: "individual",
320 kind: "monthly",
321 label: "Monthly credit limit",
322 usedPercent: 25,
323 resetsAt: "2026-08-17T20:53:20.000Z",
324 },
325 ],
326 });
327​
328 const retained = mergeCodexRateLimits(changed, { primary: null });
329 expect(retained).toEqual({ ...snapshot, primary: null, individualLimit });
330 const cleared = mergeCodexRateLimits(retained, { individualLimit: null });
331 expect(cleared).toEqual({ ...snapshot, primary: null, individualLimit: null });
332 expect(codexRateLimitsToLimits({ checkedAt, snapshot: cleared! }).windows).toEqual([]);
333 });
334​
335 it("keeps windows an update does not carry", () => {
336 const merged = mergeCodexRateLimits(
337 {
338 limitId: "codex",
339 planType: "business",
340 primary: { usedPercent: 100, resetsAt: 1_800_000_000, windowDurationMins: 300 },
341 },
342 { rateLimitReachedType: "rate_limit_reached" },
343 );
344​
345 expect(merged).toEqual({
346 limitId: "codex",
347 planType: "business",
348 rateLimitReachedType: "rate_limit_reached",
349 primary: { usedPercent: 100, resetsAt: 1_800_000_000, windowDurationMins: 300 },
350 });
351 });
352​
353 it("ignores a model-specific snapshot so it cannot replace the main allowance", () => {
354 const main = {
355 limitId: "codex",
356 primary: { usedPercent: 100, resetsAt: 1_800_000_000, windowDurationMins: 300 },
357 };
358 expect(
359 mergeCodexRateLimits(main, {
360 limitId: "spark",
361 individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
362 primary: { usedPercent: 3, resetsAt: 1_800_000_000, windowDurationMins: 300 },
363 }),
364 ).toBe(main);
365 });
366});
367​
368import { codexUsageLimitResetAt } from "./codexUsageLimits.ts";
369​
370describe("codexUsageLimitResetAt", () => {
371 it("waits for every exhausted window and never invents an unknown reset", () => {
372 expect(
373 codexUsageLimitResetAt({
374 primary: { usedPercent: 100, resetsAt: 2000000000 },
375 secondary: { usedPercent: 100, resetsAt: 2000100000 },
376 }),
377 ).toBe("2033-05-19T07:20:00.000Z");
378 expect(codexUsageLimitResetAt({ primary: { usedPercent: 100 } })).toBeNull();
379 expect(
380 codexUsageLimitResetAt({ primary: { usedPercent: 50, resetsAt: 2000000000 } }),
381 ).toBeNull();
382 });
383});