|
| 1 | +import fs from "node:fs"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, beforeAll, describe, expect, it, vi } from "vitest"; |
| 5 | +import { createPluginRecord } from "../plugins/status.test-fixtures.js"; |
| 6 | + |
| 7 | +const testConfig = { session: { store: "/tmp/x" } }; |
| 8 | +const tempPaths: string[] = []; |
| 9 | + |
| 10 | +let setActivePluginRegistry: typeof import("../plugins/runtime.js").setActivePluginRegistry; |
| 11 | +let setActiveDegradedPlugins: typeof import("../plugins/runtime-degraded-state.js").setActiveDegradedPlugins; |
| 12 | +let createTestRegistry: typeof import("../test-utils/channel-plugins.js").createTestRegistry; |
| 13 | +let getHealthSnapshot: typeof import("./health.js").getHealthSnapshot; |
| 14 | + |
| 15 | +describe("getHealthSnapshot plugin state", () => { |
| 16 | + beforeAll(async () => { |
| 17 | + vi.doMock("../config/config.js", () => ({ |
| 18 | + getRuntimeConfig: () => testConfig, |
| 19 | + loadConfig: () => testConfig, |
| 20 | + })); |
| 21 | + vi.doMock("../config/sessions/paths.js", () => ({ |
| 22 | + resolveStorePath: () => "/tmp/sessions.json", |
| 23 | + })); |
| 24 | + vi.doMock("../config/sessions/session-accessor.js", () => ({ |
| 25 | + listSessionEntries: () => [], |
| 26 | + })); |
| 27 | + vi.doMock("../channels/plugins/read-only.js", () => ({ |
| 28 | + listReadOnlyChannelPluginsForConfig: () => [], |
| 29 | + })); |
| 30 | + |
| 31 | + const [pluginsRuntime, degradedState, channelTestUtils, health] = await Promise.all([ |
| 32 | + import("../plugins/runtime.js"), |
| 33 | + import("../plugins/runtime-degraded-state.js"), |
| 34 | + import("../test-utils/channel-plugins.js"), |
| 35 | + import("./health.js"), |
| 36 | + ]); |
| 37 | + setActivePluginRegistry = pluginsRuntime.setActivePluginRegistry; |
| 38 | + setActiveDegradedPlugins = degradedState.setActiveDegradedPlugins; |
| 39 | + createTestRegistry = channelTestUtils.createTestRegistry; |
| 40 | + getHealthSnapshot = health.getHealthSnapshot; |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + setActiveDegradedPlugins([]); |
| 45 | + setActivePluginRegistry(createTestRegistry([])); |
| 46 | + for (const tempPath of tempPaths.splice(0)) { |
| 47 | + fs.rmSync(tempPath, { recursive: true, force: true }); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + it("deduplicates canonical-root quarantine while retaining unrelated same-id errors", async () => { |
| 52 | + const pluginRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-health-plugin-")); |
| 53 | + const pluginRootAlias = `${pluginRoot}-alias`; |
| 54 | + fs.symlinkSync(pluginRoot, pluginRootAlias, "dir"); |
| 55 | + tempPaths.push(pluginRootAlias, pluginRoot); |
| 56 | + setActivePluginRegistry({ |
| 57 | + ...createTestRegistry([]), |
| 58 | + plugins: [ |
| 59 | + createPluginRecord({ |
| 60 | + id: "discord", |
| 61 | + origin: "global", |
| 62 | + rootDir: pluginRoot, |
| 63 | + status: "error", |
| 64 | + activated: false, |
| 65 | + activationReason: "configured-unavailable: unreadable-package-json", |
| 66 | + failurePhase: "validation", |
| 67 | + error: "configured plugin payload verification failed", |
| 68 | + }), |
| 69 | + createPluginRecord({ |
| 70 | + id: "discord", |
| 71 | + origin: "config", |
| 72 | + rootDir: "/workspace/discord", |
| 73 | + status: "error", |
| 74 | + activated: false, |
| 75 | + failurePhase: "load", |
| 76 | + error: "healthy override has an unrelated import error", |
| 77 | + }), |
| 78 | + ], |
| 79 | + }); |
| 80 | + setActiveDegradedPlugins([ |
| 81 | + { |
| 82 | + pluginId: "discord", |
| 83 | + state: "configured-unavailable", |
| 84 | + diagnostic: { |
| 85 | + kind: "plugin-verification", |
| 86 | + reason: "unreadable-package-json", |
| 87 | + detail: `Could not read ${pluginRootAlias}/package.json: permission denied`, |
| 88 | + installPath: pluginRootAlias, |
| 89 | + }, |
| 90 | + }, |
| 91 | + ]); |
| 92 | + |
| 93 | + const snap = await getHealthSnapshot({ timeoutMs: 10, probe: false }); |
| 94 | + |
| 95 | + expect(snap.plugins?.unavailable).toEqual([ |
| 96 | + { |
| 97 | + id: "discord", |
| 98 | + state: "configured-unavailable", |
| 99 | + diagnostic: { |
| 100 | + kind: "plugin-verification", |
| 101 | + reason: "unreadable-package-json", |
| 102 | + detail: "Could not read <plugin-install>/package.json: permission denied", |
| 103 | + }, |
| 104 | + }, |
| 105 | + ]); |
| 106 | + expect(JSON.stringify(snap.plugins?.unavailable)).not.toContain(pluginRoot); |
| 107 | + expect(snap.plugins?.errors).toEqual([ |
| 108 | + { |
| 109 | + id: "discord", |
| 110 | + origin: "config", |
| 111 | + activated: false, |
| 112 | + activationSource: "explicit", |
| 113 | + failurePhase: "load", |
| 114 | + error: "healthy override has an unrelated import error", |
| 115 | + }, |
| 116 | + ]); |
| 117 | + }); |
| 118 | +}); |
0 commit comments