workspace-file-tree-compact-hierarchy.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { mkdir, rm, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. TID_WORKSPACE_FILE_TREE_PANEL,
  5. TID_WORKSPACE_FILE_TREE_ROW,
  6. testId,
  7. } from "@zcode/shared/test-ids";
  8. import {
  9. DEFAULT_WORKSPACE,
  10. clearAppData,
  11. waitForDefaultWorkspaceReady,
  12. waitForTestIdByDom,
  13. } from "#e2e/helpers/desktop-app";
  14. import { openProjectFileTree } from "#e2e/helpers/project-sidebar";
  15. const CASE_ROOT = join(DEFAULT_WORKSPACE, "E2E_FILE_TREE_COMPACT_HIERARCHY");
  16. const COMPACT_DIRECTORY = join(CASE_ROOT, "api", "architecture");
  17. const CHAT_PARENT_DIRECTORY = join(COMPACT_DIRECTORY, "chat");
  18. const CHAT_DIRECTORY = join(CHAT_PARENT_DIRECTORY, "transport");
  19. const FIRST_CHAT_FILE = join(CHAT_DIRECTORY, "item-00.txt");
  20. const SECOND_CHAT_FILE = join(CHAT_DIRECTORY, "item-01.txt");
  21. describe("工作区文件树 compact hierarchy E2E", () => {
  22. after(async () => {
  23. await browser.electron.restoreAllMocks();
  24. await clearAppData();
  25. await rm(CASE_ROOT, { recursive: true, force: true });
  26. });
  27. it("嵌套 compact folders 首次单击可折叠,重开后保持缩进、引导线和无间隙吸顶", async function () {
  28. this.timeout(150000);
  29. await createCaseFiles();
  30. await waitForDefaultWorkspaceReady();
  31. await openWorkspaceFileTree();
  32. await waitForWorkspaceFileTreeRow(COMPACT_DIRECTORY);
  33. await clickWorkspaceFileTreeRow(COMPACT_DIRECTORY);
  34. await waitForWorkspaceFileTreeRow(CHAT_PARENT_DIRECTORY);
  35. await clickWorkspaceFileTreeRow(CHAT_PARENT_DIRECTORY);
  36. await waitForWorkspaceFileTreeRow(CHAT_DIRECTORY);
  37. await waitForWorkspaceFileTreeRow(FIRST_CHAT_FILE);
  38. await assertNestedHierarchy();
  39. // 首次懒加载合并时只有 chat 已展开;重开后则由 transport 保存展开标记。
  40. for (let cycle = 0; cycle < 2; cycle += 1) {
  41. await collapseDirectoryWithOneClick(CHAT_DIRECTORY);
  42. await waitForWorkspaceFileTreeRow(COMPACT_DIRECTORY);
  43. await clickWorkspaceFileTreeRow(CHAT_DIRECTORY);
  44. await waitForWorkspaceFileTreeRow(FIRST_CHAT_FILE);
  45. await assertNestedHierarchy();
  46. }
  47. await collapseDirectoryWithOneClick(COMPACT_DIRECTORY);
  48. await clickWorkspaceFileTreeRow(COMPACT_DIRECTORY);
  49. await waitForWorkspaceFileTreeRow(FIRST_CHAT_FILE);
  50. await assertNestedHierarchy();
  51. for (const theme of ["zai-light", "zai-dark"]) {
  52. await browser.execute((value) => {
  53. if (!("__testActions" in window)) throw new Error("缺少主题测试入口");
  54. const actions = window.__testActions;
  55. if (
  56. !actions ||
  57. typeof actions !== "object" ||
  58. !("setTheme" in actions) ||
  59. typeof actions.setTheme !== "function"
  60. ) {
  61. throw new Error("缺少 setTheme 测试入口");
  62. }
  63. actions.setTheme(value);
  64. }, theme);
  65. await browser.waitUntil(
  66. () =>
  67. browser.execute(
  68. (value) => document.documentElement.classList.contains(`theme-${value}`),
  69. theme,
  70. ),
  71. { timeout: 10000, timeoutMsg: `主题未切换到 ${theme}` },
  72. );
  73. await assertNestedHierarchy();
  74. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR;
  75. if (!artifactDir) throw new Error("缺少 E2E artifact 目录");
  76. await writeFile(
  77. join(artifactDir, `nested-hierarchy-${theme}.json`),
  78. JSON.stringify(await readHierarchyMetrics(CHAT_DIRECTORY, FIRST_CHAT_FILE), null, 2),
  79. );
  80. await browser.saveScreenshot(join(artifactDir, `nested-hierarchy-${theme}.png`));
  81. }
  82. await scrollWorkspaceFileTree(280);
  83. const sticky = await waitForStickyHierarchy(COMPACT_DIRECTORY, CHAT_DIRECTORY);
  84. expect(sticky.parentIsStatic).toBe(true);
  85. expect(sticky.childIsStatic).toBe(true);
  86. expect(Math.abs(sticky.gap)).toBeLessThanOrEqual(0.5);
  87. });
  88. });
  89. async function collapseDirectoryWithOneClick(path: string) {
  90. await clickWorkspaceFileTreeRow(path);
  91. await browser.waitUntil(
  92. async () => {
  93. const snapshot = await browser.execute(
  94. (directoryId, childIds) => {
  95. const rows = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]"));
  96. const directory = rows.find((element) => element.dataset.testid === directoryId);
  97. return {
  98. expanded: directory?.getAttribute("aria-expanded") ?? null,
  99. visibleChildren: childIds.filter((childId) =>
  100. rows.some((element) => element.dataset.testid === childId),
  101. ).length,
  102. };
  103. },
  104. testId(TID_WORKSPACE_FILE_TREE_ROW, path),
  105. [FIRST_CHAT_FILE, SECOND_CHAT_FILE].map((file) =>
  106. testId(TID_WORKSPACE_FILE_TREE_ROW, file),
  107. ),
  108. );
  109. return snapshot.expanded === "false" && snapshot.visibleChildren === 0;
  110. },
  111. { timeout: 10000, timeoutMsg: `目录单击一次后未折叠: ${path}` },
  112. );
  113. }
  114. async function assertNestedHierarchy() {
  115. for (const childPath of [FIRST_CHAT_FILE, SECOND_CHAT_FILE]) {
  116. const hierarchy = await readHierarchyMetrics(CHAT_DIRECTORY, childPath);
  117. expect(hierarchy.parentDepth).toBe(1);
  118. expect(hierarchy.childDepth).toBe(2);
  119. expect(hierarchy.childPaddingLeft - hierarchy.parentPaddingLeft).toBe(12);
  120. expect(hierarchy.parentGuideWidth).toBe(12);
  121. expect(hierarchy.childGuideWidth).toBe(24);
  122. expect(hierarchy.childGuideHeight).toBeGreaterThanOrEqual(hierarchy.childRowHeight);
  123. expect(hierarchy.childGuideBackground).toContain("repeating-linear-gradient");
  124. expect(hierarchy.childGuideVisibility).toBe("visible");
  125. expect(hierarchy.childGuideOpacity).toBe("1");
  126. }
  127. }
  128. async function createCaseFiles() {
  129. await rm(CASE_ROOT, { recursive: true, force: true });
  130. await mkdir(CHAT_DIRECTORY, { recursive: true });
  131. await writeFile(
  132. join(COMPACT_DIRECTORY, "index.ts"),
  133. "export const marker = 'E2E_FILE_TREE_COMPACT_HIERARCHY';\n",
  134. "utf-8",
  135. );
  136. await Promise.all(
  137. Array.from({ length: 40 }, (_, index) =>
  138. writeFile(
  139. join(CHAT_DIRECTORY, `item-${String(index).padStart(2, "0")}.txt`),
  140. `E2E_FILE_TREE_COMPACT_HIERARCHY_${index}\n`,
  141. "utf-8",
  142. ),
  143. ),
  144. );
  145. }
  146. async function openWorkspaceFileTree() {
  147. // 修复原因:workspace action 只在整行 hover/focus 后挂载。
  148. await openProjectFileTree(DEFAULT_WORKSPACE, {
  149. timeoutMsg: "没有找到工作区文件树入口",
  150. });
  151. await waitForTestIdByDom(TID_WORKSPACE_FILE_TREE_PANEL, {
  152. timeoutMsg: "文件树面板没有打开",
  153. });
  154. }
  155. async function clickWorkspaceFileTreeRow(path: string) {
  156. const rowTestId = testId(TID_WORKSPACE_FILE_TREE_ROW, path);
  157. await browser.waitUntil(
  158. async () =>
  159. browser.execute((currentRowTestId) => {
  160. const row = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
  161. (element) => element.dataset.testid === currentRowTestId,
  162. );
  163. row?.click();
  164. return Boolean(row);
  165. }, rowTestId),
  166. {
  167. timeout: 10000,
  168. timeoutMsg: `文件树没有可点击行: ${path}`,
  169. },
  170. );
  171. }
  172. async function waitForWorkspaceFileTreeRow(path: string) {
  173. const rowTestId = testId(TID_WORKSPACE_FILE_TREE_ROW, path);
  174. await browser.waitUntil(
  175. async () =>
  176. browser.execute(
  177. (currentRowTestId) =>
  178. Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).some(
  179. (element) => element.dataset.testid === currentRowTestId,
  180. ),
  181. rowTestId,
  182. ),
  183. {
  184. timeout: 10000,
  185. timeoutMsg: `文件树没有显示目标路径: ${path}`,
  186. },
  187. );
  188. }
  189. async function readHierarchyMetrics(parentPath: string, childPath: string) {
  190. return browser.execute(
  191. (parentTestId, childTestId) => {
  192. const rows = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]"));
  193. const parent = rows.find((element) => element.dataset.testid === parentTestId);
  194. const child = rows.find((element) => element.dataset.testid === childTestId);
  195. const parentGuide = parent?.querySelector<HTMLElement>(
  196. "[data-workspace-file-tree-hierarchy-guides]",
  197. );
  198. const childGuide = child?.querySelector<HTMLElement>(
  199. "[data-workspace-file-tree-hierarchy-guides]",
  200. );
  201. if (!parent || !child || !parentGuide || !childGuide) {
  202. throw new Error("文件树层级行或引导线不存在");
  203. }
  204. return {
  205. parentDepth: Number(parentGuide.dataset.workspaceFileTreeHierarchyGuides),
  206. childDepth: Number(childGuide.dataset.workspaceFileTreeHierarchyGuides),
  207. parentPaddingLeft: Number.parseFloat(getComputedStyle(parent).paddingLeft),
  208. childPaddingLeft: Number.parseFloat(getComputedStyle(child).paddingLeft),
  209. parentGuideWidth: parentGuide.getBoundingClientRect().width,
  210. childGuideWidth: childGuide.getBoundingClientRect().width,
  211. childGuideHeight: childGuide.getBoundingClientRect().height,
  212. childRowHeight: child.getBoundingClientRect().height,
  213. childGuideBackground: getComputedStyle(childGuide).backgroundImage,
  214. childGuideVisibility: getComputedStyle(childGuide).visibility,
  215. childGuideOpacity: getComputedStyle(childGuide).opacity,
  216. };
  217. },
  218. testId(TID_WORKSPACE_FILE_TREE_ROW, parentPath),
  219. testId(TID_WORKSPACE_FILE_TREE_ROW, childPath),
  220. );
  221. }
  222. async function scrollWorkspaceFileTree(scrollTop: number) {
  223. await browser.execute(
  224. (panelTestId, nextScrollTop) => {
  225. const panel = document.querySelector<HTMLElement>(`[data-testid="${panelTestId}"]`);
  226. const scrollContainer = Array.from(panel?.querySelectorAll<HTMLElement>("div") ?? []).find(
  227. (element) => {
  228. const overflowY = getComputedStyle(element).overflowY;
  229. return (
  230. (overflowY === "auto" || overflowY === "scroll") &&
  231. element.scrollHeight > element.clientHeight + 1
  232. );
  233. },
  234. );
  235. if (!scrollContainer) {
  236. throw new Error("文件树滚动容器不存在或没有溢出");
  237. }
  238. scrollContainer.scrollTop = nextScrollTop;
  239. scrollContainer.dispatchEvent(new Event("scroll"));
  240. },
  241. TID_WORKSPACE_FILE_TREE_PANEL,
  242. scrollTop,
  243. );
  244. }
  245. async function waitForStickyHierarchy(parentPath: string, childPath: string) {
  246. const parentTestId = testId(TID_WORKSPACE_FILE_TREE_ROW, parentPath);
  247. const childTestId = testId(TID_WORKSPACE_FILE_TREE_ROW, childPath);
  248. await browser.waitUntil(
  249. async () => (await readStickyHierarchy(parentTestId, childTestId)) !== null,
  250. {
  251. timeout: 10000,
  252. timeoutMsg: "滚动后 compact folder 的后代目录没有进入吸顶区域",
  253. },
  254. );
  255. const result = await readStickyHierarchy(parentTestId, childTestId);
  256. if (!result) {
  257. throw new Error("没有读取到文件树吸顶目录");
  258. }
  259. return result;
  260. }
  261. function readStickyHierarchy(parentTestId: string, childTestId: string) {
  262. return browser.execute(
  263. (currentParentTestId, currentChildTestId) => {
  264. const rows = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]"));
  265. const findStaticRow = (rowTestId: string) =>
  266. rows.find(
  267. (element) =>
  268. element.dataset.testid === rowTestId &&
  269. !element.parentElement?.classList.contains("absolute"),
  270. );
  271. const parent = findStaticRow(currentParentTestId);
  272. const child = findStaticRow(currentChildTestId);
  273. if (!parent || !child) {
  274. return null;
  275. }
  276. const parentRect = parent.getBoundingClientRect();
  277. const childRect = child.getBoundingClientRect();
  278. return {
  279. parentIsStatic: true,
  280. childIsStatic: true,
  281. gap: childRect.top - parentRect.bottom,
  282. };
  283. },
  284. parentTestId,
  285. childTestId,
  286. );
  287. }