|
| 1 | +import fs from "node:fs"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { |
| 6 | + copyBundledPluginMetadata, |
| 7 | + rewritePackageExtensions, |
| 8 | +} from "../../scripts/copy-bundled-plugin-metadata.mjs"; |
| 9 | + |
| 10 | +const tempDirs: string[] = []; |
| 11 | + |
| 12 | +function makeRepoRoot(prefix: string): string { |
| 13 | + const repoRoot = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); |
| 14 | + tempDirs.push(repoRoot); |
| 15 | + return repoRoot; |
| 16 | +} |
| 17 | + |
| 18 | +function writeJson(filePath: string, value: unknown): void { |
| 19 | + fs.mkdirSync(path.dirname(filePath), { recursive: true }); |
| 20 | + fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`, "utf8"); |
| 21 | +} |
| 22 | + |
| 23 | +afterEach(() => { |
| 24 | + for (const dir of tempDirs.splice(0, tempDirs.length)) { |
| 25 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 26 | + } |
| 27 | +}); |
| 28 | + |
| 29 | +describe("rewritePackageExtensions", () => { |
| 30 | + it("rewrites TypeScript extension entries to built JS paths", () => { |
| 31 | + expect(rewritePackageExtensions(["./index.ts", "./nested/entry.mts"])).toEqual([ |
| 32 | + "./index.js", |
| 33 | + "./nested/entry.js", |
| 34 | + ]); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe("copyBundledPluginMetadata", () => { |
| 39 | + it("copies plugin manifests, package metadata, and local skill directories", () => { |
| 40 | + const repoRoot = makeRepoRoot("openclaw-bundled-plugin-meta-"); |
| 41 | + const pluginDir = path.join(repoRoot, "extensions", "acpx"); |
| 42 | + fs.mkdirSync(path.join(pluginDir, "skills", "acp-router"), { recursive: true }); |
| 43 | + fs.writeFileSync( |
| 44 | + path.join(pluginDir, "skills", "acp-router", "SKILL.md"), |
| 45 | + "# ACP Router\n", |
| 46 | + "utf8", |
| 47 | + ); |
| 48 | + writeJson(path.join(pluginDir, "openclaw.plugin.json"), { |
| 49 | + id: "acpx", |
| 50 | + configSchema: { type: "object" }, |
| 51 | + skills: ["./skills"], |
| 52 | + }); |
| 53 | + writeJson(path.join(pluginDir, "package.json"), { |
| 54 | + name: "@openclaw/acpx", |
| 55 | + openclaw: { extensions: ["./index.ts"] }, |
| 56 | + }); |
| 57 | + |
| 58 | + copyBundledPluginMetadata({ repoRoot }); |
| 59 | + |
| 60 | + expect( |
| 61 | + fs.existsSync(path.join(repoRoot, "dist", "extensions", "acpx", "openclaw.plugin.json")), |
| 62 | + ).toBe(true); |
| 63 | + expect( |
| 64 | + fs.readFileSync( |
| 65 | + path.join(repoRoot, "dist", "extensions", "acpx", "skills", "acp-router", "SKILL.md"), |
| 66 | + "utf8", |
| 67 | + ), |
| 68 | + ).toContain("ACP Router"); |
| 69 | + const packageJson = JSON.parse( |
| 70 | + fs.readFileSync(path.join(repoRoot, "dist", "extensions", "acpx", "package.json"), "utf8"), |
| 71 | + ) as { openclaw?: { extensions?: string[] } }; |
| 72 | + expect(packageJson.openclaw?.extensions).toEqual(["./index.js"]); |
| 73 | + }); |
| 74 | + |
| 75 | + it("dereferences node_modules-backed skill paths into the bundled dist tree", () => { |
| 76 | + const repoRoot = makeRepoRoot("openclaw-bundled-plugin-node-modules-"); |
| 77 | + const pluginDir = path.join(repoRoot, "extensions", "tlon"); |
| 78 | + const storeSkillDir = path.join( |
| 79 | + repoRoot, |
| 80 | + "node_modules", |
| 81 | + ".pnpm", |
| 82 | + |
| 83 | + "node_modules", |
| 84 | + "@tloncorp", |
| 85 | + "tlon-skill", |
| 86 | + ); |
| 87 | + fs.mkdirSync(storeSkillDir, { recursive: true }); |
| 88 | + fs.writeFileSync(path.join(storeSkillDir, "SKILL.md"), "# Tlon Skill\n", "utf8"); |
| 89 | + fs.mkdirSync(path.join(pluginDir, "node_modules", "@tloncorp"), { recursive: true }); |
| 90 | + fs.symlinkSync( |
| 91 | + storeSkillDir, |
| 92 | + path.join(pluginDir, "node_modules", "@tloncorp", "tlon-skill"), |
| 93 | + process.platform === "win32" ? "junction" : "dir", |
| 94 | + ); |
| 95 | + writeJson(path.join(pluginDir, "openclaw.plugin.json"), { |
| 96 | + id: "tlon", |
| 97 | + configSchema: { type: "object" }, |
| 98 | + skills: ["node_modules/@tloncorp/tlon-skill"], |
| 99 | + }); |
| 100 | + writeJson(path.join(pluginDir, "package.json"), { |
| 101 | + name: "@openclaw/tlon", |
| 102 | + openclaw: { extensions: ["./index.ts"] }, |
| 103 | + }); |
| 104 | + |
| 105 | + copyBundledPluginMetadata({ repoRoot }); |
| 106 | + |
| 107 | + const copiedSkillDir = path.join( |
| 108 | + repoRoot, |
| 109 | + "dist", |
| 110 | + "extensions", |
| 111 | + "tlon", |
| 112 | + "node_modules", |
| 113 | + "@tloncorp", |
| 114 | + "tlon-skill", |
| 115 | + ); |
| 116 | + expect(fs.existsSync(path.join(copiedSkillDir, "SKILL.md"))).toBe(true); |
| 117 | + expect(fs.lstatSync(copiedSkillDir).isSymbolicLink()).toBe(false); |
| 118 | + }); |
| 119 | + |
| 120 | + it("omits missing declared skill paths from the bundled manifest", () => { |
| 121 | + const repoRoot = makeRepoRoot("openclaw-bundled-plugin-missing-skill-"); |
| 122 | + const pluginDir = path.join(repoRoot, "extensions", "tlon"); |
| 123 | + fs.mkdirSync(pluginDir, { recursive: true }); |
| 124 | + writeJson(path.join(pluginDir, "openclaw.plugin.json"), { |
| 125 | + id: "tlon", |
| 126 | + configSchema: { type: "object" }, |
| 127 | + skills: ["node_modules/@tloncorp/tlon-skill"], |
| 128 | + }); |
| 129 | + writeJson(path.join(pluginDir, "package.json"), { |
| 130 | + name: "@openclaw/tlon", |
| 131 | + openclaw: { extensions: ["./index.ts"] }, |
| 132 | + }); |
| 133 | + |
| 134 | + copyBundledPluginMetadata({ repoRoot }); |
| 135 | + |
| 136 | + const bundledManifest = JSON.parse( |
| 137 | + fs.readFileSync( |
| 138 | + path.join(repoRoot, "dist", "extensions", "tlon", "openclaw.plugin.json"), |
| 139 | + "utf8", |
| 140 | + ), |
| 141 | + ) as { skills?: string[] }; |
| 142 | + expect(bundledManifest.skills).toEqual([]); |
| 143 | + }); |
| 144 | +}); |
0 commit comments