Skip to content

Commit 2fea410

Browse files
test(codex): focus enterprise monthly limit coverage
1 parent 938bf0a commit 2fea410

1 file changed

Lines changed: 59 additions & 101 deletions

File tree

‎apps/server/src/provider/codexUsageLimits.test.ts‎

Lines changed: 59 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -12,116 +12,35 @@ import {
1212

1313
const checkedAt = "2026-07-18T10:00:00.000Z";
1414

15-
describe("Codex monthly credit limits", () => {
16-
const individualLimit = {
17-
remainingPercent: 99,
18-
resetsAt: 1_784_500_000,
19-
used: "1",
20-
limit: "100",
21-
};
22-
const snapshot = {
23-
limitId: "codex",
24-
planType: "enterprise_cbp_usage_based",
25-
primary: null,
26-
secondary: null,
27-
individualLimit,
28-
};
29-
const monthlyWindow = {
30-
id: "individual",
31-
kind: "monthly",
32-
label: "Monthly credit limit",
33-
usedPercent: 1,
34-
resetsAt: "2026-07-19T22:26:40.000Z",
35-
};
15+
describe("codexRateLimitsToLimits", () => {
16+
it("shows an Enterprise monthly credit allowance in reads and notifications without a duration", () => {
17+
const snapshot = {
18+
limitId: "codex",
19+
planType: "enterprise_cbp_usage_based",
20+
primary: null,
21+
secondary: null,
22+
individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
23+
};
24+
const windows = [
25+
{
26+
id: "individual",
27+
kind: "monthly",
28+
label: "Monthly credit limit",
29+
usedPercent: 1,
30+
resetsAt: "2026-07-19T22:26:40.000Z",
31+
},
32+
];
3633

37-
it("shows the Enterprise allowance from the main bucket without ordinary windows", () => {
3834
expect(
3935
codexRateLimitsToLimits({
4036
checkedAt,
4137
snapshot: { limitId: "spark" },
4238
rateLimitsByLimitId: { codex: snapshot },
4339
}),
44-
).toEqual({ checkedAt, windows: [monthlyWindow] });
45-
});
46-
47-
it("publishes an individual-limit-only notification", () => {
48-
expect(codexRateLimitsToUpdate(snapshot)).toEqual({ windows: [monthlyWindow] });
49-
});
50-
51-
it("keeps ordinary windows alongside the monthly credit limit", () => {
52-
expect(
53-
codexRateLimitsToLimits({
54-
checkedAt,
55-
snapshot: { ...snapshot, primary: { usedPercent: 12 } },
56-
}).windows,
57-
).toEqual([
58-
{
59-
id: "primary",
60-
kind: "session",
61-
label: "Session",
62-
usedPercent: 12,
63-
windowDurationMins: 300,
64-
},
65-
monthlyWindow,
66-
]);
67-
});
68-
69-
it.each([
70-
[120, 0],
71-
[-10, 100],
72-
])("clamps remaining percentage %s to used percentage %s", (remainingPercent, usedPercent) => {
73-
const limits = codexRateLimitsToLimits({
74-
checkedAt,
75-
snapshot: { ...snapshot, individualLimit: { ...individualLimit, remainingPercent } },
76-
});
77-
expect(limits.windows).toEqual([{ ...monthlyWindow, usedPercent }]);
78-
});
79-
80-
it.each([NaN, Infinity, -Infinity])(
81-
"ignores non-finite remaining percentage %s",
82-
(remainingPercent) => {
83-
expect(
84-
codexRateLimitsToLimits({
85-
checkedAt,
86-
snapshot: { ...snapshot, individualLimit: { ...individualLimit, remainingPercent } },
87-
}).windows,
88-
).toEqual([]);
89-
},
90-
);
91-
92-
it("omits invalid reset times without inventing a window duration", () => {
93-
expect(
94-
codexRateLimitsToLimits({
95-
checkedAt,
96-
snapshot: { ...snapshot, individualLimit: { ...individualLimit, resetsAt: 0 } },
97-
}).windows,
98-
).toEqual([
99-
{ id: "individual", kind: "monthly", label: "Monthly credit limit", usedPercent: 1 },
100-
]);
101-
});
102-
103-
it("updates, retains omitted values, and clears explicitly null credit limits", () => {
104-
const update = { individualLimit: { ...individualLimit, remainingPercent: 75 } };
105-
const changed = mergeCodexRateLimits(snapshot, { limitId: "codex", ...update });
106-
expect(changed).toEqual({ ...snapshot, ...update });
107-
expect(mergeCodexRateLimits(changed, { primary: { usedPercent: 12 } })).toEqual({
108-
...snapshot,
109-
...update,
110-
primary: { usedPercent: 12 },
111-
});
112-
const cleared = mergeCodexRateLimits(changed, { limitId: "codex", individualLimit: null });
113-
expect(cleared).toEqual({ ...snapshot, individualLimit: null });
114-
expect(codexRateLimitsToLimits({ checkedAt, snapshot: cleared! }).windows).toEqual([]);
40+
).toEqual({ checkedAt, windows });
41+
expect(codexRateLimitsToUpdate(snapshot)).toEqual({ windows });
11542
});
11643

117-
it("ignores monthly credit limits from model-specific buckets", () => {
118-
const spark = { ...snapshot, limitId: "spark" };
119-
expect(codexRateLimitsToUpdate(spark)).toBeUndefined();
120-
expect(mergeCodexRateLimits(snapshot, spark)).toBe(snapshot);
121-
});
122-
});
123-
124-
describe("codexRateLimitsToLimits", () => {
12544
it("maps primary and secondary onto the session and weekly windows", () => {
12645
expect(
12746
codexRateLimitsToLimits({
@@ -175,6 +94,7 @@ describe("codexRateLimitsToLimits", () => {
17594
it("selects the main Codex allowance and leaves Spark out", () => {
17695
const spark = {
17796
limitId: "codex_bengalfox",
97+
individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
17898
primary: { usedPercent: 0, windowDurationMins: 300 },
17999
secondary: { usedPercent: 90, windowDurationMins: 10080 },
180100
};
@@ -214,6 +134,7 @@ describe("codexRateLimitsToLimits", () => {
214134
checkedAt,
215135
snapshot: {
216136
limitId: "codex_bengalfox",
137+
individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
217138
secondary: { usedPercent: 90 },
218139
},
219140
}).windows,
@@ -245,6 +166,7 @@ describe("codexRateLimitsToUpdate", () => {
245166
expect(
246167
codexRateLimitsToUpdate({
247168
limitId: "codex_bengalfox",
169+
individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
248170
primary: { usedPercent: 0, windowDurationMins: 300 },
249171
secondary: { usedPercent: 90, windowDurationMins: 10080 },
250172
}),
@@ -375,6 +297,41 @@ describe("codexUsageLimitMessage", () => {
375297
});
376298

377299
describe("mergeCodexRateLimits", () => {
300+
it("updates monthly credit usage, retains it when omitted, and clears an explicit null", () => {
301+
const snapshot = {
302+
limitId: "codex",
303+
primary: { usedPercent: 12 },
304+
individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
305+
};
306+
const individualLimit = { remainingPercent: 75, resetsAt: 1_787_000_000 };
307+
const changed = mergeCodexRateLimits(snapshot, { individualLimit });
308+
expect(changed).toEqual({ ...snapshot, individualLimit });
309+
expect(codexRateLimitsToUpdate(changed!)).toEqual({
310+
windows: [
311+
{
312+
id: "primary",
313+
kind: "session",
314+
label: "Session",
315+
usedPercent: 12,
316+
windowDurationMins: 300,
317+
},
318+
{
319+
id: "individual",
320+
kind: "monthly",
321+
label: "Monthly credit limit",
322+
usedPercent: 25,
323+
resetsAt: "2026-08-17T20:53:20.000Z",
324+
},
325+
],
326+
});
327+
328+
const retained = mergeCodexRateLimits(changed, { primary: null });
329+
expect(retained).toEqual({ ...snapshot, primary: null, individualLimit });
330+
const cleared = mergeCodexRateLimits(retained, { individualLimit: null });
331+
expect(cleared).toEqual({ ...snapshot, primary: null, individualLimit: null });
332+
expect(codexRateLimitsToLimits({ checkedAt, snapshot: cleared! }).windows).toEqual([]);
333+
});
334+
378335
it("keeps windows an update does not carry", () => {
379336
const merged = mergeCodexRateLimits(
380337
{
@@ -401,6 +358,7 @@ describe("mergeCodexRateLimits", () => {
401358
expect(
402359
mergeCodexRateLimits(main, {
403360
limitId: "spark",
361+
individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
404362
primary: { usedPercent: 3, resetsAt: 1_800_000_000, windowDurationMins: 300 },
405363
}),
406364
).toBe(main);

0 commit comments

Comments
 (0)