|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { |
| 4 | + createPluginRegistryFixture, |
| 5 | + registerVirtualTestPlugin, |
| 6 | +} from "openclaw/plugin-sdk/plugin-test-contracts"; |
| 7 | +import { withTempHome } from "openclaw/plugin-sdk/test-env"; |
| 8 | +import { typedCases } from "openclaw/plugin-sdk/test-fixtures"; |
| 9 | +import { describe, expect, it } from "vitest"; |
| 10 | + |
| 11 | +const echoCases = typedCases([ |
| 12 | + { message: "alpha", expected: "scoped:alpha" }, |
| 13 | + { message: "beta", expected: "scoped:beta" }, |
| 14 | +]); |
| 15 | + |
| 16 | +describe("plugin testing harness contracts", () => { |
| 17 | + it("executes declared tools and reports missing tool contracts", async () => { |
| 18 | + const previousHome = process.env.HOME; |
| 19 | + const previousStateDir = process.env.OPENCLAW_STATE_DIR; |
| 20 | + |
| 21 | + await withTempHome(async (home) => { |
| 22 | + const stateDir = path.join(home, ".openclaw"); |
| 23 | + await expect( |
| 24 | + fs.stat(path.join(stateDir, "agents", "main", "sessions")), |
| 25 | + ).resolves.toBeDefined(); |
| 26 | + expect(process.env.HOME).toBe(home); |
| 27 | + expect(process.env.OPENCLAW_STATE_DIR).toBe(stateDir); |
| 28 | + |
| 29 | + const { config, registry } = createPluginRegistryFixture({ |
| 30 | + plugins: { |
| 31 | + entries: { |
| 32 | + "fixture-echo": { |
| 33 | + config: { prefix: "scoped" }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + registerVirtualTestPlugin({ |
| 40 | + registry, |
| 41 | + config, |
| 42 | + id: "fixture-echo", |
| 43 | + name: "Fixture Echo", |
| 44 | + contracts: { tools: ["fixture_echo"] }, |
| 45 | + register(api) { |
| 46 | + const prefix = api.config.plugins?.entries?.["fixture-echo"]?.config?.prefix; |
| 47 | + if (typeof prefix !== "string") { |
| 48 | + throw new Error("fixture prefix missing"); |
| 49 | + } |
| 50 | + api.registerTool({ |
| 51 | + name: "fixture_echo", |
| 52 | + label: "Fixture Echo", |
| 53 | + description: "Echo a fixture value", |
| 54 | + parameters: {}, |
| 55 | + execute: async (_toolCallId, params) => { |
| 56 | + const message = (params as { message: string }).message; |
| 57 | + return { |
| 58 | + content: [{ type: "text", text: `${prefix}:${message}` }], |
| 59 | + details: { home, message }, |
| 60 | + }; |
| 61 | + }, |
| 62 | + }); |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + expect(registry.registry.tools).toHaveLength(1); |
| 67 | + const registration = registry.registry.tools[0]; |
| 68 | + expect(registration?.pluginId).toBe("fixture-echo"); |
| 69 | + expect(registration?.names).toEqual(["fixture_echo"]); |
| 70 | + const tool = registration?.factory({ workspaceDir: home }); |
| 71 | + if (!tool) { |
| 72 | + throw new Error("registered fixture tool missing"); |
| 73 | + } |
| 74 | + expect(tool.name).toBe("fixture_echo"); |
| 75 | + |
| 76 | + for (const testCase of echoCases) { |
| 77 | + await expect(tool.execute("fixture-call", { message: testCase.message })).resolves.toEqual({ |
| 78 | + content: [{ type: "text", text: testCase.expected }], |
| 79 | + details: { home, message: testCase.message }, |
| 80 | + }); |
| 81 | + } |
| 82 | + expect(registry.registry.diagnostics).toEqual([]); |
| 83 | + |
| 84 | + const missingContract = createPluginRegistryFixture(); |
| 85 | + registerVirtualTestPlugin({ |
| 86 | + registry: missingContract.registry, |
| 87 | + config: missingContract.config, |
| 88 | + id: "missing-contract", |
| 89 | + name: "Missing Contract", |
| 90 | + register(api) { |
| 91 | + api.registerTool({ |
| 92 | + name: "fixture_echo", |
| 93 | + label: "Fixture Echo", |
| 94 | + description: "Echo a fixture value", |
| 95 | + parameters: {}, |
| 96 | + execute: async () => ({ content: [], details: {} }), |
| 97 | + }); |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + expect(missingContract.registry.registry.tools).toEqual([]); |
| 102 | + expect(missingContract.registry.registry.diagnostics).toContainEqual({ |
| 103 | + level: "error", |
| 104 | + pluginId: "missing-contract", |
| 105 | + source: "/virtual/missing-contract/index.ts", |
| 106 | + message: "plugin must declare contracts.tools before registering agent tools", |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + expect(process.env.HOME).toBe(previousHome); |
| 111 | + expect(process.env.OPENCLAW_STATE_DIR).toBe(previousStateDir); |
| 112 | + }); |
| 113 | +}); |
0 commit comments