77import { describe , expect , it } from "@effect/vitest" ;
88import * as Deferred from "effect/Deferred" ;
99import * as Effect from "effect/Effect" ;
10+ import * as Fiber from "effect/Fiber" ;
1011import * as Layer from "effect/Layer" ;
1112import * as Option from "effect/Option" ;
1213import * as Random from "effect/Random" ;
@@ -32,6 +33,7 @@ import {
3233 ConnectionTransientError ,
3334 PrimaryConnectionTarget ,
3435 RelayConnectionTarget ,
36+ SshConnectionTarget ,
3537 type ConnectionAttemptError ,
3638 type ConnectionTarget ,
3739 type NetworkStatus ,
@@ -103,6 +105,16 @@ const LAN_THEN_RELAY_ENTRY: ConnectionCatalogEntry = {
103105 enabled : true ,
104106} ;
105107
108+ const SSH_ENTRY : ConnectionCatalogEntry = {
109+ target : new SshConnectionTarget ( {
110+ environmentId : TARGET . environmentId ,
111+ label : TARGET . label ,
112+ connectionId : "ssh-connection" ,
113+ } ) ,
114+ profile : Option . none ( ) ,
115+ enabled : true ,
116+ } ;
117+
106118const LAN_ENTRY : ConnectionCatalogEntry = {
107119 target : LAN_ROUTE . target ,
108120 profile : LAN_ROUTE . profile ,
@@ -194,6 +206,7 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
194206 target : ConnectionTarget ,
195207 reportProgress : ( progress : ConnectionDriver . ConnectionDriverProgress ) => Effect . Effect < void > ,
196208 ) {
209+ yield * reportProgress ( { stage : "preparing" } ) ;
197210 const prepared = yield * prepare ( target ) ;
198211 yield * reportProgress ( { stage : "opening" , prepared } ) ;
199212
@@ -556,6 +569,126 @@ describe("EnvironmentSupervisor", () => {
556569 } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
557570 ) ;
558571
572+ it . effect ( "reports a slow SSH preparation failure instead of the setup timeout" , ( ) =>
573+ Effect . gen ( function * ( ) {
574+ const preparationFailure = new ConnectionTransientError ( {
575+ reason : "remote-unavailable" ,
576+ detail :
577+ "Could not prepare the SSH environment: curl: (28) Operation timed out after 240003 milliseconds" ,
578+ } ) ;
579+ const harness = yield * makeHarness ( {
580+ prepare : ( ) =>
581+ Effect . sleep ( "4 minutes" ) . pipe ( Effect . andThen ( Effect . fail ( preparationFailure ) ) ) ,
582+ } ) ;
583+ const supervisor = yield * EnvironmentSupervisor . make ( SSH_ENTRY , {
584+ initiallyDesired : true ,
585+ } ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
586+
587+ yield * awaitState (
588+ supervisor . state ,
589+ ( state ) => state . phase === "connecting" && state . stage === "preparing" ,
590+ ) ;
591+ const firstFailure = yield * awaitState (
592+ supervisor . state ,
593+ ( state ) => state . phase === "backoff" ,
594+ ) . pipe ( Effect . forkChild ( { startImmediately : true } ) ) ;
595+ yield * TestClock . adjust ( "4 minutes" ) ;
596+
597+ expect ( yield * Fiber . join ( firstFailure ) ) . toMatchObject ( {
598+ attempt : 1 ,
599+ lastFailure : {
600+ _tag : "ConnectionTransientError" ,
601+ reason : "remote-unavailable" ,
602+ message : preparationFailure . message ,
603+ } ,
604+ } ) ;
605+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
606+ ) ;
607+
608+ it . effect ( "times out an SSH route that stops answering after a slow preparation" , ( ) =>
609+ Effect . gen ( function * ( ) {
610+ const harness = yield * makeHarness ( {
611+ prepare : ( ) => Effect . sleep ( "4 minutes" ) . pipe ( Effect . as ( PREPARED_CONNECTION ) ) ,
612+ ready : ( ) => Effect . never ,
613+ } ) ;
614+ const supervisor = yield * EnvironmentSupervisor . make ( SSH_ENTRY , {
615+ initiallyDesired : true ,
616+ } ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
617+
618+ yield * awaitState (
619+ supervisor . state ,
620+ ( state ) => state . phase === "connecting" && state . stage === "preparing" ,
621+ ) ;
622+ yield * TestClock . adjust ( "4 minutes" ) ;
623+ yield * awaitState (
624+ supervisor . state ,
625+ ( state ) => state . phase === "connecting" && state . stage === "synchronizing" ,
626+ ) ;
627+ const firstFailure = yield * awaitState (
628+ supervisor . state ,
629+ ( state ) => state . phase === "backoff" ,
630+ ) . pipe ( Effect . forkChild ( { startImmediately : true } ) ) ;
631+ yield * TestClock . adjust ( "90 seconds" ) ;
632+
633+ expect ( yield * Fiber . join ( firstFailure ) ) . toMatchObject ( {
634+ attempt : 1 ,
635+ lastFailure : {
636+ _tag : "ConnectionTransientError" ,
637+ reason : "timeout" ,
638+ message : "Test environment did not respond during connection setup." ,
639+ } ,
640+ } ) ;
641+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
642+ ) ;
643+
644+ it . effect ( "does not time a second SSH route's slow preparation" , ( ) =>
645+ Effect . gen ( function * ( ) {
646+ const secondSshTarget = new SshConnectionTarget ( {
647+ environmentId : TARGET . environmentId ,
648+ label : TARGET . label ,
649+ connectionId : "ssh-connection-2" ,
650+ } ) ;
651+ const firstRouteFailure = transient ( "The first SSH route dropped while synchronizing." ) ;
652+ const harness = yield * makeHarness ( {
653+ prepare : ( attempt , target ) =>
654+ attempt === 1
655+ ? Effect . succeed ( preparedFor ( target ) )
656+ : Effect . sleep ( "4 minutes" ) . pipe (
657+ Effect . andThen (
658+ Effect . fail (
659+ new ConnectionTransientError ( {
660+ reason : "remote-unavailable" ,
661+ detail : "Could not prepare the SSH environment." ,
662+ } ) ,
663+ ) ,
664+ ) ,
665+ ) ,
666+ ready : ( ) => Effect . fail ( firstRouteFailure ) ,
667+ } ) ;
668+ const supervisor = yield * EnvironmentSupervisor . make (
669+ { ...SSH_ENTRY , alternateRoutes : [ { target : secondSshTarget , profile : Option . none ( ) } ] } ,
670+ { initiallyDesired : true } ,
671+ ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
672+
673+ yield * awaitState (
674+ supervisor . state ,
675+ ( state ) => state . phase === "connecting" && state . stage === "preparing" ,
676+ ) ;
677+ const firstFailure = yield * awaitState (
678+ supervisor . state ,
679+ ( state ) => state . phase === "backoff" ,
680+ ) . pipe ( Effect . forkChild ( { startImmediately : true } ) ) ;
681+ yield * TestClock . adjust ( "4 minutes" ) ;
682+
683+ // The driver reports the first transient failure across its routes.
684+ expect ( yield * Fiber . join ( firstFailure ) ) . toMatchObject ( {
685+ attempt : 1 ,
686+ lastFailure : { reason : "transport" , message : firstRouteFailure . message } ,
687+ } ) ;
688+ expect ( yield * Ref . get ( harness . prepareCount ) ) . toBe ( 2 ) ;
689+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
690+ ) ;
691+
559692 it . effect ( "treats an involuntary session close as transient and reconnects" , ( ) =>
560693 Effect . gen ( function * ( ) {
561694 const harness = yield * makeHarness ( ) ;
0 commit comments