|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { createEmptyPluginRegistry } from "../plugins/registry-empty.js"; |
| 3 | +import type { PluginRegistry } from "../plugins/registry-types.js"; |
| 4 | +import { resetPluginRuntimeStateForTest, setActivePluginRegistry } from "../plugins/runtime.js"; |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => ({ |
| 7 | + getCurrentPluginMetadataSnapshot: vi.fn(), |
| 8 | + loadOpenClawPlugins: vi.fn<typeof import("../plugins/loader.js").loadOpenClawPlugins>(), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("../plugins/current-plugin-metadata-snapshot.js", () => ({ |
| 12 | + getCurrentPluginMetadataSnapshot: mocks.getCurrentPluginMetadataSnapshot, |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("../plugins/loader.js", async (importOriginal) => { |
| 16 | + const actual = await importOriginal<typeof import("../plugins/loader.js")>(); |
| 17 | + return { |
| 18 | + ...actual, |
| 19 | + loadOpenClawPlugins: (...args: Parameters<typeof mocks.loadOpenClawPlugins>) => |
| 20 | + mocks.loadOpenClawPlugins(...args), |
| 21 | + }; |
| 22 | +}); |
| 23 | + |
| 24 | +const [{ ensureRuntimePluginsLoaded }, { clearPluginLoaderCache, testing }] = await Promise.all([ |
| 25 | + import("./runtime-plugins.js"), |
| 26 | + import("../plugins/loader.js"), |
| 27 | +]); |
| 28 | + |
| 29 | +function createRegistryWithPlugin(pluginId: string): PluginRegistry { |
| 30 | + const registry = createEmptyPluginRegistry(); |
| 31 | + registry.plugins.push({ |
| 32 | + id: pluginId, |
| 33 | + status: "loaded", |
| 34 | + } as never); |
| 35 | + return registry; |
| 36 | +} |
| 37 | + |
| 38 | +beforeEach(() => { |
| 39 | + mocks.getCurrentPluginMetadataSnapshot.mockReset(); |
| 40 | + mocks.loadOpenClawPlugins.mockReset(); |
| 41 | +}); |
| 42 | + |
| 43 | +afterEach(() => { |
| 44 | + clearPluginLoaderCache(); |
| 45 | + resetPluginRuntimeStateForTest(); |
| 46 | +}); |
| 47 | + |
| 48 | +describe("ensureRuntimePluginsLoaded registry reuse", () => { |
| 49 | + it("reuses the compatible gateway startup registry on the dispatch caller path", () => { |
| 50 | + const config = { plugins: { allow: ["telegram"] } }; |
| 51 | + const activeRegistry = createRegistryWithPlugin("telegram"); |
| 52 | + activeRegistry.coreGatewayMethodNames = ["sessions.get", "sessions.list"]; |
| 53 | + const startupLoadOptions = { |
| 54 | + config, |
| 55 | + activationSourceConfig: config, |
| 56 | + autoEnabledReasons: {}, |
| 57 | + workspaceDir: "/tmp/workspace", |
| 58 | + onlyPluginIds: ["telegram"], |
| 59 | + coreGatewayMethodNames: ["sessions.get", "sessions.list"], |
| 60 | + runtimeOptions: { |
| 61 | + allowGatewaySubagentBinding: true, |
| 62 | + }, |
| 63 | + preferBuiltPluginArtifacts: true, |
| 64 | + }; |
| 65 | + const { cacheKey } = testing.resolvePluginLoadCacheContext(startupLoadOptions); |
| 66 | + setActivePluginRegistry(activeRegistry, cacheKey, "gateway-bindable", "/tmp/workspace"); |
| 67 | + mocks.getCurrentPluginMetadataSnapshot.mockReturnValue({ |
| 68 | + startup: { |
| 69 | + pluginIds: ["telegram"], |
| 70 | + }, |
| 71 | + }); |
| 72 | + mocks.loadOpenClawPlugins.mockImplementation(() => { |
| 73 | + throw new Error("dispatch should reuse the active gateway startup registry"); |
| 74 | + }); |
| 75 | + |
| 76 | + ensureRuntimePluginsLoaded({ |
| 77 | + config, |
| 78 | + workspaceDir: "/tmp/workspace", |
| 79 | + }); |
| 80 | + |
| 81 | + expect(mocks.getCurrentPluginMetadataSnapshot).toHaveBeenCalledWith({ |
| 82 | + config, |
| 83 | + workspaceDir: "/tmp/workspace", |
| 84 | + }); |
| 85 | + expect(mocks.loadOpenClawPlugins).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | +}); |
0 commit comments