11import { expect , it } from "@effect/vitest" ;
22import * as NodeServices from "@effect/platform-node/NodeServices" ;
33import * as Net from "@t3tools/shared/Net" ;
4+ import { HostProcessPlatform } from "@t3tools/shared/hostProcess" ;
45import * as Effect from "effect/Effect" ;
56import * as FileSystem from "effect/FileSystem" ;
67import * as Layer from "effect/Layer" ;
@@ -9,6 +10,7 @@ import * as Sink from "effect/Sink";
910import * as Stream from "effect/Stream" ;
1011import * as HttpClient from "effect/http/HttpClient" ;
1112import * as HttpClientResponse from "effect/http/HttpClientResponse" ;
13+ import * as ChildProcess from "effect/process/ChildProcess" ;
1214import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
1315import * as ServerConfig from "../config.ts" ;
1416import * as DeviceHost from "./DeviceHost.ts" ;
@@ -26,7 +28,7 @@ it.effect("preserves installed status after probes and cleans failed agent activ
2628 const spawner = ChildProcessSpawner . make ( ( command ) =>
2729 Effect . gen ( function * ( ) {
2830 if ( command . _tag !== "StandardCommand" ) return yield * Effect . die ( "Unexpected command" ) ;
29- const forwarding = command . args . includes ( "-N " ) ;
31+ const forwarding = command . args . includes ( "-L " ) ;
3032 let output = "" ;
3133 if ( forwarding ) {
3234 if ( failForward ) {
@@ -136,3 +138,69 @@ it.effect("preserves installed status after probes and cleans failed agent activ
136138 expect ( forwards ) . toBe ( 0 ) ;
137139 } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
138140) ;
141+
142+ it . live . skipIf ( HostProcessPlatform . defaultValue ( ) === "win32" ) (
143+ "ends the tunnel when the server's end of its stdin pipe closes" ,
144+ ( ) =>
145+ Effect . gen ( function * ( ) {
146+ const fs = yield * FileSystem . FileSystem ;
147+ const home = yield * fs . makeTempDirectoryScoped ( ) ;
148+ const real = yield * ChildProcessSpawner . ChildProcessSpawner ;
149+ let tunnel : ChildProcessSpawner . ChildProcessHandle | undefined ;
150+ let tunnelStdin : unknown ;
151+ const target = "test.example" ;
152+ const spawner = ChildProcessSpawner . make ( ( command ) =>
153+ Effect . gen ( function * ( ) {
154+ if ( command . _tag !== "StandardCommand" ) return yield * Effect . die ( "Unexpected command" ) ;
155+ if ( command . args . includes ( "-L" ) ) {
156+ // Run the tunnel's remote command locally, as the remote shell would.
157+ const remote = command . args . slice ( command . args . indexOf ( target ) + 1 ) . join ( " " ) ;
158+ tunnelStdin = command . options . stdin ;
159+ tunnel = yield * real . spawn (
160+ ChildProcess . make ( "sh" , [ "-c" , remote ] , { ...command . options , stdin : "pipe" } ) ,
161+ ) ;
162+ return tunnel ;
163+ }
164+ const output = JSON . stringify ( {
165+ nodePath : "/node" ,
166+ platforms : [ ] ,
167+ hubPort : 1234 ,
168+ helpers : { serveSimAxSettings : null , serveSimCli : null } ,
169+ } ) ;
170+ return ChildProcessSpawner . makeHandle ( {
171+ pid : ChildProcessSpawner . ProcessId ( 123 ) ,
172+ stdout : Stream . make ( new TextEncoder ( ) . encode ( output ) ) ,
173+ stderr : Stream . empty ,
174+ all : Stream . empty ,
175+ exitCode : Effect . succeed ( ChildProcessSpawner . ExitCode ( 0 ) ) ,
176+ isRunning : Effect . succeed ( false ) ,
177+ kill : ( ) => Effect . void ,
178+ stdin : Sink . drain ,
179+ getInputFd : ( ) => Sink . drain ,
180+ getOutputFd : ( ) => Stream . empty ,
181+ unref : Effect . succeed ( Effect . void ) ,
182+ } ) ;
183+ } ) ,
184+ ) ;
185+ const host = yield * SshDeviceHost . make ( { id : "test" , label : "Test" , target } ) . pipe (
186+ Effect . provide ( Layer . mergeAll ( ServerConfig . layerTest ( home , home ) , Net . layer ) ) ,
187+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
188+ Effect . provideService (
189+ HttpClient . HttpClient ,
190+ HttpClient . make ( ( request ) =>
191+ Effect . succeed ( HttpClientResponse . fromWeb ( request , new Response ( "ok" ) ) ) ,
192+ ) ,
193+ ) ,
194+ ) ;
195+ yield * host . ensureReady ( ( ) => Effect . void ) ;
196+ const handle = tunnel ;
197+ if ( ! handle ) return yield * Effect . die ( "Tunnel was not spawned" ) ;
198+ expect ( tunnelStdin ) . toBe ( "pipe" ) ;
199+ // A pipe buffers at most 64 KiB, so this write only completes while the remote command
200+ // keeps reading stdin; one that has already exited fails it with EPIPE. The sink then
201+ // ends stdin, which is what the server's death does.
202+ yield * Stream . make ( new Uint8Array ( 1 << 20 ) ) . pipe ( Stream . run ( handle . stdin ) ) ;
203+ expect ( yield * handle . exitCode ) . toBe ( 0 ) ;
204+ yield * host . stop ;
205+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
206+ ) ;
0 commit comments