@@ -37,6 +37,8 @@ const mocks = vi.hoisted(() => {
3737 applyClawUpdatePlan : vi . fn ( ) ,
3838 buildClawUpdatePlan : vi . fn ( ) ,
3939 exportClawAgent : vi . fn ( ) ,
40+ callGatewayFromCli : vi . fn ( ) ,
41+ sleep : vi . fn ( ) ,
4042 } ;
4143} ) ;
4244
@@ -58,6 +60,14 @@ vi.mock("../config/mcp-config.js", async () => ({
5860 listConfiguredMcpServers : mocks . listConfiguredMcpServers ,
5961} ) ) ;
6062
63+ vi . mock ( "./gateway-rpc.js" , ( ) => ( {
64+ callGatewayFromCli : mocks . callGatewayFromCli ,
65+ } ) ) ;
66+
67+ vi . mock ( "../utils/sleep.js" , ( ) => ( {
68+ sleep : mocks . sleep ,
69+ } ) ) ;
70+
6171vi . mock ( "../state/openclaw-state-db.js" , async ( ) => ( {
6272 ...( await vi . importActual < typeof import ( "../state/openclaw-state-db.js" ) > (
6373 "../state/openclaw-state-db.js" ,
@@ -95,6 +105,7 @@ vi.mock("../claws/update-apply.js", async () => ({
95105} ) ) ;
96106
97107const { registerClawsCli } = await import ( "./claws-cli.js" ) ;
108+ const { waitUntilGatewayConfigApplied } = await import ( "./claws-cli.gateway-readiness.js" ) ;
98109const { runClawsAddCommand } = await import ( "./claws-cli.runtime.js" ) ;
99110const { ClawUpdateMutationError } = await import ( "../claws/update-apply.js" ) ;
100111const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
@@ -178,6 +189,9 @@ describe("claws cli", () => {
178189 config : { } ,
179190 mcpServers : { } ,
180191 } ) ;
192+ mocks . callGatewayFromCli . mockReset ( ) ;
193+ mocks . sleep . mockReset ( ) ;
194+ mocks . sleep . mockResolvedValue ( undefined ) ;
181195 mocks . closeReadOnlyDatabase . mockReset ( ) ;
182196 mocks . stateTableGet . mockReset ( ) ;
183197 mocks . stateTableGet . mockReturnValue ( { 1 : 1 } ) ;
@@ -312,6 +326,7 @@ describe("claws cli", () => {
312326 } ) ;
313327
314328 afterEach ( ( ) => {
329+ vi . restoreAllMocks ( ) ;
315330 closeOpenClawStateDatabaseForTest ( ) ;
316331 } ) ;
317332
@@ -339,6 +354,49 @@ describe("claws cli", () => {
339354 ] ) ;
340355 } ) ;
341356
357+ it ( "accepts an already-applied Gateway config revision" , async ( ) => {
358+ mocks . callGatewayFromCli . mockResolvedValue ( {
359+ configRevisionHash : "revision-new" ,
360+ appliedConfigHash : "revision-new" ,
361+ } ) ;
362+
363+ await expect ( waitUntilGatewayConfigApplied ( ) ) . resolves . toBeUndefined ( ) ;
364+
365+ expect ( mocks . callGatewayFromCli ) . toHaveBeenCalledOnce ( ) ;
366+ expect ( mocks . callGatewayFromCli ) . toHaveBeenCalledWith ( "config.get" , { timeout : "5000" } , { } ) ;
367+ expect ( mocks . sleep ) . not . toHaveBeenCalled ( ) ;
368+ } ) ;
369+
370+ it ( "retries until the Gateway applies the persisted config revision" , async ( ) => {
371+ mocks . callGatewayFromCli
372+ . mockResolvedValueOnce ( {
373+ configRevisionHash : "revision-new" ,
374+ appliedConfigHash : "revision-old" ,
375+ } )
376+ . mockResolvedValueOnce ( {
377+ configRevisionHash : "revision-new" ,
378+ appliedConfigHash : "revision-new" ,
379+ } ) ;
380+
381+ await expect ( waitUntilGatewayConfigApplied ( ) ) . resolves . toBeUndefined ( ) ;
382+
383+ expect ( mocks . callGatewayFromCli ) . toHaveBeenCalledTimes ( 2 ) ;
384+ expect ( mocks . sleep ) . toHaveBeenCalledOnce ( ) ;
385+ expect ( mocks . sleep ) . toHaveBeenCalledWith ( 100 ) ;
386+ } ) ;
387+
388+ it ( "reports the last Gateway error after the reload deadline" , async ( ) => {
389+ vi . spyOn ( Date , "now" ) . mockReturnValueOnce ( 0 ) . mockReturnValueOnce ( 0 ) . mockReturnValue ( 15_000 ) ;
390+ mocks . callGatewayFromCli . mockRejectedValue ( new Error ( "gateway unavailable" ) ) ;
391+
392+ await expect ( waitUntilGatewayConfigApplied ( ) ) . rejects . toThrow (
393+ "Gateway did not apply the Claw agent configuration in time: gateway unavailable" ,
394+ ) ;
395+
396+ expect ( mocks . callGatewayFromCli ) . toHaveBeenCalledOnce ( ) ;
397+ expect ( mocks . sleep ) . toHaveBeenCalledOnce ( ) ;
398+ } ) ;
399+
342400 it ( "prints versioned experimental JSON for a development manifest" , async ( ) => {
343401 const manifestPath = await writeManifest ( ) ;
344402
0 commit comments