|
| 1 | +import { describe, test, expect } from "bun:test"; |
| 2 | +import { dedupePluginModules, type PluginModule } from "./loader.js"; |
| 3 | +import { isPluginModuleEnabled } from "./register.js"; |
| 4 | + |
| 5 | +function repoDefaultEnabled(id: string): PluginModule { |
| 6 | + return { |
| 7 | + manifest: { id, name: id, kind: "agent", defaultEnabled: true }, |
| 8 | + origin: "repo", |
| 9 | + }; |
| 10 | +} |
| 11 | + |
| 12 | +function userInstall(id: string): PluginModule { |
| 13 | + return { |
| 14 | + manifest: { id, name: id, kind: "agent" }, |
| 15 | + origin: "user", |
| 16 | + source: "claude", |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +describe("dedupePluginModules", () => { |
| 21 | + test("last occurrence wins for content", () => { |
| 22 | + const repo = repoDefaultEnabled("scout"); |
| 23 | + const user = userInstall("scout"); |
| 24 | + const [result] = dedupePluginModules([repo, user]); |
| 25 | + expect(result).toMatchObject({ origin: "user", source: "claude" }); |
| 26 | + }); |
| 27 | + |
| 28 | + // CL-6716: a later non-repo install with the same id as a repo |
| 29 | + // defaultEnabled plugin must not silently turn the bundled default off. |
| 30 | + test("stamps shadowedRepoDefaultEnabled when a non-repo module shadows a repo defaultEnabled id", () => { |
| 31 | + const repo = repoDefaultEnabled("scout"); |
| 32 | + const user = userInstall("scout"); |
| 33 | + const [result] = dedupePluginModules([repo, user]); |
| 34 | + expect(result!.shadowedRepoDefaultEnabled).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + test("does not stamp shadowedRepoDefaultEnabled when the repo module wasn't defaultEnabled", () => { |
| 38 | + const repo: PluginModule = { manifest: { id: "scout", name: "scout", kind: "agent" }, origin: "repo" }; |
| 39 | + const user = userInstall("scout"); |
| 40 | + const [result] = dedupePluginModules([repo, user]); |
| 41 | + expect(result!.shadowedRepoDefaultEnabled).toBeUndefined(); |
| 42 | + }); |
| 43 | + |
| 44 | + test("does not stamp unrelated ids", () => { |
| 45 | + const repo = repoDefaultEnabled("scout"); |
| 46 | + const other = userInstall("other"); |
| 47 | + const result = dedupePluginModules([repo, other]); |
| 48 | + expect(result.find((m) => m.manifest?.id === "other")!.shadowedRepoDefaultEnabled).toBeUndefined(); |
| 49 | + }); |
| 50 | + |
| 51 | + test("propagates the shadow stamp through a chain of later installs", () => { |
| 52 | + const repo = repoDefaultEnabled("scout"); |
| 53 | + const user = userInstall("scout"); |
| 54 | + const path: PluginModule = { manifest: { id: "scout", name: "scout", kind: "agent" }, origin: "path" }; |
| 55 | + const [result] = dedupePluginModules([repo, user, path]); |
| 56 | + expect(result).toMatchObject({ origin: "path" }); |
| 57 | + expect(result!.shadowedRepoDefaultEnabled).toBe(true); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe("isPluginModuleEnabled with dedupe shadowing", () => { |
| 62 | + test("a same-id later install stays enabled by default after shadowing a repo defaultEnabled plugin", () => { |
| 63 | + const repo = repoDefaultEnabled("scout"); |
| 64 | + const user = userInstall("scout"); |
| 65 | + const [survivor] = dedupePluginModules([repo, user]); |
| 66 | + expect(isPluginModuleEnabled(survivor!, {})).toBe(true); |
| 67 | + }); |
| 68 | + |
| 69 | + test("an explicit disable in settings still wins over the preserved default-on", () => { |
| 70 | + const repo = repoDefaultEnabled("scout"); |
| 71 | + const user = userInstall("scout"); |
| 72 | + const [survivor] = dedupePluginModules([repo, user]); |
| 73 | + expect(isPluginModuleEnabled(survivor!, { scout: { enabled: false } })).toBe(false); |
| 74 | + }); |
| 75 | + |
| 76 | + test("without dedupe shadowing, a plain user-origin module needs an explicit enable", () => { |
| 77 | + const user = userInstall("scout"); |
| 78 | + expect(isPluginModuleEnabled(user, {})).toBe(false); |
| 79 | + }); |
| 80 | +}); |
0 commit comments