|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | +import { mkdir, mkdtemp, rm, stat, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | + |
| 6 | +import type { PluginConfig } from "../config/settings.js"; |
| 7 | +import { |
| 8 | + classifyPluginRemove, |
| 9 | + deleteOwnedPluginDir, |
| 10 | + disableBundledPluginSettings, |
| 11 | + isOwnedDiskInstall, |
| 12 | + nextPluginPathsAfterRemove, |
| 13 | + ownedDiskOriginRoot, |
| 14 | + projectPluginsRoot, |
| 15 | + userPluginsRoot, |
| 16 | +} from "./uninstall.js"; |
| 17 | + |
| 18 | +const temps: string[] = []; |
| 19 | + |
| 20 | +afterEach(async () => { |
| 21 | + await Promise.all(temps.splice(0).map((d) => rm(d, { recursive: true, force: true }))); |
| 22 | +}); |
| 23 | + |
| 24 | +async function tempDir(prefix: string): Promise<string> { |
| 25 | + const dir = await mkdtemp(join(tmpdir(), prefix)); |
| 26 | + temps.push(dir); |
| 27 | + return dir; |
| 28 | +} |
| 29 | + |
| 30 | +async function exists(path: string): Promise<boolean> { |
| 31 | + try { |
| 32 | + await stat(path); |
| 33 | + return true; |
| 34 | + } catch { |
| 35 | + return false; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +describe("deleteOwnedPluginDir", () => { |
| 40 | + test("owned dir under fake user root is deleted", async () => { |
| 41 | + const home = await tempDir("uninstall-user-"); |
| 42 | + const root = userPluginsRoot(home); |
| 43 | + const plugin = join(root, "exa"); |
| 44 | + await mkdir(plugin, { recursive: true }); |
| 45 | + await writeFile(join(plugin, "manifest.json"), "{}"); |
| 46 | + const result = await deleteOwnedPluginDir({ |
| 47 | + pluginPath: plugin, |
| 48 | + originRoot: root, |
| 49 | + claudeRoot: join(home, ".claude"), |
| 50 | + }); |
| 51 | + expect(result).toEqual({ ok: true }); |
| 52 | + expect(await exists(plugin)).toBe(false); |
| 53 | + expect(await exists(root)).toBe(true); |
| 54 | + }); |
| 55 | + |
| 56 | + test("path outside origin root is refused", async () => { |
| 57 | + const home = await tempDir("uninstall-outside-"); |
| 58 | + const root = userPluginsRoot(home); |
| 59 | + await mkdir(root, { recursive: true }); |
| 60 | + const outside = join(home, "other", "exa"); |
| 61 | + await mkdir(outside, { recursive: true }); |
| 62 | + const result = await deleteOwnedPluginDir({ |
| 63 | + pluginPath: outside, |
| 64 | + originRoot: root, |
| 65 | + claudeRoot: join(home, ".claude"), |
| 66 | + }); |
| 67 | + expect(result.ok).toBe(false); |
| 68 | + expect(await exists(outside)).toBe(true); |
| 69 | + }); |
| 70 | + |
| 71 | + test("path equal to plugins root is refused", async () => { |
| 72 | + const home = await tempDir("uninstall-root-"); |
| 73 | + const root = userPluginsRoot(home); |
| 74 | + await mkdir(root, { recursive: true }); |
| 75 | + const result = await deleteOwnedPluginDir({ |
| 76 | + pluginPath: root, |
| 77 | + originRoot: root, |
| 78 | + claudeRoot: join(home, ".claude"), |
| 79 | + }); |
| 80 | + expect(result.ok).toBe(false); |
| 81 | + expect(await exists(root)).toBe(true); |
| 82 | + }); |
| 83 | + |
| 84 | + test("path under .claude/plugins is refused", async () => { |
| 85 | + const home = await tempDir("uninstall-claude-"); |
| 86 | + const root = userPluginsRoot(home); |
| 87 | + const claudeRoot = join(home, ".claude"); |
| 88 | + const plugin = join(claudeRoot, "plugins", "market"); |
| 89 | + await mkdir(root, { recursive: true }); |
| 90 | + await mkdir(plugin, { recursive: true }); |
| 91 | + const result = await deleteOwnedPluginDir({ |
| 92 | + pluginPath: plugin, |
| 93 | + originRoot: root, |
| 94 | + claudeRoot, |
| 95 | + }); |
| 96 | + expect(result.ok).toBe(false); |
| 97 | + if (!result.ok) expect(result.message).toContain("~/.claude"); |
| 98 | + expect(await exists(plugin)).toBe(true); |
| 99 | + }); |
| 100 | + |
| 101 | + test("project: delete only inside <cwd>/.corbits/plugins/", async () => { |
| 102 | + const cwd = await tempDir("uninstall-project-"); |
| 103 | + const root = projectPluginsRoot(cwd); |
| 104 | + const inside = join(root, "local"); |
| 105 | + const outside = join(cwd, "not-plugins", "local"); |
| 106 | + await mkdir(inside, { recursive: true }); |
| 107 | + await mkdir(outside, { recursive: true }); |
| 108 | + const claudeRoot = join(cwd, "home", ".claude"); |
| 109 | + expect( |
| 110 | + await deleteOwnedPluginDir({ pluginPath: inside, originRoot: root, claudeRoot }), |
| 111 | + ).toEqual({ ok: true }); |
| 112 | + expect(await exists(inside)).toBe(false); |
| 113 | + const refused = await deleteOwnedPluginDir({ |
| 114 | + pluginPath: outside, |
| 115 | + originRoot: root, |
| 116 | + claudeRoot, |
| 117 | + }); |
| 118 | + expect(refused.ok).toBe(false); |
| 119 | + expect(await exists(outside)).toBe(true); |
| 120 | + }); |
| 121 | + |
| 122 | + test("already-missing path returns { ok: true }", async () => { |
| 123 | + const home = await tempDir("uninstall-missing-"); |
| 124 | + const root = userPluginsRoot(home); |
| 125 | + await mkdir(root, { recursive: true }); |
| 126 | + const missing = join(root, "gone"); |
| 127 | + const result = await deleteOwnedPluginDir({ |
| 128 | + pluginPath: missing, |
| 129 | + originRoot: root, |
| 130 | + claudeRoot: join(home, ".claude"), |
| 131 | + }); |
| 132 | + expect(result).toEqual({ ok: true }); |
| 133 | + expect(await exists(missing)).toBe(false); |
| 134 | + }); |
| 135 | +}); |
| 136 | + |
| 137 | +describe("isOwnedDiskInstall", () => { |
| 138 | + test("project path is owned only for the passed cwd", () => { |
| 139 | + const cwd = "/tmp/project-a"; |
| 140 | + const pluginPath = join(projectPluginsRoot(cwd), "local"); |
| 141 | + expect(isOwnedDiskInstall({ origin: "project", pluginPath, home: "/tmp/home", cwd })).toBe( |
| 142 | + true, |
| 143 | + ); |
| 144 | + expect( |
| 145 | + isOwnedDiskInstall({ |
| 146 | + origin: "project", |
| 147 | + pluginPath, |
| 148 | + home: "/tmp/home", |
| 149 | + cwd: "/tmp/other", |
| 150 | + }), |
| 151 | + ).toBe(false); |
| 152 | + }); |
| 153 | + |
| 154 | + test("~/.claude is never owned", () => { |
| 155 | + const home = "/tmp/home"; |
| 156 | + const cwd = "/tmp/cwd"; |
| 157 | + const pluginPath = join(home, ".claude", "plugins", "exa"); |
| 158 | + expect(isOwnedDiskInstall({ origin: "user", pluginPath, home, cwd })).toBe(false); |
| 159 | + expect(isOwnedDiskInstall({ origin: "project", pluginPath, home, cwd })).toBe(false); |
| 160 | + }); |
| 161 | + |
| 162 | + test("path origin under userPluginsRoot is owned", () => { |
| 163 | + const home = "/tmp/home"; |
| 164 | + const cwd = "/tmp/cwd"; |
| 165 | + const pluginPath = join(userPluginsRoot(home), "exa"); |
| 166 | + expect(isOwnedDiskInstall({ origin: "path", pluginPath, home, cwd })).toBe(true); |
| 167 | + expect(ownedDiskOriginRoot({ pluginPath, home, cwd })).toBe(userPluginsRoot(home)); |
| 168 | + }); |
| 169 | + |
| 170 | + test("path origin under /tmp is not owned", () => { |
| 171 | + const home = "/tmp/home"; |
| 172 | + const cwd = "/tmp/cwd"; |
| 173 | + const pluginPath = "/tmp/elsewhere/my-plugin"; |
| 174 | + expect(isOwnedDiskInstall({ origin: "path", pluginPath, home, cwd })).toBe(false); |
| 175 | + expect(ownedDiskOriginRoot({ pluginPath, home, cwd })).toBeUndefined(); |
| 176 | + }); |
| 177 | +}); |
| 178 | + |
| 179 | +describe("classifyPluginRemove", () => { |
| 180 | + test("origin and owned classify into one shared action", () => { |
| 181 | + expect(classifyPluginRemove({ origin: "repo", owned: false })).toBe("disable-bundled"); |
| 182 | + expect(classifyPluginRemove({ origin: "repo", owned: true })).toBe("disable-bundled"); |
| 183 | + expect(classifyPluginRemove({ origin: "user", owned: false })).toBe("disable-unowned-user"); |
| 184 | + expect(classifyPluginRemove({ origin: "user", owned: true })).toBe("delete-owned"); |
| 185 | + expect(classifyPluginRemove({ origin: "project", owned: true })).toBe("delete-owned"); |
| 186 | + expect(classifyPluginRemove({ origin: "project", owned: false })).toBe("cannot"); |
| 187 | + expect(classifyPluginRemove({ origin: "path", owned: false })).toBe("remove-path"); |
| 188 | + expect(classifyPluginRemove({ origin: "path", owned: true })).toBe("delete-owned"); |
| 189 | + }); |
| 190 | +}); |
| 191 | + |
| 192 | +describe("plugin remove settings policy", () => { |
| 193 | + test("owned remove writes enabled:false and keeps plugins[id]", () => { |
| 194 | + const plugins: Record<string, PluginConfig> = { |
| 195 | + exa: { enabled: true, credentials: { apiKey: "k" } }, |
| 196 | + other: { enabled: true }, |
| 197 | + }; |
| 198 | + const next = disableBundledPluginSettings(plugins, "exa"); |
| 199 | + expect(next.exa?.enabled).toBe(false); |
| 200 | + expect(next.exa?.credentials).toEqual({ apiKey: "k" }); |
| 201 | + expect(next.other).toEqual({ enabled: true }); |
| 202 | + expect("exa" in next).toBe(true); |
| 203 | + }); |
| 204 | + |
| 205 | + test("repo writes enabled:false", () => { |
| 206 | + const plugins: Record<string, PluginConfig> = { |
| 207 | + "corbits-skills": { enabled: true }, |
| 208 | + }; |
| 209 | + expect(disableBundledPluginSettings(plugins, "corbits-skills")).toEqual({ |
| 210 | + "corbits-skills": { enabled: false }, |
| 211 | + }); |
| 212 | + expect(disableBundledPluginSettings({}, "corbits-skills")).toEqual({ |
| 213 | + "corbits-skills": { enabled: false }, |
| 214 | + }); |
| 215 | + }); |
| 216 | + |
| 217 | + test("disableBundledPluginSettings keeps plugins[id].enabled === false", () => { |
| 218 | + const plugins: Record<string, PluginConfig> = { |
| 219 | + exa: { enabled: true, credentials: { apiKey: "k" } }, |
| 220 | + }; |
| 221 | + const next = disableBundledPluginSettings(plugins, "exa"); |
| 222 | + expect(next.exa?.enabled).toBe(false); |
| 223 | + expect(next.exa?.credentials).toEqual({ apiKey: "k" }); |
| 224 | + expect("exa" in next).toBe(true); |
| 225 | + }); |
| 226 | + |
| 227 | + test("path drops unique pluginPaths and keeps a shared marketplace root", async () => { |
| 228 | + const unique = await nextPluginPathsAfterRemove({ |
| 229 | + pluginPaths: ["/tmp/my-plugin"], |
| 230 | + pluginPath: "/tmp/my-plugin", |
| 231 | + cwd: "/tmp", |
| 232 | + otherLivePluginPaths: [], |
| 233 | + expandMembers: async (abs) => [abs], |
| 234 | + }); |
| 235 | + expect(unique.pluginPaths).toEqual([]); |
| 236 | + expect(unique.keptSharedRoot).toBe(false); |
| 237 | + |
| 238 | + const market = "/tmp/market"; |
| 239 | + const a = join(market, "plugins", "a"); |
| 240 | + const b = join(market, "plugins", "b"); |
| 241 | + const shared = await nextPluginPathsAfterRemove({ |
| 242 | + pluginPaths: [market, "/tmp/other"], |
| 243 | + pluginPath: a, |
| 244 | + cwd: "/tmp", |
| 245 | + otherLivePluginPaths: [b], |
| 246 | + expandMembers: async (abs) => (abs === market ? [a, b] : [abs]), |
| 247 | + }); |
| 248 | + expect(shared.pluginPaths).toEqual([market, "/tmp/other"]); |
| 249 | + expect(shared.keptSharedRoot).toBe(true); |
| 250 | + }); |
| 251 | +}); |
0 commit comments