settings-navigation-search.test.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import { join } from "node:path";
  2. import {
  3. TID_SETTINGS_PAGE,
  4. TID_SETTINGS_SECTION_NAV,
  5. TID_TASK_SETTINGS_BUTTON,
  6. testId,
  7. } from "@zcode/shared/test-ids";
  8. import { clearAppData, clickTestIdByDom, readSettings } from "#e2e/helpers/desktop-app";
  9. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  10. const input = () => $('[data-testid="settings-search-input"]');
  11. const page = () => $(`[data-testid="${TID_SETTINGS_PAGE}"]`);
  12. const result = (id: string) => $(`[data-settings-search-result="${id}"]`);
  13. const anchor = (id: string) => $(`[data-settings-search-id="${id}"]`);
  14. async function openSearch() {
  15. await clickTestIdByDom("settings-search-open");
  16. await input().waitForDisplayed();
  17. }
  18. async function setPresentation(theme: string, locale: string) {
  19. await browser.execute(
  20. (theme, locale) => {
  21. const actions = (
  22. window as typeof window & {
  23. __testActions?: { setTheme(value: string): void; setLocale(value: string): void };
  24. }
  25. ).__testActions;
  26. if (!actions) throw new Error("Missing UI test actions");
  27. actions.setTheme(theme);
  28. actions.setLocale(locale);
  29. },
  30. theme,
  31. locale,
  32. );
  33. await browser.waitUntil(() =>
  34. browser.execute(
  35. (theme, locale) =>
  36. document.documentElement.classList.contains(`theme-${theme}`) &&
  37. document.documentElement.lang === locale,
  38. theme,
  39. locale,
  40. ),
  41. );
  42. }
  43. async function expectLocated(id: string) {
  44. await $('[data-testid="settings-search-dialog"]').waitForDisplayed({ reverse: true });
  45. await anchor(id).waitForDisplayed({ timeout: 15000 });
  46. expect(await anchor(id).getAttribute("data-settings-search-highlighted")).toBe("true");
  47. await browser.waitUntil(() =>
  48. browser.execute((id) => {
  49. const target = document.querySelector(`[data-settings-search-id="${id}"]`);
  50. if (!target) return false;
  51. const rect = target.getBoundingClientRect();
  52. return (
  53. document.activeElement === target &&
  54. rect.top >= 0 &&
  55. rect.top < innerHeight &&
  56. rect.bottom > 0
  57. );
  58. }, id),
  59. );
  60. }
  61. describe("设置项模糊搜索", () => {
  62. before(async () => {
  63. await prepareV4ConversationE2E({ skipProvider: true });
  64. await setPresentation("zai-light", "zh-CN");
  65. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON);
  66. await page().waitForDisplayed();
  67. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "general"));
  68. });
  69. after(async () => {
  70. await browser.electron.restoreAllMocks();
  71. await clearAppData();
  72. });
  73. it("SS01:搜索具体设置名称、拼音、英文与说明,显示所属分类", async () => {
  74. await openSearch();
  75. for (const query of [
  76. "通知声",
  77. "通声",
  78. "tongzhishengyin",
  79. "tzsy",
  80. " NOTIFICATION SOUND ",
  81. ]) {
  82. await input().setValue(query);
  83. await result("settings.notificationSound").waitForDisplayed();
  84. expect(await result("settings.notificationSound").getText()).toContain("常规");
  85. expect(await result("settings.uiFontSize").isExisting()).toBe(false);
  86. expect(await page().getAttribute("data-active-section")).toBe("general");
  87. }
  88. await input().setValue("出口流量");
  89. await result("settings.httpProxy").waitForDisplayed();
  90. await input().setValue("zzzz-no-such-setting");
  91. await $('[data-testid="settings-search-empty"]').waitForDisplayed();
  92. expect(await result("automations").isExisting()).toBe(false);
  93. await clickTestIdByDom("settings-search-clear");
  94. expect(await input().getValue()).toBe("");
  95. expect(await result("settings.themeMode").isExisting()).toBe(true);
  96. await input().click();
  97. await browser.keys("Escape");
  98. });
  99. it("SS02:点击和 Enter 定位具体行;重新定位、清空和普通导航保持原行为", async function () {
  100. this.timeout(180000);
  101. const settings = await readSettings();
  102. // 每个固定设置项都走真实搜索和导航,验证索引没有悬空的定位 id。
  103. await openSearch();
  104. const ids = await browser.execute(() =>
  105. [...document.querySelectorAll<HTMLElement>("[data-settings-search-result]")]
  106. .map((node) => node.dataset.settingsSearchResult)
  107. .filter((id): id is string => Boolean(id?.includes("."))),
  108. );
  109. await browser.keys("Escape");
  110. for (const id of ids) {
  111. await openSearch();
  112. await result(id).scrollIntoView();
  113. await result(id).click();
  114. await expectLocated(id);
  115. }
  116. await openSearch();
  117. await input().setValue("界面字号");
  118. await browser.keys("Enter");
  119. await expectLocated("settings.uiFontSize");
  120. expect(await page().getAttribute("data-active-section")).toBe("appearance");
  121. await openSearch();
  122. await input().setValue("界面字号");
  123. await browser.keys("Enter");
  124. await expectLocated("settings.uiFontSize");
  125. expect((await readSettings()).httpProxy).toBe(settings.httpProxy);
  126. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "general"));
  127. expect(await $('[data-settings-search-highlighted="true"]').isExisting()).toBe(false);
  128. await openSearch();
  129. expect(await input().getValue()).toBe("");
  130. await input().setValue("no-match-value");
  131. await browser.keys("Escape");
  132. expect(await input().getValue()).toBe("");
  133. await browser.keys("Escape");
  134. await $('[data-testid="settings-search-dialog"]').waitForDisplayed({ reverse: true });
  135. });
  136. it("SS04:高亮自动消失,重复命中重新计时且保留焦点和位置", async () => {
  137. const id = "settings.uiFontSize";
  138. const locate = async () => {
  139. await openSearch();
  140. await input().setValue("界面字号");
  141. await result(id).click();
  142. await expectLocated(id);
  143. };
  144. await locate();
  145. // 跨过旧一轮的截止时间,但仍处于新一轮高亮期,验证不会被旧回调提前清除。
  146. await browser.pause(1100);
  147. await locate();
  148. const top = await anchor(id).getLocation("y");
  149. await browser.pause(1100);
  150. expect(await anchor(id).getAttribute("data-settings-search-highlighted")).toBe("true");
  151. await browser.waitUntil(
  152. async () => (await anchor(id).getAttribute("data-settings-search-highlighted")) !== "true",
  153. { timeout: 2000, timeoutMsg: "设置项定位高亮未自动消失" },
  154. );
  155. expect(await anchor(id).getLocation("y")).toBe(top);
  156. expect(
  157. await browser.execute(
  158. (id) => document.activeElement?.getAttribute("data-settings-search-id") === id,
  159. id,
  160. ),
  161. ).toBe(true);
  162. await locate();
  163. await browser.waitUntil(
  164. async () => (await anchor(id).getAttribute("data-settings-search-highlighted")) !== "true",
  165. { timeout: 3000, timeoutMsg: "再次命中后的高亮未自动消失" },
  166. );
  167. });
  168. it("SS03:宽窄桌面、窄屏 Web、中英文与深浅主题均可搜索定位", async () => {
  169. const platformClasses = await browser.execute(() =>
  170. [...document.documentElement.classList].filter((value) => value.startsWith("platform-")),
  171. );
  172. try {
  173. for (const width of [1200, 600, 390]) {
  174. await browser.electron.execute(async (electron, width) => {
  175. const window = electron.BrowserWindow.getAllWindows().find((candidate) =>
  176. candidate.isVisible(),
  177. );
  178. if (!window) throw new Error("Missing renderer");
  179. if (!window.webContents.debugger.isAttached()) window.webContents.debugger.attach("1.3");
  180. await window.webContents.debugger.sendCommand("Emulation.setDeviceMetricsOverride", {
  181. width,
  182. height: 844,
  183. deviceScaleFactor: 1,
  184. mobile: false,
  185. });
  186. }, width);
  187. await browser.execute(
  188. (width, classes) => {
  189. for (const value of classes)
  190. document.documentElement.classList.toggle(value, width !== 390);
  191. },
  192. width,
  193. platformClasses,
  194. );
  195. for (const theme of ["zai-light", "zai-dark"]) {
  196. for (const locale of ["zh-CN", "en-US"]) {
  197. await setPresentation(theme, locale);
  198. await openSearch();
  199. await input().setValue(locale === "zh-CN" ? "界面字号" : "UI font");
  200. await result("settings.uiFontSize").waitForDisplayed();
  201. const bounds = await browser.execute(() => {
  202. const element = document.querySelector('[data-testid="settings-search-input"]');
  203. if (!element) throw new Error("Missing search input");
  204. const { x, width } = element.getBoundingClientRect();
  205. return { x, width };
  206. });
  207. expect(bounds.width).toBeGreaterThan(100);
  208. expect(bounds.x).toBeGreaterThanOrEqual(0);
  209. expect(bounds.x + bounds.width).toBeLessThanOrEqual(width);
  210. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR;
  211. if (artifactDir)
  212. await browser.saveScreenshot(
  213. join(artifactDir, `settings-search-${width}-${theme}-${locale}.png`),
  214. );
  215. await result("settings.uiFontSize").click();
  216. await expectLocated("settings.uiFontSize");
  217. }
  218. }
  219. }
  220. } finally {
  221. await browser.execute(
  222. (classes) => document.documentElement.classList.add(...classes),
  223. platformClasses,
  224. );
  225. await browser.electron.execute(async (electron) => {
  226. const window = electron.BrowserWindow.getAllWindows().find((candidate) =>
  227. candidate.isVisible(),
  228. );
  229. if (window?.webContents.debugger.isAttached())
  230. await window.webContents.debugger.sendCommand("Emulation.clearDeviceMetricsOverride");
  231. });
  232. }
  233. });
  234. });