@@ -6,6 +6,7 @@ import * as Layer from "effect/Layer";
66import * as Option from "effect/Option" ;
77import * as Path from "effect/Path" ;
88import * as PlatformError from "effect/PlatformError" ;
9+ import * as Ref from "effect/Ref" ;
910import * as FileSystem from "effect/FileSystem" ;
1011import * as Sink from "effect/Sink" ;
1112import * as Stream from "effect/Stream" ;
@@ -45,6 +46,7 @@ const agentFixture = Effect.fn("agentFixture")(function* (
4546 const baseDir = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-device-agent-start-" } ) ;
4647 const hub = { running : true , spawnFails : false } ;
4748 const stateDir = agentDeviceStateDir ( path , path . join ( baseDir , "userdata" ) ) ;
49+ const daemonHealthy = yield * Ref . make ( true ) ;
4850 for ( const [ name , version , entry ] of [
4951 [ "expo-device-hub" , DEVICE_HUB_VERSION , "dist/server/cli.mjs" ] ,
5052 [ "agent-device" , AGENT_DEVICE_VERSION , "bin/agent-device.mjs" ] ,
@@ -98,15 +100,56 @@ const agentFixture = Effect.fn("agentFixture")(function* (
98100 Effect . provideService (
99101 HttpClient . HttpClient ,
100102 HttpClient . make ( ( request ) =>
101- Effect . succeed ( HttpClientResponse . fromWeb ( request , new Response ( "ok" ) ) ) ,
103+ Ref . get ( daemonHealthy ) . pipe (
104+ Effect . map ( ( healthy ) =>
105+ HttpClientResponse . fromWeb (
106+ request ,
107+ new Response ( "ok" , {
108+ status : request . url . endsWith ( "/health" ) && ! healthy ? 503 : 200 ,
109+ } ) ,
110+ ) ,
111+ ) ,
112+ ) ,
102113 ) ,
103114 ) ,
104115 ) ;
105116 yield * host . ensureReady ( ( ) => Effect . void ) ;
106- return { host, fs, stateDir, hub } ;
117+ return { host, fs, stateDir, hub, daemonHealthy } ;
107118} ) ;
108119
109120describe ( "AgentDevice startup" , ( ) => {
121+ it . effect ( "restarts a cached daemon that no longer answers its health check" , ( ) =>
122+ Effect . gen ( function * ( ) {
123+ const fs = yield * FileSystem . FileSystem ;
124+ let attempts = 0 ;
125+ const { host, daemonHealthy } = yield * agentFixture ( ( stateDir ) =>
126+ Effect . gen ( function * ( ) {
127+ attempts ++ ;
128+ yield * fs
129+ . writeFileString (
130+ `${ stateDir } /daemon.json` ,
131+ JSON . stringify ( { httpPort : 12345 + attempts , token : `token-${ attempts } ` } ) ,
132+ )
133+ . pipe ( Effect . orDie ) ;
134+ return processOutput ( 0 ) ;
135+ } ) ,
136+ ) ;
137+ const first = yield * host . ensureAgentReady ( ( ) => Effect . void ) ;
138+ expect ( ( yield * host . ensureAgentReady ( ( ) => Effect . void ) ) . agentDevice ) . toEqual (
139+ first . agentDevice ,
140+ ) ;
141+ expect ( attempts ) . toBe ( 1 ) ;
142+
143+ yield * Ref . set ( daemonHealthy , false ) ;
144+ const restarted = yield * host . ensureAgentReady ( ( ) => Effect . void ) ;
145+ expect ( attempts ) . toBe ( 2 ) ;
146+ expect ( restarted . agentDevice ) . toMatchObject ( {
147+ baseUrl : "http://127.0.0.1:12347" ,
148+ token : "token-2" ,
149+ } ) ;
150+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
151+ ) ;
152+
110153 it . effect ( "reports an incompatible lock immediately and retains it for manual repair" , ( ) =>
111154 Effect . gen ( function * ( ) {
112155 const result = processOutput (
0 commit comments