|
| 1 | +// Verifies bundled plugin public-surface metadata assembly. |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { beforeEach, describe, expect, it } from "vitest"; |
| 5 | +import { listBundledPluginMetadata } from "./bundled-plugin-metadata.js"; |
| 6 | +import { |
| 7 | + createGeneratedPluginTempRoot, |
| 8 | + installGeneratedPluginTempRootCleanup, |
| 9 | + writeJson, |
| 10 | +} from "./generated-plugin-test-helpers.js"; |
| 11 | +import { clearPluginMetadataLifecycleCaches } from "./plugin-metadata-lifecycle.js"; |
| 12 | + |
| 13 | +installGeneratedPluginTempRootCleanup(); |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + clearPluginMetadataLifecycleCaches(); |
| 17 | +}); |
| 18 | + |
| 19 | +describe("bundled plugin public surfaces", () => { |
| 20 | + it("merges runtime channel schema metadata with manifest-owned channel config fields", () => { |
| 21 | + const tempRoot = createGeneratedPluginTempRoot("openclaw-bundled-plugin-channel-configs-"); |
| 22 | + |
| 23 | + writeJson(path.join(tempRoot, "extensions", "alpha", "package.json"), { |
| 24 | + name: "@openclaw/alpha", |
| 25 | + version: "0.0.1", |
| 26 | + openclaw: { |
| 27 | + extensions: ["./index.ts"], |
| 28 | + channel: { |
| 29 | + id: "alpha", |
| 30 | + label: "Alpha Root Label", |
| 31 | + blurb: "Alpha Root Description", |
| 32 | + preferOver: ["alpha-legacy"], |
| 33 | + }, |
| 34 | + }, |
| 35 | + }); |
| 36 | + writeJson(path.join(tempRoot, "extensions", "alpha", "openclaw.plugin.json"), { |
| 37 | + id: "alpha", |
| 38 | + channels: ["alpha"], |
| 39 | + configSchema: { type: "object" }, |
| 40 | + channelConfigs: { |
| 41 | + alpha: { |
| 42 | + schema: { type: "object", properties: { stale: { type: "boolean" } } }, |
| 43 | + label: "Manifest Label", |
| 44 | + uiHints: { |
| 45 | + "channels.alpha.explicitOnly": { |
| 46 | + help: "manifest hint", |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }); |
| 52 | + fs.writeFileSync( |
| 53 | + path.join(tempRoot, "extensions", "alpha", "index.ts"), |
| 54 | + "export {};\n", |
| 55 | + "utf8", |
| 56 | + ); |
| 57 | + fs.mkdirSync(path.join(tempRoot, "extensions", "alpha", "src"), { recursive: true }); |
| 58 | + fs.writeFileSync( |
| 59 | + path.join(tempRoot, "extensions", "alpha", "src", "config-schema.js"), |
| 60 | + [ |
| 61 | + "export const AlphaChannelConfigSchema = {", |
| 62 | + " schema: {", |
| 63 | + " type: 'object',", |
| 64 | + " properties: { generated: { type: 'string' } },", |
| 65 | + " },", |
| 66 | + " uiHints: {", |
| 67 | + " 'channels.alpha.generatedOnly': { help: 'generated hint' },", |
| 68 | + " },", |
| 69 | + "};", |
| 70 | + "", |
| 71 | + ].join("\n"), |
| 72 | + "utf8", |
| 73 | + ); |
| 74 | + const entries = listBundledPluginMetadata({ rootDir: tempRoot }); |
| 75 | + const channelConfigs = entries[0]?.manifest.channelConfigs as |
| 76 | + | Record<string, unknown> |
| 77 | + | undefined; |
| 78 | + expect(channelConfigs?.alpha).toEqual({ |
| 79 | + schema: { |
| 80 | + type: "object", |
| 81 | + properties: { |
| 82 | + generated: { type: "string" }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + label: "Manifest Label", |
| 86 | + description: "Alpha Root Description", |
| 87 | + preferOver: ["alpha-legacy"], |
| 88 | + uiHints: { |
| 89 | + "channels.alpha.generatedOnly": { help: "generated hint" }, |
| 90 | + "channels.alpha.explicitOnly": { help: "manifest hint" }, |
| 91 | + }, |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it("captures top-level public surface artifacts without duplicating the primary entrypoints", () => { |
| 96 | + const tempRoot = createGeneratedPluginTempRoot("openclaw-bundled-plugin-public-artifacts-"); |
| 97 | + |
| 98 | + writeJson(path.join(tempRoot, "extensions", "alpha", "package.json"), { |
| 99 | + name: "@openclaw/alpha", |
| 100 | + version: "0.0.1", |
| 101 | + openclaw: { |
| 102 | + extensions: ["./index.ts"], |
| 103 | + setupEntry: "./setup-entry.ts", |
| 104 | + }, |
| 105 | + }); |
| 106 | + writeJson(path.join(tempRoot, "extensions", "alpha", "openclaw.plugin.json"), { |
| 107 | + id: "alpha", |
| 108 | + configSchema: { type: "object" }, |
| 109 | + }); |
| 110 | + fs.writeFileSync( |
| 111 | + path.join(tempRoot, "extensions", "alpha", "index.ts"), |
| 112 | + "export {};\n", |
| 113 | + "utf8", |
| 114 | + ); |
| 115 | + fs.writeFileSync( |
| 116 | + path.join(tempRoot, "extensions", "alpha", "setup-entry.ts"), |
| 117 | + "export {};\n", |
| 118 | + "utf8", |
| 119 | + ); |
| 120 | + fs.writeFileSync(path.join(tempRoot, "extensions", "alpha", "api.ts"), "export {};\n", "utf8"); |
| 121 | + fs.writeFileSync( |
| 122 | + path.join(tempRoot, "extensions", "alpha", "runtime-api.ts"), |
| 123 | + "export {};\n", |
| 124 | + "utf8", |
| 125 | + ); |
| 126 | + const entries = listBundledPluginMetadata({ rootDir: tempRoot }); |
| 127 | + const firstEntry = entries[0] as |
| 128 | + | { |
| 129 | + publicSurfaceArtifacts?: string[]; |
| 130 | + runtimeSidecarArtifacts?: string[]; |
| 131 | + } |
| 132 | + | undefined; |
| 133 | + expect(firstEntry?.publicSurfaceArtifacts).toEqual(["api.js", "runtime-api.js"]); |
| 134 | + expect(firstEntry?.runtimeSidecarArtifacts).toEqual(["runtime-api.js"]); |
| 135 | + }); |
| 136 | + |
| 137 | + it("loads channel config metadata from built public surfaces in dist-only roots", () => { |
| 138 | + const tempRoot = createGeneratedPluginTempRoot("openclaw-bundled-plugin-dist-config-"); |
| 139 | + const distRoot = path.join(tempRoot, "dist"); |
| 140 | + |
| 141 | + writeJson(path.join(distRoot, "extensions", "alpha", "package.json"), { |
| 142 | + name: "@openclaw/alpha", |
| 143 | + version: "0.0.1", |
| 144 | + openclaw: { |
| 145 | + extensions: ["./index.ts"], |
| 146 | + channel: { |
| 147 | + id: "alpha", |
| 148 | + label: "Alpha Root Label", |
| 149 | + blurb: "Alpha Root Description", |
| 150 | + }, |
| 151 | + }, |
| 152 | + }); |
| 153 | + writeJson(path.join(distRoot, "extensions", "alpha", "openclaw.plugin.json"), { |
| 154 | + id: "alpha", |
| 155 | + configSchema: { |
| 156 | + type: "object", |
| 157 | + properties: {}, |
| 158 | + }, |
| 159 | + channels: ["alpha"], |
| 160 | + channelConfigs: { |
| 161 | + alpha: { |
| 162 | + schema: { type: "object", properties: { stale: { type: "boolean" } } }, |
| 163 | + uiHints: { |
| 164 | + "channels.alpha.explicitOnly": { |
| 165 | + help: "manifest hint", |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + }); |
| 171 | + fs.writeFileSync( |
| 172 | + path.join(distRoot, "extensions", "alpha", "index.js"), |
| 173 | + "export {};\n", |
| 174 | + "utf8", |
| 175 | + ); |
| 176 | + fs.writeFileSync( |
| 177 | + path.join(distRoot, "extensions", "alpha", "channel-config-api.js"), |
| 178 | + [ |
| 179 | + "export const AlphaChannelConfigSchema = {", |
| 180 | + " schema: {", |
| 181 | + " type: 'object',", |
| 182 | + " properties: { built: { type: 'string' } },", |
| 183 | + " },", |
| 184 | + " uiHints: {", |
| 185 | + " 'channels.alpha.generatedOnly': { help: 'built hint' },", |
| 186 | + " },", |
| 187 | + "};", |
| 188 | + "", |
| 189 | + ].join("\n"), |
| 190 | + "utf8", |
| 191 | + ); |
| 192 | + const entries = listBundledPluginMetadata({ rootDir: distRoot }); |
| 193 | + const channelConfigs = entries[0]?.manifest.channelConfigs as |
| 194 | + | Record<string, unknown> |
| 195 | + | undefined; |
| 196 | + expect(channelConfigs?.alpha).toEqual({ |
| 197 | + schema: { |
| 198 | + type: "object", |
| 199 | + properties: { |
| 200 | + built: { type: "string" }, |
| 201 | + }, |
| 202 | + }, |
| 203 | + label: "Alpha Root Label", |
| 204 | + description: "Alpha Root Description", |
| 205 | + uiHints: { |
| 206 | + "channels.alpha.generatedOnly": { help: "built hint" }, |
| 207 | + "channels.alpha.explicitOnly": { help: "manifest hint" }, |
| 208 | + }, |
| 209 | + }); |
| 210 | + }); |
| 211 | + |
| 212 | + it("does not probe broad runtime public surfaces for channel config metadata", () => { |
| 213 | + const tempRoot = createGeneratedPluginTempRoot("openclaw-bundled-plugin-dist-config-runtime-"); |
| 214 | + const distRoot = path.join(tempRoot, "dist"); |
| 215 | + const markerPath = path.join(tempRoot, "runtime-api-loaded"); |
| 216 | + |
| 217 | + writeJson(path.join(distRoot, "extensions", "alpha", "package.json"), { |
| 218 | + name: "@openclaw/alpha", |
| 219 | + version: "0.0.1", |
| 220 | + openclaw: { |
| 221 | + extensions: ["./index.ts"], |
| 222 | + channel: { |
| 223 | + id: "alpha", |
| 224 | + label: "Alpha Root Label", |
| 225 | + blurb: "Alpha Root Description", |
| 226 | + }, |
| 227 | + }, |
| 228 | + }); |
| 229 | + writeJson(path.join(distRoot, "extensions", "alpha", "openclaw.plugin.json"), { |
| 230 | + id: "alpha", |
| 231 | + configSchema: { |
| 232 | + type: "object", |
| 233 | + properties: {}, |
| 234 | + }, |
| 235 | + channels: ["alpha"], |
| 236 | + channelConfigs: { |
| 237 | + alpha: { |
| 238 | + schema: { type: "object", properties: { manifest: { type: "boolean" } } }, |
| 239 | + }, |
| 240 | + }, |
| 241 | + }); |
| 242 | + fs.writeFileSync( |
| 243 | + path.join(distRoot, "extensions", "alpha", "index.js"), |
| 244 | + "export {};\n", |
| 245 | + "utf8", |
| 246 | + ); |
| 247 | + fs.writeFileSync( |
| 248 | + path.join(distRoot, "extensions", "alpha", "runtime-api.js"), |
| 249 | + [ |
| 250 | + "import fs from 'node:fs';", |
| 251 | + `fs.writeFileSync(${JSON.stringify(markerPath)}, "loaded", "utf8");`, |
| 252 | + "export const AlphaChannelConfigSchema = {", |
| 253 | + " schema: { type: 'object', properties: { runtimeApi: { type: 'string' } } },", |
| 254 | + "};", |
| 255 | + "", |
| 256 | + ].join("\n"), |
| 257 | + "utf8", |
| 258 | + ); |
| 259 | + fs.writeFileSync( |
| 260 | + path.join(distRoot, "extensions", "alpha", "api.js"), |
| 261 | + [ |
| 262 | + "import fs from 'node:fs';", |
| 263 | + `fs.writeFileSync(${JSON.stringify(markerPath)}, "loaded", "utf8");`, |
| 264 | + "export const AlphaChannelConfigSchema = {", |
| 265 | + " schema: { type: 'object', properties: { api: { type: 'string' } } },", |
| 266 | + "};", |
| 267 | + "", |
| 268 | + ].join("\n"), |
| 269 | + "utf8", |
| 270 | + ); |
| 271 | + const entries = listBundledPluginMetadata({ rootDir: distRoot }); |
| 272 | + const channelConfigs = entries[0]?.manifest.channelConfigs as |
| 273 | + | Record<string, unknown> |
| 274 | + | undefined; |
| 275 | + expect(channelConfigs?.alpha).toEqual({ |
| 276 | + schema: { |
| 277 | + type: "object", |
| 278 | + properties: { |
| 279 | + manifest: { type: "boolean" }, |
| 280 | + }, |
| 281 | + }, |
| 282 | + label: "Alpha Root Label", |
| 283 | + description: "Alpha Root Description", |
| 284 | + }); |
| 285 | + expect(fs.existsSync(markerPath)).toBe(false); |
| 286 | + }); |
| 287 | +}); |
0 commit comments