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

codexUsageLimits.test.ts

131 lines3.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​
65describe("codexRateLimitsToUpdate", () => {
66 it("carries only the windows the notification names", () => {
67 expect(
68 codexRateLimitsToUpdate({
69 secondary: { usedPercent: 51, windowDurationMins: 10080 },
70 }),
71 ).toEqual({
72 windows: [
73 {
74 id: "secondary",
75 kind: "weekly",
76 label: "Weekly",
77 usedPercent: 51,
78 windowDurationMins: 10080,
79 },
80 ],
81 });
82 expect(codexRateLimitsToUpdate({ planType: "plus" })).toBeUndefined();
83 });
84});
85​
86describe("codexRateLimitsFailureMessage", () => {
87 it("keeps the JSON-RPC code and nothing else from a request failure", () => {
88 expect(
89 codexRateLimitsFailureMessage(
90 new CodexErrors.CodexAppServerRequestError({
91 code: -32603,
92 errorMessage:
93 "failed to fetch codex rate limits: GET https://chatgpt.com/backend-api/wham/usage failed: 401 Unauthorized",
94 }),
95 ),
96 ).toBe("Codex could not read usage (JSON-RPC -32603).");
97 });
98​
99 it("phrases a dead process differently from a bad answer", () => {
100 expect(
101 codexRateLimitsFailureMessage(new CodexErrors.CodexAppServerProcessExitedError({ code: 1 })),
102 ).toBe("Codex exited before it could report usage.");
103 });
104});
105​
106describe("codexResetCreditsToContract", () => {
107 it("counts available credits and reports the soonest expiry", () => {
108 expect(
109 codexResetCreditsToContract({
110 availableCount: 2,
111 credits: [
112 { status: "available", expiresAt: 1_784_500_000 },
113 { status: "redeemed", expiresAt: 1_700_000_000 },
114 { status: "available", expiresAt: 1_784_000_000 },
115 ],
116 }),
117 ).toEqual({ availableCount: 2, nextExpiresAt: "2026-07-14T03:33:20.000Z" });
118 expect(codexResetCreditsToContract({ availableCount: 0 })).toEqual({ availableCount: 0 });
119 expect(codexResetCreditsToContract(null)).toBeUndefined();
120 });
121​
122 it("rides along on the probe's limits", () => {
123 expect(
124 codexRateLimitsToLimits({
125 checkedAt,
126 snapshot: { primary: { usedPercent: 5, windowDurationMins: 300 } },
127 resetCredits: { availableCount: 1 },
128 }).resetCredits,
129 ).toEqual({ availableCount: 1 });
130 });
131});