@@ -7,10 +7,12 @@ import type { StaleOpenClawUpdateLaunchdJob } from "../../daemon/launchd.js";
77import { createMockGatewayService } from "../../daemon/service.test-helpers.js" ;
88import type { PortListener , PortUsageStatus } from "../../infra/ports.js" ;
99import type { GatewayRestartHandoff } from "../../infra/restart-handoff.js" ;
10+ import { defaultRuntime } from "../../runtime.js" ;
1011import { captureEnv , deleteTestEnvValue , setTestEnvValue } from "../../test-utils/env.js" ;
1112import { VERSION } from "../../version.js" ;
1213import type { GatewayRestartSnapshot } from "./restart-health.js" ;
1314import { gatherDaemonStatus } from "./status.gather.js" ;
15+ import { printDaemonStatus } from "./status.print.js" ;
1416
1517type PortConnections = Awaited <
1618 ReturnType < typeof import ( "../../infra/ports.js" ) . inspectPortConnections >
@@ -103,10 +105,15 @@ const inspectWindowsGatewayFirewall = vi.fn<(opts?: unknown) => Promise<unknown>
103105 details : [ ] ,
104106} ) ) ;
105107const auditGatewayServiceConfig = vi . fn ( async ( _opts ?: unknown ) => undefined ) ;
106- const serviceIsLoaded = vi . fn ( async ( _opts ?: unknown ) => true ) ;
108+ const serviceIsLoaded = vi . fn <
109+ ( opts ?: { env ?: NodeJS . ProcessEnv ; timeoutMs ?: number } ) => Promise < boolean >
110+ > ( async ( _opts ?: { env ?: NodeJS . ProcessEnv ; timeoutMs ?: number } ) => true ) ;
107111const serviceReadRuntime = vi . fn <
108- ( _env ?: NodeJS . ProcessEnv ) => Promise < { status : string ; detail ?: string } >
109- > ( async ( _env ?: NodeJS . ProcessEnv ) => ( { status : "running" } ) ) ;
112+ (
113+ _env ?: NodeJS . ProcessEnv ,
114+ _opts ?: { timeoutMs ?: number } ,
115+ ) => Promise < { status : string ; detail ?: string } >
116+ > ( async ( _env ?: NodeJS . ProcessEnv , _opts ?: { timeoutMs ?: number } ) => ( { status : "running" } ) ) ;
110117const inspectGatewayRestart = vi . fn < ( opts ?: unknown ) => Promise < GatewayRestartSnapshot > > (
111118 async ( _opts ?: unknown ) => ( {
112119 runtime : { status : "running" , pid : 1234 } ,
@@ -210,7 +217,8 @@ vi.mock("../../daemon/inspect.js", () => ({
210217 findExtraGatewayServices : ( env : unknown , opts ?: unknown ) => findExtraGatewayServices ( env , opts ) ,
211218} ) ) ;
212219
213- vi . mock ( "../../daemon/launchd.js" , ( ) => ( {
220+ vi . mock ( "../../daemon/launchd.js" , async ( importOriginal ) => ( {
221+ ...( await importOriginal < typeof import ( "../../daemon/launchd.js" ) > ( ) ) ,
214222 findStaleOpenClawUpdateLaunchdJobs : ( env ?: NodeJS . ProcessEnv ) =>
215223 findStaleOpenClawUpdateLaunchdJobs ( env ) ,
216224} ) ) ;
@@ -219,7 +227,8 @@ vi.mock("../../daemon/service-audit.js", () => ({
219227 auditGatewayServiceConfig : ( opts : unknown ) => auditGatewayServiceConfig ( opts ) ,
220228} ) ) ;
221229
222- vi . mock ( "../../daemon/service.js" , ( ) => ( {
230+ vi . mock ( "../../daemon/service.js" , async ( importOriginal ) => ( {
231+ ...( await importOriginal < typeof import ( "../../daemon/service.js" ) > ( ) ) ,
223232 resolveGatewayService : ( ) =>
224233 createMockGatewayService ( {
225234 isLoaded : serviceIsLoaded ,
@@ -374,6 +383,9 @@ describe("gatherDaemonStatus", () => {
374383 readLastGatewayErrorLine . mockReset ( ) ;
375384 readLastGatewayErrorLine . mockResolvedValue ( null ) ;
376385 readGatewayRestartHandoffSync . mockClear ( ) ;
386+ serviceIsLoaded . mockClear ( ) ;
387+ serviceReadCommand . mockClear ( ) ;
388+ serviceReadRuntime . mockClear ( ) ;
377389 readConfigFileSnapshotCalls . mockClear ( ) ;
378390 loadConfigCalls . mockClear ( ) ;
379391 daemonConfigWarnings = [ ] ;
@@ -640,6 +652,67 @@ describe("gatherDaemonStatus", () => {
640652 expect ( ( status . service . runtime as { detail ?: string } ) . detail ) . toBe ( "19001" ) ;
641653 } ) ;
642654
655+ it ( "bounds both service-manager reads and still emits JSON after they time out" , async ( ) => {
656+ serviceIsLoaded . mockImplementationOnce ( async ( args ?: { timeoutMs ?: number } ) => {
657+ if ( args ?. timeoutMs === undefined ) {
658+ return await new Promise < boolean > ( ( ) => { } ) ;
659+ }
660+ throw new Error ( "systemctl is-enabled timed out" ) ;
661+ } ) ;
662+ serviceReadRuntime . mockImplementationOnce ( async ( _env , opts ) => {
663+ if ( opts ?. timeoutMs === undefined ) {
664+ return await new Promise < { status : string } > ( ( ) => { } ) ;
665+ }
666+ throw new Error ( "systemctl show timed out" ) ;
667+ } ) ;
668+
669+ const status = await gatherDaemonStatus ( {
670+ rpc : { timeout : "100" , json : true } ,
671+ probe : false ,
672+ deep : true ,
673+ } ) ;
674+
675+ expect ( serviceIsLoaded ) . toHaveBeenCalledWith ( expect . objectContaining ( { timeoutMs : 100 } ) ) ;
676+ expect ( serviceReadRuntime ) . toHaveBeenCalledWith ( expect . any ( Object ) , { timeoutMs : 100 } ) ;
677+ expect ( status . service . loaded ) . toBe ( false ) ;
678+ expect ( status . service . runtime ) . toEqual ( {
679+ status : "unknown" ,
680+ detail : "Error: systemctl show timed out" ,
681+ } ) ;
682+
683+ const writeJson = vi . spyOn ( defaultRuntime , "writeJson" ) . mockImplementation ( ( ) => { } ) ;
684+ try {
685+ printDaemonStatus ( status , { json : true , deep : true } ) ;
686+ expect ( writeJson ) . toHaveBeenCalledOnce ( ) ;
687+ const serialized = JSON . stringify ( writeJson . mock . calls [ 0 ] ?. [ 0 ] ) ;
688+ if ( ! serialized ) {
689+ throw new Error ( "expected terminal JSON output" ) ;
690+ }
691+ expect ( JSON . parse ( serialized ) ) . toMatchObject ( {
692+ service : {
693+ loaded : false ,
694+ runtime : {
695+ status : "unknown" ,
696+ detail : "Error: systemctl show timed out" ,
697+ } ,
698+ } ,
699+ } ) ;
700+ } finally {
701+ writeJson . mockRestore ( ) ;
702+ }
703+
704+ const log = vi . spyOn ( defaultRuntime , "log" ) . mockImplementation ( ( ) => { } ) ;
705+ const error = vi . spyOn ( defaultRuntime , "error" ) . mockImplementation ( ( ) => { } ) ;
706+ try {
707+ printDaemonStatus ( status , { json : false , deep : true } ) ;
708+ const output = log . mock . calls . flat ( ) . join ( "\n" ) ;
709+ expect ( output ) . toContain ( "Runtime: unknown (Error: systemctl show timed out)" ) ;
710+ } finally {
711+ log . mockRestore ( ) ;
712+ error . mockRestore ( ) ;
713+ }
714+ } , 1_000 ) ;
715+
643716 it ( "keeps gateway status read-only when service management is unsupported" , async ( ) => {
644717 serviceReadCommand . mockResolvedValueOnce ( null ) ;
645718 serviceIsLoaded . mockResolvedValueOnce ( false ) ;
0 commit comments