Open History to see the last commit
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 mergeCodexRateLimits,
9} from "./codexUsageLimits.ts";
10
11const checkedAt = "2026-07-18T10:00:00.000Z";
12
13describe("codexRateLimitsToLimits", () => {
14 it("shows an Enterprise monthly credit allowance in reads and notifications without a duration", () => {
15 const snapshot = {
16 limitId: "codex",
17 planType: "enterprise_cbp_usage_based",
18 primary: null,
19 secondary: null,
20 individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
21 };
22 const windows = [
23 {
24 id: "individual",
25 kind: "monthly",
26 label: "Monthly credit limit",
27 usedPercent: 1,
28 resetsAt: "2026-07-19T22:26:40.000Z",
29 },
30 ];
31
32 expect(
33 codexRateLimitsToLimits({
34 checkedAt,
35 snapshot: { limitId: "spark" },
36 rateLimitsByLimitId: { codex: snapshot },
37 }),
38 ).toEqual({ checkedAt, windows });
39 expect(codexRateLimitsToUpdate(snapshot)).toEqual({ windows });
40 });
41
42 it("maps primary and secondary onto the session and weekly windows", () => {
43 expect(
44 codexRateLimitsToLimits({
45 checkedAt,
46 snapshot: {
47 planType: "plus",
48 primary: { usedPercent: 12, resetsAt: 1_784_000_000, windowDurationMins: 300 },
49 secondary: { usedPercent: 47, resetsAt: 1_784_500_000, windowDurationMins: 10080 },
50 },
51 }),
52 ).toEqual({
53 checkedAt,
54 windows: [
55 {
56 id: "primary",
57 kind: "session",
58 label: "Session",
59 usedPercent: 12,
60 windowDurationMins: 300,
61 resetsAt: "2026-07-14T03:33:20.000Z",
62 },
63 {
64 id: "secondary",
65 kind: "weekly",
66 label: "Weekly",
67 usedPercent: 47,
68 windowDurationMins: 10080,
69 resetsAt: "2026-07-19T22:26:40.000Z",
70 },
71 ],
72 });
73 });
74
75 it("treats a lone duration-less primary as monthly on Free and Go", () => {
76 expect(
77 codexRateLimitsToLimits({
78 checkedAt,
79 snapshot: { planType: "free", primary: { usedPercent: 80, resetsAt: null } },
80 }).windows,
81 ).toEqual([
82 {
83 id: "primary",
84 kind: "monthly",
85 label: "Monthly",
86 usedPercent: 80,
87 windowDurationMins: 43_200,
88 },
89 ]);
90 });
91
92 it("selects the main Codex allowance and leaves Spark out", () => {
93 const spark = {
94 limitId: "codex_bengalfox",
95 individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
96 primary: { usedPercent: 0, windowDurationMins: 300 },
97 secondary: { usedPercent: 90, windowDurationMins: 10080 },
98 };
99 expect(
100 codexRateLimitsToLimits({
101 checkedAt,
102 snapshot: spark,
103 rateLimitsByLimitId: {
104 codex_bengalfox: spark,
105 codex: { secondary: { usedPercent: 42, windowDurationMins: 10080 } },
106 },
107 }).windows,
108 ).toEqual([
109 {
110 id: "secondary",
111 kind: "weekly",
112 label: "Weekly",
113 usedPercent: 42,
114 windowDurationMins: 10080,
115 },
116 ]);
117 });
118
119 it.each([undefined, null, {}])(
120 "supports legacy reads with no bucket map: %j",
121 (rateLimitsByLimitId) => {
122 const snapshot = { primary: { usedPercent: 12, windowDurationMins: 300 } };
123 expect(codexRateLimitsToLimits({ checkedAt, snapshot, rateLimitsByLimitId })).toEqual(
124 codexRateLimitsToLimits({ checkedAt, snapshot }),
125 );
126 },
127 );
128});
129
130describe("codexRateLimitsToUpdate", () => {
131 it("does not republish cached windows over a newer account probe", () => {
132 const cached = {
133 primary: { usedPercent: 0, resetsAt: 1_784_000_000, windowDurationMins: 300 },
134 secondary: { usedPercent: 20, resetsAt: 1_784_500_000, windowDurationMins: 10080 },
135 };
136 expect(codexRateLimitsToUpdate(cached)?.windows).toHaveLength(2);
137 expect(codexRateLimitsToUpdate(structuredClone(cached), cached)).toBeUndefined();
138
139 const changed = { ...cached, secondary: { ...cached.secondary, usedPercent: 21 } };
140 expect(codexRateLimitsToUpdate(changed, cached)?.windows.map((window) => window.id)).toEqual([
141 "secondary",
142 ]);
143 expect(codexRateLimitsToUpdate({ primary: cached.primary }, cached)).toBeUndefined();
144 expect(
145 codexRateLimitsToUpdate({ primary: { ...cached.primary, resetsAt: 1_784_001_000 } }, cached)
146 ?.windows,
147 ).toHaveLength(1);
148 expect(
149 codexRateLimitsToUpdate({ primary: { ...cached.primary, usedPercent: 76 } }, cached)
150 ?.windows[0]?.usedPercent,
151 ).toBe(76);
152 });
153
154 it("carries only the windows the notification names", () => {
155 expect(
156 codexRateLimitsToUpdate({
157 secondary: { usedPercent: 51, windowDurationMins: 10080 },
158 }),
159 ).toEqual({
160 windows: [
161 {
162 id: "secondary",
163 kind: "weekly",
164 label: "Weekly",
165 usedPercent: 51,
166 windowDurationMins: 10080,
167 },
168 ],
169 });
170 expect(codexRateLimitsToUpdate({ planType: "plus" })).toBeUndefined();
171 });
172
173 it("ignores Spark notifications so they cannot overwrite the main allowance", () => {
174 expect(
175 codexRateLimitsToUpdate({
176 limitId: "codex_bengalfox",
177 individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
178 primary: { usedPercent: 0, windowDurationMins: 300 },
179 secondary: { usedPercent: 90, windowDurationMins: 10080 },
180 }),
181 ).toBeUndefined();
182 expect(
183 codexRateLimitsToUpdate({
184 limitId: "codex",
185 secondary: { usedPercent: 42, windowDurationMins: 10080 },
186 })?.windows,
187 ).toEqual([
188 {
189 id: "secondary",
190 kind: "weekly",
191 label: "Weekly",
192 usedPercent: 42,
193 windowDurationMins: 10080,
194 },
195 ]);
196 });
197});
198
199describe("codexRateLimitsFailureMessage", () => {
200 it("keeps the JSON-RPC code and nothing else from a request failure", () => {
201 expect(
202 codexRateLimitsFailureMessage(
203 new CodexErrors.CodexAppServerRequestError({
204 code: -32603,
205 errorMessage:
206 "failed to fetch codex rate limits: GET https://chatgpt.com/backend-api/wham/usage failed: 401 Unauthorized",
207 }),
208 ),
209 ).toBe("Codex could not read usage (JSON-RPC -32603).");
210 });
211});
212
213describe("mergeCodexRateLimits", () => {
214 it("updates monthly credit usage, retains it when omitted, and clears an explicit null", () => {
215 const snapshot = {
216 limitId: "codex",
217 primary: { usedPercent: 12 },
218 individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
219 };
220 const individualLimit = { remainingPercent: 75, resetsAt: 1_787_000_000 };
221 const changed = mergeCodexRateLimits(snapshot, { individualLimit });
222 expect(changed).toEqual({ ...snapshot, individualLimit });
223 expect(codexRateLimitsToUpdate(changed!)).toEqual({
224 windows: [
225 {
226 id: "primary",
227 kind: "session",
228 label: "Session",
229 usedPercent: 12,
230 windowDurationMins: 300,
231 },
232 {
233 id: "individual",
234 kind: "monthly",
235 label: "Monthly credit limit",
236 usedPercent: 25,
237 resetsAt: "2026-08-17T20:53:20.000Z",
238 },
239 ],
240 });
241
242 const retained = mergeCodexRateLimits(changed, { primary: null });
243 expect(retained).toEqual({ ...snapshot, primary: null, individualLimit });
244 const cleared = mergeCodexRateLimits(retained, { individualLimit: null });
245 expect(cleared).toEqual({ ...snapshot, primary: null, individualLimit: null });
246 expect(codexRateLimitsToLimits({ checkedAt, snapshot: cleared! }).windows).toEqual([]);
247 });
248
249 it("ignores a model-specific snapshot so it cannot replace the main allowance", () => {
250 const main = {
251 limitId: "codex",
252 primary: { usedPercent: 100, resetsAt: 1_800_000_000, windowDurationMins: 300 },
253 };
254 expect(
255 mergeCodexRateLimits(main, {
256 limitId: "spark",
257 individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
258 primary: { usedPercent: 3, resetsAt: 1_800_000_000, windowDurationMins: 300 },
259 }),
260 ).toBe(main);
261 });
262});
263
264import { codexUsageLimitResetAt } from "./codexUsageLimits.ts";
265
266describe("codexUsageLimitResetAt", () => {
267 it("waits for every exhausted window and never invents an unknown reset", () => {
268 expect(
269 codexUsageLimitResetAt({
270 primary: { usedPercent: 100, resetsAt: 2000000000 },
271 secondary: { usedPercent: 100, resetsAt: 2000100000 },
272 }),
273 ).toBe("2033-05-19T07:20:00.000Z");
274 expect(codexUsageLimitResetAt({ primary: { usedPercent: 100 } })).toBeNull();
275 expect(
276 codexUsageLimitResetAt({ primary: { usedPercent: 50, resetsAt: 2000000000 } }),
277 ).toBeNull();
278 });
279});