|
| 1 | +// Installed plugin health snapshot tests cover should-run drift wiring: the eager |
| 2 | +// startup plan is read, deferred channel plugins are excluded, and the not-loaded |
| 3 | +// remainder surfaces as drift in detailed status. |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { resolveReadOnlyChannelPluginsForConfig } from "../channels/plugins/read-only.js"; |
| 6 | +import { |
| 7 | + clearRuntimeConfigSnapshot, |
| 8 | + setRuntimeConfigSnapshot, |
| 9 | +} from "../config/runtime-snapshot.js"; |
| 10 | +import { resolveGatewayStartupPluginActivationConfig } from "../gateway/plugin-activation-runtime-config.js"; |
| 11 | +import { resetPluginStateStoreForTests } from "../plugin-state/plugin-state-store.js"; |
| 12 | +import { loadGatewayStartupPluginPlan } from "../plugins/gateway-startup-plugin-ids.js"; |
| 13 | +import { createEmptyPluginRegistry } from "../plugins/registry-empty.js"; |
| 14 | +import { resetPluginRuntimeStateForTest, setActivePluginRegistry } from "../plugins/runtime.js"; |
| 15 | +import { withStateDirEnv } from "../test-helpers/state-dir-env.js"; |
| 16 | +import { formatDetailedPluginHealth } from "./status-plugin-health.js"; |
| 17 | +import { collectInstalledPluginHealthSnapshot } from "./status-plugin-health.runtime.js"; |
| 18 | + |
| 19 | +vi.mock("../channels/plugins/read-only.js", () => ({ |
| 20 | + resolveReadOnlyChannelPluginsForConfig: vi.fn(), |
| 21 | +})); |
| 22 | +// Keep the installed disk-scan report empty and deterministic so the snapshot's |
| 23 | +// plugin records come only from the seeded runtime registry. |
| 24 | +vi.mock("../plugins/status.js", async (importOriginal) => { |
| 25 | + const actual = await importOriginal<typeof import("../plugins/status.js")>(); |
| 26 | + const emptyReport = { plugins: [], diagnostics: [] } as unknown as ReturnType< |
| 27 | + typeof actual.buildPluginSnapshotReport |
| 28 | + >; |
| 29 | + return { |
| 30 | + ...actual, |
| 31 | + buildPluginSnapshotReport: vi.fn(() => emptyReport), |
| 32 | + buildPluginCompatibilityNotices: vi.fn(() => []), |
| 33 | + } as typeof actual; |
| 34 | +}); |
| 35 | +// Override only the startup-plan resolver; preserve every other real export so any |
| 36 | +// eager importer in the graph keeps working. |
| 37 | +vi.mock("../plugins/gateway-startup-plugin-ids.js", async (importOriginal) => { |
| 38 | + const actual = await importOriginal<typeof import("../plugins/gateway-startup-plugin-ids.js")>(); |
| 39 | + return { ...actual, loadGatewayStartupPluginPlan: vi.fn() } as typeof actual; |
| 40 | +}); |
| 41 | +// The startup-plan activation assembly is the gateway's own shared helper; mock it to |
| 42 | +// identity (return the runtime config) so the status wiring stays deterministic. The helper's |
| 43 | +// real auto-enable + merge behavior is covered by its own unit test and the gateway startup tests. |
| 44 | +vi.mock("../gateway/plugin-activation-runtime-config.js", async (importOriginal) => { |
| 45 | + const actual = |
| 46 | + await importOriginal<typeof import("../gateway/plugin-activation-runtime-config.js")>(); |
| 47 | + return { |
| 48 | + ...actual, |
| 49 | + resolveGatewayStartupPluginActivationConfig: vi.fn( |
| 50 | + (params: { runtimeConfig: unknown }) => |
| 51 | + params.runtimeConfig as ReturnType< |
| 52 | + typeof actual.resolveGatewayStartupPluginActivationConfig |
| 53 | + >, |
| 54 | + ), |
| 55 | + } as typeof actual; |
| 56 | +}); |
| 57 | + |
| 58 | +const resolveReadOnlyChannelPluginsForConfigMock = vi.mocked( |
| 59 | + resolveReadOnlyChannelPluginsForConfig, |
| 60 | +); |
| 61 | +const loadGatewayStartupPluginPlanMock = vi.mocked(loadGatewayStartupPluginPlan); |
| 62 | +const resolveGatewayStartupPluginActivationConfigMock = vi.mocked( |
| 63 | + resolveGatewayStartupPluginActivationConfig, |
| 64 | +); |
| 65 | + |
| 66 | +afterEach(() => { |
| 67 | + resolveReadOnlyChannelPluginsForConfigMock.mockReset(); |
| 68 | + loadGatewayStartupPluginPlanMock.mockReset(); |
| 69 | + resolveGatewayStartupPluginActivationConfigMock.mockClear(); |
| 70 | + clearRuntimeConfigSnapshot(); |
| 71 | + resetPluginRuntimeStateForTest(); |
| 72 | + resetPluginStateStoreForTests(); |
| 73 | +}); |
| 74 | + |
| 75 | +describe("installed plugin health should-run drift", () => { |
| 76 | + it("excludes deferred channel plugins and flags the not-loaded remainder as drift", async () => { |
| 77 | + await withStateDirEnv("openclaw-status-should-run-drift-", async () => { |
| 78 | + resolveReadOnlyChannelPluginsForConfigMock.mockReturnValue({ |
| 79 | + loadFailures: [], |
| 80 | + missingConfiguredChannelIds: [], |
| 81 | + } as never); |
| 82 | + loadGatewayStartupPluginPlanMock.mockReturnValue({ |
| 83 | + channelPluginIds: [], |
| 84 | + // deferred-chan finishes loading only after listen, so it must not count as drift. |
| 85 | + configuredDeferredChannelPluginIds: ["deferred-chan"], |
| 86 | + pluginIds: ["deferred-chan", "planned-missing", "runtime-ok"], |
| 87 | + }); |
| 88 | + |
| 89 | + const registry = createEmptyPluginRegistry(); |
| 90 | + registry.plugins.push({ id: "runtime-ok", status: "loaded", enabled: true } as never); |
| 91 | + setActivePluginRegistry(registry, "runtime-ok", "default", "/tmp/ws"); |
| 92 | + |
| 93 | + const rawConfig = {} as never; |
| 94 | + const snapshot = await collectInstalledPluginHealthSnapshot({ |
| 95 | + config: rawConfig, |
| 96 | + workspaceDir: "/tmp/ws", |
| 97 | + }); |
| 98 | + |
| 99 | + // Plan resolved from the auto-enabled effective config with the raw config as the |
| 100 | + // activation source — matching gateway boot, so auto-enabled plugins are not missed. |
| 101 | + expect(loadGatewayStartupPluginPlanMock).toHaveBeenCalledWith( |
| 102 | + expect.objectContaining({ config: rawConfig, activationSourceConfig: rawConfig }), |
| 103 | + ); |
| 104 | + // Deferred channel plugin dropped from the eager should-run set. |
| 105 | + expect(snapshot.shouldRunPluginIds).toEqual(["planned-missing", "runtime-ok"]); |
| 106 | + |
| 107 | + const text = formatDetailedPluginHealth(snapshot); |
| 108 | + expect(text).toContain("Loaded: 1 (runtime-ok)"); |
| 109 | + expect(text).toContain("Configured to run but not loaded: 1 (planned-missing)"); |
| 110 | + expect(text).not.toContain("deferred-chan"); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it("builds the plan via the shared gateway helper using source + runtime config", async () => { |
| 115 | + await withStateDirEnv("openclaw-status-should-run-source-cfg-", async () => { |
| 116 | + resolveReadOnlyChannelPluginsForConfigMock.mockReturnValue({ |
| 117 | + loadFailures: [], |
| 118 | + missingConfiguredChannelIds: [], |
| 119 | + } as never); |
| 120 | + loadGatewayStartupPluginPlanMock.mockReturnValue({ |
| 121 | + channelPluginIds: [], |
| 122 | + configuredDeferredChannelPluginIds: [], |
| 123 | + pluginIds: [], |
| 124 | + }); |
| 125 | + // /status passes the live runtime config; the activation source must be the original |
| 126 | + // operator source config, and the effective config must be assembled from the runtime |
| 127 | + // config (so runtime/defaulted fields survive) — via the shared gateway-boot helper. |
| 128 | + const sourceConfig = { plugins: { entries: {} } } as never; |
| 129 | + const runtimeConfig = { plugins: { entries: { extra: { enabled: true } } } } as never; |
| 130 | + setRuntimeConfigSnapshot(runtimeConfig, sourceConfig); |
| 131 | + setActivePluginRegistry(createEmptyPluginRegistry(), "empty", "default", "/tmp/ws"); |
| 132 | + |
| 133 | + await collectInstalledPluginHealthSnapshot({ |
| 134 | + config: runtimeConfig, |
| 135 | + workspaceDir: "/tmp/ws", |
| 136 | + }); |
| 137 | + |
| 138 | + // The shared gateway-boot helper assembles the effective config from the runtime config |
| 139 | + // with the operator source config as the activation source (not the runtime snapshot). |
| 140 | + expect(resolveGatewayStartupPluginActivationConfigMock).toHaveBeenCalledWith( |
| 141 | + expect.objectContaining({ runtimeConfig, activationSourceConfig: sourceConfig }), |
| 142 | + ); |
| 143 | + // The plan is resolved from that effective config (here the helper's identity output = |
| 144 | + // runtimeConfig) with the operator source config as the activation source. |
| 145 | + expect(loadGatewayStartupPluginPlanMock).toHaveBeenCalledWith( |
| 146 | + expect.objectContaining({ config: runtimeConfig, activationSourceConfig: sourceConfig }), |
| 147 | + ); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + it("omits the should-run set entirely when no config is provided", async () => { |
| 152 | + await withStateDirEnv("openclaw-status-should-run-no-config-", async () => { |
| 153 | + resolveReadOnlyChannelPluginsForConfigMock.mockReturnValue({ |
| 154 | + loadFailures: [], |
| 155 | + missingConfiguredChannelIds: [], |
| 156 | + } as never); |
| 157 | + setActivePluginRegistry(createEmptyPluginRegistry(), "empty", "default", "/tmp/ws"); |
| 158 | + |
| 159 | + const snapshot = await collectInstalledPluginHealthSnapshot({ workspaceDir: "/tmp/ws" }); |
| 160 | + |
| 161 | + expect(snapshot.shouldRunPluginIds).toBeUndefined(); |
| 162 | + expect(loadGatewayStartupPluginPlanMock).not.toHaveBeenCalled(); |
| 163 | + expect(formatDetailedPluginHealth(snapshot)).not.toContain( |
| 164 | + "Configured to run but not loaded:", |
| 165 | + ); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments