marketplace-import-e2e.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 隔离。真实外网导入用例见 marketplace-import-network.test.ts。
  12. import { existsSync } from "node:fs";
  13. import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  14. import { tmpdir } from "node:os";
  15. import { join } from "node:path";
  16. import { describe, expect, it } from "vitest";
  17. import { addZCodePluginMarketplace, getZCodePluginsOverview } from "../src/plugins.js";
  18. // popover 调用约定:source 为自由文本;pluginStorageRoot 真实运行时由 config 推导,
  19. // 这里显式覆盖到临时目录做隔离,等价「换一个干净工作区做导入」。
  20. function addViaFacade(source: string, storageRoot: string, workingDirectory: string) {
  21. return addZCodePluginMarketplace({
  22. source,
  23. pluginStorageRoot: storageRoot,
  24. workingDirectory,
  25. skipUserConfig: true,
  26. });
  27. }
  28. function overview(storageRoot: string, workingDirectory: string) {
  29. return getZCodePluginsOverview({
  30. pluginStorageRoot: storageRoot,
  31. workingDirectory,
  32. skipUserConfig: true,
  33. });
  34. }
  35. async function makeLocalMarketplace(root: string, name: string): Promise<void> {
  36. const pluginDir = join(root, "plugins", "hello");
  37. await mkdir(join(root, ".claude-plugin"), { recursive: true });
  38. await mkdir(join(pluginDir, ".claude-plugin"), { recursive: true });
  39. await mkdir(join(pluginDir, "skills", "hello"), { recursive: true });
  40. await writeFile(
  41. join(root, ".claude-plugin", "marketplace.json"),
  42. JSON.stringify(
  43. { name, plugins: [{ name: "hello", version: "1.0.0", source: "./plugins/hello" }] },
  44. null,
  45. 2,
  46. ),
  47. );
  48. await writeFile(
  49. join(pluginDir, ".claude-plugin", "plugin.json"),
  50. JSON.stringify({ name: "hello", version: "1.0.0", skills: "skills" }, null, 2),
  51. );
  52. await writeFile(join(pluginDir, "skills", "hello", "SKILL.md"), "# hello\n");
  53. }
  54. describe("marketplace 自定义导入 E2E (真实 agent facade)", () => {
  55. it("本地目录导入:raw 目录路径 -> directory 来源 -> 落盘 + overview 可见", async () => {
  56. const base = await mkdtemp(join(tmpdir(), "zcode-e2e-localdir-"));
  57. const storageRoot = join(base, "storage");
  58. const market = join(base, "my-local-market");
  59. try {
  60. await makeLocalMarketplace(market, "e2e-local-dir-market");
  61. // popover 直接把目录路径作为 source 传入(未结构化)。
  62. const summary = await addViaFacade(market, storageRoot, base);
  63. expect(summary.id).toBe("e2e-local-dir-market");
  64. expect(summary.pluginCount).toBe(1);
  65. const knownPath = join(storageRoot, "known_marketplaces.json");
  66. expect(existsSync(knownPath)).toBe(true);
  67. const known = JSON.parse(await readFile(knownPath, "utf8")) as {
  68. marketplaces: Array<{ id: string; source: { source: string; path?: string } }>;
  69. };
  70. const rec = known.marketplaces.find((m) => m.id === "e2e-local-dir-market");
  71. expect(rec?.source.source).toBe("directory");
  72. expect(rec?.source.path).toBe(market);
  73. // manifest 复制进 storage,供后续 install 引用。
  74. const manifestCopy = join(
  75. storageRoot,
  76. "marketplaces",
  77. "e2e-local-dir-market",
  78. "marketplace.json",
  79. );
  80. expect(existsSync(manifestCopy)).toBe(true);
  81. // 复核:overview 列出新市场(popover 关闭后 UI refresh 能看到它)。
  82. const ov = overview(storageRoot, base);
  83. expect(ov.marketplaces.some((m) => m.id === "e2e-local-dir-market")).toBe(true);
  84. } finally {
  85. await rm(base, { force: true, recursive: true });
  86. }
  87. });
  88. it("非法来源:facade 抛错(驱动 store 失败分支 -> 返回 false -> popover 保留输入),且不误落盘", async () => {
  89. const base = await mkdtemp(join(tmpdir(), "zcode-e2e-invalid-"));
  90. const storageRoot = join(base, "storage");
  91. try {
  92. // popover 里随手敲的一个不存在的本地路径。
  93. await expect(
  94. addViaFacade("./definitely-not-a-real-marketplace-xyz", storageRoot, base),
  95. ).rejects.toThrow();
  96. // 失败时不应写 known_marketplaces.json。
  97. expect(existsSync(join(storageRoot, "known_marketplaces.json"))).toBe(false);
  98. } finally {
  99. await rm(base, { force: true, recursive: true });
  100. }
  101. });
  102. });