@@ -11,6 +11,7 @@ import * as Stream from "effect/Stream";
1111import * as HttpClient from "effect/http/HttpClient" ;
1212import * as HttpClientResponse from "effect/http/HttpClientResponse" ;
1313import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
14+ import * as TestClock from "effect/testing/TestClock" ;
1415import * as ServerConfig from "../config.ts" ;
1516import * as DeviceHost from "./DeviceHost.ts" ;
1617import * as SshDeviceHost from "./SshDeviceHost.ts" ;
@@ -309,3 +310,76 @@ it.effect("lists Android devices without probing the host again once it reports
309310 yield * host . stop ;
310311 } ) . pipe ( Effect . scoped , Effect . provide ( Layer . mergeAll ( NodeServices . layer , NodeCrypto . layer ) ) ) ,
311312) ;
313+
314+ it . effect (
315+ "keeps the SSH forward through slow health probes and reconnects once the hub stays down" ,
316+ ( ) =>
317+ Effect . gen ( function * ( ) {
318+ const fs = yield * FileSystem . FileSystem ;
319+ const home = yield * fs . makeTempDirectoryScoped ( ) ;
320+ let forwardSpawns = 0 ;
321+ let hubAnswers = true ;
322+ const spawner = ChildProcessSpawner . make ( ( command ) =>
323+ Effect . gen ( function * ( ) {
324+ if ( command . _tag !== "StandardCommand" ) return yield * Effect . die ( "Unexpected command" ) ;
325+ const forwarding = command . args . includes ( "-L" ) ;
326+ // A fresh forward reaches a working hub again.
327+ if ( forwarding && ++ forwardSpawns > 1 ) hubAnswers = true ;
328+ const output = JSON . stringify ( {
329+ nodePath : "/node" ,
330+ platforms : [ { platform : "ios" , available : true } ] ,
331+ hubPort : 1234 ,
332+ helpers : { serveSimAxSettings : null , serveSimCli : null } ,
333+ } ) ;
334+ return ChildProcessSpawner . makeHandle ( {
335+ pid : ChildProcessSpawner . ProcessId ( 123 ) ,
336+ stdout : Stream . make ( new TextEncoder ( ) . encode ( output ) ) ,
337+ stderr : Stream . empty ,
338+ all : Stream . empty ,
339+ exitCode : forwarding ? Effect . never : Effect . succeed ( ChildProcessSpawner . ExitCode ( 0 ) ) ,
340+ isRunning : Effect . succeed ( forwarding ) ,
341+ kill : ( ) => Effect . void ,
342+ stdin : Sink . drain ,
343+ getInputFd : ( ) => Sink . drain ,
344+ getOutputFd : ( ) => Stream . empty ,
345+ unref : Effect . succeed ( Effect . void ) ,
346+ } ) ;
347+ } ) ,
348+ ) ;
349+ const host = yield * SshDeviceHost . make ( {
350+ kind : "ssh" ,
351+ id : "test" ,
352+ label : "Test" ,
353+ target : "test.example" ,
354+ } ) . pipe (
355+ Effect . provide ( Layer . mergeAll ( ServerConfig . layerTest ( home , home ) , Net . layer ) ) ,
356+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
357+ Effect . provideService (
358+ HttpClient . HttpClient ,
359+ HttpClient . make ( ( request ) =>
360+ hubAnswers
361+ ? Effect . succeed ( HttpClientResponse . fromWeb ( request , new Response ( "ok" ) ) )
362+ : Effect . never ,
363+ ) ,
364+ ) ,
365+ ) ;
366+ yield * host . ensureReady ( ( ) => Effect . void ) ;
367+ expect ( forwardSpawns ) . toBe ( 1 ) ;
368+
369+ // A loaded host can miss a probe or two without being gone; the forward stays up.
370+ hubAnswers = false ;
371+ yield * TestClock . adjust ( "30 seconds" ) ;
372+ hubAnswers = true ;
373+ yield * TestClock . adjust ( "2 minutes" ) ;
374+ expect ( forwardSpawns ) . toBe ( 1 ) ;
375+
376+ // A hub that keeps missing probes is still replaced.
377+ hubAnswers = false ;
378+ yield * TestClock . adjust ( "1 minute" ) ;
379+ expect ( forwardSpawns ) . toBe ( 2 ) ;
380+ yield * host . stop ;
381+ } ) . pipe (
382+ Effect . scoped ,
383+ Effect . provide ( Layer . mergeAll ( NodeServices . layer , NodeCrypto . layer , TestClock . layer ( ) ) ) ,
384+ ) ,
385+ ) ;
0 commit comments