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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "../../../helpers/desktop-app.js";
  5. import {
  6. capturePluginManualReview,
  7. openPluginManagementSettings,
  8. requirePluginManualReview,
  9. restartPluginLifecycleApp,
  10. selectPluginSettingsScope,
  11. waitForPluginControl,
  12. } from "../../../helpers/plugin-management-lifecycle.js";
  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 覆盖下「恢复 User 默认」与其他菜单项都保持单行", 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 Boolean(row?.querySelector('[data-settings-scope="workspace"]'));
  44. }, BUILTIN_ID),
  45. { timeout: 30000, timeoutMsg: "已安装列表没有把内置插件标记为 Workspace 覆盖" },
  46. );
  47. await browser.execute((pluginId) => {
  48. const row = Array.from(
  49. document.querySelectorAll<HTMLElement>('[data-testid="plugin-settings-plugin-row"]'),
  50. ).find((candidate) => candidate.dataset.pluginId === pluginId);
  51. const trigger = row?.querySelector<HTMLElement>('button[aria-haspopup="menu"]');
  52. if (!trigger) throw new Error("已安装列表行内没有「…」菜单触发器");
  53. // 内置插件行位于列表下方,先滚到视口中央,人工审核截图才能同时看到行与展开的菜单。
  54. row?.scrollIntoView({ block: "center" });
  55. // Radix DropdownMenu 在 pointerdown 上切换 open;仅 click 不会打开菜单。
  56. trigger.dispatchEvent(
  57. new PointerEvent("pointerdown", { bubbles: true, button: 0, pointerType: "mouse" }),
  58. );
  59. }, BUILTIN_ID);
  60. await browser.waitUntil(
  61. () =>
  62. browser.execute(
  63. () =>
  64. document.querySelectorAll(
  65. '[data-slot="dropdown-menu-content"][data-state="open"] [role="menuitem"]',
  66. ).length >= 2,
  67. ),
  68. { timeout: 15000, timeoutMsg: "行内「…」菜单没有展开" },
  69. );
  70. const items = await browser.execute((): MenuItemLayout[] => {
  71. const content = document.querySelector<HTMLElement>(
  72. '[data-slot="dropdown-menu-content"][data-state="open"]',
  73. );
  74. if (!content) return [];
  75. return Array.from(content.querySelectorAll<HTMLElement>('[role="menuitem"]')).map((item) => {
  76. // 只统计文本节点的行框:菜单项是 flex 行,文本节点会成为匿名 flex item,
  77. // 折行时 Range 会返回多个非空 rect。
  78. let textLineCount = 0;
  79. for (const node of Array.from(item.childNodes)) {
  80. if (node.nodeType !== Node.TEXT_NODE || !node.textContent?.trim()) continue;
  81. const range = document.createRange();
  82. range.selectNodeContents(node);
  83. textLineCount += Array.from(range.getClientRects()).filter(
  84. (rect) => rect.width > 0 && rect.height > 0,
  85. ).length;
  86. }
  87. return {
  88. text: item.innerText.trim(),
  89. textLineCount,
  90. height: item.getBoundingClientRect().height,
  91. };
  92. });
  93. });
  94. await capturePluginManualReview("PLM-LC-017", "installed-row-menu-workspace-override");
  95. const restoreItem = items.find((item) => RESTORE_USER_DEFAULT_PATTERN.test(item.text));
  96. if (!restoreItem) {
  97. throw new Error(`菜单里没有「恢复 User 默认」项:${JSON.stringify(items)}`);
  98. }
  99. const wrapped = items.filter((item) => item.textLineCount !== 1);
  100. if (wrapped.length > 0) {
  101. throw new Error(`菜单项折行:${JSON.stringify(wrapped)}`);
  102. }
  103. // 所有菜单项等高,进一步排除折行把某一行撑高。
  104. const heights = new Set(items.map((item) => Math.round(item.height)));
  105. if (heights.size !== 1) {
  106. throw new Error(`菜单项高度不一致:${JSON.stringify(items)}`);
  107. }
  108. expect(items.length).toBeGreaterThanOrEqual(2);
  109. });
  110. });
  111. async function seedWorkspacePluginOverride(): Promise<void> {
  112. const { workspace } = getE2EAppDataPaths();
  113. const configPath = join(workspace, ".zcode", "config.json");
  114. await mkdir(join(workspace, ".zcode"), { recursive: true });
  115. const existing = existsSync(configPath)
  116. ? (JSON.parse(await readFile(configPath, "utf8")) as Record<string, unknown>)
  117. : {};
  118. const plugins = isRecord(existing.plugins) ? existing.plugins : {};
  119. const enabledPlugins = isRecord(plugins.enabledPlugins) ? plugins.enabledPlugins : {};
  120. await writeFile(
  121. configPath,
  122. JSON.stringify(
  123. {
  124. ...existing,
  125. plugins: { ...plugins, enabledPlugins: { ...enabledPlugins, [BUILTIN_ID]: true } },
  126. },
  127. null,
  128. 2,
  129. ),
  130. "utf8",
  131. );
  132. }
  133. function isRecord(value: unknown): value is Record<string, unknown> {
  134. return typeof value === "object" && value !== null && !Array.isArray(value);
  135. }