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