command-center-focus.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import { writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { DEFAULT_WORKSPACE, waitForWorkspaceApp } from "#e2e/helpers/desktop-app";
  4. async function setTheme(theme: string) {
  5. await browser.execute((value) => {
  6. const actions = (
  7. window as typeof window & { __testActions?: { setTheme(theme: string): void } }
  8. ).__testActions;
  9. if (!actions) throw new Error("缺少主题测试入口");
  10. actions.setTheme(value);
  11. }, theme);
  12. await browser.waitUntil(() =>
  13. browser.execute(
  14. (value) => document.documentElement.classList.contains(`theme-${value}`),
  15. theme,
  16. ),
  17. );
  18. }
  19. async function emulate(width?: number, forcedColors = false) {
  20. await browser.electron.execute(
  21. async (electron, width, forcedColors) => {
  22. const window = electron.BrowserWindow.getAllWindows().find(
  23. (candidate) => !candidate.isDestroyed() && candidate.isVisible(),
  24. );
  25. if (!window) throw new Error("No visible renderer");
  26. if (!window.webContents.debugger.isAttached()) window.webContents.debugger.attach("1.3");
  27. await window.webContents.debugger.sendCommand(
  28. width ? "Emulation.setDeviceMetricsOverride" : "Emulation.clearDeviceMetricsOverride",
  29. width ? { width, height: 844, deviceScaleFactor: 1, mobile: false } : {},
  30. );
  31. await window.webContents.debugger.sendCommand("Emulation.setEmulatedMedia", {
  32. features: forcedColors ? [{ name: "forced-colors", value: "active" }] : [],
  33. });
  34. },
  35. width,
  36. forcedColors,
  37. );
  38. }
  39. describe("组合输入框焦点", () => {
  40. afterEach(async () => {
  41. await writeFile(
  42. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "command-center-focus-dialogs.json"),
  43. JSON.stringify(
  44. await browser.execute(() =>
  45. Array.from(document.querySelectorAll('[role="dialog"], [role="alertdialog"]')).map(
  46. (dialog) => dialog.textContent,
  47. ),
  48. ),
  49. ),
  50. );
  51. await browser.keys("Escape");
  52. await $('[role="dialog"]').waitForDisplayed({ reverse: true });
  53. });
  54. it("CC-FOCUS-01:深浅主题和窄屏只保留外层胶囊边框", async () => {
  55. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 45_000);
  56. for (const theme of ["zai-light", "zai-dark"]) {
  57. await setTheme(theme);
  58. const search = $('[data-testid="sidebar-search-open"]');
  59. await search.waitForDisplayed();
  60. await $('[data-testid="plugin-store-sidebar-open"]').moveTo();
  61. const entrance = await browser.execute(() => {
  62. const button = document.querySelector<HTMLElement>('[data-testid="sidebar-search-open"]')!;
  63. button.blur();
  64. const filter = document.querySelector<HTMLElement>('[data-testid="sidebar-view-menu"]')!;
  65. const title = document.querySelector<HTMLElement>('[data-testid="sidebar-list-title"]')!;
  66. const section = document.querySelector<HTMLElement>(
  67. '[data-purpose-section-drag-trigger="projects"]',
  68. )!;
  69. const titleColorProbe = document.createElement("span");
  70. titleColorProbe.className = "text-foreground-subtle";
  71. title.parentElement!.appendChild(titleColorProbe);
  72. const titleColor = getComputedStyle(titleColorProbe).color;
  73. titleColorProbe.remove();
  74. const searchRect = button.getBoundingClientRect();
  75. const filterRect = filter.getBoundingClientRect();
  76. return {
  77. titleUsesBase:
  78. parseFloat(getComputedStyle(title).fontSize) ===
  79. parseFloat(
  80. getComputedStyle(document.documentElement).getPropertyValue("--ui-font-size"),
  81. ),
  82. sectionUsesBase:
  83. parseFloat(getComputedStyle(section).fontSize) ===
  84. parseFloat(
  85. getComputedStyle(document.documentElement).getPropertyValue("--ui-font-size"),
  86. ),
  87. titleSizeDelta:
  88. parseFloat(getComputedStyle(title).fontSize) -
  89. parseFloat(getComputedStyle(section).fontSize),
  90. titleHasSecondaryColor: getComputedStyle(title).color === titleColor,
  91. titleTopSpacing: getComputedStyle(
  92. title.closest<HTMLElement>('[data-testid="sidebar-task-list-region"]')!,
  93. ).paddingTop,
  94. titleBottomSpacing: getComputedStyle(
  95. title.closest<HTMLElement>('[data-testid="sidebar-search-toolbar"]')!,
  96. ).paddingBottom,
  97. listDividerWidth: getComputedStyle(
  98. title.closest<HTMLElement>('[data-testid="sidebar-task-list-region"]')!,
  99. ).borderTopWidth,
  100. independentHeader:
  101. title.closest('[data-testid="sidebar-task-list-region"]') !== null &&
  102. title.closest('[data-testid="sidebar-primary-navigation"]') === null,
  103. tag: button.tagName,
  104. searchWidth: searchRect.width,
  105. searchHeight: searchRect.height,
  106. buttonGap: filterRect.left - searchRect.right,
  107. filterWidth: filterRect.width,
  108. filterHeight: filterRect.height,
  109. ordered:
  110. title.getBoundingClientRect().right <= searchRect.left &&
  111. searchRect.right <= filterRect.left,
  112. text: button.textContent,
  113. };
  114. });
  115. expect(entrance).toEqual({
  116. titleUsesBase: true,
  117. sectionUsesBase: true,
  118. titleSizeDelta: 0,
  119. titleHasSecondaryColor: true,
  120. titleTopSpacing: "12px",
  121. titleBottomSpacing: "8px",
  122. listDividerWidth: "1px",
  123. independentHeader: true,
  124. tag: "BUTTON",
  125. searchWidth: 24,
  126. searchHeight: 24,
  127. buttonGap: 4,
  128. filterWidth: 24,
  129. filterHeight: 24,
  130. ordered: true,
  131. text: "",
  132. });
  133. const divider = await browser.execute(() => {
  134. const region = document.querySelector<HTMLElement>(
  135. '[data-testid="sidebar-task-list-region"]',
  136. )!;
  137. const extension = document.querySelector<HTMLElement>("[data-sidebar-divider-extension]")!;
  138. const handle = document.querySelector<HTMLElement>('[data-testid="resizable-handle"]')!;
  139. const regionRect = region.getBoundingClientRect();
  140. const extensionRect = extension.getBoundingClientRect();
  141. const handleRect = handle.getBoundingClientRect();
  142. const probe = document.createElement("span");
  143. probe.className = "border-t border-border/50";
  144. region.appendChild(probe);
  145. const expectedColor = getComputedStyle(probe).borderTopColor;
  146. probe.remove();
  147. return {
  148. startGap: extensionRect.left - regionRect.right,
  149. endGap: handleRect.right - extensionRect.right,
  150. verticalGap: extensionRect.top - regionRect.top,
  151. height: extensionRect.height,
  152. width: handleRect.width,
  153. pointerEvents: getComputedStyle(extension).pointerEvents,
  154. sameColor:
  155. getComputedStyle(extension).backgroundColor ===
  156. getComputedStyle(region).borderTopColor &&
  157. getComputedStyle(region).borderTopColor === expectedColor,
  158. };
  159. });
  160. expect(divider).toEqual({
  161. startGap: 0,
  162. endGap: 0,
  163. verticalGap: 0,
  164. height: 1,
  165. width: 4,
  166. pointerEvents: "none",
  167. sameColor: true,
  168. });
  169. await browser.saveScreenshot(
  170. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, `sidebar-title-actions-${theme}.png`),
  171. );
  172. const dragStart = await browser.execute(() => {
  173. const region = document.querySelector<HTMLElement>(
  174. '[data-testid="sidebar-task-list-region"]',
  175. )!;
  176. const handle = document.querySelector<HTMLElement>('[data-testid="resizable-handle"]')!;
  177. const sidebar = document.querySelector<HTMLElement>(
  178. '[data-workspace-sidebar-panel="true"]',
  179. )!;
  180. return {
  181. x: Math.round(handle.getBoundingClientRect().left + 2),
  182. y: Math.round(region.getBoundingClientRect().top),
  183. width: sidebar.getBoundingClientRect().width,
  184. };
  185. });
  186. await browser.performActions([
  187. {
  188. type: "pointer",
  189. id: "divider-resize",
  190. parameters: { pointerType: "mouse" },
  191. actions: [
  192. { type: "pointerMove", duration: 0, x: dragStart.x, y: dragStart.y },
  193. { type: "pointerDown", button: 0 },
  194. { type: "pointerMove", duration: 200, x: dragStart.x + 24, y: dragStart.y },
  195. { type: "pointerUp", button: 0 },
  196. ],
  197. },
  198. ]);
  199. await browser.releaseActions();
  200. await browser.waitUntil(
  201. async () =>
  202. (await browser.execute(
  203. () =>
  204. document
  205. .querySelector<HTMLElement>('[data-workspace-sidebar-panel="true"]')!
  206. .getBoundingClientRect().width,
  207. )) >
  208. dragStart.width + 10,
  209. { timeout: 5000, timeoutMsg: "分隔线处拖拽未改变侧栏宽度" },
  210. );
  211. await search.moveTo();
  212. const hint = $('[data-slot="tooltip-content"]');
  213. await hint.waitForDisplayed();
  214. expect(await hint.$('[data-slot="kbd"]').getText()).toMatch(/K/);
  215. expect(await hint.getText()).not.toContain(" · ");
  216. await browser.saveScreenshot(
  217. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, `sidebar-search-tooltip-${theme}.png`),
  218. );
  219. await search.click();
  220. const input = $('[role="dialog"] [cmdk-input]');
  221. await input.waitForDisplayed();
  222. for (const width of [512, 360]) {
  223. // 样式回归只收窄共享面板,不依赖任务 fixture 或手机连接。
  224. await browser.execute((value) => {
  225. const dialog = document.querySelector<HTMLElement>('[role="dialog"]')!;
  226. dialog.style.setProperty("width", `${value}px`, "important");
  227. dialog.style.setProperty("max-width", `${value}px`, "important");
  228. }, width);
  229. // 面板宽度有过渡动画,读取最终几何后再断言,避免把动画中间帧当成回归。
  230. await browser.waitUntil(() =>
  231. browser.execute(
  232. (value) =>
  233. document.querySelector('[role="dialog"]')!.getBoundingClientRect().width === value,
  234. width,
  235. ),
  236. );
  237. await input.click();
  238. const focusStyle = await browser.execute(() => {
  239. const input = document.querySelector('[role="dialog"] [cmdk-input]')!;
  240. return {
  241. focusVisible: input.matches(":focus-visible"),
  242. // outline 为 none 时,当前 Electron 仍返回 medium 的 3px;按是否绘制断言。
  243. outlineStyle: getComputedStyle(input).outlineStyle,
  244. boxShadow: getComputedStyle(input).boxShadow,
  245. shellBorderWidth: getComputedStyle(input.parentElement!).borderTopWidth,
  246. width: document.querySelector('[role="dialog"]')!.getBoundingClientRect().width,
  247. };
  248. });
  249. expect(focusStyle).toEqual({
  250. focusVisible: true,
  251. outlineStyle: "none",
  252. boxShadow: "none",
  253. shellBorderWidth: "1px",
  254. width,
  255. });
  256. await browser.saveScreenshot(
  257. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, `command-center-focus-${theme}-${width}.png`),
  258. );
  259. }
  260. await browser.keys("Escape");
  261. }
  262. });
  263. it("DIALOG-FOCUS-01:项目名称沿用共享 Input 焦点边框,点击和 Tab 不叠加 outline", async () => {
  264. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 45_000);
  265. try {
  266. // 窄屏会收起侧栏;先打开共享弹窗,再在同一弹窗切换主题与宽度,不创建项目。
  267. await emulate(1200);
  268. await $('[data-testid="virtual-project-add"]').click();
  269. // 创建项目已直接进入表单,旧类型选择步骤不再存在。
  270. const input = $('[data-testid="virtual-project-name"]');
  271. await input.waitForDisplayed();
  272. await browser.waitUntil(() => input.isFocused());
  273. for (const theme of ["zai-light", "zai-dark"]) {
  274. await setTheme(theme);
  275. for (const width of [1200, 390]) {
  276. for (const forcedColors of [false, true]) {
  277. await emulate(width, forcedColors);
  278. await browser.waitUntil(() =>
  279. browser.execute(
  280. (expectedWidth, forced) =>
  281. innerWidth === expectedWidth &&
  282. matchMedia("(forced-colors: active)").matches === forced,
  283. width,
  284. forcedColors,
  285. ),
  286. );
  287. await input.click();
  288. await browser.keys("Tab");
  289. expect(await input.isFocused()).toBe(false);
  290. await browser.keys(["Shift", "Tab"]);
  291. expect(await input.isFocused()).toBe(true);
  292. await input.setValue("E2E_DIALOG_FOCUS");
  293. expect(await input.getValue()).toBe("E2E_DIALOG_FOCUS");
  294. const focusStyle = await browser.execute(() => {
  295. const input = document.querySelector('[data-testid="virtual-project-name"]')!;
  296. const dialog = input.closest('[role="dialog"]')!.getBoundingClientRect();
  297. // 名称已使用独立 Input;旧测试把普通布局父元素当成组合输入外壳。
  298. const probe = document.createElement("span");
  299. probe.style.border = "1px solid var(--color-input-border-focused)";
  300. document.body.appendChild(probe);
  301. const expectedBorderColor = getComputedStyle(probe).borderTopColor;
  302. probe.remove();
  303. return {
  304. focusVisible: input.matches(":focus-visible"),
  305. // outline 为 none 时,当前 Electron 仍返回 medium 的 3px;按是否绘制断言。
  306. outlineStyle: getComputedStyle(input).outlineStyle,
  307. boxShadow: getComputedStyle(input).boxShadow,
  308. inputBorderWidth: getComputedStyle(input).borderTopWidth,
  309. inputBorderColor: getComputedStyle(input).borderTopColor,
  310. expectedBorderColor,
  311. parentBorderWidth: getComputedStyle(input.parentElement!).borderTopWidth,
  312. fits: dialog.left >= 0 && dialog.right <= innerWidth,
  313. inputSize: Number.parseFloat(getComputedStyle(input).fontSize),
  314. };
  315. });
  316. expect(focusStyle.focusVisible).toBe(true);
  317. expect(focusStyle.outlineStyle).toBe("none");
  318. expect(focusStyle.boxShadow).toBe("none");
  319. expect(focusStyle.inputBorderWidth).toBe("1px");
  320. expect(focusStyle.parentBorderWidth).toBe("0px");
  321. if (!forcedColors)
  322. expect(focusStyle.inputBorderColor).toBe(focusStyle.expectedBorderColor);
  323. expect(focusStyle.fits).toBe(true);
  324. if (width === 390) expect(focusStyle.inputSize).toBeGreaterThanOrEqual(16);
  325. await browser.saveScreenshot(
  326. join(
  327. process.env.ZCODE_E2E_ARTIFACT_DIR!,
  328. `project-focus-${theme}-${width}-${forcedColors ? "forced" : "normal"}.png`,
  329. ),
  330. );
  331. }
  332. }
  333. }
  334. } finally {
  335. await emulate();
  336. await browser.keys("Escape");
  337. }
  338. });
  339. });