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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import {
  2. TID_SETTINGS_PAGE,
  3. TID_SETTINGS_SECTION_NAV,
  4. TID_TASK_SETTINGS_BUTTON,
  5. testId,
  6. } from "@zcode/shared";
  7. import {
  8. DEFAULT_WORKSPACE,
  9. clearAppData,
  10. clickTestIdByDom,
  11. waitForDefaultWorkspaceReady,
  12. waitForWorkspaceApp,
  13. } from "../helpers/desktop-app.js";
  14. import { sel } from "../helpers/selectors.js";
  15. /**
  16. * 快捷键「按组合键搜索」E2E(spec docs/desktop/shortcut-settings-spec.md §8/§10):
  17. * 武装态捕获组合 → 物理等价口径过滤命令表 → 清除恢复全量 → 无命中空态 → Escape 退出武装态。
  18. */
  19. describe("快捷键按组合键搜索 E2E", () => {
  20. before(async () => {
  21. await waitForDefaultWorkspaceReady(30_000);
  22. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30_000);
  23. await openShortcutSettings();
  24. });
  25. after(async () => {
  26. await browser.electron.restoreAllMocks();
  27. await clearAppData();
  28. });
  29. it("SC-11: 武装后按组合键,过滤出物理等价绑定的命令;清除后恢复全量", async function () {
  30. this.timeout(90_000);
  31. // 默认绑定 CmdOrCtrl+k(openCommandCenter):武装后按下平台主修饰+K 即命中。
  32. // 录制器平台归一后捕获串与默认串同为 CmdOrCtrl+k —— 等价归一路径(win 显式
  33. // Ctrl 默认 vs CmdOrCtrl 产物的互检)在单测覆盖,这里验证端到端过滤行为。
  34. await clickTestIdByDom("settings-shortcut-key-search", {
  35. timeout: 15_000,
  36. timeoutMsg: "搜索框右端没有出现按组合键搜索按钮",
  37. });
  38. expect(await $(sel("settings-shortcut-key-search")).getAttribute("aria-pressed")).toBe("true");
  39. await dispatchShortcut("k", "KeyK");
  40. // 捕获徽标出现;列表过滤为绑定该组合的命令(openCommandCenter 在、toggleSidebar 不在)
  41. await $(sel("settings-shortcut-key-search-clear")).waitForDisplayed({ timeout: 10_000 });
  42. await $(sel("settings-shortcut-row-openCommandCenter")).waitForDisplayed({ timeout: 10_000 });
  43. expect(await $(sel("settings-shortcut-row-toggleSidebar")).isExisting()).toBe(false);
  44. // 清除按键过滤后恢复全量列表
  45. await clickTestIdByDom("settings-shortcut-key-search-clear", {
  46. timeout: 10_000,
  47. timeoutMsg: "捕获组合的清除按钮没有出现",
  48. });
  49. await $(sel("settings-shortcut-row-toggleSidebar")).waitForDisplayed({ timeout: 10_000 });
  50. expect(await $(sel("settings-shortcut-row-openCommandCenter")).isExisting()).toBe(true);
  51. });
  52. it("SC-12: 无命中组合展示空态;Escape 退出武装态不再吞键", async function () {
  53. this.timeout(60_000);
  54. await clickTestIdByDom("settings-shortcut-key-search", {
  55. timeout: 10_000,
  56. timeoutMsg: "搜索框右端没有出现按组合键搜索按钮",
  57. });
  58. // CmdOrCtrl+Shift+9 未被任何命令绑定 → 空态文案
  59. await dispatchShortcut("9", "Digit9", { shiftKey: true });
  60. await $(sel("settings-shortcut-search-empty")).waitForDisplayed({ timeout: 10_000 });
  61. expect(await $(sel("settings-shortcut-row-openCommandCenter")).isExisting()).toBe(false);
  62. // 清除空态过滤,重新武装后按 Escape 退出(aria-pressed 复位、列表仍在)
  63. await clickTestIdByDom("settings-shortcut-key-search-clear", {
  64. timeout: 10_000,
  65. timeoutMsg: "捕获组合的清除按钮没有出现",
  66. });
  67. await $(sel("settings-shortcut-row-openCommandCenter")).waitForDisplayed({ timeout: 10_000 });
  68. await clickTestIdByDom("settings-shortcut-key-search", { timeout: 10_000 });
  69. expect(await $(sel("settings-shortcut-key-search")).getAttribute("aria-pressed")).toBe("true");
  70. await dispatchKey("Escape", "Escape", {});
  71. await browser.pause(300);
  72. expect(await $(sel("settings-shortcut-key-search")).getAttribute("aria-pressed")).toBe("false");
  73. // 退出武装态后命令行仍完整(Escape 只退出捕获,不动过滤/列表)
  74. expect(await $(sel("settings-shortcut-row-openCommandCenter")).isDisplayed()).toBe(true);
  75. });
  76. });
  77. async function openShortcutSettings() {
  78. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, {
  79. timeout: 15_000,
  80. timeoutMsg: "没有找到设置入口按钮",
  81. });
  82. await $(sel(TID_SETTINGS_PAGE)).waitForDisplayed({ timeout: 15_000 });
  83. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "shortcuts"), {
  84. timeout: 15_000,
  85. timeoutMsg: "设置页没有出现快捷键分区入口",
  86. });
  87. }
  88. /** 按平台主修饰键(mac=Meta,其他=Ctrl)派发合成 keydown 到 window(capture 监听可收到)。 */
  89. async function dispatchShortcut(key: string, code: string, opts: { shiftKey?: boolean } = {}) {
  90. const isMac = await browser.execute(() => /Mac|iPhone|iPad/.test(navigator.userAgent));
  91. await dispatchKey(key, code, {
  92. metaKey: isMac,
  93. ctrlKey: !isMac,
  94. shiftKey: opts.shiftKey ?? false,
  95. });
  96. }
  97. async function dispatchKey(
  98. key: string,
  99. code: string,
  100. modifiers: { metaKey?: boolean; ctrlKey?: boolean; shiftKey?: boolean },
  101. ) {
  102. await browser.execute(
  103. (eventKey, eventCode, metaKey, ctrlKey, shiftKey) => {
  104. window.dispatchEvent(
  105. new KeyboardEvent("keydown", {
  106. key: eventKey,
  107. code: eventCode,
  108. metaKey,
  109. ctrlKey,
  110. shiftKey,
  111. bubbles: true,
  112. cancelable: true,
  113. }),
  114. );
  115. },
  116. key,
  117. code,
  118. modifiers.metaKey ?? false,
  119. modifiers.ctrlKey ?? false,
  120. modifiers.shiftKey ?? false,
  121. );
  122. }