@@ -7,7 +7,7 @@ import { describe, expect, it } from "@effect/vitest";
77import * as Effect from "effect/Effect" ;
88import * as FileSystem from "effect/FileSystem" ;
99import * as Schema from "effect/Schema" ;
10- import { HttpClient , HttpClientResponse } from "effect/http" ;
10+ import { HttpClient , HttpClientError , HttpClientResponse } from "effect/http" ;
1111import { GrokSettings } from "@t3tools/contracts" ;
1212
1313import {
@@ -758,44 +758,86 @@ it.layer(NodeServices.layer)("readGrokAccount", (it) => {
758758 } ) ,
759759 ) ;
760760
761- it . effect ( "classifies billing failures without exposing response bodies, keeping the account" , ( ) =>
762- Effect . gen ( function * ( ) {
763- for ( const [ response , message ] of [
764- [
765- new Response ( "private response" , { status : 401 } ) ,
766- "Grok sign-in was rejected. Reconnect Grok in provider settings." ,
767- ] ,
768- [ new Response ( "private response" , { status : 403 } ) , "Grok usage limits are unavailable for this account." ] ,
769- [
770- new Response ( "private response" , { status : 429 } ) ,
771- "Grok usage-limit checks are rate limited. Try again soon." ,
772- ] ,
773- [ new Response ( "private response" , { status : 503 } ) , "Grok billing is temporarily unavailable." ] ,
774- [
775- Response . json ( { config : { creditUsagePercent : "private-value" } } ) ,
776- "Grok returned an invalid usage-limits response." ,
777- ] ,
778- ] as const ) {
779- const { email, usageLimits : limits } = yield * readGrokAccount ( {
780- HOME : "/definitely/not/a/grok-home" ,
781- GROK_AUTH :
782- '{"https://accounts.x.ai/sign-in":{"key":"private-token","email":"[email protected] "}}' , 783- } ) . pipe (
784- Effect . provideService (
785- HttpClient . HttpClient ,
786- HttpClient . make ( ( request ) =>
787- Effect . succeed ( HttpClientResponse . fromWeb ( request , response ) ) ,
761+ it . effect (
762+ "classifies billing failures without exposing response bodies, keeping the account" ,
763+ ( ) =>
764+ Effect . gen ( function * ( ) {
765+ for ( const [ response , message ] of [
766+ [
767+ new Response ( "private response" , { status : 401 } ) ,
768+ "Grok sign-in was rejected. Reconnect Grok in provider settings." ,
769+ ] ,
770+ [
771+ new Response ( "private response" , { status : 403 } ) ,
772+ "Grok usage limits are unavailable for this account." ,
773+ ] ,
774+ [
775+ new Response ( "private response" , { status : 429 } ) ,
776+ "Grok usage-limit checks are rate limited. Try again soon." ,
777+ ] ,
778+ [
779+ new Response ( "private response" , { status : 503 } ) ,
780+ "Grok billing is temporarily unavailable." ,
781+ ] ,
782+ [ new Response ( "private response" , { status : 418 } ) , "Grok billing returned HTTP 418." ] ,
783+ [
784+ new Response ( "{" , { headers : { "content-type" : "application/json" } } ) ,
785+ "Grok returned an invalid usage-limits response." ,
786+ ] ,
787+ [
788+ Response . json ( { config : { creditUsagePercent : "private-value" } } ) ,
789+ "Grok returned an invalid usage-limits response." ,
790+ ] ,
791+ ] as const ) {
792+ const { email, usageLimits : limits } = yield * readGrokAccount ( {
793+ HOME : "/definitely/not/a/grok-home" ,
794+ GROK_AUTH :
795+ '{"https://accounts.x.ai/sign-in":{"key":"private-token","email":"[email protected] "}}' , 796+ } ) . pipe (
797+ Effect . provideService (
798+ HttpClient . HttpClient ,
799+ HttpClient . make ( ( request ) =>
800+ Effect . succeed ( HttpClientResponse . fromWeb ( request , response ) ) ,
801+ ) ,
788802 ) ,
803+ ) ;
804+ expect ( email ) . toBe ( "[email protected] " ) ; 805+ expect ( limits . windows ) . toEqual ( [ ] ) ;
806+ expect ( limits . unavailable ) . toEqual ( {
807+ reason : "probeFailed" ,
808+ message,
809+ } ) ;
810+ }
811+ } ) ,
812+ ) ;
813+
814+ it . effect ( "reports a transport failure without exposing its cause" , ( ) =>
815+ readGrokAccount ( {
816+ HOME : "/definitely/not/a/grok-home" ,
817+ GROK_AUTH : '{"https://accounts.x.ai/sign-in":{"key":"private-token"}}' ,
818+ } ) . pipe (
819+ Effect . provideService (
820+ HttpClient . HttpClient ,
821+ HttpClient . make ( ( request ) =>
822+ Effect . fail (
823+ new HttpClientError . HttpClientError ( {
824+ reason : new HttpClientError . TransportError ( {
825+ request,
826+ cause : new Error ( "private transport cause" ) ,
827+ } ) ,
828+ } ) ,
789829 ) ,
790- ) ;
791- expect ( email ) . toBe ( "[email protected] " ) ; 792- expect ( limits . windows ) . toEqual ( [ ] ) ;
793- expect ( limits . unavailable ) . toEqual ( {
794- reason : "probeFailed" ,
795- message,
796- } ) ;
797- }
798- } ) ,
830+ ) ,
831+ ) ,
832+ Effect . tap ( ( { usageLimits } ) =>
833+ Effect . sync ( ( ) =>
834+ expect ( usageLimits . unavailable ) . toEqual ( {
835+ reason : "probeFailed" ,
836+ message : "Grok could not connect to the billing service." ,
837+ } ) ,
838+ ) ,
839+ ) ,
840+ ) ,
799841 ) ;
800842} ) ;
801843
0 commit comments