workspace-file-tree-shared-search.test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import { mkdir, rm, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. TID_PREVIEW_PANE,
  5. TID_WORKSPACE_FILE_TREE_PANEL,
  6. TID_WORKSPACE_FILE_TREE_ROW,
  7. testId,
  8. } from "@zcode/shared/test-ids";
  9. import {
  10. DEFAULT_WORKSPACE,
  11. clearAppData,
  12. waitForDefaultWorkspaceReady,
  13. waitForTestIdByDom,
  14. } from "../helpers/desktop-app.js";
  15. import { openProjectFileTree } from "../helpers/project-sidebar.js";
  16. import { reloadElectronSessionPreservingBrowserProfile } from "../helpers/e2e-electron-reload.js";
  17. const CASE_ROOT = join(DEFAULT_WORKSPACE, "E2E_FILE_TREE_SHARED_SEARCH");
  18. const JAVA_DIRECTORY = join(CASE_ROOT, "src", "main", "java", "com", "example", "user");
  19. const JAVA_FILE = join(JAVA_DIRECTORY, "UserProfileService.java");
  20. describe("工作区文件树共享搜索 E2E", () => {
  21. after(async () => {
  22. await browser.electron.restoreAllMocks();
  23. await clearAppData();
  24. await rm(CASE_ROOT, { recursive: true, force: true });
  25. });
  26. it("未展开深层 Java 文件可以通过左侧文件树搜索命中并打开预览", async function () {
  27. this.timeout(150000);
  28. await rm(CASE_ROOT, { recursive: true, force: true });
  29. await mkdir(JAVA_DIRECTORY, { recursive: true });
  30. await writeFile(
  31. JAVA_FILE,
  32. "package com.example.user;\npublic final class UserProfileService {}\n",
  33. "utf-8",
  34. );
  35. await waitForDefaultWorkspaceReady();
  36. await openWorkspaceFileTree();
  37. await typeWorkspaceFileTreeSearch("UserProfileService");
  38. await waitForWorkspaceFileTreeRow(JAVA_FILE);
  39. await clickWorkspaceFileTreeRow(JAVA_FILE);
  40. await waitForTestIdByDom(TID_PREVIEW_PANE, {
  41. timeoutMsg: "文件树搜索结果点击后没有打开 PreviewPane",
  42. });
  43. });
  44. it("搜索结果中的深层目录点击后会回到树态并逐级展开定位", async function () {
  45. this.timeout(150000);
  46. await rm(CASE_ROOT, { recursive: true, force: true });
  47. await mkdir(JAVA_DIRECTORY, { recursive: true });
  48. await writeFile(
  49. JAVA_FILE,
  50. "package com.example.user;\npublic final class UserProfileService {}\n",
  51. "utf-8",
  52. );
  53. await waitForDefaultWorkspaceReady();
  54. await openWorkspaceFileTree();
  55. await typeWorkspaceFileTreeSearch("com/example/user");
  56. await waitForWorkspaceFileTreeRow(JAVA_DIRECTORY);
  57. await clickWorkspaceFileTreeRow(JAVA_DIRECTORY);
  58. await waitForWorkspaceFileTreeSearchValue("");
  59. await waitForWorkspaceFileTreeRow(JAVA_DIRECTORY);
  60. await waitForWorkspaceFileTreeRow(JAVA_FILE);
  61. });
  62. it("显式收起目录后,其他文件与 App 重启沿用同一 workspace 偏好", async function () {
  63. this.timeout(240000);
  64. const secondFile = join(CASE_ROOT, "src", "main", "java", "NavigationPreference.java");
  65. await mkdir(JAVA_DIRECTORY, { recursive: true });
  66. await writeFile(JAVA_FILE, "public final class UserProfileService {}\n", "utf-8");
  67. await writeFile(secondFile, "public final class NavigationPreference {}\n", "utf-8");
  68. await waitForDefaultWorkspaceReady();
  69. await openFileFromCommandCenter("UserProfileService.java");
  70. await toggleFileNavigation(false);
  71. await openFileFromCommandCenter("NavigationPreference.java");
  72. await waitForFileNavigationExpanded(false);
  73. await reloadElectronSessionPreservingBrowserProfile(browser);
  74. await waitForDefaultWorkspaceReady(30000);
  75. await openFileFromCommandCenter("UserProfileService.java");
  76. await waitForFileNavigationExpanded(false);
  77. });
  78. });
  79. async function openFileFromCommandCenter(fileName: string) {
  80. await $('[data-testid="sidebar-search-open"]').click();
  81. const input = $('[role="dialog"] [cmdk-input]');
  82. await input.waitForDisplayed({ timeout: 10000 });
  83. await input.setValue(fileName);
  84. await browser.waitUntil(
  85. async () =>
  86. browser.execute((targetFileName) => {
  87. const item = Array.from(
  88. document.querySelectorAll<HTMLElement>('[role="dialog"] [cmdk-item]'),
  89. ).find((candidate) => candidate.textContent?.includes(targetFileName));
  90. item?.click();
  91. return Boolean(item);
  92. }, fileName),
  93. {
  94. timeout: 15000,
  95. timeoutMsg: `命令中心没有找到文件: ${fileName}`,
  96. },
  97. );
  98. await waitForTestIdByDom(TID_PREVIEW_PANE, {
  99. timeoutMsg: `命令中心打开文件后没有显示 PreviewPane: ${fileName}`,
  100. });
  101. }
  102. async function readFileNavigationExpanded(): Promise<boolean | null> {
  103. return browser.execute(() => {
  104. const button = Array.from(
  105. document.querySelectorAll<HTMLButtonElement>(
  106. "[data-file-manager-pane] button[aria-expanded]",
  107. ),
  108. ).find((candidate) => candidate.getClientRects().length > 0);
  109. if (!button) return null;
  110. return button.getAttribute("aria-expanded") === "true";
  111. });
  112. }
  113. async function waitForFileNavigationExpanded(expected: boolean) {
  114. await browser.waitUntil(async () => (await readFileNavigationExpanded()) === expected, {
  115. timeout: 10000,
  116. timeoutMsg: `文件导航显隐状态没有变为: ${String(expected)}`,
  117. });
  118. }
  119. async function toggleFileNavigation(expanded: boolean) {
  120. const changed = await browser.execute((nextExpanded) => {
  121. const button = Array.from(
  122. document.querySelectorAll<HTMLButtonElement>(
  123. "[data-file-manager-pane] button[aria-expanded]",
  124. ),
  125. ).find((candidate) => candidate.getClientRects().length > 0);
  126. if (!button) return false;
  127. if ((button.getAttribute("aria-expanded") === "true") !== nextExpanded) {
  128. button.click();
  129. }
  130. return true;
  131. }, expanded);
  132. expect(changed).toBe(true);
  133. await waitForFileNavigationExpanded(expanded);
  134. }
  135. async function openWorkspaceFileTree() {
  136. const alreadyOpen = await browser.execute(
  137. (panelTestId) => Boolean(document.querySelector(`[data-testid="${panelTestId}"]`)),
  138. TID_WORKSPACE_FILE_TREE_PANEL,
  139. );
  140. // 修复原因:同一 spec 的第二个 case 会复用已打开的文件树;此时 workspace 行可能
  141. // 被面板挤出视口,继续 moveTo 会报 out-of-bounds,且没有必要重复点击入口。
  142. if (alreadyOpen) return;
  143. // 修复原因:workspace action 只在整行 hover/focus 后挂载。
  144. await openProjectFileTree(DEFAULT_WORKSPACE, {
  145. timeoutMsg: "没有找到工作区文件树入口",
  146. });
  147. await waitForTestIdByDom(TID_WORKSPACE_FILE_TREE_PANEL, {
  148. timeoutMsg: "文件树面板没有打开",
  149. });
  150. }
  151. async function typeWorkspaceFileTreeSearch(query: string) {
  152. await browser.waitUntil(
  153. async () =>
  154. browser.execute(
  155. (panelTestId, nextQuery) => {
  156. const panel = document.querySelector<HTMLElement>(`[data-testid="${panelTestId}"]`);
  157. const input = panel?.querySelector<HTMLInputElement>("input");
  158. if (!input) {
  159. return false;
  160. }
  161. const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set;
  162. setter?.call(input, nextQuery);
  163. input.dispatchEvent(new Event("input", { bubbles: true }));
  164. return true;
  165. },
  166. TID_WORKSPACE_FILE_TREE_PANEL,
  167. query,
  168. ),
  169. {
  170. timeout: 10000,
  171. timeoutMsg: "文件树搜索框不可输入",
  172. },
  173. );
  174. }
  175. async function waitForWorkspaceFileTreeSearchValue(expectedValue: string) {
  176. await browser.waitUntil(
  177. async () =>
  178. browser.execute(
  179. (panelTestId, currentExpectedValue) => {
  180. const panel = document.querySelector<HTMLElement>(`[data-testid="${panelTestId}"]`);
  181. const input = panel?.querySelector<HTMLInputElement>("input");
  182. return input?.value === currentExpectedValue;
  183. },
  184. TID_WORKSPACE_FILE_TREE_PANEL,
  185. expectedValue,
  186. ),
  187. {
  188. timeout: 10000,
  189. timeoutMsg: `文件树搜索框没有变为: ${expectedValue}`,
  190. },
  191. );
  192. }
  193. async function clickWorkspaceFileTreeRow(path: string) {
  194. const rowTestId = testId(TID_WORKSPACE_FILE_TREE_ROW, path);
  195. await browser.waitUntil(
  196. async () =>
  197. browser.execute((currentRowTestId) => {
  198. const row = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
  199. (element) => element.dataset.testid === currentRowTestId,
  200. );
  201. row?.click();
  202. return Boolean(row);
  203. }, rowTestId),
  204. {
  205. timeout: 10000,
  206. timeoutMsg: `文件树没有可点击行: ${path}`,
  207. },
  208. );
  209. }
  210. async function waitForWorkspaceFileTreeRow(path: string) {
  211. const rowTestId = testId(TID_WORKSPACE_FILE_TREE_ROW, path);
  212. await browser.waitUntil(
  213. async () =>
  214. browser.execute(
  215. (currentRowTestId) =>
  216. Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).some(
  217. (element) => element.dataset.testid === currentRowTestId,
  218. ),
  219. rowTestId,
  220. ),
  221. {
  222. timeout: 15000,
  223. timeoutMsg: `文件树搜索没有显示目标路径: ${path}`,
  224. },
  225. );
  226. }