// Provides test support for bundled MCP plugin packaging checks. import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { withEnvAsync } from "../test-utils/env.js"; export function createBundleMcpTempHarness() { const tempDirs: string[] = []; return { async createTempDir(prefix: string): Promise { const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix)); tempDirs.push(dir); return dir; }, async cleanup() { await Promise.all( tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })), ); }, }; } export function resolveBundlePluginRoot(homeDir: string, pluginId: string) { return path.join(homeDir, ".openclaw", "extensions", pluginId); } export async function writeBundleTextFiles( pluginRoot: string, files: Record, ): Promise { await Promise.all( Object.entries(files).map(async ([relativePath, content]) => { const filePath = path.join(pluginRoot, relativePath); await fs.mkdir(path.dirname(filePath), { recursive: true }); await fs.writeFile(filePath, content, "utf-8"); }), ); } export async function writeClaudeBundleManifest(params: { homeDir: string; pluginId: string; manifest: Record; }) { 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(params.manifest, null, 2)}\n`, "utf-8", ); return pluginRoot; } export function createEnabledPluginEntries(pluginIds: readonly string[]) { return Object.fromEntries(pluginIds.map((pluginId) => [pluginId, { enabled: true }])); } export async function createBundleProbePlugin(homeDir: string) { const pluginRoot = resolveBundlePluginRoot(homeDir, "bundle-probe"); const serverPath = path.join(pluginRoot, "servers", "probe.mjs"); await fs.mkdir(path.dirname(serverPath), { recursive: true }); await fs.writeFile(serverPath, "export {};\n", "utf-8"); await writeClaudeBundleManifest({ homeDir, pluginId: "bundle-probe", manifest: { name: "bundle-probe" }, }); await fs.writeFile( path.join(pluginRoot, ".mcp.json"), `${JSON.stringify( { mcpServers: { bundleProbe: { command: "node", args: ["./servers/probe.mjs"], }, }, }, null, 2, )}\n`, "utf-8", ); return { pluginRoot, serverPath }; } export async function withBundleHomeEnv( tempHarness: { createTempDir: (prefix: string) => Promise }, prefix: string, run: (params: { homeDir: string; workspaceDir: string }) => Promise, ): Promise { const homeDir = await tempHarness.createTempDir(`${prefix}-home-`); const workspaceDir = await tempHarness.createTempDir(`${prefix}-workspace-`); return withEnvAsync( { HOME: homeDir, USERPROFILE: homeDir, OPENCLAW_HOME: undefined, OPENCLAW_STATE_DIR: undefined, }, () => run({ homeDir, workspaceDir }), ); }