|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + createPluginEntry, |
| 4 | + loadDefinePluginEntry, |
| 5 | + type AgentControlPluginEntry, |
| 6 | +} from "../src/plugin-entry.ts"; |
| 7 | + |
| 8 | +describe("plugin entry compatibility", () => { |
| 9 | + it("returns the legacy register function when no modern helper is available", () => { |
| 10 | + // Given a plugin entry and a gateway without definePluginEntry support |
| 11 | + const legacyRegister = vi.fn(); |
| 12 | + const entry: AgentControlPluginEntry = { |
| 13 | + id: "agent-control-openclaw-plugin", |
| 14 | + name: "Agent Control", |
| 15 | + description: "test entry", |
| 16 | + register: legacyRegister, |
| 17 | + }; |
| 18 | + |
| 19 | + // When the plugin entry is created without a modern helper |
| 20 | + const resolved = createPluginEntry(entry, null); |
| 21 | + |
| 22 | + // Then the legacy raw register function is exported |
| 23 | + expect(resolved).toBe(legacyRegister); |
| 24 | + }); |
| 25 | + |
| 26 | + it("wraps the entry with definePluginEntry when the helper is available", () => { |
| 27 | + // Given a plugin entry and a gateway that exposes definePluginEntry |
| 28 | + const legacyRegister = vi.fn(); |
| 29 | + const entry: AgentControlPluginEntry = { |
| 30 | + id: "agent-control-openclaw-plugin", |
| 31 | + name: "Agent Control", |
| 32 | + description: "test entry", |
| 33 | + register: legacyRegister, |
| 34 | + }; |
| 35 | + const definePluginEntry = vi.fn((value: AgentControlPluginEntry) => ({ |
| 36 | + ...value, |
| 37 | + wrapped: true, |
| 38 | + })); |
| 39 | + |
| 40 | + // When the plugin entry is created with the helper |
| 41 | + const resolved = createPluginEntry(entry, definePluginEntry); |
| 42 | + |
| 43 | + // Then the modern helper receives the descriptor and its result is exported |
| 44 | + expect(definePluginEntry).toHaveBeenCalledWith(entry); |
| 45 | + expect(resolved).toEqual({ |
| 46 | + ...entry, |
| 47 | + wrapped: true, |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it("prefers the dedicated plugin-entry module when it is present", () => { |
| 52 | + // Given a createRequire implementation that exposes the dedicated helper module |
| 53 | + const defineFromPluginEntry = vi.fn(); |
| 54 | + const createRequireImpl = vi.fn(() => |
| 55 | + vi.fn((specifier: string) => { |
| 56 | + if (specifier === "openclaw/plugin-sdk/plugin-entry") { |
| 57 | + return { definePluginEntry: defineFromPluginEntry }; |
| 58 | + } |
| 59 | + throw new Error(`unexpected module lookup: ${specifier}`); |
| 60 | + }), |
| 61 | + ); |
| 62 | + |
| 63 | + // When definePluginEntry is loaded from the gateway SDK |
| 64 | + const loaded = loadDefinePluginEntry(createRequireImpl); |
| 65 | + |
| 66 | + // Then the dedicated helper is returned without probing fallback modules |
| 67 | + expect(loaded).toBe(defineFromPluginEntry); |
| 68 | + }); |
| 69 | + |
| 70 | + it("falls back to the core helper during the SDK migration window", () => { |
| 71 | + // Given a createRequire implementation without the dedicated helper module |
| 72 | + const defineFromCore = vi.fn(); |
| 73 | + const requireFn = vi.fn((specifier: string) => { |
| 74 | + if (specifier === "openclaw/plugin-sdk/plugin-entry") { |
| 75 | + throw new Error("module not found"); |
| 76 | + } |
| 77 | + if (specifier === "openclaw/plugin-sdk/core") { |
| 78 | + return { definePluginEntry: defineFromCore }; |
| 79 | + } |
| 80 | + throw new Error(`unexpected module lookup: ${specifier}`); |
| 81 | + }); |
| 82 | + |
| 83 | + // When definePluginEntry is loaded from the gateway SDK |
| 84 | + const loaded = loadDefinePluginEntry(vi.fn(() => requireFn)); |
| 85 | + |
| 86 | + // Then the core-exported helper is used as a migration fallback |
| 87 | + expect(loaded).toBe(defineFromCore); |
| 88 | + expect(requireFn).toHaveBeenCalledWith("openclaw/plugin-sdk/plugin-entry"); |
| 89 | + expect(requireFn).toHaveBeenCalledWith("openclaw/plugin-sdk/core"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("returns null when neither modern helper is available", () => { |
| 93 | + // Given a createRequire implementation where both helper lookups fail |
| 94 | + const requireFn = vi.fn(() => { |
| 95 | + throw new Error("module not found"); |
| 96 | + }); |
| 97 | + |
| 98 | + // When definePluginEntry is loaded from the gateway SDK |
| 99 | + const loaded = loadDefinePluginEntry(vi.fn(() => requireFn)); |
| 100 | + |
| 101 | + // Then the plugin can fall back to the legacy raw register export |
| 102 | + expect(loaded).toBeNull(); |
| 103 | + }); |
| 104 | +}); |
0 commit comments