@@ -186,15 +186,20 @@ describe("credit balances", () => {
186186 ) . toBe ( "20" ) ;
187187 expect ( collectLimitAccounts ( makePresentations ( null ) ) [ 0 ] ?. limits . credits ) . toBeNull ( ) ;
188188 } ) ;
189- it . each ( [ "native" , "source" ] ) (
190- "honors a newer %s clear without windows and retains another environment's quota" ,
191- ( kind ) => {
189+ it . each ( [
190+ [ "native" , null ] ,
191+ [ "source" , null ] ,
192+ [ "native" , { hasCredits : true , unlimited : false , balance : "20" } ] ,
193+ [ "source" , { hasCredits : true , unlimited : false , balance : "20" } ] ,
194+ ] as const ) (
195+ "honors a newer %s credit-only snapshot and retains the freshest quota windows" ,
196+ ( kind , credits ) => {
192197 const oldLimits = {
193198 checkedAt : "2026-09-03T10:00:00.000Z" ,
194199 windows : [ window ] ,
195200 credits : { hasCredits : true , unlimited : false , balance : "42" } ,
196201 } ;
197- const cleared = { checkedAt : "2026-09-03T11:00:00.000Z" , windows : [ ] , credits : null } ;
202+ const cleared = { checkedAt : "2026-09-03T11:00:00.000Z" , windows : [ ] , credits } ;
198203 const auth = { status :
"authenticated" , email :
"[email protected] " } as const ; 199204 const newer = {
200205 entry : { target : { label : "Laptop" } } ,
@@ -228,21 +233,44 @@ describe("credit balances", () => {
228233 usageLimitSources : [ ] ,
229234 } ,
230235 } ;
236+ const middle = {
237+ entry : { target : { label : "Tablet" } } ,
238+ serverConfig : {
239+ providers : [
240+ provider ( {
241+ auth,
242+ usageLimits : {
243+ checkedAt : "2026-09-03T10:30:00.000Z" ,
244+ windows : [ { ...window , usedPercent : 60 } ] ,
245+ } ,
246+ } ) ,
247+ ] ,
248+ usageLimitSources : [ ] ,
249+ } ,
250+ } ;
231251 const entries = [
232252 [ EnvironmentId . make ( "desktop" ) , older ] ,
233253 [ EnvironmentId . make ( "laptop" ) , newer ] ,
254+ [ EnvironmentId . make ( "tablet" ) , middle ] ,
234255 ] as const ;
235256 for ( const order of [ entries , entries . toReversed ( ) ] ) {
236257 const accounts = collectLimitAccounts ( new Map ( order ) ) ;
237258 expect ( accounts ) . toHaveLength ( 1 ) ;
238- expect ( accounts [ 0 ] ?. limits . credits ) . toBeNull ( ) ;
239- expect ( accounts [ 0 ] ?. limits . windows ) . toEqual ( [ window ] ) ;
259+ expect ( accounts [ 0 ] ?. limits . credits ) . toEqual ( credits ) ;
260+ expect ( accounts [ 0 ] ?. limits . windows ) . toEqual ( [ { ...window , usedPercent : 60 } ] ) ;
261+ }
262+ const lone = collectLimitAccounts ( new Map ( [ [ EnvironmentId . make ( "laptop" ) , newer ] ] ) ) ;
263+ expect ( lone ) . toHaveLength ( credits === null ? 0 : 1 ) ;
264+ if ( credits !== null ) {
265+ expect ( lone [ 0 ] ?. limits . credits ) . toEqual ( credits ) ;
266+ expect ( lone [ 0 ] ?. limits . windows ) . toEqual ( [ ] ) ;
240267 }
241- expect ( collectLimitAccounts ( new Map ( [ [ EnvironmentId . make ( "laptop" ) , newer ] ] ) ) ) . toEqual ( [ ] ) ;
242268 older . serverConfig . providers = [
243269 provider ( { auth, usageLimits : { ...oldLimits , windows : [ ] } } ) ,
244270 ] ;
245- expect ( collectLimitAccounts ( new Map ( entries ) ) ) . toEqual ( [ ] ) ;
271+ const withoutWindows = collectLimitAccounts ( new Map ( entries . slice ( 0 , 2 ) ) ) ;
272+ expect ( withoutWindows ) . toHaveLength ( credits === null ? 0 : 1 ) ;
273+ expect ( withoutWindows [ 0 ] ?. limits . windows ?? [ ] ) . toEqual ( [ ] ) ;
246274 } ,
247275 ) ;
248276} ) ;
0 commit comments