@@ -17,6 +17,7 @@ import * as FileSystem from "effect/FileSystem";
1717import * as Layer from "effect/Layer" ;
1818import * as Path from "effect/Path" ;
1919import * as PubSub from "effect/PubSub" ;
20+ import * as Queue from "effect/Queue" ;
2021import * as Ref from "effect/Ref" ;
2122import * as Schema from "effect/Schema" ;
2223import * as Scope from "effect/Scope" ;
@@ -788,6 +789,86 @@ it.layer(
788789 } ) ,
789790 ) ;
790791
792+ it . effect ( "shows Codex usage that answers after three seconds inside the probe budget" , ( ) =>
793+ Effect . gen ( function * ( ) {
794+ const usageRequested = yield * Deferred . make < void > ( ) ;
795+ const spawner = ChildProcessSpawner . make ( ( ) =>
796+ Effect . gen ( function * ( ) {
797+ const scope = yield * Effect . scope ;
798+ const output = yield * Queue . unbounded < Uint8Array > ( ) ;
799+ const send = ( message : unknown ) =>
800+ Queue . offer ( output , encoder . encode ( `${ JSON . stringify ( message ) } \n` ) ) ;
801+ const results : Record < string , unknown > = {
802+ initialize : {
803+ userAgent : "codex_cli_rs/0.167.0" ,
804+ codexHome : "/nonexistent-codex-home" ,
805+ platformFamily : "unix" ,
806+ platformOs : "linux" ,
807+ } ,
808+ "account/read" : {
809+ account :
{ type :
"chatgpt" , email :
"[email protected] " , planType :
"pro" } , 810+ requiresOpenaiAuth : true ,
811+ } ,
812+ "config/read" : { config : { } , origins : { } } ,
813+ "skills/list" : { data : [ ] } ,
814+ "model/list" : { data : [ ] } ,
815+ } ;
816+ const answer = ( line : string ) => {
817+ const { id, method } = JSON . parse ( line ) as { id ?: number ; method ?: string } ;
818+ if ( id === undefined || method === undefined ) return Effect . void ;
819+ if ( method !== "account/rateLimits/read" ) {
820+ return send ( { id, result : results [ method ] ?? { } } ) ;
821+ }
822+ return Deferred . succeed ( usageRequested , undefined ) . pipe (
823+ Effect . andThen ( Effect . sleep ( "4 seconds" ) ) ,
824+ Effect . andThen (
825+ send ( {
826+ id,
827+ result : {
828+ rateLimits : {
829+ limitId : "codex" ,
830+ planType : "pro" ,
831+ primary : { usedPercent : 42 , windowDurationMins : 300 , resetsAt : 2e9 } ,
832+ } ,
833+ rateLimitsByLimitId : null ,
834+ rateLimitResetCredits : null ,
835+ accountId : null ,
836+ } ,
837+ } ) ,
838+ ) ,
839+ Effect . forkIn ( scope ) ,
840+ ) ;
841+ } ;
842+ const decoder = new TextDecoder ( ) ;
843+ let buffered = "" ;
844+ return ChildProcessSpawner . makeHandle ( {
845+ ...mockHandle ( { stdout : "" , stderr : "" , code : 0 } ) ,
846+ exitCode : Effect . never ,
847+ isRunning : Effect . succeed ( true ) ,
848+ stdin : Sink . forEach ( ( chunk : Uint8Array ) => {
849+ buffered += decoder . decode ( chunk , { stream : true } ) ;
850+ const lines = buffered . split ( "\n" ) ;
851+ buffered = lines . pop ( ) ?? "" ;
852+ return Effect . forEach ( lines . filter ( Boolean ) , answer , { discard : true } ) ;
853+ } ) ,
854+ stdout : Stream . fromQueue ( output ) ,
855+ } ) ;
856+ } ) ,
857+ ) ;
858+ const fiber = yield * checkCodexProviderStatus ( defaultCodexSettings ) . pipe (
859+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
860+ Effect . forkChild ,
861+ ) ;
862+ yield * Deferred . await ( usageRequested ) ;
863+ yield * TestClock . adjust ( "25 seconds" ) ;
864+ const status = yield * Fiber . join ( fiber ) ;
865+
866+ assert . strictEqual ( status . auth . status , "authenticated" ) ;
867+ assert . isUndefined ( status . usageLimits ?. unavailable ) ;
868+ assert . strictEqual ( status . usageLimits ?. windows [ 0 ] ?. usedPercent , 42 ) ;
869+ } ) ,
870+ ) ;
871+
791872 it . effect ( "does not run --version when the Codex executable cannot start" , ( ) =>
792873 Effect . gen ( function * ( ) {
793874 if ( ( yield * HostProcess . Platform ) === "win32" ) return ;
0 commit comments