plugin-management-ui-market.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import { startDraftFromProject } from "../helpers/project-sidebar.js";
  2. import { selectUpstreamModel } from "../helpers/upstream-provider.js";
  3. import { mkdir, writeFile } from "node:fs/promises";
  4. import { join } from "node:path";
  5. import { TID_SIDE_PANE_TOGGLE } from "@zcode/shared/test-ids";
  6. import { clearAppData, clickTestIdByDom, getE2EAppDataPaths } from "../helpers/desktop-app.js";
  7. import {
  8. getV4PaneSnapshot,
  9. prepareV4ConversationE2E,
  10. selectV4TaskById,
  11. sendV4Prompt,
  12. waitForV4AssistantMessageContaining,
  13. } from "../helpers/v4-conversation.js";
  14. import {
  15. PLUGIN_LIFECYCLE_ID,
  16. addLifecycleMarketplaceThroughUi,
  17. capturePluginManualReview,
  18. clickPluginControl,
  19. createPluginLifecycleFixture,
  20. getPluginList,
  21. getPluginOverview,
  22. installPluginThroughUi,
  23. openManageView,
  24. openPluginSettings,
  25. requirePluginManualReview,
  26. removeLifecyclePluginPayload,
  27. selectPluginSettingsScope,
  28. setPluginEnabledThroughUi,
  29. uninstallPluginThroughUi,
  30. waitForPluginControl,
  31. } from "../helpers/plugin-management-lifecycle.js";
  32. import { startElectronWindowRecording } from "../helpers/electron-window-recorder.js";
  33. const ARTIFACT_DIR = join(process.cwd(), ".e2e-artifacts", "plugin-management", "plm-ui-001");
  34. // 每个评审步骤截图后停留,录像里能看清稳定态;同时记录时间点供生成字幕。
  35. const STEP_HOLD_MS = 1500;
  36. describe("PLM-UI-001 UI 插件市场与项目配置", () => {
  37. after(async () => {
  38. await browser.electron.restoreAllMocks();
  39. await clearAppData();
  40. });
  41. it("在真实 MCP 沙箱打开界面,关闭不卸载,项目暂停不影响全局和其他项目", async function () {
  42. this.timeout(240000);
  43. requirePluginManualReview();
  44. const fixture = await createPluginLifecycleFixture("1.0.0", true);
  45. await mkdir(ARTIFACT_DIR, { recursive: true });
  46. const recording = await startElectronWindowRecording({
  47. frameIntervalMs: 200,
  48. outputPath: join(ARTIFACT_DIR, "plm-ui-001.webm"),
  49. });
  50. const recordingStartedAt = Date.now();
  51. const chapters: Array<{ label: string; atMs: number }> = [];
  52. const step = async (label: string) => {
  53. await capturePluginManualReview("PLM-UI-001", label);
  54. chapters.push({ label, atMs: Date.now() - recordingStartedAt });
  55. await browser.pause(STEP_HOLD_MS);
  56. };
  57. try {
  58. await openPluginSettings();
  59. await waitForPluginControl("plugin-store-list");
  60. await browser.$('[data-testid="plugin-store-card"]').waitForDisplayed({ timeout: 60000 });
  61. // 截图按 PRD 章节编号,便于逐项对照(PRD:插件市场 & Side Pane 交互调整)。
  62. await step("01-market-header-public");
  63. await addLifecycleMarketplaceThroughUi(fixture);
  64. await clickPluginControl("plugin-store-segment-community");
  65. await step("02-market-community");
  66. await clickPluginControl("plugin-store-segment-personal");
  67. await waitForPluginControl("plugin-store-ui-section");
  68. await step("03-market-personal-ui-section");
  69. // 缺失源包必须保留可重试错误,不能伪装成已安装或打开 UI。
  70. await removeLifecyclePluginPayload(fixture);
  71. try {
  72. await clickPluginControl("plugin-store-install", { pluginId: PLUGIN_LIFECYCLE_ID });
  73. await waitForPluginControl("plugin-store-error");
  74. expect(
  75. (await getPluginList()).plugins.some((plugin) => plugin.id === PLUGIN_LIFECYCLE_ID),
  76. ).toBe(false);
  77. await step("04-install-failure-before-retry");
  78. } finally {
  79. await createPluginLifecycleFixture("1.0.0", true);
  80. }
  81. await installPluginThroughUi(PLUGIN_LIFECYCLE_ID);
  82. await step("05-installed-ui-card");
  83. await startDraftFromProject(getE2EAppDataPaths().workspace);
  84. await prepareV4ConversationE2E();
  85. await startDraftFromProject(getE2EAppDataPaths().workspace);
  86. await selectUpstreamModel();
  87. await sendV4Prompt("E2E_PLUGIN_MARKET_SESSION_READY");
  88. await waitForV4AssistantMessageContaining("plugin-market-session-ready");
  89. const sessionId = (await getV4PaneSnapshot()).sessionId;
  90. if (!sessionId || sessionId === "draft") throw new Error("前置会话没有创建成功");
  91. // 打开 UI 只在会话右侧的插件市场提供:主导航卡片整行进详情,右侧卡片多一个「打开」。
  92. const openSidePaneMarket = async (stepLabel?: string) => {
  93. await selectV4TaskById(sessionId);
  94. const addTab = browser.$("[data-side-pane-add-tab-trigger]");
  95. if (await addTab.isDisplayed().catch(() => false)) {
  96. await addTab.click();
  97. await browser.$('[data-state="active"] [data-testid="side-pane-plugin-market"]').click();
  98. } else {
  99. await clickTestIdByDom(TID_SIDE_PANE_TOGGLE);
  100. await waitForPluginControl("side-pane-ui-recommendations");
  101. // 启动页内容必须完整落在窗口内:「查看所有插件」右边缘不能被面板裁切。
  102. await browser.waitUntil(
  103. async () =>
  104. browser.execute(() => {
  105. const button = document.querySelector('[data-testid="side-pane-plugin-market"]');
  106. const rect = button?.getBoundingClientRect();
  107. return Boolean(rect && rect.width > 0 && rect.right <= window.innerWidth);
  108. }),
  109. { timeout: 10000, timeoutMsg: "Side Pane 启动页内容超出窗口" },
  110. );
  111. if (stepLabel) await step(stepLabel);
  112. await clickPluginControl("side-pane-plugin-market");
  113. }
  114. await waitForPluginControl("plugin-store-root");
  115. await clickPluginControl("plugin-store-segment-personal");
  116. await waitForPluginControl("plugin-store-open-ui", { pluginId: PLUGIN_LIFECYCLE_ID });
  117. };
  118. const openCardDetail = async () => {
  119. await clickPluginControl("plugin-store-card", { pluginId: PLUGIN_LIFECYCLE_ID });
  120. await waitForPluginControl("plugin-store-detail");
  121. };
  122. const waitForSandboxReady = () =>
  123. browser.waitUntil(
  124. async () =>
  125. (await browser.electron.execute(async (electron) => {
  126. const guests = electron.webContents
  127. .getAllWebContents()
  128. .filter((guest) => guest.getURL().startsWith("zcode-sandbox://"));
  129. for (const guest of guests) {
  130. for (const frame of guest.mainFrame.frames) {
  131. const ready = await frame
  132. .executeJavaScript('document.getElementById("market-ui-ready")?.textContent')
  133. .catch(() => null);
  134. if (ready === "PLUGIN_MARKET_UI_READY") return true;
  135. }
  136. }
  137. return false;
  138. })) === true,
  139. { timeout: 60000, timeoutMsg: "真实插件沙箱没有完成握手" },
  140. );
  141. // 主导航市场:已安装 UI 插件没有「打开」「详情」按钮,整行进入详情。
  142. await openPluginSettings();
  143. await clickPluginControl("plugin-store-segment-personal");
  144. await waitForPluginControl("plugin-store-card", { pluginId: PLUGIN_LIFECYCLE_ID });
  145. expect(await browser.$$('[data-testid="plugin-store-open-ui"]')).toHaveLength(0);
  146. expect(await browser.$$('[data-testid="plugin-store-ui-details"]')).toHaveLength(0);
  147. await step("06-main-nav-card-no-open");
  148. await openCardDetail();
  149. expect(await browser.$$('[data-testid="plugin-store-open-ui"]')).toHaveLength(0);
  150. await waitForPluginControl("plugin-store-try-now", { pluginId: PLUGIN_LIFECYCLE_ID });
  151. await step("07-main-nav-detail");
  152. expect(await browser.$$('[data-side-pane-tab-id^="plugin-ui:"]')).toHaveLength(0);
  153. // 会话右侧市场:卡片多一个「打开」,在当前会话的右侧标签打开 UI。
  154. await openSidePaneMarket("08-side-pane-launcher");
  155. await step("09-side-pane-market-open-button");
  156. await clickPluginControl("plugin-store-open-ui", { pluginId: PLUGIN_LIFECYCLE_ID });
  157. await browser.$('[data-side-pane-tab-id^="plugin-ui:"]').waitForDisplayed({ timeout: 30000 });
  158. await waitForSandboxReady();
  159. await step("10-ui-opened-in-session");
  160. await browser.execute(() => {
  161. const tab = document.querySelector<HTMLElement>('[data-side-pane-tab-id^="plugin-ui:"]');
  162. const close = tab?.querySelector<HTMLButtonElement>("button");
  163. if (!close) throw new Error("UI 面板缺少关闭按钮");
  164. close.click();
  165. });
  166. expect(
  167. (await getPluginOverview()).installedPlugins.some(
  168. (plugin) => plugin.id === PLUGIN_LIFECYCLE_ID,
  169. ),
  170. ).toBe(true);
  171. await browser.$('[data-side-pane-tab-id="plugin-market"]').click();
  172. await openManageView();
  173. expect(await browser.$$('[role="tab"][id$="-trigger-mcps"]')).toHaveLength(0);
  174. // 管理列表异步加载:等「打开」出现在插件行内,避免截到「加载中」。
  175. await browser
  176. .$(
  177. `[data-testid="plugin-settings-plugin-row"][data-plugin-id="${PLUGIN_LIFECYCLE_ID}"] [data-testid="plugin-store-open-ui"]`,
  178. )
  179. .waitForDisplayed({ timeout: 15000 });
  180. await step("11-side-pane-manage-open-button");
  181. await clickPluginControl("plugin-store-manage-back");
  182. await browser.$('[data-side-pane-tab-id="plugin-market"] button').click();
  183. // 项目内暂停:主导航详情不提供启用并打开;右侧市场按钮显示「启用」,先启用再打开。
  184. await openPluginSettings();
  185. await openManageView();
  186. await selectPluginSettingsScope("workspace");
  187. await waitForPluginControl("plugin-settings-plugin-row", { pluginId: PLUGIN_LIFECYCLE_ID });
  188. await setPluginEnabledThroughUi(PLUGIN_LIFECYCLE_ID, false, "manage");
  189. const { workspace, homeDir } = getE2EAppDataPaths();
  190. const other = join(homeDir, "fixtures", "other-project");
  191. await mkdir(other, { recursive: true });
  192. expect(
  193. (await getPluginList(workspace, undefined, "user")).plugins.find(
  194. (plugin) => plugin.id === PLUGIN_LIFECYCLE_ID,
  195. )?.enabled,
  196. ).toBe(true);
  197. expect(
  198. (await getPluginList(other)).plugins.find((plugin) => plugin.id === PLUGIN_LIFECYCLE_ID)
  199. ?.enabled,
  200. ).toBe(true);
  201. await browser.$('[data-testid="plugin-store-manage-back"]').scrollIntoView();
  202. await step("12-project-override");
  203. await clickPluginControl("plugin-store-manage-back");
  204. await clickPluginControl("plugin-store-segment-personal");
  205. await openCardDetail();
  206. expect(
  207. (await getPluginList()).plugins.find((plugin) => plugin.id === PLUGIN_LIFECYCLE_ID)
  208. ?.enabled,
  209. ).toBe(false);
  210. expect(await browser.$$('[data-testid="plugin-store-open-ui"]')).toHaveLength(0);
  211. await step("13-paused-main-nav-detail");
  212. await openSidePaneMarket();
  213. await step("14-side-pane-paused-enable-button");
  214. await clickPluginControl("plugin-store-open-ui", { pluginId: PLUGIN_LIFECYCLE_ID });
  215. await browser.waitUntil(
  216. async () =>
  217. (await getPluginList()).plugins.find((plugin) => plugin.id === PLUGIN_LIFECYCLE_ID)
  218. ?.enabled === true,
  219. { timeout: 30000, timeoutMsg: "暂停的 UI 插件没有先启用" },
  220. );
  221. await browser.$('[data-side-pane-tab-id^="plugin-ui:"]').waitForDisplayed({ timeout: 30000 });
  222. await step("15-paused-enables-and-opens");
  223. // 关掉右侧市场标签:会话视图在主导航后台保持挂载,两份市场同时存在会让按 testid 取到隐藏的那份。
  224. // 非激活标签的关闭按钮被遮挡,走 DOM 点击。
  225. await browser.execute(() => {
  226. const close = document.querySelector<HTMLButtonElement>(
  227. '[data-side-pane-tab-id="plugin-market"] button',
  228. );
  229. if (!close) throw new Error("右侧市场标签缺少关闭按钮");
  230. close.click();
  231. });
  232. await browser
  233. .$('[data-side-pane-tab-id="plugin-market"]')
  234. .waitForExist({ reverse: true, timeout: 10000 });
  235. // 管理页的行同样整行进入详情,再从详情恢复继承与卸载。
  236. await openPluginSettings();
  237. await openManageView();
  238. await selectPluginSettingsScope("workspace");
  239. expect(await browser.$$('[data-testid="plugin-settings-ui-details"]')).toHaveLength(0);
  240. expect(await browser.$$('[data-testid="plugin-store-open-ui"]')).toHaveLength(0);
  241. // 切换作用域后列表会重新加载,等插件行出现再点。
  242. await waitForPluginControl("plugin-settings-plugin-row", { pluginId: PLUGIN_LIFECYCLE_ID });
  243. await browser.execute((pluginId) => {
  244. const row = document.querySelector(
  245. `[data-testid="plugin-settings-plugin-row"][data-plugin-id="${pluginId}"] [data-testid="plugin-store-card"]`,
  246. );
  247. if (!(row instanceof HTMLElement)) throw new Error("管理页缺少插件行");
  248. row.click();
  249. }, PLUGIN_LIFECYCLE_ID);
  250. await waitForPluginControl("plugin-store-detail");
  251. await clickPluginControl("plugin-store-reset-config");
  252. await browser.waitUntil(
  253. async () => {
  254. const plugin = (await getPluginList()).plugins.find(
  255. (item) => item.id === PLUGIN_LIFECYCLE_ID,
  256. );
  257. return plugin?.enabled === true && plugin.enabledSource === "user";
  258. },
  259. { timeout: 30000, timeoutMsg: "项目配置没有恢复继承全局" },
  260. );
  261. await clickPluginControl("plugin-store-item-menu", { pluginId: PLUGIN_LIFECYCLE_ID });
  262. await step("16-more-menu");
  263. await browser.keys(["Escape"]);
  264. // 菜单关闭完成后再走卸载 helper,否则 helper 的点击会把仍在关闭的菜单切回关闭态。
  265. await browser
  266. .$('[data-testid="plugin-store-menu-uninstall"]')
  267. .waitForDisplayed({ reverse: true, timeout: 10000 });
  268. await uninstallPluginThroughUi(PLUGIN_LIFECYCLE_ID, false);
  269. await uninstallPluginThroughUi(PLUGIN_LIFECYCLE_ID, true);
  270. } finally {
  271. const result = await recording.stop({ tailDurationMs: 500 });
  272. await writeFile(
  273. join(ARTIFACT_DIR, "plm-ui-001.chapters.json"),
  274. JSON.stringify(
  275. { videoPath: result.videoPath, frameCount: result.frameCount, chapters },
  276. null,
  277. 2,
  278. ),
  279. );
  280. }
  281. });
  282. });