@@ -580,6 +580,63 @@ it.layer(NodeServices.layer)("checkGrokProviderStatus", (it) => {
580580
581581describe ( "Grok usage limits" , ( ) => {
582582 const checkedAt = "2026-09-16T00:00:00.000Z" ;
583+ const weeklyPeriod = {
584+ type : "USAGE_PERIOD_TYPE_WEEKLY" ,
585+ start : "2026-09-11T03:10:30.159171+00:00" ,
586+ end : "2026-09-18T03:10:30.159171+00:00" ,
587+ } ;
588+
589+ it ( "shows zero usage for complete weekly and monthly periods with no percentage" , ( ) => {
590+ for ( const [ currentPeriod , kind , label , resetsAt ] of [
591+ [ weeklyPeriod , "weekly" , "Weekly" , "2026-09-18T03:10:30.159Z" ] ,
592+ [
593+ {
594+ type : "USAGE_PERIOD_TYPE_MONTHLY" ,
595+ start : "2026-09-01T00:00:00.000Z" ,
596+ end : "2026-10-01T00:00:00.000Z" ,
597+ } ,
598+ "monthly" ,
599+ "Monthly" ,
600+ "2026-10-01T00:00:00.000Z" ,
601+ ] ,
602+ ] as const ) {
603+ const response = { config : { currentPeriod } } ;
604+ expect
605+ . soft ( grokUsageResponseToLimits ( response , checkedAt ) . windows )
606+ . toEqual ( [ { id : "subscription" , kind, label, usedPercent : 0 , resetsAt } ] ) ;
607+ }
608+ } ) ;
609+
610+ it ( "keeps incomplete, invalid, unordered, and unknown periods unreported" , ( ) => {
611+ for ( const currentPeriod of [
612+ undefined ,
613+ { ...weeklyPeriod , type : undefined } ,
614+ { ...weeklyPeriod , type : "USAGE_PERIOD_TYPE_DAILY" } ,
615+ { ...weeklyPeriod , start : undefined } ,
616+ { ...weeklyPeriod , end : undefined } ,
617+ { ...weeklyPeriod , start : "invalid" } ,
618+ { ...weeklyPeriod , end : "invalid" } ,
619+ { ...weeklyPeriod , start : weeklyPeriod . end } ,
620+ { ...weeklyPeriod , start : weeklyPeriod . end , end : weeklyPeriod . start } ,
621+ ] ) {
622+ const response = { config : { currentPeriod } } ;
623+ expect
624+ . soft ( grokUsageResponseToLimits ( response , checkedAt ) . windows , JSON . stringify ( response ) )
625+ . toEqual ( [ ] ) ;
626+ }
627+ } ) ;
628+
629+ it ( "rejects impossible calendar dates at either period boundary" , ( ) => {
630+ for ( const [ start , end ] of [
631+ [ "2026-02-30T00:00:00.000Z" , "2026-03-09T00:00:00.000Z" ] ,
632+ [ "2026-02-23T00:00:00.000Z" , "2026-02-30T00:00:00.000Z" ] ,
633+ ] ) {
634+ const response = { config : { currentPeriod : { ...weeklyPeriod , start, end } } } ;
635+ expect
636+ . soft ( grokUsageResponseToLimits ( response , checkedAt ) . windows , JSON . stringify ( response ) )
637+ . toEqual ( [ ] ) ;
638+ }
639+ } ) ;
583640
584641 it ( "uses the reported subscription percentage and weekly reset" , ( ) => {
585642 const limits = grokUsageResponseToLimits (
@@ -693,6 +750,50 @@ it.layer(NodeServices.layer)("readGrokAccount", (it) => {
693750 ) ,
694751 ) ;
695752
753+ it . effect ( "ignores malformed period starts without inventing usage" , ( ) =>
754+ Effect . gen ( function * ( ) {
755+ for ( const start of [ null , 42 ] ) {
756+ for ( const creditUsagePercent of [ 37.5 , undefined ] ) {
757+ const response = {
758+ config : {
759+ creditUsagePercent,
760+ currentPeriod : {
761+ type : "USAGE_PERIOD_TYPE_WEEKLY" ,
762+ start,
763+ end : "2026-09-18T03:10:30.159171+00:00" ,
764+ } ,
765+ } ,
766+ } ;
767+ const { usageLimits } = yield * readGrokAccount ( {
768+ HOME : "/definitely/not/a/grok-home" ,
769+ GROK_AUTH : '{"https://accounts.x.ai/sign-in":{"key":"session-token"}}' ,
770+ } ) . pipe (
771+ Effect . provideService (
772+ HttpClient . HttpClient ,
773+ HttpClient . make ( ( request ) =>
774+ Effect . succeed ( HttpClientResponse . fromWeb ( request , Response . json ( response ) ) ) ,
775+ ) ,
776+ ) ,
777+ ) ;
778+ expect . soft ( usageLimits . unavailable , JSON . stringify ( response ) ) . toBeUndefined ( ) ;
779+ expect . soft ( usageLimits . windows , JSON . stringify ( response ) ) . toEqual (
780+ creditUsagePercent === undefined
781+ ? [ ]
782+ : [
783+ {
784+ id : "subscription" ,
785+ kind : "weekly" ,
786+ label : "Weekly" ,
787+ usedPercent : creditUsagePercent ,
788+ resetsAt : "2026-09-18T03:10:30.159Z" ,
789+ } ,
790+ ] ,
791+ ) ;
792+ }
793+ }
794+ } ) ,
795+ ) ;
796+
696797 it . effect ( "does not use unrelated scopes, API keys, or custom auth deployments" , ( ) =>
697798 Effect . gen ( function * ( ) {
698799 for ( const environment of [
0 commit comments