@@ -3,11 +3,13 @@ import { beforeEach, describe, expect, it } from "vitest";
33import {
44 cleanupCommandLogMessages ,
55 createCleanupCommandRuntime ,
6+ gatewayService ,
67 prepareLegacyWorkspaceStateReset ,
78 removeLegacyWorkspaceStateForReset ,
89 removeStateAndLinkedPaths ,
910 removeWorkspaceDirs ,
1011 resetCleanupCommandMocks ,
12+ setCleanupNixMode ,
1113 silenceCleanupCommandRuntime ,
1214} from "./cleanup-command.test-support.js" ;
1315
@@ -21,6 +23,98 @@ describe("uninstallCommand", () => {
2123 silenceCleanupCommandRuntime ( runtime ) ;
2224 } ) ;
2325
26+ it . each ( [
27+ {
28+ failure : "inspection fails" ,
29+ arrange : ( ) => gatewayService . isLoaded . mockRejectedValue ( new Error ( "inspection failed" ) ) ,
30+ } ,
31+ {
32+ failure : "stop fails" ,
33+ arrange : ( ) => gatewayService . stop . mockRejectedValue ( new Error ( "stop failed" ) ) ,
34+ } ,
35+ {
36+ failure : "service removal fails" ,
37+ arrange : ( ) => gatewayService . uninstall . mockRejectedValue ( new Error ( "uninstall failed" ) ) ,
38+ } ,
39+ ] ) ( "preserves user data when gateway $failure" , async ( { arrange } ) => {
40+ arrange ( ) ;
41+
42+ await expect (
43+ uninstallCommand ( runtime , {
44+ all : true ,
45+ yes : true ,
46+ nonInteractive : true ,
47+ } ) ,
48+ ) . rejects . toMatchObject ( { name : "ExitError" , code : 1 } ) ;
49+
50+ expect ( removeStateAndLinkedPaths ) . not . toHaveBeenCalled ( ) ;
51+ expect ( removeWorkspaceDirs ) . not . toHaveBeenCalled ( ) ;
52+ expect ( prepareLegacyWorkspaceStateReset ) . not . toHaveBeenCalled ( ) ;
53+ expect ( cleanupCommandLogMessages ( runtime ) ) . not . toContain (
54+ "CLI still installed. Remove via npm/pnpm if desired." ,
55+ ) ;
56+ } ) ;
57+
58+ it ( "preserves user data when Nix owns service lifecycle" , async ( ) => {
59+ setCleanupNixMode ( true ) ;
60+
61+ await expect (
62+ uninstallCommand ( runtime , {
63+ all : true ,
64+ yes : true ,
65+ nonInteractive : true ,
66+ } ) ,
67+ ) . rejects . toMatchObject ( { name : "ExitError" , code : 1 } ) ;
68+
69+ expect ( gatewayService . isLoaded ) . not . toHaveBeenCalled ( ) ;
70+ expect ( gatewayService . stop ) . not . toHaveBeenCalled ( ) ;
71+ expect ( gatewayService . uninstall ) . not . toHaveBeenCalled ( ) ;
72+ expect ( removeStateAndLinkedPaths ) . not . toHaveBeenCalled ( ) ;
73+ expect ( removeWorkspaceDirs ) . not . toHaveBeenCalled ( ) ;
74+ } ) ;
75+
76+ it ( "still removes service registration after a failed gateway stop" , async ( ) => {
77+ gatewayService . stop . mockRejectedValue ( new Error ( "listener still active" ) ) ;
78+
79+ await expect (
80+ uninstallCommand ( runtime , {
81+ service : true ,
82+ yes : true ,
83+ nonInteractive : true ,
84+ } ) ,
85+ ) . rejects . toMatchObject ( { name : "ExitError" , code : 1 } ) ;
86+
87+ expect ( gatewayService . uninstall ) . toHaveBeenCalledOnce ( ) ;
88+ } ) ;
89+
90+ it ( "removes requested data after successful gateway teardown" , async ( ) => {
91+ await uninstallCommand ( runtime , {
92+ all : true ,
93+ yes : true ,
94+ nonInteractive : true ,
95+ } ) ;
96+
97+ expect ( gatewayService . stop ) . toHaveBeenCalledOnce ( ) ;
98+ expect ( gatewayService . uninstall ) . toHaveBeenCalledOnce ( ) ;
99+ expect ( removeStateAndLinkedPaths ) . toHaveBeenCalledOnce ( ) ;
100+ expect ( removeWorkspaceDirs ) . toHaveBeenCalledOnce ( ) ;
101+ } ) ;
102+
103+ it ( "removes an unloaded service definition before deleting user data" , async ( ) => {
104+ gatewayService . isLoaded . mockResolvedValue ( false ) ;
105+
106+ await uninstallCommand ( runtime , {
107+ all : true ,
108+ yes : true ,
109+ nonInteractive : true ,
110+ } ) ;
111+
112+ expect ( gatewayService . stop ) . not . toHaveBeenCalled ( ) ;
113+ expect ( gatewayService . uninstall ) . toHaveBeenCalledOnce ( ) ;
114+ expect ( removeStateAndLinkedPaths ) . toHaveBeenCalledOnce ( ) ;
115+ expect ( removeWorkspaceDirs ) . toHaveBeenCalledOnce ( ) ;
116+ } ) ;
117+
24118 it ( "recommends creating a backup before removing state or workspaces" , async ( ) => {
25119 await uninstallCommand ( runtime , {
26120 state : true ,
0 commit comments