11import { describe , expect , it } from "@effect/vitest" ;
22import * as NodePath from "@effect/platform-node/NodePath" ;
33import * as HostProcess from "@t3tools/shared/HostProcess" ;
4+ import * as Deferred from "effect/Deferred" ;
45import * as Effect from "effect/Effect" ;
6+ import * as Fiber from "effect/Fiber" ;
57import * as Layer from "effect/Layer" ;
68import * as Option from "effect/Option" ;
79import * as Path from "effect/Path" ;
@@ -45,7 +47,8 @@ const agentFixture = Effect.fn("agentFixture")(function* (
4547 const fs = yield * FileSystem . FileSystem ;
4648 const path = yield * Path . Path ;
4749 const baseDir = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-device-agent-start-" } ) ;
48- const hub = { running : true , spawnFails : false } ;
50+ const hub = { running : true , spawnFails : false , cleanup : Effect . void } ;
51+ const daemonStopRequested = yield * Deferred . make < ReadonlyArray < string > > ( ) ;
4952 const stateDir = agentDeviceStateDir ( path , path . join ( baseDir , "userdata" ) ) ;
5053 const daemonHealthy = yield * Ref . make ( true ) ;
5154 const runs : Array < ProcessRunner . ProcessRunInput > = [ ] ;
@@ -80,20 +83,22 @@ const agentFixture = Effect.fn("agentFixture")(function* (
8083 description : "Hub entry missing" ,
8184 } ) ,
8285 )
83- : Effect . succeed (
84- ChildProcessSpawner . makeHandle ( {
85- pid : ChildProcessSpawner . ProcessId ( 123 ) ,
86- stdout : Stream . empty ,
87- stderr : Stream . empty ,
88- all : Stream . empty ,
89- exitCode : Effect . never ,
90- isRunning : Effect . sync ( ( ) => hub . running ) ,
91- kill : ( ) => Effect . void ,
92- stdin : Sink . drain ,
93- getInputFd : ( ) => Sink . drain ,
94- getOutputFd : ( ) => Stream . empty ,
95- unref : Effect . succeed ( Effect . void ) ,
96- } ) ,
86+ : Effect . addFinalizer ( ( ) => hub . cleanup ) . pipe (
87+ Effect . as (
88+ ChildProcessSpawner . makeHandle ( {
89+ pid : ChildProcessSpawner . ProcessId ( 123 ) ,
90+ stdout : Stream . empty ,
91+ stderr : Stream . empty ,
92+ all : Stream . empty ,
93+ exitCode : Effect . never ,
94+ isRunning : Effect . sync ( ( ) => hub . running ) ,
95+ kill : ( ) => Effect . void ,
96+ stdin : Sink . drain ,
97+ getInputFd : ( ) => Sink . drain ,
98+ getOutputFd : ( ) => Stream . empty ,
99+ unref : Effect . succeed ( Effect . void ) ,
100+ } ) ,
101+ ) ,
97102 ) ,
98103 ) ,
99104 ) ,
@@ -102,7 +107,9 @@ const agentFixture = Effect.fn("agentFixture")(function* (
102107 runs . push ( input ) ;
103108 return input . args . includes ( "devices" )
104109 ? startup ( stateDir )
105- : Effect . succeed ( input . args . includes ( "-list-avds" ) ? avdOutput : processOutput ( 0 ) ) ;
110+ : input . args . includes ( "daemon" )
111+ ? Deferred . succeed ( daemonStopRequested , input . args ) . pipe ( Effect . as ( processOutput ( 0 ) ) )
112+ : Effect . succeed ( input . args . includes ( "-list-avds" ) ? avdOutput : processOutput ( 0 ) ) ;
106113 } ,
107114 } ) ,
108115 Effect . provideService (
@@ -122,7 +129,7 @@ const agentFixture = Effect.fn("agentFixture")(function* (
122129 ) ,
123130 ) ;
124131 yield * host . ensureReady ( ( ) => Effect . void ) ;
125- return { host, fs, baseDir, stateDir, hub, daemonHealthy, runs } ;
132+ return { host, fs, baseDir, stateDir, hub, daemonHealthy, daemonStopRequested , runs } ;
126133} ) ;
127134
128135describe ( "AgentDevice startup" , ( ) => {
@@ -353,6 +360,33 @@ describe("AgentDevice startup", () => {
353360 expect ( yield * fs . exists ( path . join ( stateDir , "daemon.json" ) ) ) . toBe ( false ) ;
354361 } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
355362 ) ;
363+
364+ it . effect ( "stops the agent daemon while the hub is still shutting down" , ( ) =>
365+ Effect . gen ( function * ( ) {
366+ const fs = yield * FileSystem . FileSystem ;
367+ const { host, hub, stateDir, daemonStopRequested } = yield * agentFixture ( ( stateDir ) =>
368+ fs
369+ . writeFileString (
370+ `${ stateDir } /daemon.json` ,
371+ JSON . stringify ( { httpPort : 12345 , token : "test-token" } ) ,
372+ )
373+ . pipe ( Effect . orDie , Effect . as ( processOutput ( 0 ) ) ) ,
374+ ) ;
375+ yield * host . ensureAgentReady ( ( ) => Effect . void ) ;
376+ const hubExited = yield * Deferred . make < void > ( ) ;
377+ hub . cleanup = Deferred . await ( hubExited ) ;
378+ const stopping = yield * Effect . forkChild ( host . stop ) ;
379+ expect ( ( yield * Deferred . await ( daemonStopRequested ) ) . slice ( 1 ) ) . toEqual ( [
380+ "daemon" ,
381+ "stop" ,
382+ "--state-dir" ,
383+ stateDir ,
384+ ] ) ;
385+ yield * Deferred . succeed ( hubExited , undefined ) ;
386+ yield * Fiber . join ( stopping ) ;
387+ expect ( yield * fs . exists ( `${ stateDir } /hub.json` ) ) . toBe ( false ) ;
388+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
389+ ) ;
356390} ) ;
357391
358392const diagnose = (
0 commit comments