|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { ChannelPluginCatalogEntry } from "../../channels/plugins/catalog.js"; |
| 3 | + |
| 4 | +const listRawChannelPluginCatalogEntries = vi.hoisted(() => |
| 5 | + vi.fn((_opts?: unknown): ChannelPluginCatalogEntry[] => []), |
| 6 | +); |
| 7 | +const getChannelPluginCatalogEntry = vi.hoisted(() => |
| 8 | + vi.fn((_id?: unknown, _opts?: unknown): ChannelPluginCatalogEntry | undefined => undefined), |
| 9 | +); |
| 10 | +const applyPluginAutoEnable = vi.hoisted(() => |
| 11 | + vi.fn(({ config }: { config: unknown }) => ({ |
| 12 | + config: config as never, |
| 13 | + changes: [] as string[], |
| 14 | + autoEnabledReasons: {}, |
| 15 | + })), |
| 16 | +); |
| 17 | + |
| 18 | +vi.mock("../../channels/plugins/catalog.js", () => ({ |
| 19 | + listRawChannelPluginCatalogEntries: (opts?: unknown) => listRawChannelPluginCatalogEntries(opts), |
| 20 | + getChannelPluginCatalogEntry: (id?: unknown, opts?: unknown) => |
| 21 | + getChannelPluginCatalogEntry(id, opts), |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock("../../config/plugin-auto-enable.js", () => ({ |
| 25 | + applyPluginAutoEnable: (args: unknown) => |
| 26 | + applyPluginAutoEnable(args as { config: unknown; env?: NodeJS.ProcessEnv }), |
| 27 | +})); |
| 28 | + |
| 29 | +import { |
| 30 | + getTrustedChannelPluginCatalogEntry, |
| 31 | + listSetupDiscoveryChannelPluginCatalogEntries, |
| 32 | + listTrustedChannelPluginCatalogEntries, |
| 33 | +} from "./trusted-catalog.js"; |
| 34 | + |
| 35 | +function createCatalogEntry(params: { |
| 36 | + id: string; |
| 37 | + pluginId: string; |
| 38 | + origin?: "workspace" | "bundled"; |
| 39 | +}): ChannelPluginCatalogEntry { |
| 40 | + return { |
| 41 | + id: params.id, |
| 42 | + pluginId: params.pluginId, |
| 43 | + origin: params.origin, |
| 44 | + meta: { |
| 45 | + id: params.id, |
| 46 | + label: params.id, |
| 47 | + selectionLabel: params.id, |
| 48 | + docsPath: `/channels/${params.id}`, |
| 49 | + blurb: `${params.id} channel`, |
| 50 | + }, |
| 51 | + install: { |
| 52 | + npmSpec: params.pluginId, |
| 53 | + }, |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +describe("trusted catalog helpers", () => { |
| 58 | + beforeEach(() => { |
| 59 | + vi.clearAllMocks(); |
| 60 | + applyPluginAutoEnable.mockImplementation(({ config }: { config: unknown }) => ({ |
| 61 | + config: config as never, |
| 62 | + changes: [] as string[], |
| 63 | + autoEnabledReasons: {}, |
| 64 | + })); |
| 65 | + }); |
| 66 | + |
| 67 | + it("falls back to the bundled entry for an untrusted workspace shadow", () => { |
| 68 | + const workspaceEntry = createCatalogEntry({ |
| 69 | + id: "telegram", |
| 70 | + pluginId: "evil-telegram-shadow", |
| 71 | + origin: "workspace", |
| 72 | + }); |
| 73 | + const bundledEntry = createCatalogEntry({ |
| 74 | + id: "telegram", |
| 75 | + pluginId: "telegram", |
| 76 | + origin: "bundled", |
| 77 | + }); |
| 78 | + getChannelPluginCatalogEntry |
| 79 | + .mockReturnValueOnce(workspaceEntry) |
| 80 | + .mockReturnValueOnce(bundledEntry); |
| 81 | + |
| 82 | + const result = getTrustedChannelPluginCatalogEntry("telegram", { |
| 83 | + cfg: {} as never, |
| 84 | + workspaceDir: "/tmp/workspace", |
| 85 | + env: process.env, |
| 86 | + }); |
| 87 | + |
| 88 | + expect(result?.pluginId).toBe("telegram"); |
| 89 | + expect(getChannelPluginCatalogEntry).toHaveBeenNthCalledWith(1, "telegram", { |
| 90 | + workspaceDir: "/tmp/workspace", |
| 91 | + }); |
| 92 | + expect(getChannelPluginCatalogEntry).toHaveBeenNthCalledWith(2, "telegram", { |
| 93 | + workspaceDir: "/tmp/workspace", |
| 94 | + excludeWorkspace: true, |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it("keeps trusted workspace overrides eligible", () => { |
| 99 | + const workspaceEntry = createCatalogEntry({ |
| 100 | + id: "telegram", |
| 101 | + pluginId: "trusted-telegram-shadow", |
| 102 | + origin: "workspace", |
| 103 | + }); |
| 104 | + getChannelPluginCatalogEntry.mockReturnValue(workspaceEntry); |
| 105 | + |
| 106 | + const result = getTrustedChannelPluginCatalogEntry("telegram", { |
| 107 | + cfg: { |
| 108 | + plugins: { |
| 109 | + enabled: true, |
| 110 | + allow: ["trusted-telegram-shadow"], |
| 111 | + }, |
| 112 | + } as never, |
| 113 | + workspaceDir: "/tmp/workspace", |
| 114 | + env: process.env, |
| 115 | + }); |
| 116 | + |
| 117 | + expect(result?.pluginId).toBe("trusted-telegram-shadow"); |
| 118 | + expect(getChannelPluginCatalogEntry).toHaveBeenCalledTimes(1); |
| 119 | + }); |
| 120 | + |
| 121 | + it("omits untrusted workspace-only entries from the trusted list", () => { |
| 122 | + const workspaceOnlyEntry = createCatalogEntry({ |
| 123 | + id: "my-cool-plugin", |
| 124 | + pluginId: "my-cool-plugin", |
| 125 | + origin: "workspace", |
| 126 | + }); |
| 127 | + listRawChannelPluginCatalogEntries.mockImplementation((opts?: unknown) => |
| 128 | + (opts as { excludeWorkspace?: boolean } | undefined)?.excludeWorkspace |
| 129 | + ? [] |
| 130 | + : [workspaceOnlyEntry], |
| 131 | + ); |
| 132 | + |
| 133 | + const result = listTrustedChannelPluginCatalogEntries({ |
| 134 | + cfg: {} as never, |
| 135 | + workspaceDir: "/tmp/workspace", |
| 136 | + env: process.env, |
| 137 | + }); |
| 138 | + |
| 139 | + expect(result).toEqual([]); |
| 140 | + }); |
| 141 | + |
| 142 | + it("keeps workspace-only install candidates visible in discovery", () => { |
| 143 | + const workspaceOnlyEntry = createCatalogEntry({ |
| 144 | + id: "my-cool-plugin", |
| 145 | + pluginId: "my-cool-plugin", |
| 146 | + origin: "workspace", |
| 147 | + }); |
| 148 | + listRawChannelPluginCatalogEntries.mockImplementation((opts?: unknown) => |
| 149 | + (opts as { excludeWorkspace?: boolean } | undefined)?.excludeWorkspace |
| 150 | + ? [] |
| 151 | + : [workspaceOnlyEntry], |
| 152 | + ); |
| 153 | + |
| 154 | + const result = listSetupDiscoveryChannelPluginCatalogEntries({ |
| 155 | + cfg: {} as never, |
| 156 | + workspaceDir: "/tmp/workspace", |
| 157 | + env: process.env, |
| 158 | + }); |
| 159 | + |
| 160 | + expect(result.map((entry) => entry.pluginId)).toEqual(["my-cool-plugin"]); |
| 161 | + }); |
| 162 | + |
| 163 | + it("treats auto-enabled workspace plugins as trusted", () => { |
| 164 | + const workspaceEntry = createCatalogEntry({ |
| 165 | + id: "telegram", |
| 166 | + pluginId: "trusted-telegram-shadow", |
| 167 | + origin: "workspace", |
| 168 | + }); |
| 169 | + getChannelPluginCatalogEntry.mockReturnValue(workspaceEntry); |
| 170 | + applyPluginAutoEnable.mockImplementation(({ config }: { config: unknown }) => ({ |
| 171 | + config: { |
| 172 | + ...(config as Record<string, unknown>), |
| 173 | + plugins: { |
| 174 | + enabled: true, |
| 175 | + allow: ["trusted-telegram-shadow"], |
| 176 | + }, |
| 177 | + } as never, |
| 178 | + changes: ["trusted-telegram-shadow"] as string[], |
| 179 | + autoEnabledReasons: { |
| 180 | + "trusted-telegram-shadow": ["channel configured"], |
| 181 | + }, |
| 182 | + })); |
| 183 | + |
| 184 | + const result = getTrustedChannelPluginCatalogEntry("telegram", { |
| 185 | + cfg: { |
| 186 | + channels: { |
| 187 | + telegram: { token: "existing-token" }, |
| 188 | + }, |
| 189 | + } as never, |
| 190 | + workspaceDir: "/tmp/workspace", |
| 191 | + env: process.env, |
| 192 | + }); |
| 193 | + |
| 194 | + expect(result?.pluginId).toBe("trusted-telegram-shadow"); |
| 195 | + expect(getChannelPluginCatalogEntry).toHaveBeenCalledTimes(1); |
| 196 | + }); |
| 197 | +}); |
0 commit comments