File size: 9,104 Bytes
fcd8223 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | /** Ensures bundled plugin command manifests are scanned without loading command runtimes. */
import fs from "node:fs/promises";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { createTempDirTracker } from "../../test/helpers/temp-dir.js";
import { withEnvAsync } from "../test-utils/env.js";
import type { PluginManifestRecord } from "./manifest-registry.js";
const mocks = vi.hoisted(() => ({
plugins: [] as PluginManifestRecord[],
warn: vi.fn(),
}));
vi.mock("../logging/subsystem.js", () => ({
createSubsystemLogger: () => ({ warn: mocks.warn }),
}));
vi.mock("./manifest-registry.js", () => ({
loadPluginManifestRegistryCore: () => ({ diagnostics: [], plugins: mocks.plugins }),
}));
vi.mock("./plugin-registry-contributions.js", () => ({
loadPluginManifestRegistryForPluginRegistry: () => ({ diagnostics: [], plugins: mocks.plugins }),
}));
vi.mock("./config-state.js", () => ({
hasExplicitPluginConfig: (plugins?: { entries?: Record<string, unknown> }) =>
Boolean(plugins?.entries && Object.keys(plugins.entries).length > 0),
normalizePluginsConfig: (plugins?: unknown) => plugins,
resolveEffectivePluginActivationState: (params: {
config?: { entries?: Record<string, { enabled?: boolean }> };
id: string;
}) => ({
activated: params.config?.entries?.[params.id]?.enabled !== false,
}),
}));
const { loadEnabledClaudeBundleCommands } = await import("./bundle-commands.js");
const tempDirs = createTempDirTracker();
afterEach(() => {
mocks.plugins = [];
mocks.warn.mockReset();
tempDirs.cleanup();
});
function resolveBundlePluginRoot(homeDir: string, pluginId: string) {
return path.join(homeDir, ".openclaw", "extensions", pluginId);
}
async function writeClaudeBundleCommandFixture(params: {
homeDir: string;
pluginId: string;
commands: Array<{ relativePath: string; contents: string[] }>;
}) {
const pluginRoot = resolveBundlePluginRoot(params.homeDir, params.pluginId);
await fs.mkdir(path.join(pluginRoot, ".claude-plugin"), { recursive: true });
await fs.writeFile(
path.join(pluginRoot, ".claude-plugin", "plugin.json"),
`${JSON.stringify({ name: params.pluginId }, null, 2)}\n`,
"utf-8",
);
await Promise.all(
params.commands.map(async (command) => {
const filePath = path.join(pluginRoot, command.relativePath);
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, [...command.contents, ""].join("\n"), "utf-8");
}),
);
mocks.plugins = [
{
id: params.pluginId,
format: "bundle",
bundleFormat: "claude",
bundleCapabilities: ["commands"],
origin: "global",
rootDir: pluginRoot,
} as PluginManifestRecord,
];
}
function expectEnabledClaudeBundleCommands(
commands: ReturnType<typeof loadEnabledClaudeBundleCommands>,
expected: Array<{
pluginId: string;
rawName: string;
description: string;
promptTemplate: string;
sourceFilePath: string;
}>,
) {
expect(commands).toEqual(expected);
}
describe("loadEnabledClaudeBundleCommands", () => {
it("loads enabled Claude bundle markdown commands and honors invocation policy", async () => {
const homeDir = tempDirs.make("openclaw-bundle-commands-home-");
const workspaceDir = tempDirs.make("openclaw-bundle-commands-workspace-");
await withEnvAsync(
{
HOME: homeDir,
USERPROFILE: homeDir,
OPENCLAW_HOME: undefined,
OPENCLAW_STATE_DIR: undefined,
},
async () => {
await writeClaudeBundleCommandFixture({
homeDir,
pluginId: "compound-bundle",
commands: [
{
relativePath: "commands/office-hours.md",
contents: [
"---",
"description: Help with scoping and architecture",
"---",
"Give direct engineering advice.",
],
},
{
relativePath: "commands/workflows/review.md",
contents: [
"---",
"name: workflows:review",
"description: Run a structured review",
"---",
"Review the code. $ARGUMENTS",
],
},
{
relativePath: "commands/model-hidden.md",
contents: ["---", "disable-model-invocation: on", "---", "Manual only."],
},
{
relativePath: "commands/user-enabled.md",
contents: ["---", "user-invocable: on", "---", "User enabled."],
},
{
relativePath: "commands/user-hidden.md",
contents: ["---", "user-invocable: off", "---", "User hidden."],
},
{
relativePath: "commands/user-hidden-false.md",
contents: ["---", "user-invocable: false", "---", "User hidden."],
},
{
relativePath: "commands/not-frontmatter.md",
contents: ["---not", "name: nope", "---not", "Treat this as Markdown."],
},
],
});
const commands = loadEnabledClaudeBundleCommands({
workspaceDir,
cfg: {
plugins: {
entries: { "compound-bundle": { enabled: true } },
},
},
});
expectEnabledClaudeBundleCommands(commands, [
{
pluginId: "compound-bundle",
rawName: "model-hidden",
description: "Manual only.",
promptTemplate: "Manual only.",
sourceFilePath: path.join(
resolveBundlePluginRoot(homeDir, "compound-bundle"),
"commands",
"model-hidden.md",
),
},
{
pluginId: "compound-bundle",
rawName: "not-frontmatter",
description: "---not",
promptTemplate: "---not\nname: nope\n---not\nTreat this as Markdown.",
sourceFilePath: path.join(
resolveBundlePluginRoot(homeDir, "compound-bundle"),
"commands",
"not-frontmatter.md",
),
},
{
pluginId: "compound-bundle",
rawName: "office-hours",
description: "Help with scoping and architecture",
promptTemplate: "Give direct engineering advice.",
sourceFilePath: path.join(
resolveBundlePluginRoot(homeDir, "compound-bundle"),
"commands",
"office-hours.md",
),
},
{
pluginId: "compound-bundle",
rawName: "user-enabled",
description: "User enabled.",
promptTemplate: "User enabled.",
sourceFilePath: path.join(
resolveBundlePluginRoot(homeDir, "compound-bundle"),
"commands",
"user-enabled.md",
),
},
{
pluginId: "compound-bundle",
rawName: "workflows:review",
description: "Run a structured review",
promptTemplate: "Review the code. $ARGUMENTS",
sourceFilePath: path.join(
resolveBundlePluginRoot(homeDir, "compound-bundle"),
"commands",
"workflows",
"review.md",
),
},
]);
const rawNames = commands.map((entry) => entry.rawName);
expect(rawNames).not.toContain("user-hidden");
expect(rawNames).not.toContain("user-hidden-false");
},
);
});
it("warns and skips oversized bundle commands without dropping siblings", async () => {
const homeDir = tempDirs.make("openclaw-bundle-commands-oversized-");
const workspaceDir = tempDirs.make("openclaw-bundle-commands-oversized-ws-");
await writeClaudeBundleCommandFixture({
homeDir,
pluginId: "oversized-test",
commands: [
{
relativePath: "commands/normal.md",
contents: [
"---",
"description: Normal command that should be loaded",
"---",
"This is a normal command.",
],
},
],
});
const pluginRoot = resolveBundlePluginRoot(homeDir, "oversized-test");
const oversizedFilePath = path.join(pluginRoot, "commands", "oversized.md");
await fs.mkdir(path.dirname(oversizedFilePath), { recursive: true });
const oversizedContent = Buffer.alloc(1 * 1024 * 1024 + 1, "x");
await fs.writeFile(oversizedFilePath, oversizedContent);
const commands = loadEnabledClaudeBundleCommands({
workspaceDir,
cfg: {
plugins: {
entries: { "oversized-test": { enabled: true } },
},
},
});
expect(commands.map((entry) => entry.rawName)).toEqual(["normal"]);
expect(mocks.warn).toHaveBeenCalledOnce();
const warning = String(mocks.warn.mock.calls[0]?.[0]);
expect(warning).toContain(oversizedFilePath);
expect(warning).toContain("1048576");
});
});
|