@@ -78,6 +78,43 @@ async function startCronListGateway(token: string): Promise<{ url: string }> {
7878 return { url : `ws://127.0.0.1:${ address . port } ` } ;
7979}
8080
81+ async function startRateLimitedGateway ( ) : Promise < { url : string } > {
82+ const wss = new WebSocketServer ( { host : "127.0.0.1" , port : 0 } ) ;
83+ activeServers . add ( wss ) ;
84+ wss . on ( "connection" , ( ws ) => {
85+ sendMinimalGatewayConnectChallenge ( ws ) ;
86+ ws . on ( "message" , ( data ) => {
87+ const frame = parseMinimalGatewayRequestFrame ( data ) ;
88+ if ( frame . type !== "req" || ! frame . id || frame . method !== "connect" ) {
89+ return ;
90+ }
91+ const message = "unauthorized: too many failed authentication attempts (retry later)" ;
92+ ws . send (
93+ JSON . stringify ( {
94+ type : "res" ,
95+ id : frame . id ,
96+ ok : false ,
97+ error : {
98+ code : "INVALID_REQUEST" ,
99+ message,
100+ retryable : true ,
101+ retryAfterMs : 60_000 ,
102+ details : {
103+ code : "AUTH_RATE_LIMITED" ,
104+ authReason : "rate_limited" ,
105+ recommendedNextStep : "wait_then_retry" ,
106+ } ,
107+ } ,
108+ } ) ,
109+ ( ) => ws . close ( 1008 , message ) ,
110+ ) ;
111+ } ) ;
112+ } ) ;
113+ await once ( wss , "listening" ) ;
114+ const address = wss . address ( ) as AddressInfo ;
115+ return { url : `ws://127.0.0.1:${ address . port } ` } ;
116+ }
117+
81118async function runIsolatedGatewayCli ( params : {
82119 args : string [ ] ;
83120 root : string ;
@@ -298,4 +335,44 @@ describe("gateway-backed CLI process exit", () => {
298335 gateway : { url : `ws://127.0.0.1:${ port } ` } ,
299336 } ) ;
300337 } , 30_000 ) ;
338+
339+ it ( "preserves pre-hello rate-limit details through the real health entry point" , async ( ) => {
340+ const root = tempDirs . make ( "openclaw-gateway-rate-limit-json-" ) ;
341+ const stateDir = path . join ( root , "state" ) ;
342+ const configPath = path . join ( stateDir , "openclaw.json" ) ;
343+ const gateway = await startRateLimitedGateway ( ) ;
344+ await fs . mkdir ( stateDir , { recursive : true } ) ;
345+ await fs . writeFile (
346+ configPath ,
347+ JSON . stringify ( {
348+ gateway : {
349+ mode : "remote" ,
350+ remote : { url : gateway . url , token : "test-token" } ,
351+ } ,
352+ } ) ,
353+ ) ;
354+
355+ const result = await runIsolatedGatewayCli ( {
356+ args : [ "health" , "--json" , "--timeout" , "2000" ] ,
357+ root,
358+ stateDir,
359+ configPath,
360+ } ) ;
361+
362+ expect ( result , result . stderr ) . toMatchObject ( { code : 1 , signal : null , stderr : "" } ) ;
363+ expect ( JSON . parse ( result . stdout ) ) . toEqual ( {
364+ ok : false ,
365+ error : {
366+ type : "gateway_request_error" ,
367+ code : "AUTH_RATE_LIMITED" ,
368+ message :
369+ "Gateway authentication is temporarily rate-limited. Wait for the temporary lockout to expire, then retry." ,
370+ retryable : true ,
371+ retryAfterMs : 60_000 ,
372+ } ,
373+ gateway : { reachable : true } ,
374+ } ) ;
375+ expect ( result . stdout ) . not . toContain ( "gateway.remote.token" ) ;
376+ expect ( result . stdout ) . not . toContain ( "devices rotate" ) ;
377+ } , 30_000 ) ;
301378} ) ;
0 commit comments