marketplace-import-network.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { execFile } from "node:child_process";
  2. import { existsSync } from "node:fs";
  3. import { mkdtemp, readFile, rm } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { join } from "node:path";
  6. import { promisify } from "node:util";
  7. import { describe, expect, it } from "vitest";
  8. import { addZCodePluginMarketplace } from "../src/plugins.js";
  9. const execFileAsync = promisify(execFile);
  10. function addViaFacade(source: string, storageRoot: string, workingDirectory: string) {
  11. return addZCodePluginMarketplace({
  12. source,
  13. pluginStorageRoot: storageRoot,
  14. workingDirectory,
  15. skipUserConfig: true,
  16. });
  17. }
  18. async function githubReachable(repoHttps: string): Promise<boolean> {
  19. try {
  20. await execFileAsync("git", ["ls-remote", "--heads", repoHttps], { timeout: 30_000 });
  21. return true;
  22. } catch {
  23. return false;
  24. }
  25. }
  26. // 真实外网导入已多次定向复跑失败;拆分后闭源保留,开源只排除网络用例。
  27. describe("marketplace 自定义导入 E2E (真实 agent facade)", () => {
  28. it("GitHub 导入:raw git URL -> git 来源 -> 真实 clone -> 落盘", async () => {
  29. // 选体积小且仓库根带 .claude-plugin/marketplace.json 的公开仓库,避免大仓库 clone 超时。
  30. const repoUrl = "https://github.com/wshobson/agents.git";
  31. if (!(await githubReachable(repoUrl))) {
  32. console.warn("[e2e] GitHub 不可达,跳过 git URL clone 用例");
  33. return;
  34. }
  35. const base = await mkdtemp(join(tmpdir(), "zcode-e2e-github-"));
  36. const storageRoot = join(base, "storage");
  37. try {
  38. // https .git -> parseMarketplaceSourceInput 归类 source:"git" -> 真实 clone --depth 1。
  39. const summary = await addViaFacade(repoUrl, storageRoot, base);
  40. // manifest.name 即市场 id(该仓库 manifest 名为 claude-code-workflows)。
  41. expect(summary.id).toBe("claude-code-workflows");
  42. expect(summary.pluginCount).toBeGreaterThan(0);
  43. const known = JSON.parse(
  44. await readFile(join(storageRoot, "known_marketplaces.json"), "utf8"),
  45. ) as {
  46. marketplaces: Array<{ id: string; source: { source: string; url?: string } }>;
  47. };
  48. const rec = known.marketplaces.find((m) => m.id === summary.id);
  49. expect(rec?.source.source).toBe("git");
  50. expect(rec?.source.url).toBe(repoUrl);
  51. // clone 出的 manifest 复制进 storage。
  52. const manifestCopy = join(storageRoot, "marketplaces", summary.id, "marketplace.json");
  53. expect(existsSync(manifestCopy)).toBe(true);
  54. } finally {
  55. await rm(base, { force: true, recursive: true });
  56. }
  57. }, 120_000);
  58. it("GitHub 简写导入:raw owner/repo -> github 来源 -> 真实 clone -> 落盘", async () => {
  59. const repo = "wshobson/agents";
  60. if (!(await githubReachable(`https://github.com/${repo}.git`))) {
  61. console.warn("[e2e] GitHub 不可达,跳过 owner/repo 简写用例");
  62. return;
  63. }
  64. const base = await mkdtemp(join(tmpdir(), "zcode-e2e-gh-short-"));
  65. const storageRoot = join(base, "storage");
  66. try {
  67. // owner/repo 简写 -> parseMarketplaceSourceInput 归类 source:"github"。
  68. const summary = await addViaFacade(repo, storageRoot, base);
  69. expect(summary.id).toBe("claude-code-workflows");
  70. const known = JSON.parse(
  71. await readFile(join(storageRoot, "known_marketplaces.json"), "utf8"),
  72. ) as {
  73. marketplaces: Array<{ id: string; source: { source: string; repo?: string } }>;
  74. };
  75. const rec = known.marketplaces.find((m) => m.id === summary.id);
  76. expect(rec?.source.source).toBe("github");
  77. expect(rec?.source.repo).toBe(repo);
  78. } finally {
  79. await rm(base, { force: true, recursive: true });
  80. }
  81. }, 120_000);
  82. });