|
| 1 | +import { randomUUID } from "node:crypto"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | +import type { PluginCandidate } from "./discovery.js"; |
| 7 | +import { loadPluginManifestRegistry } from "./manifest-registry.js"; |
| 8 | + |
| 9 | +const tempDirs: string[] = []; |
| 10 | + |
| 11 | +function makeTempDir() { |
| 12 | + const dir = path.join(os.tmpdir(), `openclaw-manifest-registry-${randomUUID()}`); |
| 13 | + fs.mkdirSync(dir, { recursive: true }); |
| 14 | + tempDirs.push(dir); |
| 15 | + return dir; |
| 16 | +} |
| 17 | + |
| 18 | +function writeManifest(dir: string, manifest: Record<string, unknown>) { |
| 19 | + fs.writeFileSync(path.join(dir, "openclaw.plugin.json"), JSON.stringify(manifest), "utf-8"); |
| 20 | +} |
| 21 | + |
| 22 | +afterEach(() => { |
| 23 | + for (const dir of tempDirs.splice(0)) { |
| 24 | + try { |
| 25 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 26 | + } catch { |
| 27 | + // ignore cleanup failures |
| 28 | + } |
| 29 | + } |
| 30 | +}); |
| 31 | + |
| 32 | +describe("loadPluginManifestRegistry", () => { |
| 33 | + it("emits duplicate warning for truly distinct plugins with same id", () => { |
| 34 | + const dirA = makeTempDir(); |
| 35 | + const dirB = makeTempDir(); |
| 36 | + const manifest = { id: "test-plugin", configSchema: { type: "object" } }; |
| 37 | + writeManifest(dirA, manifest); |
| 38 | + writeManifest(dirB, manifest); |
| 39 | + |
| 40 | + const candidates: PluginCandidate[] = [ |
| 41 | + { |
| 42 | + idHint: "test-plugin", |
| 43 | + source: path.join(dirA, "index.ts"), |
| 44 | + rootDir: dirA, |
| 45 | + origin: "bundled", |
| 46 | + }, |
| 47 | + { |
| 48 | + idHint: "test-plugin", |
| 49 | + source: path.join(dirB, "index.ts"), |
| 50 | + rootDir: dirB, |
| 51 | + origin: "global", |
| 52 | + }, |
| 53 | + ]; |
| 54 | + |
| 55 | + const registry = loadPluginManifestRegistry({ |
| 56 | + candidates, |
| 57 | + cache: false, |
| 58 | + }); |
| 59 | + |
| 60 | + const duplicateWarnings = registry.diagnostics.filter( |
| 61 | + (d) => d.level === "warn" && d.message?.includes("duplicate plugin id"), |
| 62 | + ); |
| 63 | + expect(duplicateWarnings.length).toBe(1); |
| 64 | + }); |
| 65 | + |
| 66 | + it("suppresses duplicate warning when candidates share the same physical directory via symlink", () => { |
| 67 | + const realDir = makeTempDir(); |
| 68 | + const manifest = { id: "feishu", configSchema: { type: "object" } }; |
| 69 | + writeManifest(realDir, manifest); |
| 70 | + |
| 71 | + // Create a symlink pointing to the same directory |
| 72 | + const symlinkParent = makeTempDir(); |
| 73 | + const symlinkPath = path.join(symlinkParent, "feishu-link"); |
| 74 | + try { |
| 75 | + fs.symlinkSync(realDir, symlinkPath, "junction"); |
| 76 | + } catch { |
| 77 | + // On systems where symlinks are not supported (e.g. restricted Windows), |
| 78 | + // skip this test gracefully. |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + const candidates: PluginCandidate[] = [ |
| 83 | + { |
| 84 | + idHint: "feishu", |
| 85 | + source: path.join(realDir, "index.ts"), |
| 86 | + rootDir: realDir, |
| 87 | + origin: "bundled", |
| 88 | + }, |
| 89 | + { |
| 90 | + idHint: "feishu", |
| 91 | + source: path.join(symlinkPath, "index.ts"), |
| 92 | + rootDir: symlinkPath, |
| 93 | + origin: "bundled", |
| 94 | + }, |
| 95 | + ]; |
| 96 | + |
| 97 | + const registry = loadPluginManifestRegistry({ |
| 98 | + candidates, |
| 99 | + cache: false, |
| 100 | + }); |
| 101 | + |
| 102 | + const duplicateWarnings = registry.diagnostics.filter( |
| 103 | + (d) => d.level === "warn" && d.message?.includes("duplicate plugin id"), |
| 104 | + ); |
| 105 | + expect(duplicateWarnings.length).toBe(0); |
| 106 | + }); |
| 107 | + |
| 108 | + it("suppresses duplicate warning when candidates have identical rootDir paths", () => { |
| 109 | + const dir = makeTempDir(); |
| 110 | + const manifest = { id: "same-path-plugin", configSchema: { type: "object" } }; |
| 111 | + writeManifest(dir, manifest); |
| 112 | + |
| 113 | + const candidates: PluginCandidate[] = [ |
| 114 | + { |
| 115 | + idHint: "same-path-plugin", |
| 116 | + source: path.join(dir, "a.ts"), |
| 117 | + rootDir: dir, |
| 118 | + origin: "bundled", |
| 119 | + }, |
| 120 | + { |
| 121 | + idHint: "same-path-plugin", |
| 122 | + source: path.join(dir, "b.ts"), |
| 123 | + rootDir: dir, |
| 124 | + origin: "global", |
| 125 | + }, |
| 126 | + ]; |
| 127 | + |
| 128 | + const registry = loadPluginManifestRegistry({ |
| 129 | + candidates, |
| 130 | + cache: false, |
| 131 | + }); |
| 132 | + |
| 133 | + const duplicateWarnings = registry.diagnostics.filter( |
| 134 | + (d) => d.level === "warn" && d.message?.includes("duplicate plugin id"), |
| 135 | + ); |
| 136 | + expect(duplicateWarnings.length).toBe(0); |
| 137 | + }); |
| 138 | +}); |
0 commit comments