@@ -478,8 +478,8 @@ describe("Grok usage limits", () => {
478478 } ;
479479
480480 it ( "shows zero usage for complete weekly and monthly periods with no percentage" , ( ) => {
481- for ( const [ currentPeriod , kind , label , resetsAt ] of [
482- [ weeklyPeriod , "weekly" , "Weekly" , "2026-09-18T03:10:30.159Z" ] ,
481+ for ( const [ currentPeriod , kind , label , resetsAt , windowDurationMins ] of [
482+ [ weeklyPeriod , "weekly" , "Weekly" , "2026-09-18T03:10:30.159Z" , 7 * 24 * 60 ] ,
483483 [
484484 {
485485 type : "USAGE_PERIOD_TYPE_MONTHLY" ,
@@ -489,12 +489,15 @@ describe("Grok usage limits", () => {
489489 "monthly" ,
490490 "Monthly" ,
491491 "2026-10-01T00:00:00.000Z" ,
492+ 30 * 24 * 60 ,
492493 ] ,
493494 ] as const ) {
494495 const response = { config : { currentPeriod } } ;
495496 expect
496497 . soft ( grokUsageResponseToLimits ( response , checkedAt ) . windows )
497- . toEqual ( [ { id : "subscription" , kind, label, usedPercent : 0 , resetsAt } ] ) ;
498+ . toEqual ( [
499+ { id : "subscription" , kind, label, usedPercent : 0 , resetsAt, windowDurationMins } ,
500+ ] ) ;
498501 }
499502 } ) ;
500503
@@ -551,6 +554,60 @@ describe("Grok usage limits", () => {
551554 ) . windows ,
552555 ) . toEqual ( [ { id : "subscription" , kind : "monthly" , label : "Monthly" , usedPercent : 100 } ] ) ;
553556 } ) ;
557+
558+ it ( "measures the window from the reported period bounds" , ( ) => {
559+ const window = ( type : string , start : string , end : string ) =>
560+ grokUsageResponseToLimits (
561+ { config : { creditUsagePercent : 4 , currentPeriod : { type, start, end } } } ,
562+ checkedAt ,
563+ ) . windows [ 0 ] ;
564+ expect (
565+ window (
566+ "USAGE_PERIOD_TYPE_WEEKLY" ,
567+ "2026-10-14T20:51:37.352+00:00" ,
568+ "2026-10-21T20:51:37.352+00:00" ,
569+ ) ,
570+ ) . toEqual ( {
571+ id : "subscription" ,
572+ kind : "weekly" ,
573+ label : "Weekly" ,
574+ usedPercent : 4 ,
575+ resetsAt : "2026-10-21T20:51:37.352Z" ,
576+ windowDurationMins : 7 * 24 * 60 ,
577+ } ) ;
578+ expect (
579+ window (
580+ "USAGE_PERIOD_TYPE_MONTHLY" ,
581+ "2026-02-01T00:00:00.000+00:00" ,
582+ "2026-03-01T00:00:00.000+00:00" ,
583+ ) ?. windowDurationMins ,
584+ ) . toBe ( 28 * 24 * 60 ) ;
585+ } ) ;
586+
587+ it ( "leaves the window length unknown when the period start is unusable" , ( ) => {
588+ const end = "2026-10-21T20:51:37.352+00:00" ;
589+ for ( const start of [
590+ undefined ,
591+ null ,
592+ 42 ,
593+ "invalid" ,
594+ "2026-02-30T00:00:00.000Z" ,
595+ end ,
596+ "2026-10-28T20:51:37.352+00:00" ,
597+ ] ) {
598+ const [ window ] = grokUsageResponseToLimits (
599+ {
600+ config : {
601+ creditUsagePercent : 4 ,
602+ currentPeriod : { type : "USAGE_PERIOD_TYPE_WEEKLY" , start, end } ,
603+ } ,
604+ } ,
605+ checkedAt ,
606+ ) . windows ;
607+ expect ( window ?. resetsAt ) . toBe ( "2026-10-21T20:51:37.352Z" ) ;
608+ expect ( window ) . not . toHaveProperty ( "windowDurationMins" ) ;
609+ }
610+ } ) ;
554611} ) ;
555612
556613it . layer ( Layer . mergeAll ( NodeServices . layer , NodeCrypto . layer ) ) ( "readGrokAccount" , ( it ) => {
0 commit comments