203 lines · 5.8 KB · TypeScript
RawDownload
import * as CodexErrors from "effect-codex-app-server/errors";import { describe, expect, it } from "vite-plus/test"; import {  codexRateLimitsFailureMessage,  codexRateLimitsToLimits,  codexRateLimitsToUpdate,  codexResetCreditsToContract,} from "./codexUsageLimits.ts"; const checkedAt = "2026-07-18T10:00:00.000Z"; describe("codexRateLimitsToLimits", () => {  it("maps primary and secondary onto the session and weekly windows", () => {    expect(      codexRateLimitsToLimits({        checkedAt,        snapshot: {          planType: "plus",          primary: { usedPercent: 12, resetsAt: 1_784_000_000, windowDurationMins: 300 },          secondary: { usedPercent: 47, resetsAt: 1_784_500_000, windowDurationMins: 10080 },        },      }),    ).toEqual({      checkedAt,      windows: [        {          id: "primary",          kind: "session",          label: "Session",          usedPercent: 12,          windowDurationMins: 300,          resetsAt: "2026-07-14T03:33:20.000Z",        },        {          id: "secondary",          kind: "weekly",          label: "Weekly",          usedPercent: 47,          windowDurationMins: 10080,          resetsAt: "2026-07-19T22:26:40.000Z",        },      ],    });  });   it("treats a lone duration-less primary as monthly on Free and Go", () => {    expect(      codexRateLimitsToLimits({        checkedAt,        snapshot: { planType: "free", primary: { usedPercent: 80, resetsAt: null } },      }).windows,    ).toEqual([      {        id: "primary",        kind: "monthly",        label: "Monthly",        usedPercent: 80,        windowDurationMins: 43_200,      },    ]);  });   it("selects the main Codex allowance and leaves Spark out", () => {    const spark = {      limitId: "codex_bengalfox",      primary: { usedPercent: 0, windowDurationMins: 300 },      secondary: { usedPercent: 90, windowDurationMins: 10080 },    };    expect(      codexRateLimitsToLimits({        checkedAt,        snapshot: spark,        rateLimitsByLimitId: {          codex_bengalfox: spark,          codex: { secondary: { usedPercent: 42, windowDurationMins: 10080 } },        },      }).windows,    ).toEqual([      {        id: "secondary",        kind: "weekly",        label: "Weekly",        usedPercent: 42,        windowDurationMins: 10080,      },    ]);  });   it.each([undefined, null, {}])(    "supports legacy reads with no bucket map: %j",    (rateLimitsByLimitId) => {      const snapshot = { primary: { usedPercent: 12, windowDurationMins: 300 } };      expect(codexRateLimitsToLimits({ checkedAt, snapshot, rateLimitsByLimitId })).toEqual(        codexRateLimitsToLimits({ checkedAt, snapshot }),      );    },  );   it("does not show a model-specific legacy snapshot as the main allowance", () => {    expect(      codexRateLimitsToLimits({        checkedAt,        snapshot: {          limitId: "codex_bengalfox",          secondary: { usedPercent: 90 },        },      }).windows,    ).toEqual([]);  });}); describe("codexRateLimitsToUpdate", () => {  it("carries only the windows the notification names", () => {    expect(      codexRateLimitsToUpdate({        secondary: { usedPercent: 51, windowDurationMins: 10080 },      }),    ).toEqual({      windows: [        {          id: "secondary",          kind: "weekly",          label: "Weekly",          usedPercent: 51,          windowDurationMins: 10080,        },      ],    });    expect(codexRateLimitsToUpdate({ planType: "plus" })).toBeUndefined();  });   it("ignores Spark notifications so they cannot overwrite the main allowance", () => {    expect(      codexRateLimitsToUpdate({        limitId: "codex_bengalfox",        primary: { usedPercent: 0, windowDurationMins: 300 },        secondary: { usedPercent: 90, windowDurationMins: 10080 },      }),    ).toBeUndefined();    expect(      codexRateLimitsToUpdate({        limitId: "codex",        secondary: { usedPercent: 42, windowDurationMins: 10080 },      })?.windows,    ).toEqual([      {        id: "secondary",        kind: "weekly",        label: "Weekly",        usedPercent: 42,        windowDurationMins: 10080,      },    ]);  });}); describe("codexRateLimitsFailureMessage", () => {  it("keeps the JSON-RPC code and nothing else from a request failure", () => {    expect(      codexRateLimitsFailureMessage(        new CodexErrors.CodexAppServerRequestError({          code: -32603,          errorMessage:            "failed to fetch codex rate limits: GET https://chatgpt.com/backend-api/wham/usage failed: 401 Unauthorized",        }),      ),    ).toBe("Codex could not read usage (JSON-RPC -32603).");  });   it("phrases a dead process differently from a bad answer", () => {    expect(      codexRateLimitsFailureMessage(new CodexErrors.CodexAppServerProcessExitedError({ code: 1 })),    ).toBe("Codex exited before it could report usage.");  });}); describe("codexResetCreditsToContract", () => {  it("counts available credits and reports the soonest expiry", () => {    expect(      codexResetCreditsToContract({        availableCount: 2,        credits: [          { status: "available", expiresAt: 1_784_500_000 },          { status: "redeemed", expiresAt: 1_700_000_000 },          { status: "available", expiresAt: 1_784_000_000 },        ],      }),    ).toEqual({ availableCount: 2, nextExpiresAt: "2026-07-14T03:33:20.000Z" });    expect(codexResetCreditsToContract({ availableCount: 0 })).toEqual({ availableCount: 0 });    expect(codexResetCreditsToContract(null)).toBeUndefined();  });   it("rides along on the probe's limits", () => {    expect(      codexRateLimitsToLimits({        checkedAt,        snapshot: { primary: { usedPercent: 5, windowDurationMins: 300 } },        resetCredits: { availableCount: 1 },      }).resetCredits,    ).toEqual({ availableCount: 1 });  });});

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t