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

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