marketplace-import-e2e.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // E2E: 验证「自定义市场导入」最新 commit (feat: add custom marketplace source import popover)
  2. // 触达的真实 agent 链路。
  3. //
  4. // UI 侧链路:AddMarketplacePopover -> PluginsSection.onAddMarketplace
  5. // -> pluginManagementStore.addMarketplace(source) -> agentService.addPluginMarketplace
  6. // -> 协议 plugins/marketplace/add -> server.ts dispatch -> 本文件验证的 facade。
  7. //
  8. // 本测试直接驱动 agent 侧 facade addZCodePluginMarketplace,喂入 popover 会原样下发的「自由文本字符串」,
  9. // 覆盖 parseMarketplaceSourceInput -> loadMarketplaceFromSource(读 manifest / 真实 git clone) -> 落盘,
  10. // 再用 getZCodePluginsOverview 复核(等价 popover 关闭后 UI refresh 能看到新市场)。
  11. // 每个用例独立临时 storageRoot 隔离。GitHub 用例先探测可达性,不可达则 skip 而非误判失败。
  12. import { execFile } from "node:child_process";
  13. import { existsSync } from "node:fs";
  14. import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  15. import { tmpdir } from "node:os";
  16. import { join } from "node:path";
  17. import { promisify } from "node:util";
  18. import { describe, expect, it } from "vitest";
  19. import { addZCodePluginMarketplace, getZCodePluginsOverview } from "../src/plugins.js";
  20. const execFileAsync = promisify(execFile);
  21. // popover 调用约定:source 为自由文本;pluginStorageRoot 真实运行时由 config 推导,
  22. // 这里显式覆盖到临时目录做隔离,等价「换一个干净工作区做导入」。
  23. function addViaFacade(source: string, storageRoot: string, workingDirectory: string) {
  24. return addZCodePluginMarketplace({
  25. source,
  26. pluginStorageRoot: storageRoot,
  27. workingDirectory,
  28. skipUserConfig: true,
  29. });
  30. }
  31. function overview(storageRoot: string, workingDirectory: string) {
  32. return getZCodePluginsOverview({
  33. pluginStorageRoot: storageRoot,
  34. workingDirectory,
  35. skipUserConfig: true,
  36. });
  37. }
  38. async function makeLocalMarketplace(root: string, name: string): Promise<void> {
  39. const pluginDir = join(root, "plugins", "hello");
  40. await mkdir(join(root, ".claude-plugin"), { recursive: true });
  41. await mkdir(join(pluginDir, ".claude-plugin"), { recursive: true });
  42. await mkdir(join(pluginDir, "skills", "hello"), { recursive: true });
  43. await writeFile(
  44. join(root, ".claude-plugin", "marketplace.json"),
  45. JSON.stringify(
  46. { name, plugins: [{ name: "hello", version: "1.0.0", source: "./plugins/hello" }] },
  47. null,
  48. 2,
  49. ),
  50. );
  51. await writeFile(
  52. join(pluginDir, ".claude-plugin", "plugin.json"),
  53. JSON.stringify({ name: "hello", version: "1.0.0", skills: "skills" }, null, 2),
  54. );
  55. await writeFile(join(pluginDir, "skills", "hello", "SKILL.md"), "# hello\n");
  56. }
  57. async function githubReachable(repoHttps: string): Promise<boolean> {
  58. try {
  59. await execFileAsync("git", ["ls-remote", "--heads", repoHttps], { timeout: 30_000 });
  60. return true;
  61. } catch {
  62. return false;
  63. }
  64. }
  65. describe("marketplace 自定义导入 E2E (真实 agent facade)", () => {
  66. it("本地目录导入:raw 目录路径 -> directory 来源 -> 落盘 + overview 可见", async () => {
  67. const base = await mkdtemp(join(tmpdir(), "zcode-e2e-localdir-"));
  68. const storageRoot = join(base, "storage");
  69. const market = join(base, "my-local-market");
  70. try {
  71. await makeLocalMarketplace(market, "e2e-local-dir-market");
  72. // popover 直接把目录路径作为 source 传入(未结构化)。
  73. const summary = await addViaFacade(market, storageRoot, base);
  74. expect(summary.id).toBe("e2e-local-dir-market");
  75. expect(summary.pluginCount).toBe(1);
  76. const knownPath = join(storageRoot, "known_marketplaces.json");
  77. expect(existsSync(knownPath)).toBe(true);
  78. const known = JSON.parse(await readFile(knownPath, "utf8")) as {
  79. marketplaces: Array<{ id: string; source: { source: string; path?: string } }>;
  80. };
  81. const rec = known.marketplaces.find((m) => m.id === "e2e-local-dir-market");
  82. expect(rec?.source.source).toBe("directory");
  83. expect(rec?.source.path).toBe(market);
  84. // manifest 复制进 storage,供后续 install 引用。
  85. const manifestCopy = join(storageRoot, "marketplaces", "e2e-local-dir-market", "marketplace.json");
  86. expect(existsSync(manifestCopy)).toBe(true);
  87. // 复核:overview 列出新市场(popover 关闭后 UI refresh 能看到它)。
  88. const ov = overview(storageRoot, base);
  89. expect(ov.marketplaces.some((m) => m.id === "e2e-local-dir-market")).toBe(true);
  90. } finally {
  91. await rm(base, { force: true, recursive: true });
  92. }
  93. });
  94. it("非法来源:facade 抛错(驱动 store 失败分支 -> 返回 false -> popover 保留输入),且不误落盘", async () => {
  95. const base = await mkdtemp(join(tmpdir(), "zcode-e2e-invalid-"));
  96. const storageRoot = join(base, "storage");
  97. try {
  98. // popover 里随手敲的一个不存在的本地路径。
  99. await expect(addViaFacade("./definitely-not-a-real-marketplace-xyz", storageRoot, base)).rejects.toThrow();
  100. // 失败时不应写 known_marketplaces.json。
  101. expect(existsSync(join(storageRoot, "known_marketplaces.json"))).toBe(false);
  102. } finally {
  103. await rm(base, { force: true, recursive: true });
  104. }
  105. });
  106. it("GitHub 导入:raw git URL -> git 来源 -> 真实 clone -> 落盘", async () => {
  107. // 选体积小且仓库根带 .claude-plugin/marketplace.json 的公开仓库,避免大仓库 clone 超时。
  108. const repoUrl = "https://github.com/wshobson/agents.git";
  109. if (!(await githubReachable(repoUrl))) {
  110. console.warn("[e2e] GitHub 不可达,跳过 git URL clone 用例");
  111. return;
  112. }
  113. const base = await mkdtemp(join(tmpdir(), "zcode-e2e-github-"));
  114. const storageRoot = join(base, "storage");
  115. try {
  116. // https .git -> parseMarketplaceSourceInput 归类 source:"git" -> 真实 clone --depth 1。
  117. const summary = await addViaFacade(repoUrl, storageRoot, base);
  118. // manifest.name 即市场 id(该仓库 manifest 名为 claude-code-workflows)。
  119. expect(summary.id).toBe("claude-code-workflows");
  120. expect(summary.pluginCount).toBeGreaterThan(0);
  121. const known = JSON.parse(await readFile(join(storageRoot, "known_marketplaces.json"), "utf8")) as {
  122. marketplaces: Array<{ id: string; source: { source: string; url?: string } }>;
  123. };
  124. const rec = known.marketplaces.find((m) => m.id === summary.id);
  125. expect(rec?.source.source).toBe("git");
  126. expect(rec?.source.url).toBe(repoUrl);
  127. // clone 出的 manifest 复制进 storage。
  128. const manifestCopy = join(storageRoot, "marketplaces", summary.id, "marketplace.json");
  129. expect(existsSync(manifestCopy)).toBe(true);
  130. } finally {
  131. await rm(base, { force: true, recursive: true });
  132. }
  133. }, 120_000);
  134. it("GitHub 简写导入:raw owner/repo -> github 来源 -> 真实 clone -> 落盘", async () => {
  135. const repo = "wshobson/agents";
  136. if (!(await githubReachable(`https://github.com/${repo}.git`))) {
  137. console.warn("[e2e] GitHub 不可达,跳过 owner/repo 简写用例");
  138. return;
  139. }
  140. const base = await mkdtemp(join(tmpdir(), "zcode-e2e-gh-short-"));
  141. const storageRoot = join(base, "storage");
  142. try {
  143. // owner/repo 简写 -> parseMarketplaceSourceInput 归类 source:"github"。
  144. const summary = await addViaFacade(repo, storageRoot, base);
  145. expect(summary.id).toBe("claude-code-workflows");
  146. const known = JSON.parse(await readFile(join(storageRoot, "known_marketplaces.json"), "utf8")) as {
  147. marketplaces: Array<{ id: string; source: { source: string; repo?: string } }>;
  148. };
  149. const rec = known.marketplaces.find((m) => m.id === summary.id);
  150. expect(rec?.source.source).toBe("github");
  151. expect(rec?.source.repo).toBe(repo);
  152. } finally {
  153. await rm(base, { force: true, recursive: true });
  154. }
  155. }, 120_000);
  156. });