@@ -42,7 +42,7 @@ const resolveGatewayServiceProbeHosts = vi.fn(
4242 [ "127.0.0.1" ] as readonly string [ ] ,
4343) ;
4444const probePortUsage = vi . fn (
45- async ( _port : number , _hosts ?: readonly string [ ] ) => "free" as "free" | "busy" ,
45+ async ( _port : number , _hosts ?: readonly string [ ] ) => "free" as "free" | "busy" | "unknown" ,
4646) ;
4747const formatGatewayPidList = vi . fn < ( pids : number [ ] ) => string > ( ( pids ) => pids . join ( ", " ) ) ;
4848const probeGateway =
@@ -57,7 +57,7 @@ const callGatewayCli = vi.fn();
5757const isRestartEnabled = vi . fn < ( config ?: { commands ?: unknown } ) => boolean > ( ( ) => true ) ;
5858const loadConfig = vi . hoisted ( ( ) => vi . fn ( ( ) => ( { } ) ) ) ;
5959const createConfigIO = vi . hoisted ( ( ) =>
60- vi . fn ( ( _opts ?: { env ?: Record < string , string | undefined > } ) => ( {
60+ vi . fn ( ( _opts ?: { env ?: Record < string , string | undefined > ; observe ?: boolean } ) => ( {
6161 readBestEffortConfig : async ( ) => loadConfig ( ) ,
6262 } ) ) ,
6363) ;
@@ -787,6 +787,7 @@ describe("runDaemonRestart health checks", () => {
787787 expect ( appendGatewayLifecycleAudit ) . toHaveBeenCalledWith (
788788 expect . objectContaining ( { action : "stop" , mode : "sigterm" , pid : 4200 } ) ,
789789 ) ;
790+ expect ( service . readCommand ) . not . toHaveBeenCalled ( ) ;
790791 expect ( outcome ) . toEqual ( {
791792 result : "stopped" ,
792793 message : "Gateway stop signal sent to unmanaged process on port 18789: 4200." ,
@@ -1094,50 +1095,46 @@ describe("runDaemonRestart health checks", () => {
10941095 expect ( signalVerifiedGatewayPidSync ) . not . toHaveBeenCalled ( ) ;
10951096 } ) ;
10961097
1097- it ( "skips unmanaged signaling for pids that are not live gateway processes" , async ( ) => {
1098- findVerifiedGatewayListenerPidsOnPortSync . mockReturnValue ( [ ] ) ;
1099- readActiveGatewayLockIdentity . mockResolvedValue ( undefined ) ;
1100-
1101- const outcome = await runUnmanagedStop ( ) ;
1102-
1103- expect ( signalVerifiedGatewayPidSync ) . not . toHaveBeenCalled ( ) ;
1104- expect ( appendGatewayLifecycleAudit ) . not . toHaveBeenCalled ( ) ;
1105- expect ( probePortUsage ) . toHaveBeenCalledWith ( 18789 , [ "127.0.0.1" ] ) ;
1106- expect ( outcome ) . toBeNull ( ) ;
1107- } ) ;
1108-
1109- it ( "throws when gateway-configured port is busy without an identified owner" , async ( ) => {
1110- findVerifiedGatewayListenerPidsOnPortSync . mockReturnValue ( [ ] ) ;
1111- readActiveGatewayLockIdentity . mockResolvedValue ( undefined ) ;
1112- probePortUsage . mockResolvedValue ( "busy" ) ;
1113-
1114- await expect ( runUnmanagedStop ( ) ) . rejects . toThrow (
1115- / P o r t 1 8 7 8 9 i s i n u s e b u t t h e o w n i n g p r o c e s s c o u l d n o t b e i d e n t i f i e d / ,
1116- ) ;
1117- expect ( probePortUsage ) . toHaveBeenCalledWith ( 18789 , [ "127.0.0.1" ] ) ;
1118- expect ( signalVerifiedGatewayPidSync ) . not . toHaveBeenCalled ( ) ;
1119- } ) ;
1098+ it . each ( [
1099+ [ "free" , undefined ] ,
1100+ [ "busy" , "Port 18789 is in use but the owning process could not be identified" ] ,
1101+ [ "unknown" , "Could not determine whether port 18789 is still in use" ] ,
1102+ ] as const ) (
1103+ "handles an unowned gateway port reported as %s" ,
1104+ async ( portUsage , expectedError ) => {
1105+ findVerifiedGatewayListenerPidsOnPortSync . mockReturnValue ( [ ] ) ;
1106+ readActiveGatewayLockIdentity . mockResolvedValue ( undefined ) ;
1107+ probePortUsage . mockResolvedValue ( portUsage ) ;
1108+
1109+ const outcome = runUnmanagedStop ( ) ;
1110+ if ( expectedError ) {
1111+ await expect ( outcome ) . rejects . toThrow ( expectedError ) ;
1112+ } else {
1113+ await expect ( outcome ) . resolves . toBeNull ( ) ;
1114+ }
1115+ } ,
1116+ ) ;
11201117
11211118 it ( "resolves port and probe hosts from selected service config/env (no --port arg)" , async ( ) => {
11221119 const serviceCommand = {
11231120 programArguments : [ "openclaw" , "gateway" ] ,
11241121 environment : { OPENCLAW_STATE_DIR : "/tmp/service-state" } ,
11251122 } ;
11261123 service . readCommand . mockResolvedValue ( serviceCommand ) ;
1127- resolveGatewayPort . mockImplementation ( ( _cfg , env ) => {
1128- const e = env as Record < string , string | undefined > | undefined ;
1129- return e ?. OPENCLAW_STATE_DIR === "/tmp/service-state" ? 19000 : 18789 ;
1124+ loadConfig . mockReturnValue ( { gateway : { port : 18789 } } ) ;
1125+ createConfigIO . mockImplementation ( ( opts ) => ( {
1126+ readBestEffortConfig : async ( ) => ( {
1127+ gateway : { port : opts ?. env ?. OPENCLAW_STATE_DIR === "/tmp/service-state" ? 19000 : 18789 } ,
1128+ } ) ,
1129+ } ) ) ;
1130+ resolveGatewayPort . mockImplementation ( ( cfg ) => {
1131+ return ( cfg as { gateway ?: { port ?: number } } | undefined ) ?. gateway ?. port ?? 18789 ;
11301132 } ) ;
11311133 findVerifiedGatewayListenerPidsOnPortSync . mockReturnValue ( [ ] ) ;
11321134 readActiveGatewayLockIdentity . mockResolvedValue ( undefined ) ;
11331135 probePortUsage . mockResolvedValue ( "busy" ) ;
11341136
11351137 await expect ( runUnmanagedStop ( ) ) . rejects . toThrow ( / P o r t 1 9 0 0 0 / ) ;
1136- expect ( createConfigIO ) . toHaveBeenCalledWith (
1137- expect . objectContaining ( {
1138- env : expect . objectContaining ( { OPENCLAW_STATE_DIR : "/tmp/service-state" } ) ,
1139- } ) ,
1140- ) ;
11411138 expect ( resolveGatewayServiceProbeHosts ) . toHaveBeenCalledWith (
11421139 expect . objectContaining ( { command : serviceCommand } ) ,
11431140 ) ;
0 commit comments