scaffold.test.mjs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { mkdtemp, readFile, writeFile, mkdir, rm, symlink } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import assert from "node:assert/strict";
  5. import { test } from "node:test";
  6. import { createPlugin } from "../skills/plugin-creator/scripts/create-basic-plugin.mjs";
  7. import { preflightPlugin } from "../skills/plugin-creator/scripts/validate-plugin.mjs";
  8. test("scaffold creates valid resources and preserves marketplace data on update", async () => {
  9. const root = await mkdtemp(join(tmpdir(), "plugin-creator-"));
  10. try {
  11. const marketplacePath = join(root, ".claude-plugin", "marketplace.json");
  12. await mkdir(join(root, ".claude-plugin"));
  13. await writeFile(
  14. marketplacePath,
  15. JSON.stringify({
  16. name: "personal",
  17. owner: { name: "User" },
  18. custom: 42,
  19. plugins: [{ name: "other", source: "./other" }],
  20. }),
  21. );
  22. const plugin = await createPlugin({
  23. name: "My Plugin",
  24. parentPath: join(root, "plugins"),
  25. marketplacePath,
  26. components: ["skills", "mcp", "hooks"],
  27. });
  28. const manifest = JSON.parse(
  29. await readFile(join(plugin, ".zcode-plugin", "plugin.json"), "utf8"),
  30. );
  31. assert.equal(manifest.name, "my-plugin");
  32. assert.deepEqual(await preflightPlugin(plugin), []);
  33. const market = JSON.parse(await readFile(marketplacePath, "utf8"));
  34. assert.equal(market.custom, 42);
  35. assert.deepEqual(
  36. market.plugins.map((p) => p.name),
  37. ["other", "my-plugin"],
  38. );
  39. assert.equal(market.plugins[1].source, "./plugins/my-plugin");
  40. assert.equal(market.plugins[1].policy, undefined);
  41. await assert.rejects(
  42. createPlugin({ name: "My Plugin", parentPath: join(root, "plugins"), marketplacePath }),
  43. /exist/i,
  44. );
  45. await createPlugin({
  46. name: "My Plugin",
  47. parentPath: join(root, "plugins"),
  48. marketplacePath,
  49. force: true,
  50. });
  51. const updated = JSON.parse(await readFile(marketplacePath, "utf8"));
  52. assert.deepEqual(
  53. updated.plugins.map((p) => p.name),
  54. ["other", "my-plugin"],
  55. );
  56. assert.equal(updated.owner.name, "User");
  57. } finally {
  58. await rm(root, { recursive: true, force: true });
  59. }
  60. });
  61. test("rejects invalid identifiers, escaping marketplace sources, placeholders and symlink outputs", async () => {
  62. const root = await mkdtemp(join(tmpdir(), "creator-negative-"));
  63. try {
  64. for (const name of ["!!!", "../escape", "x".repeat(129)])
  65. await assert.rejects(createPlugin({ name, parentPath: root }));
  66. await assert.rejects(
  67. createPlugin({
  68. name: "demo",
  69. parentPath: join(root, "outside"),
  70. marketplacePath: join(root, "market", "marketplace.json"),
  71. }),
  72. /outside|escape/i,
  73. );
  74. const plugin = await createPlugin({ name: "valid", parentPath: root });
  75. const manifestPath = join(plugin, ".zcode-plugin", "plugin.json");
  76. const manifest = JSON.parse(await readFile(manifestPath, "utf8"));
  77. manifest.description = "[TODO: description]";
  78. manifest.skills = "../outside";
  79. await writeFile(manifestPath, JSON.stringify(manifest));
  80. const errors = await preflightPlugin(plugin);
  81. assert(errors.some((e) => e.includes("TODO")));
  82. assert(errors.some((e) => /outside|escape/.test(e)));
  83. if (process.platform !== "win32") {
  84. await mkdir(join(root, "actual"));
  85. await symlink(join(root, "actual"), join(root, "linked"));
  86. await assert.rejects(
  87. createPlugin({ name: "linked", parentPath: root, force: true }),
  88. /symbolic/i,
  89. );
  90. }
  91. } finally {
  92. await rm(root, { recursive: true, force: true });
  93. }
  94. });
  95. test("generated MCP scaffold completes initialization and exposes its empty tool list", async () => {
  96. const { spawn } = await import("node:child_process");
  97. const root = await mkdtemp(join(tmpdir(), "creator-mcp-"));
  98. try {
  99. const plugin = await createPlugin({
  100. name: "mcp-smoke",
  101. parentPath: root,
  102. components: ["mcp", "hooks"],
  103. });
  104. const child = spawn(process.execPath, [join(plugin, "scripts", "mcp-server.mjs")], {
  105. stdio: ["pipe", "pipe", "pipe"],
  106. });
  107. let output = "";
  108. child.stdout.on("data", (chunk) => {
  109. output += chunk;
  110. });
  111. const done = new Promise((resolve, reject) => {
  112. child.on("error", reject);
  113. child.on("close", resolve);
  114. });
  115. child.stdin.end(
  116. JSON.stringify({
  117. jsonrpc: "2.0",
  118. id: 1,
  119. method: "initialize",
  120. params: { protocolVersion: "2025-03-26" },
  121. }) +
  122. "\n" +
  123. JSON.stringify({ jsonrpc: "2.0", id: 2, method: "tools/list" }) +
  124. "\n",
  125. );
  126. assert.equal(await done, 0);
  127. const responses = output
  128. .trim()
  129. .split("\n")
  130. .map((line) => JSON.parse(line));
  131. assert.equal(responses[0].result.serverInfo.name, "mcp-smoke");
  132. assert.deepEqual(responses[1].result.tools, []);
  133. } finally {
  134. await rm(root, { recursive: true, force: true });
  135. }
  136. });
  137. test("rejects placeholders in skill content, duplicate market entries and symlinked parent escapes", async () => {
  138. const root = await mkdtemp(join(tmpdir(), "creator-integrity-"));
  139. try {
  140. const plugin = await createPlugin({
  141. name: "content",
  142. parentPath: root,
  143. components: ["skills"],
  144. });
  145. await writeFile(
  146. join(plugin, "skills", "content", "SKILL.md"),
  147. "---\nname: content\ndescription: TODO\n---\nTODO: implement this skill\n",
  148. );
  149. assert.ok((await preflightPlugin(plugin)).some((error) => /placeholder/i.test(error)));
  150. const marketplacePath = join(root, "marketplace.json");
  151. await writeFile(
  152. marketplacePath,
  153. JSON.stringify({
  154. name: "personal",
  155. plugins: [
  156. { name: "dup", source: "./first" },
  157. { name: "dup", source: "./second" },
  158. ],
  159. }),
  160. );
  161. await assert.rejects(
  162. createPlugin({ name: "dup", parentPath: root, marketplacePath, force: true }),
  163. /duplicate/i,
  164. );
  165. if (process.platform !== "win32") {
  166. await mkdir(join(root, "market"));
  167. await symlink(root, join(root, "market", "plugins"));
  168. await assert.rejects(
  169. createPlugin({
  170. name: "escape",
  171. parentPath: join(root, "market", "plugins"),
  172. marketplacePath: join(root, "market", "marketplace.json"),
  173. }),
  174. /symbolic/i,
  175. );
  176. }
  177. } finally {
  178. await rm(root, { recursive: true, force: true });
  179. }
  180. });