The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as DateTime from "effect/DateTime"; |
| 2 | import { ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts"; |
| 3 | import { exhaustedUsageWindow } from "@t3tools/shared/usageLimits"; |
| 4 | import * as CodexErrors from "effect-codex-app-server/errors"; |
| 5 | import { describe, expect, it } from "vite-plus/test"; |
| 6 | |
| 7 | import { |
| 8 | codexMessageResetAt, |
| 9 | codexRateLimitsFailureMessage, |
| 10 | codexRateLimitsToLimits, |
| 11 | codexRateLimitsToUpdate, |
| 12 | codexTimeZone, |
| 13 | mergeCodexRateLimits, |
| 14 | } from "./codexUsageLimits.ts"; |
| 15 | |
| 16 | const checkedAt = "2026-07-18T10:00:00.000Z"; |
| 17 | |
| 18 | describe("codexRateLimitsToLimits", () => { |
| 19 | it("shows an Enterprise monthly credit allowance in reads and notifications without a duration", () => { |
| 20 | const snapshot = { |
| 21 | limitId: "codex", |
| 22 | planType: "enterprise_cbp_usage_based", |
| 23 | primary: null, |
| 24 | secondary: null, |
| 25 | individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 }, |
| 26 | }; |
| 27 | const windows = [ |
| 28 | { |
| 29 | id: "individual", |
| 30 | kind: "monthly", |
| 31 | label: "Monthly credit limit", |
| 32 | usedPercent: 1, |
| 33 | resetsAt: "2026-07-19T22:26:40.000Z", |
| 34 | }, |
| 35 | ]; |
| 36 | |
| 37 | expect( |
| 38 | codexRateLimitsToLimits({ |
| 39 | checkedAt, |
| 40 | snapshot: { limitId: "spark" }, |
| 41 | rateLimitsByLimitId: { codex: snapshot }, |
| 42 | }), |
| 43 | ).toEqual({ checkedAt, windows }); |
| 44 | expect(codexRateLimitsToUpdate(snapshot)).toEqual({ windows }); |
| 45 | }); |
| 46 | |
| 47 | it("keeps a seat-based workspace with a zero credit cap usable while its plan windows have room", () => { |
| 48 | const usageLimits = codexRateLimitsToLimits({ |
| 49 | checkedAt, |
| 50 | snapshot: { |
| 51 | limitId: "codex", |
| 52 | planType: "team", |
| 53 | rateLimitReachedType: null, |
| 54 | primary: { usedPercent: 0, resetsAt: 1_784_000_000, windowDurationMins: 300 }, |
| 55 | secondary: { usedPercent: 14, resetsAt: 1_784_500_000, windowDurationMins: 10080 }, |
| 56 | individualLimit: { remainingPercent: 0, resetsAt: 1_796_083_200 }, |
| 57 | }, |
| 58 | }); |
| 59 | expect( |
| 60 | exhaustedUsageWindow( |
| 61 | { |
| 62 | instanceId: ProviderInstanceId.make("codex"), |
| 63 | driver: ProviderDriverKind.make("codex"), |
| 64 | enabled: true, |
| 65 | installed: true, |
| 66 | version: null, |
| 67 | status: "ready", |
| 68 | auth: { status: "authenticated" }, |
| 69 | checkedAt, |
| 70 | models: [], |
| 71 | slashCommands: [], |
| 72 | skills: [], |
| 73 | usageLimits, |
| 74 | }, |
| 75 | Date.parse(checkedAt), |
| 76 | "gpt-6.1-sol", |
| 77 | ), |
| 78 | ).toBeUndefined(); |
| 79 | }); |
| 80 | |
| 81 | it("maps primary and secondary onto the session and weekly windows", () => { |
| 82 | expect( |
| 83 | codexRateLimitsToLimits({ |
| 84 | checkedAt, |
| 85 | snapshot: { |
| 86 | planType: "plus", |
| 87 | primary: { usedPercent: 12, resetsAt: 1_784_000_000, windowDurationMins: 300 }, |
| 88 | secondary: { usedPercent: 47, resetsAt: 1_784_500_000, windowDurationMins: 10080 }, |
| 89 | }, |
| 90 | }), |
| 91 | ).toEqual({ |
| 92 | checkedAt, |
| 93 | windows: [ |
| 94 | { |
| 95 | id: "primary", |
| 96 | kind: "session", |
| 97 | label: "Session", |
| 98 | usedPercent: 12, |
| 99 | windowDurationMins: 300, |
| 100 | resetsAt: "2026-07-14T03:33:20.000Z", |
| 101 | }, |
| 102 | { |
| 103 | id: "secondary", |
| 104 | kind: "weekly", |
| 105 | label: "Weekly", |
| 106 | usedPercent: 47, |
| 107 | windowDurationMins: 10080, |
| 108 | resetsAt: "2026-07-19T22:26:40.000Z", |
| 109 | }, |
| 110 | ], |
| 111 | }); |
| 112 | }); |
| 113 | |
| 114 | it("treats a lone duration-less primary as monthly on Free and Go", () => { |
| 115 | expect( |
| 116 | codexRateLimitsToLimits({ |
| 117 | checkedAt, |
| 118 | snapshot: { planType: "free", primary: { usedPercent: 80, resetsAt: null } }, |
| 119 | }).windows, |
| 120 | ).toEqual([ |
| 121 | { |
| 122 | id: "primary", |
| 123 | kind: "monthly", |
| 124 | label: "Monthly", |
| 125 | usedPercent: 80, |
| 126 | windowDurationMins: 43_200, |
| 127 | }, |
| 128 | ]); |
| 129 | }); |
| 130 | |
| 131 | it("selects the main Codex allowance and leaves Spark out", () => { |
| 132 | const spark = { |
| 133 | limitId: "codex_bengalfox", |
| 134 | individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 }, |
| 135 | primary: { usedPercent: 0, windowDurationMins: 300 }, |
| 136 | secondary: { usedPercent: 90, windowDurationMins: 10080 }, |
| 137 | }; |
| 138 | expect( |
| 139 | codexRateLimitsToLimits({ |
| 140 | checkedAt, |
| 141 | snapshot: spark, |
| 142 | rateLimitsByLimitId: { |
| 143 | codex_bengalfox: spark, |
| 144 | codex: { secondary: { usedPercent: 42, windowDurationMins: 10080 } }, |
| 145 | }, |
| 146 | }).windows, |
| 147 | ).toEqual([ |
| 148 | { |
| 149 | id: "secondary", |
| 150 | kind: "weekly", |
| 151 | label: "Weekly", |
| 152 | usedPercent: 42, |
| 153 | windowDurationMins: 10080, |
| 154 | }, |
| 155 | ]); |
| 156 | }); |
| 157 | |
| 158 | it.each([undefined, null, {}])( |
| 159 | "supports legacy reads with no bucket map: %j", |
| 160 | (rateLimitsByLimitId) => { |
| 161 | const snapshot = { primary: { usedPercent: 12, windowDurationMins: 300 } }; |
| 162 | expect(codexRateLimitsToLimits({ checkedAt, snapshot, rateLimitsByLimitId })).toEqual( |
| 163 | codexRateLimitsToLimits({ checkedAt, snapshot }), |
| 164 | ); |
| 165 | }, |
| 166 | ); |
| 167 | }); |
| 168 | |
| 169 | describe("codexRateLimitsToUpdate", () => { |
| 170 | it("does not republish cached windows over a newer account probe", () => { |
| 171 | const cached = { |
| 172 | primary: { usedPercent: 0, resetsAt: 1_784_000_000, windowDurationMins: 300 }, |
| 173 | secondary: { usedPercent: 20, resetsAt: 1_784_500_000, windowDurationMins: 10080 }, |
| 174 | }; |
| 175 | expect(codexRateLimitsToUpdate(cached)?.windows).toHaveLength(2); |
| 176 | expect(codexRateLimitsToUpdate(structuredClone(cached), cached)).toBeUndefined(); |
| 177 | |
| 178 | const changed = { ...cached, secondary: { ...cached.secondary, usedPercent: 21 } }; |
| 179 | expect(codexRateLimitsToUpdate(changed, cached)?.windows.map((window) => window.id)).toEqual([ |
| 180 | "secondary", |
| 181 | ]); |
| 182 | expect(codexRateLimitsToUpdate({ primary: cached.primary }, cached)).toBeUndefined(); |
| 183 | expect( |
| 184 | codexRateLimitsToUpdate({ primary: { ...cached.primary, resetsAt: 1_784_001_000 } }, cached) |
| 185 | ?.windows, |
| 186 | ).toHaveLength(1); |
| 187 | expect( |
| 188 | codexRateLimitsToUpdate({ primary: { ...cached.primary, usedPercent: 76 } }, cached) |
| 189 | ?.windows[0]?.usedPercent, |
| 190 | ).toBe(76); |
| 191 | }); |
| 192 | |
| 193 | it("carries only the windows the notification names", () => { |
| 194 | expect( |
| 195 | codexRateLimitsToUpdate({ |
| 196 | secondary: { usedPercent: 51, windowDurationMins: 10080 }, |
| 197 | }), |
| 198 | ).toEqual({ |
| 199 | windows: [ |
| 200 | { |
| 201 | id: "secondary", |
| 202 | kind: "weekly", |
| 203 | label: "Weekly", |
| 204 | usedPercent: 51, |
| 205 | windowDurationMins: 10080, |
| 206 | }, |
| 207 | ], |
| 208 | }); |
| 209 | expect(codexRateLimitsToUpdate({ planType: "plus" })).toBeUndefined(); |
| 210 | }); |
| 211 | |
| 212 | it("ignores Spark notifications so they cannot overwrite the main allowance", () => { |
| 213 | expect( |
| 214 | codexRateLimitsToUpdate({ |
| 215 | limitId: "codex_bengalfox", |
| 216 | individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 }, |
| 217 | primary: { usedPercent: 0, windowDurationMins: 300 }, |
| 218 | secondary: { usedPercent: 90, windowDurationMins: 10080 }, |
| 219 | }), |
| 220 | ).toBeUndefined(); |
| 221 | expect( |
| 222 | codexRateLimitsToUpdate({ |
| 223 | limitId: "codex", |
| 224 | secondary: { usedPercent: 42, windowDurationMins: 10080 }, |
| 225 | })?.windows, |
| 226 | ).toEqual([ |
| 227 | { |
| 228 | id: "secondary", |
| 229 | kind: "weekly", |
| 230 | label: "Weekly", |
| 231 | usedPercent: 42, |
| 232 | windowDurationMins: 10080, |
| 233 | }, |
| 234 | ]); |
| 235 | }); |
| 236 | }); |
| 237 | |
| 238 | describe("codexRateLimitsFailureMessage", () => { |
| 239 | it("keeps the JSON-RPC code and nothing else from a request failure", () => { |
| 240 | expect( |
| 241 | codexRateLimitsFailureMessage( |
| 242 | new CodexErrors.CodexAppServerRequestError({ |
| 243 | code: -32603, |
| 244 | errorMessage: |
| 245 | "failed to fetch codex rate limits: GET https://chatgpt.com/backend-api/wham/usage failed: 401 Unauthorized", |
| 246 | }), |
| 247 | ), |
| 248 | ).toBe("Codex could not read usage (JSON-RPC -32603)."); |
| 249 | }); |
| 250 | }); |
| 251 | |
| 252 | describe("mergeCodexRateLimits", () => { |
| 253 | it("updates monthly credit usage, retains it when omitted, and clears an explicit null", () => { |
| 254 | const snapshot = { |
| 255 | limitId: "codex", |
| 256 | primary: { usedPercent: 12 }, |
| 257 | individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 }, |
| 258 | }; |
| 259 | const individualLimit = { remainingPercent: 75, resetsAt: 1_787_000_000 }; |
| 260 | const changed = mergeCodexRateLimits(snapshot, { individualLimit }); |
| 261 | expect(changed).toEqual({ ...snapshot, individualLimit }); |
| 262 | expect(codexRateLimitsToUpdate(changed!)).toEqual({ |
| 263 | windows: [ |
| 264 | { |
| 265 | id: "primary", |
| 266 | kind: "session", |
| 267 | label: "Session", |
| 268 | usedPercent: 12, |
| 269 | windowDurationMins: 300, |
| 270 | }, |
| 271 | { |
| 272 | id: "individual", |
| 273 | kind: "monthly", |
| 274 | label: "Monthly credit limit", |
| 275 | usedPercent: 25, |
| 276 | resetsAt: "2026-08-17T20:53:20.000Z", |
| 277 | }, |
| 278 | ], |
| 279 | }); |
| 280 | |
| 281 | const retained = mergeCodexRateLimits(changed, { primary: null }); |
| 282 | expect(retained).toEqual({ ...snapshot, primary: null, individualLimit }); |
| 283 | const cleared = mergeCodexRateLimits(retained, { individualLimit: null }); |
| 284 | expect(cleared).toEqual({ ...snapshot, primary: null, individualLimit: null }); |
| 285 | expect(codexRateLimitsToLimits({ checkedAt, snapshot: cleared! }).windows).toEqual([]); |
| 286 | }); |
| 287 | |
| 288 | it("ignores a model-specific snapshot so it cannot replace the main allowance", () => { |
| 289 | const main = { |
| 290 | limitId: "codex", |
| 291 | primary: { usedPercent: 100, resetsAt: 1_800_000_000, windowDurationMins: 300 }, |
| 292 | }; |
| 293 | expect( |
| 294 | mergeCodexRateLimits(main, { |
| 295 | limitId: "spark", |
| 296 | individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 }, |
| 297 | primary: { usedPercent: 3, resetsAt: 1_800_000_000, windowDurationMins: 300 }, |
| 298 | }), |
| 299 | ).toBe(main); |
| 300 | }); |
| 301 | }); |
| 302 | |
| 303 | import { codexUsageLimitResetAt } from "./codexUsageLimits.ts"; |
| 304 | |
| 305 | describe("codexUsageLimitResetAt", () => { |
| 306 | it("waits for every exhausted window and never invents an unknown reset", () => { |
| 307 | expect( |
| 308 | codexUsageLimitResetAt({ |
| 309 | primary: { usedPercent: 100, resetsAt: 2000000000 }, |
| 310 | secondary: { usedPercent: 100, resetsAt: 2000100000 }, |
| 311 | }), |
| 312 | ).toBe("2033-05-19T07:20:00.000Z"); |
| 313 | expect(codexUsageLimitResetAt({ primary: { usedPercent: 100 } })).toBeNull(); |
| 314 | expect( |
| 315 | codexUsageLimitResetAt({ primary: { usedPercent: 50, resetsAt: 2000000000 } }), |
| 316 | ).toBeNull(); |
| 317 | }); |
| 318 | }); |
| 319 | |
| 320 | describe("codexMessageResetAt", () => { |
| 321 | const dhaka = DateTime.zoneMakeNamedUnsafe("Asia/Dhaka"); |
| 322 | const at = (iso: string) => DateTime.makeUnsafe(iso); |
| 323 | const failedAt = at("2026-11-07T03:00:00Z"); // 09:00 in Dhaka |
| 324 | |
| 325 | // Codex prints local time without seconds, so the parsed reset is the minute after. |
| 326 | it("reads Codex's same-day and dated reset text in Codex's zone", () => { |
| 327 | expect( |
| 328 | codexMessageResetAt("You’ve hit your usage limit. Try again at 3:37 PM.", failedAt, dhaka), |
| 329 | ).toBe("2026-11-07T09:38:00.000Z"); |
| 330 | expect( |
| 331 | codexMessageResetAt("Upgrade, or try again at Dec 1st, 2026 12:05 AM.", failedAt, dhaka), |
| 332 | ).toBe("2026-11-30T18:06:00.000Z"); |
| 333 | expect(codexMessageResetAt("Try again at 12:30 PM.", failedAt, dhaka)).toBe( |
| 334 | "2026-11-07T06:31:00.000Z", |
| 335 | ); |
| 336 | }); |
| 337 | |
| 338 | it("dates today's form by when Codex failed, across local midnight", () => { |
| 339 | expect(codexMessageResetAt("Try again at 11:59 PM.", at("2026-11-07T17:59:59Z"), dhaka)).toBe( |
| 340 | "2026-11-07T18:00:00.000Z", |
| 341 | ); |
| 342 | }); |
| 343 | |
| 344 | it("takes the later instant of a repeated DST hour", () => { |
| 345 | // 1:30 AM occurs twice in New York on Nov 1st, 2026: 05:30Z (EDT) and 06:30Z (EST). |
| 346 | const newYork = DateTime.zoneMakeNamedUnsafe("America/New_York"); |
| 347 | for (const iso of ["2026-11-01T05:15:00Z", "2026-11-01T06:15:00Z"]) { |
| 348 | expect(codexMessageResetAt("Try again at 1:30 AM.", at(iso), newYork)).toBe( |
| 349 | "2026-11-01T06:31:00.000Z", |
| 350 | ); |
| 351 | } |
| 352 | }); |
| 353 | |
| 354 | it("leaves the reset unknown without reset text or once it has passed", () => { |
| 355 | expect( |
| 356 | codexMessageResetAt("You’ve hit your usage limit. Try again later.", failedAt, dhaka), |
| 357 | ).toBeNull(); |
| 358 | expect( |
| 359 | codexMessageResetAt( |
| 360 | "exceeded retry limit, last status: 429 Too Many Requests", |
| 361 | failedAt, |
| 362 | dhaka, |
| 363 | ), |
| 364 | ).toBeNull(); |
| 365 | expect(codexMessageResetAt("Try again at 8:15 AM.", failedAt, dhaka)).toBeNull(); |
| 366 | }); |
| 367 | }); |
| 368 | |
| 369 | describe("codexTimeZone", () => { |
| 370 | it("uses Codex's TZ and falls back to the server's zone", () => { |
| 371 | const local = DateTime.zoneToString(DateTime.zoneMakeLocal()); |
| 372 | expect(DateTime.zoneToString(codexTimeZone({ TZ: ":Asia/Dhaka" }))).toBe("Asia/Dhaka"); |
| 373 | expect(DateTime.zoneToString(codexTimeZone({ TZ: "not/a-zone" }))).toBe(local); |
| 374 | expect(DateTime.zoneToString(codexTimeZone({}))).toBe(local); |
| 375 | }); |
| 376 | }); |