@@ -5,7 +5,7 @@ import { EventEmitter } from "node:events";
55import fs from "node:fs/promises" ;
66import os from "node:os" ;
77import path from "node:path" ;
8- import { PassThrough } from "node:stream" ;
8+ import { PassThrough , type Readable } from "node:stream" ;
99import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
1010import type { DB as OpenClawStateKyselyDatabase } from "../state/openclaw-state-db.generated.js" ;
1111import {
@@ -46,6 +46,20 @@ function signalHandoffReady(child: ReturnType<typeof createSpawnMock>): void {
4646 child . stdout . write ( "OPENCLAW_UPDATE_HANDOFF_READY\n" ) ;
4747}
4848
49+ async function waitForHandoffReady ( output : Readable | null ) : Promise < void > {
50+ if ( ! output ) {
51+ throw new Error ( "expected managed handoff helper stdout" ) ;
52+ }
53+ let buffered = "" ;
54+ for await ( const chunk of output ) {
55+ buffered = `${ buffered } ${ chunk . toString ( ) } ` . slice ( - 1024 ) ;
56+ if ( buffered . includes ( "OPENCLAW_UPDATE_HANDOFF_READY\n" ) ) {
57+ return ;
58+ }
59+ }
60+ throw new Error ( "managed handoff helper exited before readiness" ) ;
61+ }
62+
4963vi . mock ( "node:child_process" , async ( ) => {
5064 const { mockNodeChildProcessModule } =
5165 await import ( "../gateway/server-methods/node-child-process.test-support.js" ) ;
@@ -300,7 +314,10 @@ async function runHelperWithCommand(params: {
300314 parentExitTimeoutMs ?: number | null ;
301315 serviceRecovery ?: Record < string , unknown > ;
302316 pathPrepend ?: string ;
303- } ) : Promise < { code : number } > {
317+ } ) : Promise < {
318+ ready : Promise < void > ;
319+ completion : Promise < { code : number } > ;
320+ } > {
304321 const { execFile } =
305322 await vi . importActual < typeof import ( "node:child_process" ) > ( "node:child_process" ) ;
306323 const { startManagedServiceUpdateHandoff } = await import ( "./update-managed-service-handoff.js" ) ;
@@ -353,12 +370,19 @@ async function runHelperWithCommand(params: {
353370 ? { PATH : `${ params . pathPrepend } ${ path . delimiter } ${ process . env . PATH ?? "" } ` }
354371 : { } ) ,
355372 } ;
356- return await new Promise < { code : number } > ( ( resolve ) => {
357- execFile ( process . execPath , [ helperScriptPath , helperParamsPath ] , { env : childEnv } , ( err ) => {
358- const childError = err as NodeJS . ErrnoException | null ;
359- resolve ( { code : typeof childError ?. code === "number" ? childError . code : 0 } ) ;
360- } ) ;
373+ let child ! : ReturnType < typeof execFile > ;
374+ const completion = new Promise < { code : number } > ( ( resolve ) => {
375+ child = execFile (
376+ process . execPath ,
377+ [ helperScriptPath , helperParamsPath ] ,
378+ { env : childEnv } ,
379+ ( err ) => {
380+ const childError = err as NodeJS . ErrnoException | null ;
381+ resolve ( { code : typeof childError ?. code === "number" ? childError . code : 0 } ) ;
382+ } ,
383+ ) ;
361384 } ) ;
385+ return { ready : waitForHandoffReady ( child . stdout ) , completion } ;
362386}
363387
364388async function writeFakeSystemctl ( ) : Promise < { binDir : string ; recordPath : string } > {
@@ -642,11 +666,12 @@ describe("managed service update handoff", () => {
642666 "starts the managed gateway service when the update command fails after handoff" ,
643667 async ( ) => {
644668 const { binDir, recordPath } = await writeFakeSystemctl ( ) ;
645- const result = await runHelperWithCommand ( {
669+ const { completion } = await runHelperWithCommand ( {
646670 commandArgv : [ process . execPath , "-e" , "process.exit(7)" ] ,
647671 serviceRecovery : { kind : "systemd" , unit : "openclaw-gateway.service" } ,
648672 pathPrepend : binDir ,
649673 } ) ;
674+ const result = await completion ;
650675
651676 expect ( result . code ) . toBe ( 7 ) ;
652677 await expect ( fs . readFile ( recordPath , "utf-8" ) ) . resolves . toBe (
@@ -657,19 +682,20 @@ describe("managed service update handoff", () => {
657682
658683 it ( "leaves the gateway service alone when the update command succeeds" , async ( ) => {
659684 const { binDir, recordPath } = await writeFakeSystemctl ( ) ;
660- const result = await runHelperWithCommand ( {
685+ const { completion } = await runHelperWithCommand ( {
661686 commandArgv : [ process . execPath , "-e" , "process.exit(0)" ] ,
662687 serviceRecovery : { kind : "systemd" , unit : "openclaw-gateway.service" } ,
663688 pathPrepend : binDir ,
664689 } ) ;
690+ const result = await completion ;
665691
666692 expect ( result . code ) . toBe ( 0 ) ;
667693 await expect ( pathExists ( recordPath ) ) . resolves . toBe ( false ) ;
668694 } ) ;
669695
670696 itUnix ( "retries launchd start when bootstrap reports an already-loaded label" , async ( ) => {
671697 const { binDir, recordPath } = await writeFakeLaunchctl ( ) ;
672- const result = await runHelperWithCommand ( {
698+ const { completion } = await runHelperWithCommand ( {
673699 commandArgv : [ process . execPath , "-e" , "process.exit(7)" ] ,
674700 serviceRecovery : {
675701 kind : "launchd" ,
@@ -679,6 +705,7 @@ describe("managed service update handoff", () => {
679705 } ,
680706 pathPrepend : binDir ,
681707 } ) ;
708+ const result = await completion ;
682709
683710 expect ( result . code ) . toBe ( 7 ) ;
684711 await expect ( fs . readFile ( recordPath , "utf-8" ) ) . resolves . toBe (
@@ -1033,19 +1060,26 @@ describe("managed service update handoff", () => {
10331060 it ( "runs the handoff after an indefinitely awaited parent exits" , async ( ) => {
10341061 const { spawn } =
10351062 await vi . importActual < typeof import ( "node:child_process" ) > ( "node:child_process" ) ;
1036- const parent = spawn ( process . execPath , [ "-e" , "setTimeout(() => {}, 750 )" ] , {
1037- stdio : " ignore",
1063+ const parent = spawn ( process . execPath , [ "-e" , "process.stdin.resume( )" ] , {
1064+ stdio : [ "pipe" , " ignore", "ignore" ] ,
10381065 } ) ;
1066+ let completion : Promise < { code : number } > | undefined ;
10391067
10401068 try {
1041- const result = await runHelperWithCommand ( {
1069+ const helper = await runHelperWithCommand ( {
10421070 commandArgv : [ process . execPath , "-e" , "" ] ,
10431071 parentPid : parent . pid ,
10441072 parentExitTimeoutMs : null ,
10451073 } ) ;
1046- expect ( result . code ) . toBe ( 0 ) ;
1074+ completion = helper . completion ;
1075+ await helper . ready ;
1076+ expect ( parent . exitCode ) . toBeNull ( ) ;
1077+ parent . stdin . end ( ) ;
1078+ await expect ( completion ) . resolves . toEqual ( { code : 0 } ) ;
10471079 } finally {
1080+ parent . stdin . end ( ) ;
10481081 parent . kill ( ) ;
1082+ await completion ?. catch ( ( ) => undefined ) ;
10491083 }
10501084 } ) ;
10511085} ) ;
0 commit comments