@@ -4,6 +4,8 @@ import type { OpenClawConfig } from "../../config/config.js";
44import type { GatewayServiceControlArgs } from "../../daemon/service-types.js" ;
55import type { GatewayService } from "../../daemon/service.js" ;
66import {
7+ createGatewayServiceRunArgs as createServiceRunArgs ,
8+ createGatewayUninstallArgs ,
79 lifecycleTestRuntime ,
810 resetLifecycleRuntimeLogs ,
911 resetLifecycleServiceMocks ,
@@ -71,23 +73,14 @@ vi.mock("./lifecycle-audit.js", () => ({
7173let runServiceRestart : typeof import ( "./lifecycle-core.js" ) . runServiceRestart ;
7274let runServiceStart : typeof import ( "./lifecycle-core.js" ) . runServiceStart ;
7375let runServiceStop : typeof import ( "./lifecycle-core.js" ) . runServiceStop ;
76+ let runServiceUninstall : typeof import ( "./lifecycle-core.js" ) . runServiceUninstall ;
7477
7578// oxlint-disable-next-line typescript/no-unnecessary-type-parameters -- Test helper lets assertions ascribe logged JSON shape.
7679function readJsonLog < T extends object > ( ) {
7780 const jsonLine = lifecycleRuntimeLogs . find ( ( line ) => line . trim ( ) . startsWith ( "{" ) ) ;
7881 return JSON . parse ( jsonLine ?? "{}" ) as T ;
7982}
8083
81- function createServiceRunArgs ( checkTokenDrift ?: boolean ) {
82- return {
83- serviceNoun : "Gateway" ,
84- service,
85- renderStartHints : ( ) => [ ] ,
86- opts : { json : true as const } ,
87- ...( checkTokenDrift ? { checkTokenDrift } : { } ) ,
88- } ;
89- }
90-
9184function stubConfigSecretRefGatewayToken ( ) {
9285 loadConfig . mockReturnValue ( {
9386 secrets : {
@@ -140,7 +133,8 @@ function expectUnsupportedServiceCheckFailure() {
140133
141134describe ( "runServiceRestart token drift" , ( ) => {
142135 beforeAll ( async ( ) => {
143- ( { runServiceRestart, runServiceStart, runServiceStop } = await import ( "./lifecycle-core.js" ) ) ;
136+ ( { runServiceRestart, runServiceStart, runServiceStop, runServiceUninstall } =
137+ await import ( "./lifecycle-core.js" ) ) ;
144138 } ) ;
145139
146140 beforeEach ( ( ) => {
@@ -234,6 +228,58 @@ describe("runServiceRestart token drift", () => {
234228 expectUnsupportedServiceCheckFailure ( ) ;
235229 } ) ;
236230
231+ it . each ( [
232+ {
233+ name : "initial uninstall inspection" ,
234+ arrange : ( ) => service . isLoaded . mockRejectedValue ( new Error ( "initial inspection failed" ) ) ,
235+ run : ( ) => runServiceUninstall ( createGatewayUninstallArgs ( ) ) ,
236+ action : "uninstall" ,
237+ detail : "initial inspection failed" ,
238+ stopCalls : 0 ,
239+ uninstallCalls : 0 ,
240+ } ,
241+ {
242+ name : "post-uninstall verification" ,
243+ arrange : ( ) =>
244+ service . isLoaded
245+ . mockResolvedValueOnce ( false )
246+ . mockRejectedValueOnce ( new Error ( "uninstall verification failed" ) ) ,
247+ run : ( ) => runServiceUninstall ( createGatewayUninstallArgs ( ) ) ,
248+ action : "uninstall" ,
249+ detail : "uninstall verification failed" ,
250+ stopCalls : 0 ,
251+ uninstallCalls : 1 ,
252+ } ,
253+ {
254+ name : "post-stop verification" ,
255+ arrange : ( ) =>
256+ service . isLoaded
257+ . mockResolvedValueOnce ( true )
258+ . mockRejectedValueOnce ( new Error ( "stop verification failed" ) ) ,
259+ run : ( ) => runServiceStop ( { serviceNoun : "Gateway" , service, opts : { json : true } } ) ,
260+ action : "stop" ,
261+ detail : "stop verification failed" ,
262+ stopCalls : 1 ,
263+ uninstallCalls : 0 ,
264+ } ,
265+ ] ) ( "fails $name without reporting false absence" , async ( testCase ) => {
266+ testCase . arrange ( ) ;
267+
268+ await expect ( testCase . run ( ) ) . rejects . toThrow ( "__exit__:1" ) ;
269+
270+ expect (
271+ readJsonLog < { action ?: string ; ok ?: boolean ; result ?: string ; error ?: string } > ( ) ,
272+ ) . toEqual (
273+ expect . objectContaining ( {
274+ action : testCase . action ,
275+ ok : false ,
276+ error : expect . stringContaining ( testCase . detail ) ,
277+ } ) ,
278+ ) ;
279+ expect ( service . stop ) . toHaveBeenCalledTimes ( testCase . stopCalls ) ;
280+ expect ( service . uninstall ) . toHaveBeenCalledTimes ( testCase . uninstallCalls ) ;
281+ } ) ;
282+
237283 it ( "fails restart with the container hint when no service is installed" , async ( ) => {
238284 service . isLoaded . mockResolvedValue ( false ) ;
239285 service . readCommand . mockResolvedValue ( null ) ;
@@ -321,7 +367,7 @@ describe("runServiceRestart token drift", () => {
321367 ) ;
322368 } ) ;
323369
324- it ( "restarts an installed system-scope service when its loaded-state probe is unavailable " , async ( ) => {
370+ it ( "fails restart when an installed service cannot be inspected " , async ( ) => {
325371 service . isLoaded . mockRejectedValue (
326372 new Error (
327373 "systemctl is-enabled unavailable: Command failed during launch or output capture (EACCES)" ,
@@ -337,14 +383,14 @@ describe("runServiceRestart token drift", () => {
337383 service : { ...service , hasInstalledDefinition } as GatewayService ,
338384 postRestartCheck,
339385 } ) ,
340- ) . resolves . toBe ( true ) ;
386+ ) . rejects . toThrow ( "__exit__:1" ) ;
341387
342- expect ( hasInstalledDefinition ) . toHaveBeenCalledWith ( { env : process . env } ) ;
343- expect ( service . restart ) . toHaveBeenCalledTimes ( 1 ) ;
344- expect ( postRestartCheck ) . toHaveBeenCalledTimes ( 1 ) ;
345- expect ( readJsonLog < { ok ?: boolean ; result ?: string } > ( ) ) . toMatchObject ( {
346- ok : true ,
347- result : "restarted" ,
388+ expect ( hasInstalledDefinition ) . not . toHaveBeenCalled ( ) ;
389+ expect ( service . restart ) . not . toHaveBeenCalled ( ) ;
390+ expect ( postRestartCheck ) . not . toHaveBeenCalled ( ) ;
391+ expect ( readJsonLog < { ok ?: boolean ; error ?: string } > ( ) ) . toMatchObject ( {
392+ ok : false ,
393+ error : expect . stringContaining ( "systemctl is-enabled unavailable" ) ,
348394 } ) ;
349395 } ) ;
350396
0 commit comments