@@ -13,6 +13,34 @@ import {
1313const checkedAt = "2026-07-18T10:00:00.000Z" ;
1414
1515describe ( "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+ ] ;
33+
34+ expect (
35+ codexRateLimitsToLimits ( {
36+ checkedAt,
37+ snapshot : { limitId : "spark" } ,
38+ rateLimitsByLimitId : { codex : snapshot } ,
39+ } ) ,
40+ ) . toEqual ( { checkedAt, windows } ) ;
41+ expect ( codexRateLimitsToUpdate ( snapshot ) ) . toEqual ( { windows } ) ;
42+ } ) ;
43+
1644 it ( "maps primary and secondary onto the session and weekly windows" , ( ) => {
1745 expect (
1846 codexRateLimitsToLimits ( {
@@ -66,6 +94,7 @@ describe("codexRateLimitsToLimits", () => {
6694 it ( "selects the main Codex allowance and leaves Spark out" , ( ) => {
6795 const spark = {
6896 limitId : "codex_bengalfox" ,
97+ individualLimit : { remainingPercent : 99 , resetsAt : 1_784_500_000 } ,
6998 primary : { usedPercent : 0 , windowDurationMins : 300 } ,
7099 secondary : { usedPercent : 90 , windowDurationMins : 10080 } ,
71100 } ;
@@ -105,6 +134,7 @@ describe("codexRateLimitsToLimits", () => {
105134 checkedAt,
106135 snapshot : {
107136 limitId : "codex_bengalfox" ,
137+ individualLimit : { remainingPercent : 99 , resetsAt : 1_784_500_000 } ,
108138 secondary : { usedPercent : 90 } ,
109139 } ,
110140 } ) . windows ,
@@ -136,6 +166,7 @@ describe("codexRateLimitsToUpdate", () => {
136166 expect (
137167 codexRateLimitsToUpdate ( {
138168 limitId : "codex_bengalfox" ,
169+ individualLimit : { remainingPercent : 99 , resetsAt : 1_784_500_000 } ,
139170 primary : { usedPercent : 0 , windowDurationMins : 300 } ,
140171 secondary : { usedPercent : 90 , windowDurationMins : 10080 } ,
141172 } ) ,
@@ -266,6 +297,41 @@ describe("codexUsageLimitMessage", () => {
266297} ) ;
267298
268299describe ( "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+
269335 it ( "keeps windows an update does not carry" , ( ) => {
270336 const merged = mergeCodexRateLimits (
271337 {
@@ -292,6 +358,7 @@ describe("mergeCodexRateLimits", () => {
292358 expect (
293359 mergeCodexRateLimits ( main , {
294360 limitId : "spark" ,
361+ individualLimit : { remainingPercent : 99 , resetsAt : 1_784_500_000 } ,
295362 primary : { usedPercent : 3 , resetsAt : 1_800_000_000 , windowDurationMins : 300 } ,
296363 } ) ,
297364 ) . toBe ( main ) ;
0 commit comments