off-peak-automations-home-layout.test.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { tmpdir } from "node:os";
  2. import { join } from "node:path";
  3. import {
  4. TID_AUTOMATION_CREATE_MANUALLY,
  5. TID_AUTOMATIONS_OPEN,
  6. TID_OFFPEAK_CREATE_BUTTON,
  7. } from "@zcode/shared";
  8. import {
  9. clearAppData,
  10. clickTestIdByDom,
  11. restoreElectronRendererContentSize,
  12. setCurrentElectronRendererContentSize,
  13. type ElectronRendererContentSizeSnapshot,
  14. waitForTestIdByDom,
  15. } from "./helpers/desktop-app.js";
  16. import { prepareConversationE2E } from "./helpers/conversation-session.js";
  17. /**
  18. * Figma 4798:2443 空首页布局回归。
  19. *
  20. * 修复原因:旧首页把 keep-awake 提示条放在空状态之前,并把模板拉成三列;
  21. * 宽屏截图因此同时出现顺序、宽度和卡片密度偏差。这里验证真实 renderer 的
  22. * 几何关系,避免只靠 Tailwind class 静态阅读误判。
  23. */
  24. describe("Automations 空首页 Figma 布局 E2E", () => {
  25. let rendererSizeSnapshot: ElectronRendererContentSizeSnapshot | undefined;
  26. after(async () => {
  27. if (rendererSizeSnapshot) {
  28. await restoreElectronRendererContentSize(rendererSizeSnapshot);
  29. }
  30. await browser.electron.restoreAllMocks();
  31. await clearAppData();
  32. });
  33. it("按空状态、保持唤醒、两组双列模板的顺序渲染", async function () {
  34. this.timeout(150000);
  35. await prepareConversationE2E({ skipProvider: true });
  36. // 修复原因:Electron 41 不实现 WebDriver window/rect 依赖的
  37. // Browser.getWindowForTarget,必须通过 BrowserWindow 调整 renderer viewport。
  38. // macOS work area 可能小于 960px;本用例只需要宽屏双列断点,不能把不可达高度
  39. // 当作产品布局契约。
  40. rendererSizeSnapshot = await setCurrentElectronRendererContentSize(1440);
  41. await clickTestIdByDom(TID_AUTOMATIONS_OPEN, {
  42. timeoutMsg: "侧栏没有出现 Automations 入口",
  43. });
  44. await waitForTestIdByDom(TID_OFFPEAK_CREATE_BUTTON, {
  45. timeoutMsg: "闲时任务创建按钮没有出现(mock 灰度未命中?)",
  46. });
  47. await browser.waitUntil(
  48. async () =>
  49. browser.execute(() =>
  50. [
  51. "[data-automations-content]",
  52. "[data-automations-empty-state]",
  53. "[data-automations-keep-awake]",
  54. "[data-automations-idle-templates]",
  55. "[data-automations-scheduled-templates]",
  56. ].every((selector) => document.querySelector(selector)?.getBoundingClientRect().width),
  57. ),
  58. {
  59. timeout: 10000,
  60. timeoutMsg: "Automations 空首页组件没有完整渲染",
  61. },
  62. );
  63. // Bug 原因:模板 section 在 skeleton 阶段已经有宽度;旧 E2E 立即读取 grid,
  64. // 把 4 个骨架卡和无障碍文案节点误当成 5 张真实模板卡。
  65. await browser.waitUntil(
  66. () =>
  67. browser.execute(() =>
  68. ["[data-automations-idle-templates]", "[data-automations-scheduled-templates]"].every(
  69. (selector) => {
  70. const section = document.querySelector<HTMLElement>(selector);
  71. return (
  72. section?.getAttribute("aria-busy") === "false" &&
  73. !section.querySelector("[data-automation-template-skeleton-card]")
  74. );
  75. },
  76. ),
  77. ),
  78. {
  79. timeout: 30000,
  80. timeoutMsg: "Automations 模板仍处于 skeleton 加载态",
  81. },
  82. );
  83. const layout = await browser.execute((offPeakCreateTestId, manualCreateTestId) => {
  84. const requireElement = (selector: string): HTMLElement => {
  85. const element = document.querySelector<HTMLElement>(selector);
  86. if (!element) throw new Error(`Missing element: ${selector}`);
  87. return element;
  88. };
  89. const rect = (element: HTMLElement) => {
  90. const value = element.getBoundingClientRect();
  91. return {
  92. top: value.top,
  93. right: value.right,
  94. bottom: value.bottom,
  95. left: value.left,
  96. width: value.width,
  97. height: value.height,
  98. };
  99. };
  100. const gridMetrics = (section: HTMLElement) => {
  101. const grid = section.querySelector<HTMLElement>(".grid");
  102. if (!grid) throw new Error("Missing template grid");
  103. return {
  104. columns: getComputedStyle(grid).gridTemplateColumns.split(" ").filter(Boolean).length,
  105. cards: grid.children.length,
  106. firstCardRadius:
  107. grid.querySelector("button") instanceof HTMLElement
  108. ? getComputedStyle(grid.querySelector("button") as HTMLElement).borderRadius
  109. : "",
  110. };
  111. };
  112. const content = requireElement("[data-automations-content]");
  113. const empty = requireElement("[data-automations-empty-state]");
  114. const keepAwake = requireElement("[data-automations-keep-awake]");
  115. const idle = requireElement("[data-automations-idle-templates]");
  116. const scheduled = requireElement("[data-automations-scheduled-templates]");
  117. const manualCreate = requireElement(`[data-testid="${manualCreateTestId}"]`);
  118. const idleCreate = requireElement(`[data-testid="${offPeakCreateTestId}"]`);
  119. const contentStyle = getComputedStyle(content);
  120. return {
  121. contentWidth:
  122. content.getBoundingClientRect().width -
  123. Number.parseFloat(contentStyle.paddingLeft) -
  124. Number.parseFloat(contentStyle.paddingRight),
  125. empty: rect(empty),
  126. keepAwake: rect(keepAwake),
  127. idle: rect(idle),
  128. scheduled: rect(scheduled),
  129. manualCreate: rect(manualCreate),
  130. idleCreate: rect(idleCreate),
  131. emptyRadius: getComputedStyle(empty).borderRadius,
  132. keepAwakeRadius: getComputedStyle(keepAwake).borderRadius,
  133. idleGrid: gridMetrics(idle),
  134. scheduledGrid: gridMetrics(scheduled),
  135. };
  136. }, TID_OFFPEAK_CREATE_BUTTON, TID_AUTOMATION_CREATE_MANUALLY);
  137. // Settings 统一内容框当前为 max-w-4xl(896px)+ 左右 32px padding,内列应为 832px;
  138. // 旧 802px 是收敛 Settings 框架前的历史值,不能继续当作当前布局的回归基线。
  139. assertNear(layout.contentWidth, 832, 1, "桌面内容列宽度");
  140. assertNear(layout.empty.height, 226, 1, "空状态卡高度");
  141. assertNear(layout.keepAwake.height, 44, 1, "Keep-awake 提示条高度");
  142. assertNear(layout.keepAwake.top - layout.empty.bottom, 16, 1, "空状态与提示条间距");
  143. if (!(layout.empty.top < layout.keepAwake.top && layout.keepAwake.top < layout.idle.top)) {
  144. throw new Error("主页顺序不是空状态 → Keep-awake → Idle-time template");
  145. }
  146. if (!(layout.idle.top < layout.scheduled.top)) {
  147. throw new Error("Scheduled task template 没有位于 Idle-time task template 之后");
  148. }
  149. if (!(layout.manualCreate.right < layout.idleCreate.left)) {
  150. throw new Error("空状态按钮顺序不是 Create via chat → Idle-time task");
  151. }
  152. // 修复原因:Keep-awake 提示条已按当前设计契约统一为 10px,旧 E2E 仍断言 8px,
  153. // 导致实际布局与 UI 单元契约一致时反而误报失败。
  154. if (layout.emptyRadius !== "16px" || layout.keepAwakeRadius !== "10px") {
  155. throw new Error(
  156. `圆角不符合设计映射:empty=${layout.emptyRadius}, keepAwake=${layout.keepAwakeRadius}`,
  157. );
  158. }
  159. assertTemplateGrid(layout.idleGrid, "Idle-time task template", 3);
  160. assertTemplateGrid(layout.scheduledGrid, "Scheduled task template", 4);
  161. if (process.env.ZCODE_E2E_VISUAL_CAPTURE === "1") {
  162. await browser.saveScreenshot(join(tmpdir(), "zcode-automations-home-4798-2443.png"));
  163. }
  164. });
  165. });
  166. function assertNear(actual: number, expected: number, tolerance: number, label: string): void {
  167. if (Math.abs(actual - expected) > tolerance) {
  168. throw new Error(`${label}应为 ${expected}px,实际为 ${actual}px`);
  169. }
  170. }
  171. function assertTemplateGrid(
  172. grid: { columns: number; cards: number; firstCardRadius: string },
  173. label: string,
  174. expectedCards: number,
  175. ): void {
  176. if (grid.columns !== 2 || grid.cards !== expectedCards || grid.firstCardRadius !== "12px") {
  177. throw new Error(
  178. `${label} 应为双列 ${expectedCards} 卡且卡片圆角 12px,实际 columns=${grid.columns}, cards=${grid.cards}, radius=${grid.firstCardRadius}`,
  179. );
  180. }
  181. }