plugin-no-git-github-archive.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { startDraftFromProject } from "#e2e/helpers/project-sidebar";
  2. import { existsSync, readFileSync } from "node:fs";
  3. import { join } from "node:path";
  4. import type { ZCodePluginInfo } from "@zcode/shared";
  5. import { clearAppData, getE2EAppDataPaths, waitForWorkspaceApp } from "#e2e/helpers/desktop-app";
  6. import {
  7. addMarketplaceSourceThroughAgent,
  8. getPluginList,
  9. getPluginOverview,
  10. installMarketplacePluginThroughAgent,
  11. requirePluginManualReview,
  12. updateMarketplaceThroughAgent,
  13. } from "#e2e/helpers/plugin-management-lifecycle";
  14. const CLAUDE_MARKETPLACE_ID = "claude-plugins-official";
  15. const CLAUDE_HTTP_PLUGIN_NAME = "aikido";
  16. const CLAUDE_HTTP_PLUGIN_ID = `${CLAUDE_HTTP_PLUGIN_NAME}@${CLAUDE_MARKETPLACE_ID}`;
  17. const NON_GITHUB_GIT_PROBE = "https://gitlab.example.invalid/e2e/no-git-plugin.git";
  18. const REAL_HTTP_INSTALL_TIMEOUT_MS = 600_000;
  19. describe("PLM-LC-011 Desktop real Claude plugin HTTP install without Git", () => {
  20. after(async () => {
  21. await browser.electron.restoreAllMocks();
  22. await clearAppData();
  23. });
  24. it("Git 不可用时通过 GitHub Archive 刷新 Claude Marketplace 并安装真实插件", async function () {
  25. this.timeout(REAL_HTTP_INSTALL_TIMEOUT_MS);
  26. requirePluginManualReview();
  27. // WebDriver 的 executeAsync 默认 30 秒;真实公网下载由 Agent 完成,Renderer bridge 只是在
  28. // 等 RPC 结果,测试门限必须覆盖生产 HTTP 客户端的 180 秒单请求上限。
  29. await browser.setTimeout({ script: REAL_HTTP_INSTALL_TIMEOUT_MS });
  30. // 全局空草稿不挂载 workspace App,旧测试 action 因此不存在;先进入项目草稿。
  31. await waitForWorkspaceApp(getE2EAppDataPaths().workspace, 45000);
  32. await startDraftFromProject(getE2EAppDataPaths().workspace);
  33. const gitBinary = await browser.electron.execute(() => process.env.ZCODE_GIT_BINARY ?? "");
  34. expect(gitBinary).toContain("missing-bin");
  35. expect(existsSync(gitBinary)).toBe(false);
  36. // 先走一次必须依赖 Git 的非 GitHub 来源:只有 Agent 真正拿到 missing binary,才会返回
  37. // plugin_git_unavailable。这样后续成功不能被开发机上已安装的 Git 误判成 Archive 成功。
  38. let probeFailure = "";
  39. try {
  40. await addMarketplaceSourceThroughAgent(NON_GITHUB_GIT_PROBE);
  41. } catch (error) {
  42. // mutation 的 error diagnostic 会被 RPC 边界提升为异常;这里验证用户真正看到的
  43. // 可执行提示,同时证明后续 Archive 成功不是偷偷使用了开发机上的 Git。
  44. probeFailure = error instanceof Error ? error.message : String(error);
  45. }
  46. expect(probeFailure).toContain("git is unavailable on this Agent Host");
  47. // 走 Renderer test bridge -> Host -> Agent Protocol 的真实服务链路。公网 archive 在抓包
  48. // 代理下可能耗时数分钟,因此不叠加 278 张商店卡片和第三方图标的渲染噪声。
  49. const refresh = await updateMarketplaceThroughAgent(CLAUDE_MARKETPLACE_ID);
  50. expect(refresh.diagnostics).not.toEqual(
  51. expect.arrayContaining([expect.objectContaining({ severity: "error" })]),
  52. );
  53. const before = await getPluginOverview();
  54. expect(before.marketplaces.find((item) => item.id === CLAUDE_MARKETPLACE_ID)).toMatchObject({
  55. id: CLAUDE_MARKETPLACE_ID,
  56. });
  57. expect(
  58. before.availablePlugins.find((plugin) => plugin.id === CLAUDE_HTTP_PLUGIN_ID),
  59. ).toMatchObject({
  60. installed: false,
  61. marketplace: CLAUDE_MARKETPLACE_ID,
  62. name: CLAUDE_HTTP_PLUGIN_NAME,
  63. });
  64. const install = await installMarketplacePluginThroughAgent(
  65. CLAUDE_HTTP_PLUGIN_NAME,
  66. CLAUDE_MARKETPLACE_ID,
  67. );
  68. expect(install.diagnostics).not.toEqual(
  69. expect.arrayContaining([expect.objectContaining({ severity: "error" })]),
  70. );
  71. expect(install.installedPlugins).toEqual(
  72. expect.arrayContaining([expect.objectContaining({ id: CLAUDE_HTTP_PLUGIN_ID })]),
  73. );
  74. const pluginStorageRoot = join(getE2EAppDataPaths().storageRoot, "cli", "plugins");
  75. const installed = readInstalledRecord(pluginStorageRoot);
  76. expect(installed.source).toMatchObject({
  77. source: "url",
  78. url: "https://github.com/AikidoSec/aikido-claude-plugin.git",
  79. });
  80. expect(installed.source?.sha ?? "").toMatch(/^[a-f0-9]{40}$/u);
  81. expect(existsSync(join(installed.installPath, ".claude-plugin", "plugin.json"))).toBe(true);
  82. const marketplaceRoot = join(pluginStorageRoot, "marketplaces", CLAUDE_MARKETPLACE_ID);
  83. // `git clone` 会携带 `.git`;GitHub Archive 不会。两个缓存都无 `.git` 是进程外证据。
  84. expect(existsSync(join(marketplaceRoot, ".git"))).toBe(false);
  85. expect(existsSync(join(installed.installPath, ".git"))).toBe(false);
  86. const plugin = (await getPluginList()).plugins.find(
  87. (item) => item.id === CLAUDE_HTTP_PLUGIN_ID,
  88. );
  89. expect(plugin).toMatchObject({
  90. enabled: true,
  91. rootPath: installed.installPath,
  92. version: installed.version,
  93. });
  94. expect(componentNames(plugin, "mcp")).not.toHaveLength(0);
  95. });
  96. });
  97. function readInstalledRecord(pluginStorageRoot: string): {
  98. installPath: string;
  99. source?: { sha?: string; source?: string; url?: string };
  100. version: string;
  101. } {
  102. const state = JSON.parse(
  103. readFileSync(join(pluginStorageRoot, "installed_plugins.json"), "utf-8"),
  104. ) as {
  105. plugins?: Array<{
  106. id?: string;
  107. installPath?: string;
  108. source?: { sha?: string; source?: string; url?: string };
  109. version?: string;
  110. }>;
  111. };
  112. const record = state.plugins?.find((plugin) => plugin.id === CLAUDE_HTTP_PLUGIN_ID);
  113. if (!record?.installPath || !record.version) {
  114. throw new Error(`Installed record missing for ${CLAUDE_HTTP_PLUGIN_ID}`);
  115. }
  116. return {
  117. installPath: record.installPath,
  118. ...(record.source ? { source: record.source } : {}),
  119. version: record.version,
  120. };
  121. }
  122. function componentNames(plugin: ZCodePluginInfo | undefined, kind: "mcp"): string[] {
  123. return (
  124. plugin?.components?.find((group) => group.kind === kind)?.items.map((item) => item.name) ?? []
  125. );
  126. }