|
| 1 | +import { test } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { cpSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { dirname, resolve } from "node:path"; |
| 6 | +import { fileURLToPath } from "node:url"; |
| 7 | +import { spawnSync } from "node:child_process"; |
| 8 | + |
| 9 | +const root = resolve(dirname(fileURLToPath(import.meta.url)), ".."); |
| 10 | +const schema = { type: "object", properties: { TOKEN: { type: "string" } } }; |
| 11 | +const config = (value) => ({ mcpServers: { example: { url: "https://example.com/mcp", headers: { Authorization: value } } } }); |
| 12 | + |
| 13 | +function validate(t, { plugin = {}, entry = {}, files = {} } = {}) { |
| 14 | + const dir = mkdtempSync(resolve(tmpdir(), "plugin-validation-")); |
| 15 | + t.after(() => rmSync(dir, { recursive: true, force: true })); |
| 16 | + mkdirSync(resolve(dir, "scripts")); |
| 17 | + cpSync(resolve(root, "scripts/validate-plugins.mjs"), resolve(dir, "scripts/validate-plugins.mjs")); |
| 18 | + cpSync(resolve(root, "schemas"), resolve(dir, "schemas"), { recursive: true }); |
| 19 | + symlinkSync(resolve(root, "node_modules"), resolve(dir, "node_modules"), process.platform === "win32" ? "junction" : "dir"); |
| 20 | + const contents = { |
| 21 | + ".cursor-plugin/marketplace.json": { name: "test", owner: { name: "Test" }, plugins: [{ name: "example", source: "example", ...entry }] }, |
| 22 | + "example/.cursor-plugin/plugin.json": { name: "example", ...plugin }, |
| 23 | + ...Object.fromEntries(Object.entries(files).map(([path, content]) => [`example/${path}`, content])), |
| 24 | + }; |
| 25 | + for (const [path, content] of Object.entries(contents)) { |
| 26 | + mkdirSync(dirname(resolve(dir, path)), { recursive: true }); |
| 27 | + writeFileSync(resolve(dir, path), typeof content === "string" ? content : JSON.stringify(content)); |
| 28 | + } |
| 29 | + return spawnSync(process.execPath, [resolve(dir, "scripts/validate-plugins.mjs")], { encoding: "utf8" }); |
| 30 | +} |
| 31 | + |
| 32 | +for (const [name, plugin, files] of [ |
| 33 | + ["default discovery", {}, { "mcp.json": config("Bearer ${TYPO}") }], |
| 34 | + ["explicit path", { mcpServers: "configs/server.json" }, { "configs/server.json": config("${TYPO}") }], |
| 35 | + ["inline config", { mcpServers: config("${TYPO}") }, {}], |
| 36 | + ["mixed array", { mcpServers: [config("${TOKEN}"), "other.json"], variables: schema }, { "other.json": config("${TYPO}") }], |
| 37 | + ["no declared schema", { mcpServers: config("${TOKEN}") }, {}], |
| 38 | + ["inherited Object property", { mcpServers: config("${toString}"), variables: schema }, {}], |
| 39 | +]) { |
| 40 | + test(`rejects undeclared variables in ${name}`, (t) => { |
| 41 | + const result = validate(t, { plugin, files }); |
| 42 | + assert.equal(result.status, 1, result.stderr); |
| 43 | + assert.match(result.stderr, /references undeclared variable/); |
| 44 | + assert.match(result.stderr, /variables.properties/); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +test("declared variables, defaults and runtime interpolation remain valid", (t) => { |
| 49 | + const result = validate(t, { plugin: { variables: schema, mcpServers: { |
| 50 | + mcpServers: { example: { command: "${CURSOR_PLUGIN_ROOT}/bin/server", cwd: "${CLAUDE_PLUGIN_ROOT}", args: ["${workspaceFolder}", "${workspaceFolderBasename}", "${userHome}", "${pathSeparator}", "${/}"], env: { TOKEN: "${TOKEN}", OTHER: "${env:OTHER}", OPTIONAL: "${CLIENT_SECRET:-}" } } }, |
| 51 | + } } }); |
| 52 | + assert.equal(result.status, 0, result.stderr); |
| 53 | +}); |
| 54 | + |
| 55 | +test("declared variables work in discovered files and mixed arrays", (t) => { |
| 56 | + for (const mcpServers of [undefined, [config("${TOKEN}"), "mcp.json"]]) { |
| 57 | + const result = validate(t, { plugin: { variables: schema, ...(mcpServers === undefined ? {} : { mcpServers }) }, files: { "mcp.json": config("${TOKEN}") } }); |
| 58 | + assert.equal(result.status, 0, result.stderr); |
| 59 | + } |
| 60 | +}); |
| 61 | + |
| 62 | +test("explicit MCP configuration replaces default discovery", (t) => { |
| 63 | + const result = validate(t, { plugin: { mcpServers: config("literal") }, files: { "mcp.json": config("${TYPO}") } }); |
| 64 | + assert.equal(result.status, 0, result.stderr); |
| 65 | +}); |
| 66 | + |
| 67 | +for (const [name, files] of [["missing", {}], ["invalid JSON", { "custom.json": "{broken" }]]) { |
| 68 | + test(`reports ${name} MCP files with plugin context`, (t) => { |
| 69 | + const result = validate(t, { plugin: { mcpServers: "custom.json" }, files }); |
| 70 | + assert.equal(result.status, 1, result.stderr); |
| 71 | + assert.match(result.stderr, /Plugin "example": cannot read MCP config "custom.json"/); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +test("plugins without MCP configuration remain valid", (t) => { |
| 76 | + assert.equal(validate(t).status, 0); |
| 77 | +}); |
0 commit comments