Skip to content

Commit 9727bdf

Browse files
committed
test(qa): cover plugin testing harness contracts
1 parent 990fdcf commit 9727bdf

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
title: Plugin testing harness contracts
2+
3+
scenario:
4+
id: plugin-testing-harness-contracts
5+
surface: plugins
6+
category: plugins.testing-plugins
7+
coverage:
8+
primary:
9+
- plugins.test-fixtures
10+
- plugins.local-test-environment
11+
- plugins.harness-contracts-tools
12+
- plugins.unit-and-integration-scaffolds
13+
objective: Verify the repo-local plugin test helpers provide isolated state, typed fixtures, real registry registration, tool execution, and contract diagnostics.
14+
successCriteria:
15+
- The temporary-home helper creates isolated OpenClaw state and restores the caller environment.
16+
- Typed fixture cases drive an actual tool registered through the plugin registry harness.
17+
- A virtual plugin with a declared tools contract registers and executes its tool.
18+
- A virtual plugin without a tools contract records the canonical diagnostic and registers no tool.
19+
docsRefs:
20+
- docs/plugins/sdk-testing.md
21+
codeRefs:
22+
- src/plugin-sdk/plugin-test-contracts.ts
23+
- src/plugin-sdk/test-env.ts
24+
- src/plugin-sdk/test-fixtures.ts
25+
- src/plugins/contracts/plugin-testing-harness.contract.test.ts
26+
execution:
27+
kind: vitest
28+
path: src/plugins/contracts/plugin-testing-harness.contract.test.ts
29+
summary: Run the repo-local plugin harness across isolated state, typed fixtures, tool execution, and contract rejection.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)