@@ -24,6 +24,13 @@ const loadConfig = vi.fn<() => OpenClawConfig>(() => ({
2424const writeGatewayRestartIntentSync = vi . fn ( ) ;
2525const clearGatewayRestartIntentSync = vi . fn ( ) ;
2626const appendGatewayLifecycleAudit = vi . fn ( ) ;
27+ const MISSING_SERVICE_PROGRAM = "/openclaw-test-missing-runtime/node" ;
28+ const SERVICE_REPAIR_COMMAND_CASES = [
29+ [ "Gateway" , "" , "" , "openclaw gateway" , "restart" ] ,
30+ [ "Node" , "" , "" , "openclaw node" , "install --force" ] ,
31+ [ "Node" , "work" , "" , "openclaw --profile work node" , "install --force" ] ,
32+ [ "Node" , "work" , "demo" , "openclaw --container demo node" , "install --force" ] ,
33+ ] as const ;
2734const createGatewayLifecycleMutationAudit = vi . fn (
2835 ( params : { action : string ; source ?: string } ) => ( mutation : { mode : string ; pid ?: number } ) =>
2936 appendGatewayLifecycleAudit ( {
@@ -777,46 +784,61 @@ describe("runServiceRestart token drift", () => {
777784 expect ( appendGatewayLifecycleAudit ) . not . toHaveBeenCalled ( ) ;
778785 } ) ;
779786
780- it ( "warns in json when an already-running gateway definition needs repair" , async ( ) => {
781- service . readRuntime . mockResolvedValue ( { status : "running" , pid : 4242 } ) ;
782- service . readCommand . mockResolvedValue ( {
783- programArguments : [ "openclaw" , "gateway" , "--port" , "18789" ] ,
784- } ) ;
787+ it . each ( SERVICE_REPAIR_COMMAND_CASES ) (
788+ "warns in json with the %s service repair command and active context" ,
789+ async ( serviceNoun , profile , container , command , repairAction ) => {
790+ vi . stubEnv ( "OPENCLAW_PROFILE" , profile ) ;
791+ vi . stubEnv ( "OPENCLAW_CONTAINER_HINT" , container ) ;
792+ service . readRuntime . mockResolvedValue ( { status : "running" , pid : 4242 } ) ;
793+ service . readCommand . mockResolvedValue ( {
794+ programArguments : [ MISSING_SERVICE_PROGRAM , "openclaw" , serviceNoun . toLowerCase ( ) ] ,
795+ } ) ;
785796
786- await runServiceStart ( { ...createServiceRunArgs ( ) , expectedPort : 19_001 } ) ;
797+ await runServiceStart ( {
798+ ...createServiceRunArgs ( ) ,
799+ serviceNoun,
800+ repairLoadedService : serviceNoun === "Gateway" ? vi . fn ( async ( ) => null ) : undefined ,
801+ } ) ;
787802
788- const payload = readJsonLog < { result ?: string ; warnings ?: string [ ] } > ( ) ;
789- expect ( payload . result ) . toBe ( "already-running" ) ;
790- expect ( payload . warnings ) . toEqual ( [
791- expect . stringMatching (
792- / ^ G a t e w a y s e r v i c e a l r e a d y r u n n i n g , b u t i t s i n s t a l l e d s e r v i c e d e f i n i t i o n n e e d s r e p a i r : s e r v i c e p o r t 1 8 7 8 9 d o e s n o t m a t c h c u r r e n t g a t e w a y c o n f i g p o r t 1 9 0 0 1 ; r u n ` o p e n c l a w g a t e w a y r e s t a r t ` t o a p p l y \. $ / ,
793- ) ,
794- ] ) ;
795- expect ( service . start ) . not . toHaveBeenCalled ( ) ;
796- } ) ;
803+ const payload = readJsonLog < { result ?: string ; warnings ?: string [ ] } > ( ) ;
804+ expect ( payload . result ) . toBe ( "already-running" ) ;
805+ expect ( payload . warnings ) . toEqual ( [
806+ `${ serviceNoun } service already running, but its installed service definition needs repair: service command points at a missing path: ${ MISSING_SERVICE_PROGRAM } ; run \`${ command } ${ repairAction } \` to apply.` ,
807+ ] ) ;
808+ expect ( service . start ) . not . toHaveBeenCalled ( ) ;
809+ } ,
810+ ) ;
797811
798- it ( "prints one warning line when an already-running gateway definition needs repair" , async ( ) => {
799- service . readRuntime . mockResolvedValue ( { status : "running" , pid : 4242 } ) ;
800- service . readCommand . mockResolvedValue ( {
801- programArguments : [ "openclaw" , "gateway" , "--port" , "18789" ] ,
802- } ) ;
812+ it . each ( [
813+ [ "Gateway" , "restart" ] ,
814+ [ "Node" , "install --force" ] ,
815+ ] ) (
816+ "prints one warning line when an already-running %s service needs repair" ,
817+ async ( serviceNoun , repairAction ) => {
818+ service . readRuntime . mockResolvedValue ( { status : "running" , pid : 4242 } ) ;
819+ service . readCommand . mockResolvedValue ( {
820+ programArguments : [ MISSING_SERVICE_PROGRAM , "openclaw" , serviceNoun . toLowerCase ( ) ] ,
821+ } ) ;
803822
804- await runServiceStart ( {
805- serviceNoun : "Gateway" ,
806- service,
807- renderStartHints : ( ) => [ ] ,
808- expectedPort : 19_001 ,
809- } ) ;
823+ await runServiceStart ( {
824+ serviceNoun ,
825+ service,
826+ renderStartHints : ( ) => [ ] ,
827+ repairLoadedService : serviceNoun === "Gateway" ? vi . fn ( async ( ) => null ) : undefined ,
828+ } ) ;
810829
811- const repairWarnings = lifecycleRuntimeLogs . filter ( ( line ) =>
812- line . startsWith (
813- "Gateway service already running, but its installed service definition needs repair:" ,
814- ) ,
815- ) ;
816- expect ( repairWarnings ) . toHaveLength ( 1 ) ;
817- expect ( repairWarnings [ 0 ] ) . toContain ( "run `openclaw gateway restart` to apply." ) ;
818- expect ( service . start ) . not . toHaveBeenCalled ( ) ;
819- } ) ;
830+ const repairWarnings = lifecycleRuntimeLogs . filter ( ( line ) =>
831+ line . startsWith (
832+ `${ serviceNoun } service already running, but its installed service definition needs repair:` ,
833+ ) ,
834+ ) ;
835+ expect ( repairWarnings ) . toHaveLength ( 1 ) ;
836+ expect ( repairWarnings [ 0 ] ) . toContain (
837+ `run \`openclaw ${ serviceNoun . toLowerCase ( ) } ${ repairAction } \` to apply.` ,
838+ ) ;
839+ expect ( service . start ) . not . toHaveBeenCalled ( ) ;
840+ } ,
841+ ) ;
820842
821843 it ( "audits a service start that actually mutates the gateway" , async ( ) => {
822844 service . start . mockImplementationOnce ( async ( args ?: GatewayServiceControlArgs ) => {
@@ -936,21 +958,42 @@ describe("runServiceRestart token drift", () => {
936958 expect ( payload . service ?. loaded ) . toBe ( true ) ;
937959 } ) ;
938960
939- it ( "fails start with an install hint when port drift has no repair callback" , async ( ) => {
940- service . readCommand . mockResolvedValue ( {
941- programArguments : [ "openclaw" , "gateway" , "--port" , "18789" ] ,
942- } ) ;
961+ it . each ( SERVICE_REPAIR_COMMAND_CASES ) (
962+ "fails %s service start with its own install hint when repair is required" ,
963+ async ( serviceNoun , profile , container , command ) => {
964+ vi . stubEnv ( "OPENCLAW_PROFILE" , profile ) ;
965+ vi . stubEnv ( "OPENCLAW_CONTAINER_HINT" , container ) ;
966+ service . readCommand . mockResolvedValue ( {
967+ programArguments : [ MISSING_SERVICE_PROGRAM , "openclaw" , serviceNoun . toLowerCase ( ) ] ,
968+ } ) ;
943969
944- await expect (
945- runServiceStart ( { ... createServiceRunArgs ( ) , expectedPort : 19_001 } ) ,
946- ) . rejects . toThrow ( "__exit__:1" ) ;
970+ await expect ( runServiceStart ( { ... createServiceRunArgs ( ) , serviceNoun } ) ) . rejects . toThrow (
971+ "__exit__:1" ,
972+ ) ;
947973
948- const payload = readJsonLog < { ok ?: boolean ; error ?: string ; hints ?: string [ ] } > ( ) ;
949- expect ( payload . ok ) . toBe ( false ) ;
950- expect ( payload . error ) . toContain ( "service needs repair" ) ;
951- expect ( payload . hints ) . toEqual ( [ "openclaw gateway install --force" ] ) ;
952- expect ( service . start ) . not . toHaveBeenCalled ( ) ;
953- } ) ;
974+ const payload = readJsonLog < {
975+ ok ?: boolean ;
976+ error ?: string ;
977+ hints ?: string [ ] ;
978+ hintItems ?: Array < { kind : string ; text : string } > ;
979+ } > ( ) ;
980+ expect ( payload . ok ) . toBe ( false ) ;
981+ expect ( payload . error ) . toContain ( "service needs repair" ) ;
982+ expect ( payload . hints ) . toEqual ( [ `${ command } install --force` ] ) ;
983+ expect ( payload . hintItems ) . toEqual ( [ { kind : "install" , text : `${ command } install --force` } ] ) ;
984+
985+ resetLifecycleRuntimeLogs ( ) ;
986+ await expect (
987+ runServiceStart ( {
988+ ...createServiceRunArgs ( ) ,
989+ serviceNoun,
990+ opts : { json : false } ,
991+ } ) ,
992+ ) . rejects . toThrow ( "__exit__:1" ) ;
993+ expect ( lifecycleRuntimeLogs ) . toContain ( `Tip: ${ command } install --force` ) ;
994+ expect ( service . start ) . not . toHaveBeenCalled ( ) ;
995+ } ,
996+ ) ;
954997
955998 it ( "fails start when starting a stopped installed service errors" , async ( ) => {
956999 service . isLoaded . mockResolvedValue ( false ) ;
0 commit comments