plugin-concurrent-install.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { createServer, type ServerResponse } from "node:http";
  2. import { mkdir, writeFile } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import { clearAppData, getE2EAppDataPaths } from "../../../helpers/desktop-app.js";
  5. import {
  6. addMarketplaceSourceThroughUi,
  7. capturePluginManualReview,
  8. clickPluginControl,
  9. openPluginSettings,
  10. requirePluginManualReview,
  11. waitForPluginCard,
  12. } from "../../../helpers/plugin-management-lifecycle.js";
  13. const marketplace = "e2e-concurrent-install";
  14. const names = ["first", "second"];
  15. describe("PLM-CONCURRENT-INSTALL 独立安装忙碌态", () => {
  16. it("后一次安装与前一次失败都不能清理另一插件的安装中状态", async function () {
  17. this.timeout(240000);
  18. requirePluginManualReview();
  19. const pending = new Map<string, ServerResponse[]>();
  20. const released = new Set<string>();
  21. // 用响应门控制真实下载,不依赖网络速度或固定 sleep 制造并发窗口。
  22. const server = createServer((request, response) => {
  23. const name = request.url?.slice(1).replace(".zip", "") ?? "";
  24. if (released.has(name)) {
  25. response.writeHead(404).end("fixture install failure");
  26. } else {
  27. pending.set(name, [...(pending.get(name) ?? []), response]);
  28. }
  29. });
  30. const release = (name: string) => {
  31. released.add(name);
  32. for (const response of pending.get(name) ?? []) {
  33. response.writeHead(404).end("fixture install failure");
  34. }
  35. pending.delete(name);
  36. };
  37. try {
  38. await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
  39. const address = server.address();
  40. if (!address || typeof address === "string") throw new Error("Fixture server unavailable");
  41. const fixture = join(getE2EAppDataPaths().homeDir, "fixtures", marketplace);
  42. await mkdir(join(fixture, ".claude-plugin"), { recursive: true });
  43. await writeFile(
  44. join(fixture, ".claude-plugin", "marketplace.json"),
  45. JSON.stringify({
  46. name: marketplace,
  47. plugins: names.map((name) => ({
  48. name,
  49. version: "1.0.0",
  50. description: `Concurrent install ${name}`,
  51. source: {
  52. source: "url",
  53. type: "zip",
  54. url: `http://127.0.0.1:${address.port}/${name}.zip`,
  55. sha256: "0".repeat(64),
  56. },
  57. })),
  58. }),
  59. );
  60. await openPluginSettings();
  61. await addMarketplaceSourceThroughUi(fixture);
  62. await clickPluginControl("plugin-store-segment-personal");
  63. for (const name of names) await waitForPluginCard(`${name}@${marketplace}`, 60000);
  64. await clickPluginControl("plugin-store-install", { pluginId: `first@${marketplace}` });
  65. await browser.waitUntil(() => pending.has("first"), { timeout: 30000 });
  66. await clickPluginControl("plugin-store-install", { pluginId: `second@${marketplace}` });
  67. await assertBusy([true, true]);
  68. await capturePluginManualReview("PLM-CONCURRENT-INSTALL", "both-pending");
  69. release("first");
  70. await browser.waitUntil(() => pending.has("second"), { timeout: 60000 });
  71. await assertBusy([false, true]);
  72. release("second");
  73. await assertBusy([false, false]);
  74. } finally {
  75. for (const name of names) release(name);
  76. server.closeAllConnections();
  77. await new Promise<void>((resolve) => server.close(() => resolve()));
  78. await clearAppData();
  79. }
  80. });
  81. });
  82. async function assertBusy(expected: boolean[]): Promise<void> {
  83. await browser.waitUntil(
  84. async () => {
  85. const states = await browser.execute(
  86. (ids) =>
  87. ids.map((id) => {
  88. const button = Array.from(
  89. document.querySelectorAll<HTMLButtonElement>('[data-testid="plugin-store-install"]'),
  90. ).find((candidate) => candidate.dataset.pluginId === id);
  91. return button ? button.disabled : null;
  92. }),
  93. names.map((name) => `${name}@${marketplace}`),
  94. );
  95. return states.every((value, index) => value === expected[index]);
  96. },
  97. { timeout: 30000, timeoutMsg: `Expected install busy states ${JSON.stringify(expected)}` },
  98. );
  99. }