@@ -6,14 +6,236 @@ import * as Layer from "effect/Layer";
66import * as Option from "effect/Option" ;
77import * as Path from "effect/Path" ;
88import * as FileSystem from "effect/FileSystem" ;
9+ import * as Sink from "effect/Sink" ;
10+ import * as Stream from "effect/Stream" ;
911
1012import * as LocalDeviceHost from "./LocalDeviceHost.ts" ;
1113import * as NodeServices from "@effect/platform-node/NodeServices" ;
1214import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
13- import { HttpClient } from "effect/http" ;
15+ import { HttpClient , HttpClientResponse } from "effect/http" ;
1416import * as NetService from "@t3tools/shared/Net" ;
1517import * as ServerConfig from "../config.ts" ;
1618import * as ProcessRunner from "../processRunner.ts" ;
19+ import {
20+ AGENT_DEVICE_VERSION ,
21+ DEVICE_HUB_VERSION ,
22+ agentDeviceStateDir ,
23+ } from "./DeviceToolchain.ts" ;
24+
25+ const processOutput = ( code : number , stdout = "" , stderr = "" , timedOut = false ) => ( {
26+ code : ChildProcessSpawner . ExitCode ( code ) ,
27+ stdout,
28+ stderr,
29+ timedOut,
30+ stdoutTruncated : false ,
31+ stderrTruncated : false ,
32+ stdoutInvalidUtf8 : false ,
33+ stderrInvalidUtf8 : false ,
34+ } ) ;
35+
36+ const agentFixture = Effect . fn ( "agentFixture" ) ( function * (
37+ startup : (
38+ stateDir : string ,
39+ ) => Effect . Effect < ProcessRunner . ProcessRunOutput , ProcessRunner . ProcessRunError > ,
40+ ) {
41+ const fs = yield * FileSystem . FileSystem ;
42+ const path = yield * Path . Path ;
43+ const baseDir = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-device-agent-start-" } ) ;
44+ const stateDir = agentDeviceStateDir ( path , path . join ( baseDir , "userdata" ) ) ;
45+ for ( const [ name , version , entry ] of [
46+ [ "expo-device-hub" , DEVICE_HUB_VERSION , "dist/server/cli.mjs" ] ,
47+ [ "agent-device" , AGENT_DEVICE_VERSION , "bin/agent-device.mjs" ] ,
48+ ] ) {
49+ const installDir = path . join ( baseDir , "tools" , name ! , version ! ) ;
50+ const entryPath = path . join ( installDir , "node_modules" , name ! , entry ! ) ;
51+ yield * fs . makeDirectory ( path . dirname ( entryPath ) , { recursive : true } ) ;
52+ yield * fs . writeFileString ( entryPath , "" ) ;
53+ yield * fs . writeFileString ( path . join ( installDir , ".install-complete" ) , version ! ) ;
54+ }
55+ const host = yield * LocalDeviceHost . make ( ) . pipe (
56+ Effect . provide ( Layer . mergeAll ( ServerConfig . layerTest ( baseDir , baseDir ) , NetService . layer ) ) ,
57+ Effect . provideService ( HostProcess . Environment , { HOME : baseDir , PATH : "" } ) ,
58+ Effect . provideService ( HostProcess . IsExecutable , false ) ,
59+ Effect . provideService ( HostProcess . Platform , "darwin" ) ,
60+ Effect . provideService (
61+ ChildProcessSpawner . ChildProcessSpawner ,
62+ ChildProcessSpawner . make ( ( ) =>
63+ Effect . succeed (
64+ ChildProcessSpawner . makeHandle ( {
65+ pid : ChildProcessSpawner . ProcessId ( 123 ) ,
66+ stdout : Stream . empty ,
67+ stderr : Stream . empty ,
68+ all : Stream . empty ,
69+ exitCode : Effect . never ,
70+ isRunning : Effect . succeed ( true ) ,
71+ kill : ( ) => Effect . void ,
72+ stdin : Sink . drain ,
73+ getInputFd : ( ) => Sink . drain ,
74+ getOutputFd : ( ) => Stream . empty ,
75+ unref : Effect . succeed ( Effect . void ) ,
76+ } ) ,
77+ ) ,
78+ ) ,
79+ ) ,
80+ Effect . provideService ( ProcessRunner . ProcessRunner , {
81+ run : ( input ) =>
82+ input . args . includes ( "devices" ) ? startup ( stateDir ) : Effect . succeed ( processOutput ( 0 ) ) ,
83+ } ) ,
84+ Effect . provideService (
85+ HttpClient . HttpClient ,
86+ HttpClient . make ( ( request ) =>
87+ Effect . succeed ( HttpClientResponse . fromWeb ( request , new Response ( "ok" ) ) ) ,
88+ ) ,
89+ ) ,
90+ ) ;
91+ yield * host . ensureReady ( ( ) => Effect . void ) ;
92+ return { host, fs, stateDir } ;
93+ } ) ;
94+
95+ describe ( "AgentDevice startup" , ( ) => {
96+ it . effect ( "reports an incompatible lock immediately and retains it for manual repair" , ( ) =>
97+ Effect . gen ( function * ( ) {
98+ const result = processOutput (
99+ 1 ,
100+ JSON . stringify ( {
101+ success : false ,
102+ error : {
103+ message : "private command output" ,
104+ details : {
105+ reason : "daemon_registration_unproven" ,
106+ inspection : { state : "unproven" , reason : "non-directory" } ,
107+ } ,
108+ } ,
109+ } ) ,
110+ ) ;
111+ const { host, fs, stateDir } = yield * agentFixture ( ( ) => Effect . succeed ( result ) ) ;
112+ const lock = `${ stateDir } /daemon.lock` ;
113+ const legacyOwner = JSON . stringify ( { pid : 85669 , processStartTime : "old start time" } ) ;
114+ yield * fs . writeFileString ( lock , legacyOwner ) ;
115+ const error = yield * host . ensureAgentReady ( ( ) => Effect . void ) . pipe ( Effect . flip ) ;
116+ expect ( error ) . toMatchObject ( {
117+ _tag : "AgentDeviceStartError" ,
118+ reason : "daemon_lock_not_directory" ,
119+ exitCode : 1 ,
120+ cause : result ,
121+ } ) ;
122+ expect ( error . message ) . toContain ( "Daemon registration ownership could not be verified" ) ;
123+ expect ( error . message ) . toContain ( "daemon.lock is not a directory" ) ;
124+ expect ( error . message ) . toContain ( stateDir ) ;
125+ expect ( error . message ) . toContain ( "original CLI" ) ;
126+ expect ( error . message ) . not . toContain ( "private command output" ) ;
127+ expect ( yield * fs . readFileString ( lock ) ) . toBe ( legacyOwner ) ;
128+ expect ( yield * host . current ) . not . toBeNull ( ) ;
129+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
130+ ) ;
131+
132+ it . effect ( "reports unknown nonzero exits without exposing arbitrary command output" , ( ) =>
133+ Effect . gen ( function * ( ) {
134+ const result = processOutput ( 2 , "invalid JSON" , "private credentials" ) ;
135+ const { host, stateDir } = yield * agentFixture ( ( ) => Effect . succeed ( result ) ) ;
136+ const error = yield * host . ensureAgentReady ( ( ) => Effect . void ) . pipe ( Effect . flip ) ;
137+ expect ( error ) . toMatchObject ( {
138+ _tag : "AgentDeviceStartError" ,
139+ reason : "command_failed" ,
140+ exitCode : 2 ,
141+ cause : result ,
142+ } ) ;
143+ expect ( error . message ) . toContain ( "exit code 2" ) ;
144+ expect ( error . message ) . toContain ( stateDir ) ;
145+ expect ( error . message ) . not . toContain ( "private credentials" ) ;
146+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
147+ ) ;
148+
149+ it . effect ( "preserves process execution failures instead of waiting for a daemon file" , ( ) =>
150+ Effect . gen ( function * ( ) {
151+ const cause = new ProcessRunner . ProcessSpawnError ( {
152+ command : "/missing/node" ,
153+ argumentCount : 3 ,
154+ cause : new Error ( "missing executable" ) ,
155+ } ) ;
156+ const { host } = yield * agentFixture ( ( ) => Effect . fail ( cause ) ) ;
157+ const error = yield * host . ensureAgentReady ( ( ) => Effect . void ) . pipe ( Effect . flip ) ;
158+ expect ( error ) . toMatchObject ( {
159+ _tag : "DeviceHostError" ,
160+ step : "starting agent tools" ,
161+ cause,
162+ } ) ;
163+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
164+ ) ;
165+
166+ it . effect ( "returns a command timeout without starting another readiness wait" , ( ) =>
167+ Effect . gen ( function * ( ) {
168+ const { host } = yield * agentFixture ( ( ) => Effect . succeed ( processOutput ( 124 , "" , "" , true ) ) ) ;
169+ const error = yield * host . ensureAgentReady ( ( ) => Effect . void ) . pipe ( Effect . flip ) ;
170+ expect ( error ) . toMatchObject ( { _tag : "DeviceHostTimeoutError" , timeoutMs : 30_000 } ) ;
171+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
172+ ) ;
173+
174+ it . effect ( "uses a registered daemon when reading startup command output fails" , ( ) =>
175+ Effect . gen ( function * ( ) {
176+ const fs = yield * FileSystem . FileSystem ;
177+ const cause = new ProcessRunner . ProcessReadError ( {
178+ command : process . execPath ,
179+ argumentCount : 3 ,
180+ stream : "stdout" ,
181+ cause : new Error ( "output read failed after registration" ) ,
182+ } ) ;
183+ let attempts = 0 ;
184+ const { host } = yield * agentFixture ( ( stateDir ) =>
185+ Effect . gen ( function * ( ) {
186+ attempts ++ ;
187+ yield * fs
188+ . writeFileString (
189+ `${ stateDir } /daemon.json` ,
190+ JSON . stringify ( { httpPort : 12345 , token : "test-token" } ) ,
191+ )
192+ . pipe ( Effect . orDie ) ;
193+ return yield * cause ;
194+ } ) ,
195+ ) ;
196+ const ready = yield * host . ensureAgentReady ( ( ) => Effect . void ) ;
197+ expect ( ready . agentDevice ) . toMatchObject ( {
198+ baseUrl : "http://127.0.0.1:12345" ,
199+ token : "test-token" ,
200+ } ) ;
201+ expect ( ( yield * host . ensureAgentReady ( ( ) => Effect . void ) ) . agentDevice ) . toEqual (
202+ ready . agentDevice ,
203+ ) ;
204+ expect ( attempts ) . toBe ( 1 ) ;
205+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
206+ ) ;
207+
208+ it . effect ( "allows retry and uses a registered daemon even when discovery exits nonzero" , ( ) =>
209+ Effect . gen ( function * ( ) {
210+ const fs = yield * FileSystem . FileSystem ;
211+ let attempts = 0 ;
212+ const { host } = yield * agentFixture ( ( stateDir ) =>
213+ Effect . gen ( function * ( ) {
214+ attempts ++ ;
215+ if ( attempts > 1 ) {
216+ yield * fs
217+ . writeFileString (
218+ `${ stateDir } /daemon.json` ,
219+ JSON . stringify ( { httpPort : 12345 , token : "test-token" } ) ,
220+ )
221+ . pipe ( Effect . orDie ) ;
222+ }
223+ return processOutput ( 1 ) ;
224+ } ) ,
225+ ) ;
226+ yield * host . ensureAgentReady ( ( ) => Effect . void ) . pipe ( Effect . flip ) ;
227+ const ready = yield * host . ensureAgentReady ( ( ) => Effect . void ) ;
228+ expect ( ready . agentDevice ) . toMatchObject ( {
229+ baseUrl : "http://127.0.0.1:12345" ,
230+ token : "test-token" ,
231+ } ) ;
232+ expect ( ( yield * host . ensureAgentReady ( ( ) => Effect . void ) ) . agentDevice ) . toEqual (
233+ ready . agentDevice ,
234+ ) ;
235+ expect ( attempts ) . toBe ( 2 ) ;
236+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
237+ ) ;
238+ } ) ;
17239
18240const diagnose = (
19241 files : ReadonlyArray < string > ,
0 commit comments