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

codexUsageLimits.test.ts

203 lines5.8 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} from "./codexUsageLimits.ts";
10​
11const checkedAt = "2026-07-18T10:00:00.000Z";
12​
13describe("codexRateLimitsToLimits", () => {
14 it("maps primary and secondary onto the session and weekly windows", () => {
15 expect(
16 codexRateLimitsToLimits({
17 checkedAt,
18 snapshot: {
19 planType: "plus",
20 primary: { usedPercent: 12, resetsAt: 1_784_000_000, windowDurationMins: 300 },
21 secondary: { usedPercent: 47, resetsAt: 1_784_500_000, windowDurationMins: 10080 },
22 },
23 }),
24 ).toEqual({
25 checkedAt,
26 windows: [
27 {
28 id: "primary",
29 kind: "session",
30 label: "Session",
31 usedPercent: 12,
32 windowDurationMins: 300,
33 resetsAt: "2026-07-14T03:33:20.000Z",
34 },
35 {
36 id: "secondary",
37 kind: "weekly",
38 label: "Weekly",
39 usedPercent: 47,
40 windowDurationMins: 10080,
41 resetsAt: "2026-07-19T22:26:40.000Z",
42 },
43 ],
44 });
45 });
46​
47 it("treats a lone duration-less primary as monthly on Free and Go", () => {
48 expect(
49 codexRateLimitsToLimits({
50 checkedAt,
51 snapshot: { planType: "free", primary: { usedPercent: 80, resetsAt: null } },
52 }).windows,
53 ).toEqual([
54 {
55 id: "primary",
56 kind: "monthly",
57 label: "Monthly",
58 usedPercent: 80,
59 windowDurationMins: 43_200,
60 },
61 ]);
62 });
63​
64 it("selects the main Codex allowance and leaves Spark out", () => {
65 const spark = {
66 limitId: "codex_bengalfox",
67 primary: { usedPercent: 0, windowDurationMins: 300 },
68 secondary: { usedPercent: 90, windowDurationMins: 10080 },
69 };
70 expect(
71 codexRateLimitsToLimits({
72 checkedAt,
73 snapshot: spark,
74 rateLimitsByLimitId: {
75 codex_bengalfox: spark,
76 codex: { secondary: { usedPercent: 42, windowDurationMins: 10080 } },
77 },
78 }).windows,
79 ).toEqual([
80 {
81 id: "secondary",
82 kind: "weekly",
83 label: "Weekly",
84 usedPercent: 42,
85 windowDurationMins: 10080,
86 },
87 ]);
88 });
89​
90 it.each([undefined, null, {}])(
91 "supports legacy reads with no bucket map: %j",
92 (rateLimitsByLimitId) => {
93 const snapshot = { primary: { usedPercent: 12, windowDurationMins: 300 } };
94 expect(codexRateLimitsToLimits({ checkedAt, snapshot, rateLimitsByLimitId })).toEqual(
95 codexRateLimitsToLimits({ checkedAt, snapshot }),
96 );
97 },
98 );
99​
100 it("does not show a model-specific legacy snapshot as the main allowance", () => {
101 expect(
102 codexRateLimitsToLimits({
103 checkedAt,
104 snapshot: {
105 limitId: "codex_bengalfox",
106 secondary: { usedPercent: 90 },
107 },
108 }).windows,
109 ).toEqual([]);
110 });
111});
112​
113describe("codexRateLimitsToUpdate", () => {
114 it("carries only the windows the notification names", () => {
115 expect(
116 codexRateLimitsToUpdate({
117 secondary: { usedPercent: 51, windowDurationMins: 10080 },
118 }),
119 ).toEqual({
120 windows: [
121 {
122 id: "secondary",
123 kind: "weekly",
124 label: "Weekly",
125 usedPercent: 51,
126 windowDurationMins: 10080,
127 },
128 ],
129 });
130 expect(codexRateLimitsToUpdate({ planType: "plus" })).toBeUndefined();
131 });
132​
133 it("ignores Spark notifications so they cannot overwrite the main allowance", () => {
134 expect(
135 codexRateLimitsToUpdate({
136 limitId: "codex_bengalfox",
137 primary: { usedPercent: 0, windowDurationMins: 300 },
138 secondary: { usedPercent: 90, windowDurationMins: 10080 },
139 }),
140 ).toBeUndefined();
141 expect(
142 codexRateLimitsToUpdate({
143 limitId: "codex",
144 secondary: { usedPercent: 42, windowDurationMins: 10080 },
145 })?.windows,
146 ).toEqual([
147 {
148 id: "secondary",
149 kind: "weekly",
150 label: "Weekly",
151 usedPercent: 42,
152 windowDurationMins: 10080,
153 },
154 ]);
155 });
156});
157​
158describe("codexRateLimitsFailureMessage", () => {
159 it("keeps the JSON-RPC code and nothing else from a request failure", () => {
160 expect(
161 codexRateLimitsFailureMessage(
162 new CodexErrors.CodexAppServerRequestError({
163 code: -32603,
164 errorMessage:
165 "failed to fetch codex rate limits: GET https://chatgpt.com/backend-api/wham/usage failed: 401 Unauthorized",
166 }),
167 ),
168 ).toBe("Codex could not read usage (JSON-RPC -32603).");
169 });
170​
171 it("phrases a dead process differently from a bad answer", () => {
172 expect(
173 codexRateLimitsFailureMessage(new CodexErrors.CodexAppServerProcessExitedError({ code: 1 })),
174 ).toBe("Codex exited before it could report usage.");
175 });
176});
177​
178describe("codexResetCreditsToContract", () => {
179 it("counts available credits and reports the soonest expiry", () => {
180 expect(
181 codexResetCreditsToContract({
182 availableCount: 2,
183 credits: [
184 { status: "available", expiresAt: 1_784_500_000 },
185 { status: "redeemed", expiresAt: 1_700_000_000 },
186 { status: "available", expiresAt: 1_784_000_000 },
187 ],
188 }),
189 ).toEqual({ availableCount: 2, nextExpiresAt: "2026-07-14T03:33:20.000Z" });
190 expect(codexResetCreditsToContract({ availableCount: 0 })).toEqual({ availableCount: 0 });
191 expect(codexResetCreditsToContract(null)).toBeUndefined();
192 });
193​
194 it("rides along on the probe's limits", () => {
195 expect(
196 codexRateLimitsToLimits({
197 checkedAt,
198 snapshot: { primary: { usedPercent: 5, windowDurationMins: 300 } },
199 resetCredits: { availableCount: 1 },
200 }).resetCredits,
201 ).toEqual({ availableCount: 1 });
202 });
203});