11import * as NodeServices from "@effect/platform-node/NodeServices" ;
22import { assert , describe , it } from "@effect/vitest" ;
3+ import type * as Duration from "effect/Duration" ;
34import * as Effect from "effect/Effect" ;
5+ import * as Fiber from "effect/Fiber" ;
46import * as Schema from "effect/Schema" ;
57import * as Sink from "effect/Sink" ;
68import * as Stream from "effect/Stream" ;
79import * as ChildProcess from "effect/process/ChildProcess" ;
810import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
11+ import * as TestClock from "effect/testing/TestClock" ;
912
1013import { FxSettings } from "../settings.ts" ;
1114import { checkFxProviderStatus } from "./status.ts" ;
1215
1316const encoder = new TextEncoder ( ) ;
1417const settings = Schema . decodeSync ( FxSettings ) ( { enabled : true } ) ;
1518
16- function processHandle ( stdout : string , code : number ) {
19+ /** Holds stdout and the exit back until `answered` completes, like a CLI slow to answer. */
20+ function processHandle ( stdout : string , code : number , answered : Effect . Effect < void > = Effect . void ) {
1721 return ChildProcessSpawner . makeHandle ( {
1822 pid : ChildProcessSpawner . ProcessId ( 900_000_101 ) ,
19- exitCode : Effect . succeed ( ChildProcessSpawner . ExitCode ( code ) ) ,
23+ exitCode : Effect . as ( answered , ChildProcessSpawner . ExitCode ( code ) ) ,
2024 isRunning : Effect . succeed ( false ) ,
2125 kill : ( ) => Effect . void ,
2226 unref : Effect . succeed ( Effect . void ) ,
2327 stdin : Sink . drain ,
24- stdout : stdout . length === 0 ? Stream . empty : Stream . succeed ( encoder . encode ( stdout ) ) ,
28+ stdout : Stream . unwrap (
29+ Effect . as (
30+ answered ,
31+ stdout . length === 0 ? Stream . empty : Stream . succeed ( encoder . encode ( stdout ) ) ,
32+ ) ,
33+ ) ,
2534 stderr : Stream . empty ,
2635 all : Stream . empty ,
2736 getInputFd : ( ) => Sink . drain ,
@@ -30,7 +39,7 @@ function processHandle(stdout: string, code: number) {
3039}
3140
3241/** Answers `fx` invocations by their arguments and records which ran. */
33- const check = ( statusJson : string ) =>
42+ const check = ( statusJson : string , versionDelay : Duration . Input = 0 ) =>
3443 Effect . gen ( function * ( ) {
3544 const outputs : Record < string , string > = {
3645 "--version" : "fx 0.0.13\n" ,
@@ -50,7 +59,13 @@ const check = (statusJson: string) =>
5059 const args = ChildProcess . isStandardCommand ( command ) ? command . args . join ( " " ) : "" ;
5160 invoked . push ( args ) ;
5261 const stdout = outputs [ args ] ;
53- return Effect . succeed ( processHandle ( stdout ?? "" , stdout === undefined ? 2 : 0 ) ) ;
62+ return Effect . succeed (
63+ processHandle (
64+ stdout ?? "" ,
65+ stdout === undefined ? 2 : 0 ,
66+ args === "--version" ? Effect . sleep ( versionDelay ) : Effect . void ,
67+ ) ,
68+ ) ;
5469 } ) ;
5570 const draft = yield * checkFxProviderStatus ( settings , { } ) . pipe (
5671 Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
@@ -59,6 +74,38 @@ const check = (statusJson: string) =>
5974 } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ;
6075
6176describe ( "checkFxProviderStatus" , ( ) => {
77+ it . effect . each ( [
78+ {
79+ label : "answers after 6 s" ,
80+ versionDelay : "6 seconds" ,
81+ status : "ready" ,
82+ version : "0.0.13" ,
83+ message : undefined ,
84+ invoked : [ "--version" , "status --json" , "models --json" ] ,
85+ } ,
86+ {
87+ label : "never answers" ,
88+ versionDelay : "Infinity" ,
89+ status : "error" ,
90+ version : null ,
91+ message : "Failed to run `fx --version`." ,
92+ invoked : [ "--version" ] ,
93+ } ,
94+ ] as const ) ( "gives `fx --version` 10 s when it $label" , ( row ) =>
95+ Effect . gen ( function * ( ) {
96+ const fiber = yield * check (
97+ JSON . stringify ( { kind : "status" , model : "anthropic/claude-opus-5.5" , auth : "fx login" } ) ,
98+ row . versionDelay ,
99+ ) . pipe ( Effect . forkChild ) ;
100+ yield * TestClock . adjust ( "10 seconds" ) ;
101+ const { draft, invoked } = yield * Fiber . join ( fiber ) ;
102+ assert . equal ( draft . status , row . status ) ;
103+ assert . equal ( draft . version , row . version ) ;
104+ assert . equal ( draft . message , row . message ) ;
105+ assert . deepEqual ( invoked , [ ...row . invoked ] ) ;
106+ } ) ,
107+ ) ;
108+
62109 // `fx status --json` from fx 0.0.13, trimmed to the fields T3 reads plus a few it ignores.
63110 it . effect ( "reports sign-in from `fx status` without opening an ACP session" , ( ) =>
64111 Effect . gen ( function * ( ) {
0 commit comments