Skip to content

Commit 3499fa9

Browse files
authored
refactor(test): share weather plugin fixture builder (#152)
1 parent c81160a commit 3499fa9

3 files changed

Lines changed: 48 additions & 95 deletions

File tree

‎test/api.test.js‎

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createWeatherPlugin } from "./helpers/weather-plugin.js";
12
import { createTempDir } from "./helpers/temp-dir.js";
23
import assert from "node:assert/strict";
34
import { mkdir, readFile, writeFile } from "node:fs/promises";
@@ -545,41 +546,5 @@ test("public API honors config-driven runtime capture", async (t) => {
545546
});
546547

547548
async function createPluginRoot(t, options = {}) {
548-
const rootDir = await createTempDir(t, "plugin-inspector-api-root-");
549-
await mkdir(path.join(rootDir, "src"), { recursive: true });
550-
const packageJson = {
551-
name: "@example/openclaw-weather",
552-
version: "1.0.0",
553-
type: "module",
554-
openclaw: {
555-
extensions: ["src/index.js"],
556-
compat: { pluginApi: "^1.0.0" },
557-
},
558-
...(options.packageJson ?? {}),
559-
};
560-
if (packageJson.openclaw === null) {
561-
delete packageJson.openclaw;
562-
}
563-
if (options.packageConfig) {
564-
packageJson.pluginInspector = {
565-
version: 1,
566-
...options.packageConfig,
567-
};
568-
}
569-
await writeFile(
570-
path.join(rootDir, "package.json"),
571-
`${JSON.stringify(packageJson, null, 2)}\n`,
572-
"utf8",
573-
);
574-
await writeFile(
575-
path.join(rootDir, "openclaw.plugin.json"),
576-
`${JSON.stringify({ id: "weather", name: "Weather", version: "1.0.0", contracts: { tools: {} } }, null, 2)}\n`,
577-
"utf8",
578-
);
579-
await writeFile(
580-
path.join(rootDir, "src", "index.js"),
581-
'import { definePluginEntry } from "openclaw/plugin-sdk";\nexport default definePluginEntry((api) => api.registerTool({ name: "weather" }));\n',
582-
"utf8",
583-
);
584-
return rootDir;
549+
return createWeatherPlugin(t, "plugin-inspector-api-root-", options);
585550
}

‎test/cli.test.js‎

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createWeatherPlugin } from "./helpers/weather-plugin.js";
12
import { createTempDir } from "./helpers/temp-dir.js";
23
import assert from "node:assert/strict";
34
import { execFile } from "node:child_process";
@@ -10,35 +11,7 @@ import { test } from "node:test";
1011
const execFileAsync = promisify(execFile);
1112

1213
test("check command runs from a plugin root without fixture config", async (t) => {
13-
const rootDir = await createTempDir(t, "plugin-inspector-cli-root-");
14-
await mkdir(path.join(rootDir, "src"), { recursive: true });
15-
await writeFile(
16-
path.join(rootDir, "package.json"),
17-
`${JSON.stringify(
18-
{
19-
name: "@example/openclaw-weather",
20-
version: "1.0.0",
21-
type: "module",
22-
openclaw: {
23-
extensions: ["src/index.js"],
24-
compat: { pluginApi: "^1.0.0" },
25-
},
26-
},
27-
null,
28-
2,
29-
)}\n`,
30-
"utf8",
31-
);
32-
await writeFile(
33-
path.join(rootDir, "openclaw.plugin.json"),
34-
`${JSON.stringify({ id: "weather", name: "Weather", version: "1.0.0", contracts: { tools: {} } }, null, 2)}\n`,
35-
"utf8",
36-
);
37-
await writeFile(
38-
path.join(rootDir, "src", "index.js"),
39-
'import { definePluginEntry } from "openclaw/plugin-sdk";\nexport default definePluginEntry((api) => api.registerTool({ name: "weather" }));\n',
40-
"utf8",
41-
);
14+
const rootDir = await createWeatherPlugin(t, "plugin-inspector-cli-root-");
4215

4316
const cliPath = path.resolve("src/cli.js");
4417
const { stdout } = await execFileAsync(process.execPath, [cliPath, "check", "--out", "reports", "--no-openclaw"], {
@@ -614,35 +587,7 @@ test("batch command applies author-facing filtering to aggregate summaries", asy
614587
});
615588

616589
async function createCliPluginRoot(t, prefix) {
617-
const rootDir = await createTempDir(t, prefix);
618-
await mkdir(path.join(rootDir, "src"), { recursive: true });
619-
await writeFile(
620-
path.join(rootDir, "package.json"),
621-
`${JSON.stringify(
622-
{
623-
name: "@example/openclaw-weather",
624-
version: "1.0.0",
625-
type: "module",
626-
openclaw: {
627-
extensions: ["src/index.js"],
628-
compat: { pluginApi: "^1.0.0" },
629-
},
630-
},
631-
null,
632-
2,
633-
)}\n`,
634-
"utf8",
635-
);
636-
await writeFile(
637-
path.join(rootDir, "openclaw.plugin.json"),
638-
`${JSON.stringify({ id: "weather", name: "Weather", version: "1.0.0", contracts: { tools: {} } }, null, 2)}\n`,
639-
"utf8",
640-
);
641-
await writeFile(
642-
path.join(rootDir, "src", "index.js"),
643-
'import { definePluginEntry } from "openclaw/plugin-sdk";\nexport default definePluginEntry((api) => api.registerTool({ name: "weather" }));\n',
644-
"utf8",
645-
);
590+
const rootDir = await createWeatherPlugin(t, prefix);
646591
await writeFile(
647592
path.join(rootDir, "plugin-inspector.config.json"),
648593
`${JSON.stringify({ version: 1, plugin: { id: "weather", priority: "high", sourceRoot: "src" } }, null, 2)}\n`,

‎test/helpers/weather-plugin.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { createTempDir } from "./temp-dir.js";
2+
import { mkdir, writeFile } from "node:fs/promises";
3+
import path from "node:path";
4+
5+
export async function createWeatherPlugin(t, prefix, options = {}) {
6+
const rootDir = await createTempDir(t, prefix);
7+
await mkdir(path.join(rootDir, "src"), { recursive: true });
8+
const packageJson = {
9+
name: "@example/openclaw-weather",
10+
version: "1.0.0",
11+
type: "module",
12+
openclaw: {
13+
extensions: ["src/index.js"],
14+
compat: { pluginApi: "^1.0.0" },
15+
},
16+
...(options.packageJson ?? {}),
17+
};
18+
if (packageJson.openclaw === null) {
19+
delete packageJson.openclaw;
20+
}
21+
if (options.packageConfig) {
22+
packageJson.pluginInspector = {
23+
version: 1,
24+
...options.packageConfig,
25+
};
26+
}
27+
await writeFile(
28+
path.join(rootDir, "package.json"),
29+
`${JSON.stringify(packageJson, null, 2)}\n`,
30+
"utf8",
31+
);
32+
await writeFile(
33+
path.join(rootDir, "openclaw.plugin.json"),
34+
`${JSON.stringify({ id: "weather", name: "Weather", version: "1.0.0", contracts: { tools: {} } }, null, 2)}\n`,
35+
"utf8",
36+
);
37+
await writeFile(
38+
path.join(rootDir, "src", "index.js"),
39+
'import { definePluginEntry } from "openclaw/plugin-sdk";\nexport default definePluginEntry((api) => api.registerTool({ name: "weather" }));\n',
40+
"utf8",
41+
);
42+
return rootDir;
43+
}

0 commit comments

Comments
 (0)