shortcut-settings-key-search.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import {
  2. TID_SETTINGS_PAGE,
  3. TID_SETTINGS_SECTION_NAV,
  4. TID_TASK_SETTINGS_BUTTON,
  5. testId,
  6. } from "@zcode/shared/test-ids";
  7. import {
  8. DEFAULT_WORKSPACE,
  9. clearAppData,
  10. setCurrentElectronRendererContentSize,
  11. restoreElectronRendererContentSize,
  12. clickTestIdByDom,
  13. waitForDefaultWorkspaceReady,
  14. waitForWorkspaceApp,
  15. } from "#e2e/helpers/desktop-app";
  16. import { sel } from "#e2e/helpers/selectors";
  17. /**
  18. * 快捷键「按组合键搜索」E2E(spec docs/desktop/shortcut-settings-spec.md §8/§10):
  19. * 武装态捕获组合 → 物理等价口径过滤命令表 → 清除恢复全量 → 无命中空态 → Escape 退出武装态。
  20. */
  21. describe("快捷键按组合键搜索 E2E", () => {
  22. before(async () => {
  23. await waitForDefaultWorkspaceReady(30_000);
  24. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30_000);
  25. await openShortcutSettings();
  26. });
  27. after(async () => {
  28. await browser.electron.restoreAllMocks();
  29. await clearAppData();
  30. });
  31. it("SC-11: 武装后按组合键,过滤出物理等价绑定的命令;清除后恢复全量", async function () {
  32. this.timeout(90_000);
  33. // 默认绑定 CmdOrCtrl+k(openCommandCenter):武装后按下平台主修饰+K 即命中。
  34. // 录制器平台归一后捕获串与默认串同为 CmdOrCtrl+k —— 等价归一路径(win 显式
  35. // Ctrl 默认 vs CmdOrCtrl 产物的互检)在单测覆盖,这里验证端到端过滤行为。
  36. await clickTestIdByDom("settings-shortcut-key-search", {
  37. timeout: 15_000,
  38. timeoutMsg: "搜索框右端没有出现按组合键搜索按钮",
  39. });
  40. expect(await $(sel("settings-shortcut-key-search")).getAttribute("aria-pressed")).toBe("true");
  41. await dispatchShortcut("k", "KeyK");
  42. // 捕获徽标出现;列表过滤为绑定该组合的命令(openCommandCenter 在、toggleSidebar 不在)
  43. await $(sel("settings-shortcut-key-search-clear")).waitForDisplayed({ timeout: 10_000 });
  44. await $(sel("settings-shortcut-row-openCommandCenter")).waitForDisplayed({ timeout: 10_000 });
  45. expect(await $(sel("settings-shortcut-row-toggleSidebar")).isExisting()).toBe(false);
  46. // 清除按键过滤后恢复全量列表
  47. await clickTestIdByDom("settings-shortcut-key-search-clear", {
  48. timeout: 10_000,
  49. timeoutMsg: "捕获组合的清除按钮没有出现",
  50. });
  51. await $(sel("settings-shortcut-row-toggleSidebar")).waitForDisplayed({ timeout: 10_000 });
  52. expect(await $(sel("settings-shortcut-row-openCommandCenter")).isExisting()).toBe(true);
  53. });
  54. it("SC-12: 无命中组合展示空态;Escape 退出武装态不再吞键", async function () {
  55. this.timeout(60_000);
  56. await clickTestIdByDom("settings-shortcut-key-search", {
  57. timeout: 10_000,
  58. timeoutMsg: "搜索框右端没有出现按组合键搜索按钮",
  59. });
  60. // CmdOrCtrl+Shift+9 未被任何命令绑定 → 空态文案
  61. await dispatchShortcut("9", "Digit9", { shiftKey: true });
  62. await $(sel("settings-shortcut-search-empty")).waitForDisplayed({ timeout: 10_000 });
  63. expect(await $(sel("settings-shortcut-row-openCommandCenter")).isExisting()).toBe(false);
  64. // 清除空态过滤,重新武装后按 Escape 退出(aria-pressed 复位、列表仍在)
  65. await clickTestIdByDom("settings-shortcut-key-search-clear", {
  66. timeout: 10_000,
  67. timeoutMsg: "捕获组合的清除按钮没有出现",
  68. });
  69. await $(sel("settings-shortcut-row-openCommandCenter")).waitForDisplayed({ timeout: 10_000 });
  70. await clickTestIdByDom("settings-shortcut-key-search", { timeout: 10_000 });
  71. expect(await $(sel("settings-shortcut-key-search")).getAttribute("aria-pressed")).toBe("true");
  72. await dispatchKey("Escape", "Escape", {});
  73. await browser.pause(300);
  74. expect(await $(sel("settings-shortcut-key-search")).getAttribute("aria-pressed")).toBe("false");
  75. // 退出武装态后命令行仍完整(Escape 只退出捕获,不动过滤/列表)
  76. expect(await $(sel("settings-shortcut-row-openCommandCenter")).isDisplayed()).toBe(true);
  77. });
  78. it("SC-13: 深浅主题与窄屏保持控件和长组合键可见", async function () {
  79. this.timeout(90_000);
  80. const originalSize = await setCurrentElectronRendererContentSize(1100, 800);
  81. const originalMinimum = await browser.electron.execute((electron, windowId) => {
  82. const window = electron.BrowserWindow.fromId(windowId)!;
  83. const minimum = window.getMinimumSize();
  84. // 测试独立窗口放开桌面最小宽度,验证手机宽度的共享设置组件;结束时恢复。
  85. window.setMinimumSize(0, 0);
  86. return minimum;
  87. }, originalSize.windowId);
  88. try {
  89. for (const theme of ["light", "dark"]) {
  90. await browser.execute((value) => {
  91. const actions = (
  92. window as typeof window & { __testActions?: { setTheme?: (theme: string) => void } }
  93. ).__testActions;
  94. if (!actions?.setTheme) throw new Error("missing setTheme action");
  95. actions.setTheme(value);
  96. }, theme);
  97. for (const width of [1100, 390]) {
  98. await setCurrentElectronRendererContentSize(width, 800);
  99. const search = await $(sel("settings-shortcut-search-input"));
  100. await search.waitForDisplayed();
  101. await browser.waitUntil(
  102. async () =>
  103. browser.execute(() => {
  104. const page = document.querySelector('[data-testid="settings-shortcuts-section"]')!;
  105. return page.scrollWidth <= page.clientWidth + 1;
  106. }),
  107. { timeout: 5000, timeoutMsg: "快捷键页面横向溢出" },
  108. );
  109. const geometry = await browser.execute(() => {
  110. const input = document.querySelector('[data-testid="settings-shortcut-search-input"]')!;
  111. const row = document.querySelector(
  112. '[data-testid="settings-shortcut-row-toggleSidebar"]',
  113. )!;
  114. const edit = row.querySelector(
  115. '[data-testid="settings-shortcut-edit-toggleSidebar-0"]',
  116. )!;
  117. const clear = row.querySelector(
  118. '[data-testid="settings-shortcut-clear-toggleSidebar"]',
  119. )!;
  120. const style = getComputedStyle(input);
  121. return {
  122. keycaps: [...row.querySelectorAll("kbd")].map((keycap) => {
  123. const style = getComputedStyle(keycap);
  124. return {
  125. background: style.backgroundColor,
  126. height: keycap.getBoundingClientRect().height,
  127. radius: style.borderRadius,
  128. };
  129. }),
  130. height: input.getBoundingClientRect().height,
  131. font: style.fontFamily,
  132. edit: edit.getBoundingClientRect().width,
  133. clear: clear.getBoundingClientRect().width,
  134. contained: [
  135. ...document.querySelectorAll('[data-testid^="settings-shortcut-row-"]'),
  136. ].every((commandRow) => {
  137. const bounds = commandRow.getBoundingClientRect();
  138. return [...commandRow.querySelectorAll("button, kbd")].every((el) => {
  139. const r = el.getBoundingClientRect();
  140. return r.left >= bounds.left && r.right <= bounds.right;
  141. });
  142. }),
  143. };
  144. });
  145. expect(geometry.keycaps.length).toBeGreaterThan(0);
  146. for (const keycap of geometry.keycaps) {
  147. expect(keycap.background).not.toBe("rgba(0, 0, 0, 0)");
  148. expect(keycap.height).toBe(16);
  149. expect(keycap.radius).toBe("6px");
  150. }
  151. expect(geometry.height).toBe(36);
  152. expect(geometry.font).not.toContain("mono");
  153. expect(geometry.edit).toBe(geometry.clear);
  154. expect(geometry.contained).toBe(true);
  155. await browser.saveScreenshot(`.e2e-artifacts/shortcut-settings-${theme}-${width}.png`);
  156. }
  157. }
  158. } finally {
  159. await browser.electron.execute(
  160. (electron, windowId, minimum) => {
  161. electron.BrowserWindow.fromId(windowId)?.setMinimumSize(minimum[0] ?? 0, minimum[1] ?? 0);
  162. },
  163. originalSize.windowId,
  164. originalMinimum,
  165. );
  166. await restoreElectronRendererContentSize(originalSize);
  167. await browser.execute(() =>
  168. (
  169. window as typeof window & { __testActions?: { setTheme?: (theme: string) => void } }
  170. ).__testActions?.setTheme?.("light"),
  171. );
  172. }
  173. });
  174. it("SC-14: 清除保存后可继续编辑并恢复默认", async function () {
  175. this.timeout(60_000);
  176. await $(sel("settings-shortcut-clear-toggleSidebar")).click();
  177. await $(sel("settings-shortcut-bind-toggleSidebar-unassigned")).waitForDisplayed({
  178. timeout: 15000,
  179. });
  180. await browser.waitUntil(
  181. async () => $(sel("settings-shortcut-bind-toggleSidebar-unassigned")).isEnabled(),
  182. { timeout: 15000 },
  183. );
  184. await $(sel("settings-shortcut-bind-toggleSidebar-unassigned")).click();
  185. await dispatchKey("Backspace", "Backspace", {});
  186. await $(sel("settings-shortcut-bind-toggleSidebar-0")).waitForDisplayed({ timeout: 15000 });
  187. await browser.waitUntil(
  188. async () => $(sel("settings-shortcut-clear-toggleSidebar")).isEnabled(),
  189. { timeout: 15000 },
  190. );
  191. });
  192. });
  193. async function openShortcutSettings() {
  194. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, {
  195. timeout: 15_000,
  196. timeoutMsg: "没有找到设置入口按钮",
  197. });
  198. await $(sel(TID_SETTINGS_PAGE)).waitForDisplayed({ timeout: 15_000 });
  199. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "shortcuts"), {
  200. timeout: 15_000,
  201. timeoutMsg: "设置页没有出现快捷键分区入口",
  202. });
  203. }
  204. /** 按平台主修饰键(mac=Meta,其他=Ctrl)派发合成 keydown 到 window(capture 监听可收到)。 */
  205. async function dispatchShortcut(key: string, code: string, opts: { shiftKey?: boolean } = {}) {
  206. const isMac = await browser.execute(() => /Mac|iPhone|iPad/.test(navigator.userAgent));
  207. await dispatchKey(key, code, {
  208. metaKey: isMac,
  209. ctrlKey: !isMac,
  210. shiftKey: opts.shiftKey ?? false,
  211. });
  212. }
  213. async function dispatchKey(
  214. key: string,
  215. code: string,
  216. modifiers: { metaKey?: boolean; ctrlKey?: boolean; shiftKey?: boolean },
  217. ) {
  218. await browser.execute(
  219. (eventKey, eventCode, metaKey, ctrlKey, shiftKey) => {
  220. window.dispatchEvent(
  221. new KeyboardEvent("keydown", {
  222. key: eventKey,
  223. code: eventCode,
  224. metaKey,
  225. ctrlKey,
  226. shiftKey,
  227. bubbles: true,
  228. cancelable: true,
  229. }),
  230. );
  231. },
  232. key,
  233. code,
  234. modifiers.metaKey ?? false,
  235. modifiers.ctrlKey ?? false,
  236. modifiers.shiftKey ?? false,
  237. );
  238. }