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

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