plugin-concurrent-install.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "#e2e/helpers/desktop-app";
  5. import {
  6. addMarketplaceSourceThroughUi,
  7. capturePluginManualReview,
  8. clickPluginControl,
  9. getPluginList,
  10. getPluginOverview,
  11. openPluginCard,
  12. openPluginSettings,
  13. requirePluginManualReview,
  14. waitForPluginCard,
  15. waitForPluginControl,
  16. } from "#e2e/helpers/plugin-management-lifecycle";
  17. const marketplace = "e2e-concurrent-install";
  18. const names = ["first", "second"];
  19. const localName = "local";
  20. const localSkill = "detail-during-install";
  21. describe("PLM-CONCURRENT-INSTALL 独立安装忙碌态", () => {
  22. it("后一次安装与前一次失败都不能清理另一插件的安装中状态", async function () {
  23. this.timeout(240000);
  24. requirePluginManualReview();
  25. const gate = await startGatedZipServer();
  26. try {
  27. await prepareGatedMarketplace(gate.port);
  28. await clickPluginControl("plugin-store-install", { pluginId: `first@${marketplace}` });
  29. await browser.waitUntil(() => gate.pending.has("first"), { timeout: 30000 });
  30. await clickPluginControl("plugin-store-install", { pluginId: `second@${marketplace}` });
  31. await assertBusy([true, true]);
  32. await capturePluginManualReview("PLM-CONCURRENT-INSTALL", "both-pending");
  33. // PLM-DETAIL-DURING-INSTALL:A 下载中、B 排在 A 后面时打开其他插件详情。
  34. // 旧实现中 describe 排在两次安装后面,组件区会一直停在 loading。
  35. await openPluginCard(`${localName}@${marketplace}`);
  36. await waitForPluginControl(
  37. "plugin-store-component-section",
  38. { componentKind: "skill" },
  39. 30000,
  40. );
  41. if (!gate.pending.has("first")) throw new Error("Install finished before detail loaded");
  42. await capturePluginManualReview("PLM-DETAIL-DURING-INSTALL", "detail-loaded");
  43. // 新建右侧标签页会新建插件 store 并加载 list/overview;旧实现中它们排在两次安装后面,
  44. // 推荐区一直停在骨架屏。这里直接走同一条服务链路,断言安装未结束时就返回。
  45. await withTimeout(
  46. Promise.all([getPluginList(), getPluginOverview()]),
  47. 10000,
  48. "plugins/list and plugins/overview waited for the install",
  49. );
  50. if (!gate.pending.has("first")) throw new Error("Install finished before list loaded");
  51. await clickPluginControl("plugin-store-detail-back");
  52. await assertBusy([true, true]);
  53. gate.release("first");
  54. await browser.waitUntil(() => gate.pending.has("second"), { timeout: 60000 });
  55. await assertBusy([false, true]);
  56. gate.release("second");
  57. await assertBusy([false, false]);
  58. } finally {
  59. await gate.close();
  60. await clearAppData();
  61. }
  62. });
  63. });
  64. // 用响应门控制真实下载,不依赖网络速度或固定 sleep 制造并发窗口。
  65. async function startGatedZipServer() {
  66. const pending = new Map<string, ServerResponse[]>();
  67. const released = new Set<string>();
  68. const server = createServer((request, response) => {
  69. const name = request.url?.slice(1).replace(".zip", "") ?? "";
  70. if (released.has(name)) {
  71. response.writeHead(404).end("fixture install failure");
  72. } else {
  73. pending.set(name, [...(pending.get(name) ?? []), response]);
  74. }
  75. });
  76. const release = (name: string) => {
  77. released.add(name);
  78. for (const response of pending.get(name) ?? []) {
  79. response.writeHead(404).end("fixture install failure");
  80. }
  81. pending.delete(name);
  82. };
  83. await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
  84. const address = server.address();
  85. if (!address || typeof address === "string") throw new Error("Fixture server unavailable");
  86. return {
  87. pending,
  88. port: address.port,
  89. release,
  90. async close() {
  91. for (const name of names) release(name);
  92. server.closeAllConnections();
  93. await new Promise<void>((resolve) => server.close(() => resolve()));
  94. },
  95. };
  96. }
  97. async function prepareGatedMarketplace(port: number): Promise<void> {
  98. const fixture = join(getE2EAppDataPaths().homeDir, "fixtures", marketplace);
  99. await mkdir(join(fixture, ".claude-plugin"), { recursive: true });
  100. const skillDir = join(fixture, "plugins", localName, "skills", localSkill);
  101. await mkdir(skillDir, { recursive: true });
  102. await writeFile(
  103. join(skillDir, "SKILL.md"),
  104. `---\nname: ${localSkill}\ndescription: Detail stays readable during install\n---\n`,
  105. );
  106. await writeFile(
  107. join(fixture, ".claude-plugin", "marketplace.json"),
  108. JSON.stringify({
  109. name: marketplace,
  110. plugins: [
  111. ...names.map((name) => ({
  112. name,
  113. version: "1.0.0",
  114. description: `Concurrent install ${name}`,
  115. source: {
  116. source: "url",
  117. type: "zip",
  118. url: `http://127.0.0.1:${port}/${name}.zip`,
  119. sha256: "0".repeat(64),
  120. },
  121. })),
  122. {
  123. name: localName,
  124. version: "1.0.0",
  125. description: "Local detail fixture",
  126. source: `./plugins/${localName}`,
  127. },
  128. ],
  129. }),
  130. );
  131. await openPluginSettings();
  132. await addMarketplaceSourceThroughUi(fixture);
  133. await clickPluginControl("plugin-store-segment-personal");
  134. for (const name of [...names, localName])
  135. await waitForPluginCard(`${name}@${marketplace}`, 60000);
  136. }
  137. async function assertBusy(expected: boolean[]): Promise<void> {
  138. await browser.waitUntil(
  139. async () => {
  140. const states = await browser.execute(
  141. (ids) =>
  142. ids.map((id) => {
  143. const button = Array.from(
  144. document.querySelectorAll<HTMLButtonElement>('[data-testid="plugin-store-install"]'),
  145. ).find((candidate) => candidate.dataset.pluginId === id);
  146. return button ? button.disabled : null;
  147. }),
  148. names.map((name) => `${name}@${marketplace}`),
  149. );
  150. return states.every((value, index) => value === expected[index]);
  151. },
  152. { timeout: 30000, timeoutMsg: `Expected install busy states ${JSON.stringify(expected)}` },
  153. );
  154. }
  155. async function withTimeout<T>(promise: Promise<T>, timeoutMs: number, message: string): Promise<T> {
  156. let timer: ReturnType<typeof setTimeout> | undefined;
  157. try {
  158. return await Promise.race([
  159. promise,
  160. new Promise<never>((_, reject) => {
  161. timer = setTimeout(() => reject(new Error(message)), timeoutMs);
  162. }),
  163. ]);
  164. } finally {
  165. clearTimeout(timer);
  166. }
  167. }