@@ -21,6 +21,9 @@ type PortConnections = Awaited<
2121 ReturnType < typeof import ( "../../infra/ports-inspect.js" ) . inspectPortConnections >
2222> ;
2323
24+ const readFile = fs . readFile . bind ( fs ) ;
25+ let readFileSpy : ReturnType < typeof vi . spyOn > ;
26+
2427const callGatewayStatusProbe = vi . fn <
2528 ( opts ?: unknown ) => Promise < {
2629 ok : boolean ;
@@ -180,6 +183,19 @@ let cliLoadedConfig: Record<string, unknown> = {
180183} ;
181184
182185vi . mock ( "../../config/config.js" , ( ) => ( {
186+ getRuntimeConfig : ( ) => cliLoadedConfig ,
187+ loadConfig : ( ) => cliLoadedConfig ,
188+ } ) ) ;
189+
190+ vi . mock ( "../../config/paths.js" , async ( importOriginal ) => ( {
191+ ...( await importOriginal < typeof import ( "../../config/paths.js" ) > ( ) ) ,
192+ isDefaultInstallIdentity : ( env ?: NodeJS . ProcessEnv ) => isDefaultInstallIdentity ( env ) ,
193+ resolveConfigPath : ( env : NodeJS . ProcessEnv , stateDir : string ) => resolveConfigPath ( env , stateDir ) ,
194+ resolveGatewayPort : ( cfg ?: unknown , env ?: unknown ) => resolveGatewayPort ( cfg , env ) ,
195+ resolveStateDir : ( env : NodeJS . ProcessEnv ) => resolveStateDir ( env ) ,
196+ } ) ) ;
197+
198+ vi . mock ( "../../config/io.runtime.js" , ( ) => ( {
183199 createConfigIO : ( {
184200 configPath,
185201 observe,
@@ -212,16 +228,6 @@ vi.mock("../../config/config.js", () => ({
212228 } ,
213229 } ;
214230 } ,
215- getRuntimeConfig : ( ) => cliLoadedConfig ,
216- loadConfig : ( ) => cliLoadedConfig ,
217- resolveConfigPath : ( env : NodeJS . ProcessEnv , stateDir : string ) => resolveConfigPath ( env , stateDir ) ,
218- resolveGatewayPort : ( cfg ?: unknown , env ?: unknown ) => resolveGatewayPort ( cfg , env ) ,
219- resolveStateDir : ( env : NodeJS . ProcessEnv ) => resolveStateDir ( env ) ,
220- } ) ) ;
221-
222- vi . mock ( "../../config/paths.js" , async ( importOriginal ) => ( {
223- ...( await importOriginal < typeof import ( "../../config/paths.js" ) > ( ) ) ,
224- isDefaultInstallIdentity : ( env ?: NodeJS . ProcessEnv ) => isDefaultInstallIdentity ( env ) ,
225231} ) ) ;
226232
227233vi . mock ( "../../daemon/diagnostics.js" , ( ) => ( {
@@ -342,10 +348,49 @@ function gatherStatus(overrides: Partial<Parameters<typeof gatherDaemonStatus>[0
342348 return gatherDaemonStatus ( { rpc : { } , probe : true , deep : false , ...overrides } ) ;
343349}
344350
351+ async function withStatusConfig < T > (
352+ rawConfig : string | undefined ,
353+ run : ( configPath : string ) => Promise < T > ,
354+ includeServiceEnv = false ,
355+ ) : Promise < T > {
356+ const tmp = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-status-config-" ) ) ;
357+ const configPath = path . join ( tmp , "openclaw.json" ) ;
358+ if ( rawConfig !== undefined ) {
359+ await fs . writeFile ( configPath , rawConfig ) ;
360+ }
361+ setTestEnvValue ( "OPENCLAW_STATE_DIR" , tmp ) ;
362+ setTestEnvValue ( "OPENCLAW_CONFIG_PATH" , configPath ) ;
363+ serviceReadCommand . mockResolvedValueOnce ( {
364+ programArguments : [ "/bin/node" , "cli" , "gateway" , "--port" , "19001" ] ,
365+ ...( includeServiceEnv
366+ ? {
367+ environment : {
368+ OPENCLAW_STATE_DIR : tmp ,
369+ OPENCLAW_CONFIG_PATH : configPath ,
370+ } ,
371+ }
372+ : { } ) ,
373+ } ) ;
374+ try {
375+ return await run ( configPath ) ;
376+ } finally {
377+ await fs . rm ( tmp , { recursive : true , force : true } ) ;
378+ }
379+ }
380+
345381describe ( "gatherDaemonStatus" , ( ) => {
346382 let envSnapshot : ReturnType < typeof captureEnv > ;
347383
348384 beforeEach ( ( ) => {
385+ readFileSpy = vi . spyOn ( fs , "readFile" ) . mockImplementation ( async ( filePath , options ) => {
386+ if (
387+ filePath === "/tmp/openclaw-cli/openclaw.json" ||
388+ filePath === "/tmp/openclaw-daemon/openclaw.json"
389+ ) {
390+ throw Object . assign ( new Error ( "test config requires full IO" ) , { code : "EACCES" } ) ;
391+ }
392+ return await readFile ( filePath , options ) ;
393+ } ) ;
349394 envSnapshot = captureEnv ( [
350395 "OPENCLAW_STATE_DIR" ,
351396 "OPENCLAW_CONFIG_PATH" ,
@@ -438,6 +483,7 @@ describe("gatherDaemonStatus", () => {
438483 } ) ;
439484
440485 afterEach ( ( ) => {
486+ readFileSpy . mockRestore ( ) ;
441487 envSnapshot . restore ( ) ;
442488 } ) ;
443489
@@ -1076,84 +1122,128 @@ describe("gatherDaemonStatus", () => {
10761122 } ) ;
10771123
10781124 it ( "uses the fast config path for plain same-file status reads" , async ( ) => {
1079- const tmp = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-status-config-" ) ) ;
1080- const configPath = path . join ( tmp , "openclaw.json" ) ;
1081- await fs . writeFile (
1082- configPath ,
1125+ await withStatusConfig (
10831126 JSON . stringify ( {
10841127 gateway : {
10851128 bind : "custom" ,
10861129 customBindHost : "10.0.0.5" ,
10871130 controlUi : { enabled : true } ,
10881131 } ,
10891132 } ) ,
1133+ async ( configPath ) => {
1134+ const status = await gatherStatus ( { probe : false } ) ;
1135+
1136+ expect ( createConfigIOCalls ) . not . toHaveBeenCalled ( ) ;
1137+ expect ( readConfigFileSnapshotCalls ) . not . toHaveBeenCalled ( ) ;
1138+ expect ( loadConfigCalls ) . not . toHaveBeenCalled ( ) ;
1139+ expect ( status . config ?. cli . path ) . toBe ( configPath ) ;
1140+ expect ( status . config ?. cli . exists ) . toBe ( true ) ;
1141+ expect ( status . config ?. cli . valid ) . toBe ( true ) ;
1142+ expect ( status . config ?. cli . controlUi ) . toEqual ( { enabled : true } ) ;
1143+ expect ( status . config ?. daemon ) . toBe ( status . config ?. cli ) ;
1144+ expect ( status . gateway ?. bindMode ) . toBe ( "custom" ) ;
1145+ expect ( status . gateway ?. customBindHost ) . toBe ( "10.0.0.5" ) ;
1146+ } ,
1147+ true ,
10901148 ) ;
1091- setTestEnvValue ( "OPENCLAW_STATE_DIR" , tmp ) ;
1092- setTestEnvValue ( "OPENCLAW_CONFIG_PATH" , configPath ) ;
1093- serviceReadCommand . mockResolvedValueOnce ( {
1094- programArguments : [ "/bin/node" , "cli" , "gateway" , "--port" , "19001" ] ,
1095- environment : {
1096- OPENCLAW_STATE_DIR : tmp ,
1097- OPENCLAW_CONFIG_PATH : configPath ,
1149+ } ) ;
1150+
1151+ it ( "uses the fast config path when the config file is missing" , async ( ) => {
1152+ await withStatusConfig (
1153+ undefined ,
1154+ async ( configPath ) => {
1155+ const status = await gatherStatus ( { probe : false } ) ;
1156+
1157+ expect ( createConfigIOCalls ) . not . toHaveBeenCalled ( ) ;
1158+ expect ( status . config ?. cli ) . toEqual ( {
1159+ path : configPath ,
1160+ exists : false ,
1161+ valid : true ,
1162+ } ) ;
1163+ expect ( status . config ?. daemon ) . toBe ( status . config ?. cli ) ;
1164+ expect ( status . gateway ) . toMatchObject ( {
1165+ bindMode : "loopback" ,
1166+ port : 19001 ,
1167+ } ) ;
10981168 } ,
1169+ true ,
1170+ ) ;
1171+ } ) ;
1172+
1173+ it ( "keeps malformed JSON5 on the fast invalid-summary path" , async ( ) => {
1174+ await withStatusConfig (
1175+ "{ gateway:" ,
1176+ async ( configPath ) => {
1177+ const status = await gatherStatus ( { probe : false } ) ;
1178+
1179+ expect ( createConfigIOCalls ) . not . toHaveBeenCalled ( ) ;
1180+ expect ( status . config ?. cli ) . toMatchObject ( {
1181+ path : configPath ,
1182+ exists : true ,
1183+ valid : false ,
1184+ } ) ;
1185+ expect ( status . config ?. cli . issues ?. [ 0 ] ?. message ) . toContain ( "JSON5 parse failed" ) ;
1186+ expect ( status . config ?. daemon ) . toBe ( status . config ?. cli ) ;
1187+ } ,
1188+ true ,
1189+ ) ;
1190+ } ) ;
1191+
1192+ it . each ( [
1193+ [ "include" , JSON . stringify ( { $include : "./base.json" } ) ] ,
1194+ [ "substitution" , JSON . stringify ( { gateway : { auth : { token : "${STATUS_TOKEN}" } } } ) ] ,
1195+ [ "root env" , JSON . stringify ( { env : { STATUS_TOKEN : "value" } } ) ] ,
1196+ ] ) ( "uses full config IO for %s config" , async ( _name , rawConfig ) => {
1197+ await withStatusConfig ( rawConfig , async ( configPath ) => {
1198+ await gatherStatus ( { probe : false } ) ;
1199+
1200+ expect ( createConfigIOCalls ) . toHaveBeenCalledOnce ( ) ;
1201+ expect ( createConfigIOCalls ) . toHaveBeenCalledWith ( configPath , "skip" , false ) ;
1202+ expect ( readConfigFileSnapshotCalls ) . toHaveBeenCalledWith ( configPath ) ;
10991203 } ) ;
1204+ } ) ;
11001205
1101- try {
1102- const status = await gatherStatus ( { probe : false } ) ;
1103-
1104- expect ( readConfigFileSnapshotCalls ) . not . toHaveBeenCalled ( ) ;
1105- expect ( loadConfigCalls ) . not . toHaveBeenCalled ( ) ;
1106- expect ( status . config ?. cli . path ) . toBe ( configPath ) ;
1107- expect ( status . config ?. cli . exists ) . toBe ( true ) ;
1108- expect ( status . config ?. cli . valid ) . toBe ( true ) ;
1109- expect ( status . config ?. cli . controlUi ) . toEqual ( { enabled : true } ) ;
1110- expect ( status . config ?. daemon ) . toBe ( status . config ?. cli ) ;
1111- expect ( status . gateway ?. bindMode ) . toBe ( "custom" ) ;
1112- expect ( status . gateway ?. customBindHost ) . toBe ( "10.0.0.5" ) ;
1113- } finally {
1114- await fs . rm ( tmp , { recursive : true , force : true } ) ;
1115- }
1206+ it ( "uses full config IO after a non-missing read failure" , async ( ) => {
1207+ await withStatusConfig ( "{}" , async ( configPath ) => {
1208+ readFileSpy . mockRejectedValueOnce (
1209+ Object . assign ( new Error ( "permission denied" ) , { code : "EACCES" } ) ,
1210+ ) ;
1211+ await gatherStatus ( { probe : false } ) ;
1212+
1213+ expect ( createConfigIOCalls ) . toHaveBeenCalledOnce ( ) ;
1214+ expect ( createConfigIOCalls ) . toHaveBeenCalledWith ( configPath , "skip" , false ) ;
1215+ } ) ;
11161216 } ) ;
11171217
11181218 it ( "uses full plugin-aware config validation for deep status" , async ( ) => {
1119- const tmp = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-status-config-" ) ) ;
1120- const configPath = path . join ( tmp , "openclaw.json" ) ;
1121- await fs . writeFile (
1122- configPath ,
1219+ await withStatusConfig (
11231220 JSON . stringify ( {
11241221 gateway : {
11251222 bind : "loopback" ,
11261223 } ,
11271224 } ) ,
1128- ) ;
1129- setTestEnvValue ( "OPENCLAW_STATE_DIR" , tmp ) ;
1130- setTestEnvValue ( "OPENCLAW_CONFIG_PATH" , configPath ) ;
1131- cliLoadedConfig = {
1132- gateway : {
1133- bind : "loopback" ,
1134- } ,
1135- } ;
1136- cliConfigWarnings = [
1137- {
1138- path : "plugins.entries.test-bad-plugin" ,
1139- message :
1140- "plugin test-bad-plugin: channel plugin manifest declares test-bad-plugin without channelConfigs metadata" ,
1141- } ,
1142- ] ;
1143- serviceReadCommand . mockResolvedValueOnce ( {
1144- programArguments : [ "/bin/node" , "cli" , "gateway" , "--port" , "19001" ] ,
1145- } ) ;
1225+ async ( configPath ) => {
1226+ cliLoadedConfig = {
1227+ gateway : {
1228+ bind : "loopback" ,
1229+ } ,
1230+ } ;
1231+ cliConfigWarnings = [
1232+ {
1233+ path : "plugins.entries.test-bad-plugin" ,
1234+ message :
1235+ "plugin test-bad-plugin: channel plugin manifest declares test-bad-plugin without channelConfigs metadata" ,
1236+ } ,
1237+ ] ;
11461238
1147- try {
1148- const status = await gatherStatus ( { probe : false , deep : true } ) ;
1239+ const status = await gatherStatus ( { probe : false , deep : true } ) ;
11491240
1150- expect ( createConfigIOCalls ) . toHaveBeenCalledWith ( configPath , "full" , false ) ;
1151- expect ( readConfigFileSnapshotCalls ) . toHaveBeenCalledWith ( configPath ) ;
1152- expect ( status . config ?. cli . warnings ) . toEqual ( cliConfigWarnings ) ;
1153- expect ( status . config ?. daemon ) . toBe ( status . config ?. cli ) ;
1154- } finally {
1155- await fs . rm ( tmp , { recursive : true , force : true } ) ;
1156- }
1241+ expect ( createConfigIOCalls ) . toHaveBeenCalledWith ( configPath , "full" , false ) ;
1242+ expect ( readConfigFileSnapshotCalls ) . toHaveBeenCalledWith ( configPath ) ;
1243+ expect ( status . config ?. cli . warnings ) . toEqual ( cliConfigWarnings ) ;
1244+ expect ( status . config ?. daemon ) . toBe ( status . config ?. cli ) ;
1245+ } ,
1246+ ) ;
11571247 } ) ;
11581248
11591249 it ( "resolves daemon gateway auth password SecretRef values before probing" , async ( ) => {
0 commit comments