1+ import fs from "node:fs" ;
12import path from "node:path" ;
2- import { afterEach , expect , it , vi } from "vitest" ;
3+ import { afterEach , beforeEach , expect , it , vi } from "vitest" ;
34import { resolveConfigWidePluginMetadataSnapshot } from "../config/io.plugin-metadata.js" ;
45import type { OpenClawConfig } from "../config/types.openclaw.js" ;
56import {
@@ -8,9 +9,12 @@ import {
89} from "../state/openclaw-state-db.js" ;
910import { setGatewayPluginMetadataSnapshot } from "./current-plugin-metadata-snapshot.js" ;
1011import { getGatewayPluginMetadataSnapshot } from "./current-plugin-metadata-state.js" ;
12+ import { resolvePluginInstallDir } from "./install-paths.js" ;
1113import { writePersistedInstalledPluginIndex } from "./installed-plugin-index-store-write.js" ;
14+ import { readPersistedInstalledPluginIndex } from "./installed-plugin-index-store.js" ;
1215import { loadInstalledPluginIndex } from "./installed-plugin-index.js" ;
1316import { clearPluginMetadataLifecycleCaches } from "./plugin-metadata-lifecycle.js" ;
17+ import { loadPluginMetadataSnapshot } from "./plugin-metadata-snapshot.js" ;
1418import { createColdPluginFixture } from "./test-helpers/cold-plugin-fixtures.js" ;
1519import {
1620 cleanupTrackedTempDirs ,
@@ -34,12 +38,19 @@ vi.mock("./official-external-plugin-catalog.js", async (importOriginal) => ({
3438 } ) ,
3539} ) ) ;
3640
37- const { listManagedPlugins, refreshManagedPluginMetadata, setManagedPluginEnabled } =
38- await import ( "./management-service.js" ) ;
41+ const {
42+ listManagedPlugins,
43+ refreshManagedPluginMetadata,
44+ setManagedPluginEnabled,
45+ uninstallManagedPlugin,
46+ } = await import ( "./management-service.js" ) ;
3947const roots : string [ ] = [ ] ;
4048
41- afterEach ( ( ) => {
49+ beforeEach ( ( ) => {
4250 clearPluginMetadataLifecycleCaches ( ) ;
51+ } ) ;
52+
53+ afterEach ( ( ) => {
4354 closeOpenClawStateDatabaseForTest ( ) ;
4455 cleanupTrackedTempDirs ( roots ) ;
4556 vi . unstubAllEnvs ( ) ;
@@ -89,6 +100,75 @@ it("refreshes an externally changed install ledger before publishing management
89100 expect ( boot . byPluginId . has ( fixture . pluginId ) ) . toBe ( false ) ;
90101} ) ;
91102
103+ it ( "removes an npm-pack plugin from management inventory without replacing Gateway metadata" , async ( ) => {
104+ const root = makeTrackedTempDir ( "managed-npm-pack-uninstall" , roots ) ;
105+ const stateDir = path . join ( root , "state" ) ;
106+ const packageName = "@example/tgz-visible" ;
107+ const pluginRoot = resolvePluginInstallDir ( "tgz-visible" , path . join ( stateDir , "extensions" ) ) ;
108+ mkdirSafeDir ( pluginRoot ) ;
109+ const fixture = createColdPluginFixture ( {
110+ rootDir : pluginRoot ,
111+ pluginId : "tgz-visible" ,
112+ packageName,
113+ } ) ;
114+ vi . stubEnv ( "OPENCLAW_STATE_DIR" , stateDir ) ;
115+ vi . stubEnv ( "OPENCLAW_DISABLE_BUNDLED_PLUGINS" , "1" ) ;
116+ let config : OpenClawConfig = {
117+ plugins : { entries : { [ fixture . pluginId ] : { enabled : true } } } ,
118+ } ;
119+ const installRecord = {
120+ source : "npm" ,
121+ spec : `${ packageName } @1.0.0` ,
122+ sourcePath : path . join ( root , `${ fixture . pluginId } .tgz` ) ,
123+ installPath : pluginRoot ,
124+ artifactKind : "npm-pack" ,
125+ artifactFormat : "tgz" ,
126+ } as const ;
127+ configIo . read . mockImplementation ( async ( ) => ( {
128+ snapshot : {
129+ valid : true ,
130+ parsed : config ,
131+ path : path . join ( stateDir , "openclaw.json" ) ,
132+ sourceConfig : config ,
133+ hash : "base-hash" ,
134+ } ,
135+ writeOptions : { expectedConfigPath : path . join ( stateDir , "openclaw.json" ) } ,
136+ } ) ) ;
137+ configIo . write . mockImplementation ( async ( { nextConfig } : { nextConfig : OpenClawConfig } ) => {
138+ config = nextConfig ;
139+ } ) ;
140+ await writePersistedInstalledPluginIndex (
141+ loadInstalledPluginIndex ( {
142+ config,
143+ env : process . env ,
144+ installRecords : { [ fixture . pluginId ] : installRecord } ,
145+ } ) ,
146+ ) ;
147+ const boot = loadPluginMetadataSnapshot ( {
148+ config,
149+ env : process . env ,
150+ preferPersisted : false ,
151+ } ) ;
152+ setGatewayPluginMetadataSnapshot ( boot , { config, env : process . env } ) ;
153+ expect ( ( await listManagedPlugins ( { config } ) ) . plugins ) . toContainEqual (
154+ expect . objectContaining ( { id : fixture . pluginId , installed : true } ) ,
155+ ) ;
156+
157+ await uninstallManagedPlugin ( { pluginId : fixture . pluginId } ) ;
158+
159+ expect ( fs . existsSync ( pluginRoot ) ) . toBe ( false ) ;
160+ expect (
161+ ( await readPersistedInstalledPluginIndex ( ) ) ?. plugins . some (
162+ ( plugin ) => plugin . pluginId === fixture . pluginId ,
163+ ) ,
164+ ) . toBe ( false ) ;
165+ expect ( ( await listManagedPlugins ( { config } ) ) . plugins ) . not . toContainEqual (
166+ expect . objectContaining ( { id : fixture . pluginId } ) ,
167+ ) ;
168+ expect ( getGatewayPluginMetadataSnapshot ( ) ) . toBe ( boot ) ;
169+ expect ( boot . byPluginId . has ( fixture . pluginId ) ) . toBe ( true ) ;
170+ } ) ;
171+
92172it . each ( [ undefined , "main" ] ) (
93173 "toggles a listed secondary-workspace plugin with system owner %s" ,
94174 async ( systemAgentId ) => {
0 commit comments