1+ import fs from "node:fs/promises" ;
2+ import os from "node:os" ;
3+ import path from "node:path" ;
14import { beforeEach , describe , expect , it , vi } from "vitest" ;
5+ import { resolveDefaultPluginNpmDir , resolvePluginNpmProjectDir } from "./install-paths.js" ;
26
37const mocks = vi . hoisted ( ( ) => ( {
48 applyUninstall : vi . fn ( ) ,
59 clawhubInstall : vi . fn ( ) ,
610 installRecords : vi . fn ( ) ,
11+ npmInstall : vi . fn ( ) ,
712 pathInstall : vi . fn ( ) ,
813 persistInstall : vi . fn ( ) ,
914 planUninstall : vi . fn ( ) ,
@@ -15,6 +20,7 @@ vi.mock("./clawhub.js", () => ({
1520
1621vi . mock ( "./install.js" , async ( importOriginal ) => ( {
1722 ...( await importOriginal < typeof import ( "./install.js" ) > ( ) ) ,
23+ installPluginFromNpmSpec : ( ...args : unknown [ ] ) => mocks . npmInstall ( ...args ) ,
1824 installPluginFromPath : ( ...args : unknown [ ] ) => mocks . pathInstall ( ...args ) ,
1925} ) ) ;
2026
@@ -35,6 +41,7 @@ vi.mock("./uninstall.js", async (importOriginal) => ({
3541} ) ) ;
3642
3743const { installManagedPluginSource } = await import ( "./management-service.js" ) ;
44+ const actualUninstall = await vi . importActual < typeof import ( "./uninstall.js" ) > ( "./uninstall.js" ) ;
3845
3946function installPersistSnapshot ( ) {
4047 return {
@@ -97,6 +104,58 @@ describe("managed plugin install compensation", () => {
97104 expect ( mocks . applyUninstall ) . toHaveBeenCalledWith ( { target : targetDir } ) ;
98105 } ) ;
99106
107+ it ( "removes a planner-validated isolated npm project after persistence conflicts" , async ( ) => {
108+ const home = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-managed-npm-conflict-" ) ) ;
109+ const env = { HOME : home } ;
110+ const packageName = "@openclaw/demo" ;
111+ const npmRoot = resolvePluginNpmProjectDir ( {
112+ npmDir : resolveDefaultPluginNpmDir ( env ) ,
113+ packageName,
114+ } ) ;
115+ const targetDir = path . join ( npmRoot , "node_modules" , "@openclaw" , "demo" ) ;
116+ const packArchive = path . join ( npmRoot , "_openclaw-pack-archives" , "demo.tgz" ) ;
117+ const conflict = new Error ( "config changed during npm plugin install" ) ;
118+
119+ try {
120+ await fs . mkdir ( targetDir , { recursive : true } ) ;
121+ await fs . mkdir ( path . dirname ( packArchive ) , { recursive : true } ) ;
122+ await fs . writeFile ( packArchive , "packed plugin" ) ;
123+ mocks . npmInstall . mockResolvedValue ( {
124+ ok : true ,
125+ pluginId : "demo" ,
126+ targetDir,
127+ extensions : [ "index.js" ] ,
128+ manifestName : packageName ,
129+ } ) ;
130+ mocks . persistInstall . mockRejectedValue ( conflict ) ;
131+ mocks . planUninstall . mockImplementation ( ( params ) =>
132+ actualUninstall . planPluginUninstall (
133+ params as Parameters < typeof actualUninstall . planPluginUninstall > [ 0 ] ,
134+ ) ,
135+ ) ;
136+ mocks . applyUninstall . mockImplementation ( async ( removal : { target : string } ) => {
137+ await fs . rm ( removal . target , { recursive : true , force : true } ) ;
138+ return { directoryRemoved : true , warnings : [ ] } ;
139+ } ) ;
140+
141+ await expect (
142+ installManagedPluginSource ( {
143+ request : { source : "npm" , spec : packageName , mode : "install" } ,
144+ snapshot : installPersistSnapshot ( ) ,
145+ env,
146+ } ) ,
147+ ) . rejects . toBe ( conflict ) ;
148+
149+ expect ( mocks . applyUninstall ) . toHaveBeenCalledWith ( {
150+ target : npmRoot ,
151+ cleanup : { kind : "npm" , npmRoot, packageName } ,
152+ } ) ;
153+ await expect ( fs . access ( npmRoot ) ) . rejects . toMatchObject ( { code : "ENOENT" } ) ;
154+ } finally {
155+ await fs . rm ( home , { recursive : true , force : true } ) ;
156+ }
157+ } ) ;
158+
100159 it ( "never deletes an operator-owned source when link persistence fails" , async ( ) => {
101160 const env = { HOME : "/tmp/openclaw-managed-link-conflict-home" } ;
102161 const sourcePath = "/tmp/operator-owned-plugin-source" ;
0 commit comments