plugin-inspector/test/cli.test.js

251 lines
9.3 KiB
JavaScript

import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { mkdir, mkdtemp, readFile, writeFile } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { promisify } from "node:util";
import { test } from "node:test";
const execFileAsync = promisify(execFile);
test("check command runs from a plugin root without fixture config", async () => {
const rootDir = await mkdtemp(path.join(os.tmpdir(), "plugin-inspector-cli-root-"));
await mkdir(path.join(rootDir, "src"), { recursive: true });
await writeFile(
path.join(rootDir, "package.json"),
`${JSON.stringify(
{
name: "@example/openclaw-weather",
version: "1.0.0",
type: "module",
openclaw: {
extensions: ["src/index.js"],
compat: { pluginApi: "^1.0.0" },
},
},
null,
2,
)}\n`,
"utf8",
);
await writeFile(
path.join(rootDir, "openclaw.plugin.json"),
`${JSON.stringify({ id: "weather", name: "Weather", version: "1.0.0", contracts: { tools: {} } }, null, 2)}\n`,
"utf8",
);
await writeFile(
path.join(rootDir, "src", "index.js"),
'import { definePluginEntry } from "openclaw/plugin-sdk";\nexport default definePluginEntry((api) => api.registerTool({ name: "weather" }));\n',
"utf8",
);
const cliPath = path.resolve("src/cli.js");
const { stdout } = await execFileAsync(process.execPath, [cliPath, "check", "--out", "reports", "--no-openclaw"], {
cwd: rootDir,
});
const report = JSON.parse(await readFile(path.join(rootDir, "reports", "plugin-inspector-report.json"), "utf8"));
const issues = await readFile(path.join(rootDir, "reports", "plugin-inspector-issues.md"), "utf8");
assert.match(stdout, /Status: PASS/);
assert.equal(report.summary.logCount, report.logs.length);
assert.match(stdout, new RegExp(`Logs: ${report.logs.length}\\b`));
assert.doesNotMatch(stdout, /Logs: undefined/);
assert.equal(report.targetOpenClaw.status, "disabled");
assert.equal(report.fixtures[0].id, "weather");
assert.ok(report.fixtures[0].package.openclaw.entrypoints.some((entrypoint) => entrypoint.exists));
assert.match(issues, /# OpenClaw Plugin Issue Findings/);
await execFileAsync(process.execPath, [cliPath, "check", "--out", "capture-reports", "--no-openclaw", "--capture"], {
cwd: rootDir,
env: {
...process.env,
PLUGIN_INSPECTOR_EXECUTE_ISOLATED: "1",
},
});
const capture = JSON.parse(
await readFile(path.join(rootDir, "capture-reports", "plugin-inspector-runtime-capture.json"), "utf8"),
);
assert.equal(capture.summary.capturedCount, 1);
assert.equal(capture.summary.registrationCount, 1);
});
test("check command can target a plugin root and use runtime aliases", async () => {
const rootDir = await createCliPluginRoot("plugin-inspector-cli-target-");
const cliPath = path.resolve("src/cli.js");
await execFileAsync(
process.execPath,
[cliPath, "--plugin-root", rootDir, "--out", "reports", "--no-openclaw", "--runtime", "--mock-sdk"],
{
cwd: os.tmpdir(),
env: {
...process.env,
PLUGIN_INSPECTOR_EXECUTE_ISOLATED: "1",
},
},
);
const report = JSON.parse(await readFile(path.join(rootDir, "reports", "plugin-inspector-report.json"), "utf8"));
const capture = JSON.parse(
await readFile(path.join(rootDir, "reports", "plugin-inspector-runtime-capture.json"), "utf8"),
);
assert.equal(report.fixtures[0].id, "weather");
assert.equal(capture.summary.capturedCount, 1);
});
test("inspect command runs from a plugin root and can write CI outputs", async () => {
const rootDir = await createCliPluginRoot("plugin-inspector-cli-inspect-");
const cliPath = path.resolve("src/cli.js");
const { stdout } = await execFileAsync(process.execPath, [
cliPath,
"inspect",
"--out",
"reports",
"--no-openclaw",
"--sarif",
"--junit",
], {
cwd: rootDir,
});
const sarif = JSON.parse(await readFile(path.join(rootDir, "reports", "plugin-inspector.sarif"), "utf8"));
const junit = await readFile(path.join(rootDir, "reports", "plugin-inspector.junit.xml"), "utf8");
assert.match(stdout, /Status: PASS/);
assert.equal(sarif.version, "2.1.0");
assert.match(junit, /<testsuite name="plugin-inspector"/);
});
test("check command can enable runtime capture from plugin config", async () => {
const rootDir = await createCliPluginRoot("plugin-inspector-cli-config-runtime-");
await writeFile(
path.join(rootDir, "plugin-inspector.config.json"),
`${JSON.stringify({ version: 1, capture: { runtime: true, mockSdk: true } }, null, 2)}\n`,
"utf8",
);
const cliPath = path.resolve("src/cli.js");
await execFileAsync(process.execPath, [cliPath, "check", "--out", "reports", "--no-openclaw"], {
cwd: rootDir,
env: {
...process.env,
PLUGIN_INSPECTOR_EXECUTE_ISOLATED: "1",
},
});
const capture = JSON.parse(
await readFile(path.join(rootDir, "reports", "plugin-inspector-runtime-capture.json"), "utf8"),
);
assert.equal(capture.summary.registrationCount, 1);
});
test("config command prints resolved plugin root config", async () => {
const rootDir = await createCliPluginRoot("plugin-inspector-cli-config-print-");
const cliPath = path.resolve("src/cli.js");
const { stdout } = await execFileAsync(process.execPath, [cliPath, "config", "--plugin-root", rootDir]);
const { stdout: jsonStdout } = await execFileAsync(process.execPath, [
cliPath,
"config",
"--plugin-root",
rootDir,
"--json",
]);
const config = JSON.parse(jsonStdout);
assert.match(stdout, /Plugin: weather/);
assert.match(stdout, /Runtime capture: off/);
assert.equal(config.fixtures[0].id, "weather");
assert.equal(config.fixtures[0].subdir, "src");
});
test("ci command writes CI summary artifacts", async () => {
const rootDir = await createCliPluginRoot("plugin-inspector-cli-ci-");
const cliPath = path.resolve("src/cli.js");
const { stdout } = await execFileAsync(
process.execPath,
[cliPath, "ci", "--config", path.join(rootDir, "plugin-inspector.config.json"), "--out", "reports", "--no-openclaw"],
{
cwd: rootDir,
},
);
const report = JSON.parse(await readFile(path.join(rootDir, "reports", "plugin-inspector-report.json"), "utf8"));
const summary = JSON.parse(
await readFile(path.join(rootDir, "reports", "plugin-inspector-ci-summary.json"), "utf8"),
);
const markdown = await readFile(path.join(rootDir, "reports", "plugin-inspector-ci-summary.md"), "utf8");
const sarif = JSON.parse(await readFile(path.join(rootDir, "reports", "plugin-inspector.sarif"), "utf8"));
const junit = await readFile(path.join(rootDir, "reports", "plugin-inspector.junit.xml"), "utf8");
assert.match(stdout, /Status: PASS/);
assert.match(stdout, /Artifacts: 1/);
assert.equal(report.targetOpenClaw.status, "disabled");
assert.ok(Array.isArray(report.issues));
assert.equal(summary.status, "pass");
assert.equal(summary.summary.breakages, 0);
assert.equal(summary.summary.issues, report.summary.issueCount);
assert.equal(summary.artifacts.compatibility, "plugin-inspector-report.json");
assert.match(markdown, /# Plugin Inspector CI Summary/);
assert.equal(sarif.runs[0].tool.driver.name, "plugin-inspector");
assert.match(junit, /failures="0"/);
});
test("init command writes plugin config and CI workflow", async () => {
const rootDir = await createCliPluginRoot("plugin-inspector-cli-init-");
const cliPath = path.resolve("src/cli.js");
const { stdout } = await execFileAsync(
process.execPath,
[cliPath, "init", "--plugin-root", rootDir, "--ci", "--package-manager", "pnpm", "--force"],
);
const config = JSON.parse(await readFile(path.join(rootDir, "plugin-inspector.config.json"), "utf8"));
const workflow = await readFile(path.join(rootDir, ".github", "workflows", "plugin-inspector.yml"), "utf8");
assert.match(stdout, /plugin-inspector\.config\.json/);
assert.equal(config.plugin.id, "weather");
assert.equal(config.plugin.sourceRoot, "src");
assert.equal(config.capture.mockSdk, true);
assert.match(workflow, /pnpm dlx @openclaw\/plugin-inspector ci --no-openclaw --runtime --mock-sdk/);
});
async function createCliPluginRoot(prefix) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), prefix));
await mkdir(path.join(rootDir, "src"), { recursive: true });
await writeFile(
path.join(rootDir, "package.json"),
`${JSON.stringify(
{
name: "@example/openclaw-weather",
version: "1.0.0",
type: "module",
openclaw: {
extensions: ["src/index.js"],
compat: { pluginApi: "^1.0.0" },
},
},
null,
2,
)}\n`,
"utf8",
);
await writeFile(
path.join(rootDir, "openclaw.plugin.json"),
`${JSON.stringify({ id: "weather", name: "Weather", version: "1.0.0", contracts: { tools: {} } }, null, 2)}\n`,
"utf8",
);
await writeFile(
path.join(rootDir, "src", "index.js"),
'import { definePluginEntry } from "openclaw/plugin-sdk";\nexport default definePluginEntry((api) => api.registerTool({ name: "weather" }));\n',
"utf8",
);
await writeFile(
path.join(rootDir, "plugin-inspector.config.json"),
`${JSON.stringify({ version: 1, plugin: { id: "weather", priority: "high", sourceRoot: "src" } }, null, 2)}\n`,
"utf8",
);
return rootDir;
}