plugin-management-installed-menu-layout.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { existsSync } from "node:fs";
  2. import { mkdir, readFile, writeFile } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import { clearAppData, getE2EAppDataPaths } from "#e2e/helpers/desktop-app";
  5. import {
  6. capturePluginManualReview,
  7. openPluginManagementSettings,
  8. requirePluginManualReview,
  9. restartPluginLifecycleApp,
  10. selectPluginSettingsScope,
  11. waitForPluginControl,
  12. } from "#e2e/helpers/plugin-management-lifecycle";
  13. // 内置插件默认已安装,只需在 Workspace 配置里显式启用即可得到 enabledSource=workspace,
  14. // 让详情页出现「恢复 User 默认」操作,不依赖额外 Marketplace fixture。
  15. const BUILTIN_ID = "skill-creator@zcode-plugins-official";
  16. const MENU_LAYOUT_TIMEOUT_MS = 240000;
  17. const RESTORE_USER_DEFAULT_PATTERN = /恢复 User 默认|Restore User default/u;
  18. interface MenuItemLayout {
  19. text: string;
  20. textLineCount: number;
  21. height: number;
  22. }
  23. describe("PLM-LC-017 已安装列表行内菜单布局 E2E", () => {
  24. after(async () => {
  25. await browser.electron.restoreAllMocks();
  26. await clearAppData();
  27. });
  28. it("Workspace 覆盖下开关外显、菜单只含卸载且保持单行", async function () {
  29. this.timeout(MENU_LAYOUT_TIMEOUT_MS);
  30. requirePluginManualReview();
  31. await seedWorkspacePluginOverride();
  32. // Agent 在 workspace 打开时读取配置;先写文件再重启,确保列表带上 workspace 归属。
  33. await restartPluginLifecycleApp();
  34. await openPluginManagementSettings();
  35. await selectPluginSettingsScope("workspace");
  36. await waitForPluginControl("plugin-settings-plugin-row", { pluginId: BUILTIN_ID }, 60000);
  37. await browser.waitUntil(
  38. () =>
  39. browser.execute((pluginId) => {
  40. const row = Array.from(
  41. document.querySelectorAll<HTMLElement>('[data-testid="plugin-settings-plugin-row"]'),
  42. ).find((candidate) => candidate.dataset.pluginId === pluginId);
  43. return (
  44. Boolean(
  45. row?.querySelector(
  46. '[data-testid="plugin-settings-enabled-switch"][data-state="checked"]',
  47. ),
  48. ) && !row?.querySelector("[data-settings-scope]")
  49. );
  50. }, BUILTIN_ID),
  51. { timeout: 30000, timeoutMsg: "已安装列表未展示启用开关或仍显示配置来源标签" },
  52. );
  53. await browser.execute((pluginId) => {
  54. const row = Array.from(
  55. document.querySelectorAll<HTMLElement>('[data-testid="plugin-settings-plugin-row"]'),
  56. ).find((candidate) => candidate.dataset.pluginId === pluginId);
  57. const trigger = row?.querySelector<HTMLElement>('button[aria-haspopup="menu"]');
  58. if (!trigger) throw new Error("已安装列表行内没有「…」菜单触发器");
  59. // 内置插件行位于列表下方,先滚到视口中央,人工审核截图才能同时看到行与展开的菜单。
  60. row?.scrollIntoView({ block: "center" });
  61. // Radix DropdownMenu 在 pointerdown 上切换 open;仅 click 不会打开菜单。
  62. trigger.dispatchEvent(
  63. new PointerEvent("pointerdown", { bubbles: true, button: 0, pointerType: "mouse" }),
  64. );
  65. }, BUILTIN_ID);
  66. await browser.waitUntil(
  67. () =>
  68. browser.execute(
  69. () =>
  70. document.querySelectorAll(
  71. '[data-slot="dropdown-menu-content"][data-state="open"] [role="menuitem"]',
  72. ).length === 1,
  73. ),
  74. { timeout: 15000, timeoutMsg: "行内「…」菜单没有展开" },
  75. );
  76. const items = await browser.execute((): MenuItemLayout[] => {
  77. const content = document.querySelector<HTMLElement>(
  78. '[data-slot="dropdown-menu-content"][data-state="open"]',
  79. );
  80. if (!content) return [];
  81. return Array.from(content.querySelectorAll<HTMLElement>('[role="menuitem"]')).map((item) => {
  82. // 只统计文本节点的行框:菜单项是 flex 行,文本节点会成为匿名 flex item,
  83. // 折行时 Range 会返回多个非空 rect。
  84. let textLineCount = 0;
  85. for (const node of Array.from(item.childNodes)) {
  86. if (node.nodeType !== Node.TEXT_NODE || !node.textContent?.trim()) continue;
  87. const range = document.createRange();
  88. range.selectNodeContents(node);
  89. textLineCount += Array.from(range.getClientRects()).filter(
  90. (rect) => rect.width > 0 && rect.height > 0,
  91. ).length;
  92. }
  93. return {
  94. text: item.innerText.trim(),
  95. textLineCount,
  96. height: item.getBoundingClientRect().height,
  97. };
  98. });
  99. });
  100. await capturePluginManualReview("PLM-LC-017", "installed-row-menu-workspace-override");
  101. const restoreItem = items.find((item) => RESTORE_USER_DEFAULT_PATTERN.test(item.text));
  102. expect(restoreItem).toBeUndefined();
  103. expect(items).toHaveLength(1);
  104. expect(items[0]?.text).toMatch(/卸载|Uninstall/u);
  105. const wrapped = items.filter((item) => item.textLineCount !== 1);
  106. if (wrapped.length > 0) {
  107. throw new Error(`菜单项折行:${JSON.stringify(wrapped)}`);
  108. }
  109. // 所有菜单项等高,进一步排除折行把某一行撑高。
  110. const heights = new Set(items.map((item) => Math.round(item.height)));
  111. if (heights.size !== 1) {
  112. throw new Error(`菜单项高度不一致:${JSON.stringify(items)}`);
  113. }
  114. });
  115. });
  116. async function seedWorkspacePluginOverride(): Promise<void> {
  117. const { workspace } = getE2EAppDataPaths();
  118. const configPath = join(workspace, ".zcode", "config.json");
  119. await mkdir(join(workspace, ".zcode"), { recursive: true });
  120. const existing = existsSync(configPath)
  121. ? (JSON.parse(await readFile(configPath, "utf8")) as Record<string, unknown>)
  122. : {};
  123. const plugins = isRecord(existing.plugins) ? existing.plugins : {};
  124. const enabledPlugins = isRecord(plugins.enabledPlugins) ? plugins.enabledPlugins : {};
  125. await writeFile(
  126. configPath,
  127. JSON.stringify(
  128. {
  129. ...existing,
  130. plugins: { ...plugins, enabledPlugins: { ...enabledPlugins, [BUILTIN_ID]: true } },
  131. },
  132. null,
  133. 2,
  134. ),
  135. "utf8",
  136. );
  137. }
  138. function isRecord(value: unknown): value is Record<string, unknown> {
  139. return typeof value === "object" && value !== null && !Array.isArray(value);
  140. }