workspaceFileTree.test.ts 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. import { describe, expect, it } from "vitest";
  2. import type { VirtualItem } from "@tanstack/react-virtual";
  3. import type { IGitService } from "@zcode/services";
  4. import { WORKSPACE_FILE_TREE_VIRTUAL_ROW_HEIGHT_PX } from "@/workspace-file-tree/constants.js";
  5. import { loadWorkspaceFileTreeGitStatus } from "@/workspace-file-tree/gitStatus.js";
  6. import { sortInstalledEditorsForFileTree } from "@/workspace-file-tree/helpers.js";
  7. import {
  8. addDeletedGitStatusRowsToWorkspaceFileTree,
  9. areWorkspaceFilePathsEqual,
  10. buildWorkspaceFileGitStatusByPath,
  11. buildWorkspaceFileIgnoredPathSet,
  12. createCodeViewerSourceForWorkspaceFile,
  13. filterWorkspaceFileTreeRows,
  14. flattenWorkspaceFileTreeRows,
  15. getWorkspaceDirectoryGitStatuses,
  16. getWorkspaceFileGitStatus,
  17. getWorkspaceFileAncestorDirectories,
  18. getWorkspaceFileDirectoryChildDepth,
  19. getWorkspaceFileParentDirectory,
  20. getWorkspaceFileRelativePath,
  21. isWorkspaceFileTreeGitStatusAvailable,
  22. isWorkspaceFileTreeDeletedFile,
  23. isWorkspaceFileGitIgnored,
  24. type WorkspaceFileTreeNode,
  25. type WorkspaceFileTreeRow,
  26. isWorkspaceFilePathInside,
  27. } from "@/workspace-file-tree/model.js";
  28. import {
  29. createWorkspaceFileTreeHtmlBrowserUrl,
  30. isWorkspaceFileTreeHtmlFile,
  31. } from "@/workspace-file-tree/helpers.js";
  32. import {
  33. getWorkspaceFileTreeBlockingRootError,
  34. getWorkspaceFileTreeDirectoryLoadDepth,
  35. } from "@/workspace-file-tree/WorkspaceFileTree.js";
  36. import {
  37. getWorkspaceFileGitStatusDotClassName,
  38. getWorkspaceFileGitStatusIndicatorClassName,
  39. getWorkspaceFileGitStatusTextClassName,
  40. getWorkspaceFileTreeRowDisplayGitStatus,
  41. } from "@/workspace-file-tree/statusStyles.js";
  42. import { getWorkspaceFileTreeStickyFolders } from "@/workspace-file-tree/useWorkspaceFileTreeStickyFolders.js";
  43. import { getWorkspaceFileTreeListMaskStyle } from "@/workspace-file-tree/WorkspaceFileTreeList.js";
  44. import {
  45. createWorkspaceFileTreeRowsFromSearchEntries,
  46. getWorkspaceFileSearchDirectoryRevealPaths,
  47. } from "@/workspace-file-tree/searchRows.js";
  48. import { getWorkspaceFileTreeRefreshDirectoryPaths } from "@/workspace-file-tree/refreshDirectories.js";
  49. import { getWorkspaceFileTreeHierarchyGuideStyle } from "@/workspace-file-tree/hierarchyGuides.js";
  50. function node(
  51. path: string,
  52. type: WorkspaceFileTreeNode["type"],
  53. depth: number,
  54. ): WorkspaceFileTreeNode {
  55. return {
  56. path,
  57. name: path.split("/").pop() ?? path,
  58. type,
  59. depth,
  60. };
  61. }
  62. function row(
  63. path: string,
  64. type: WorkspaceFileTreeNode["type"],
  65. depth: number,
  66. expanded = false,
  67. ): WorkspaceFileTreeRow {
  68. return {
  69. ...node(path, type, depth),
  70. expanded,
  71. loaded: true,
  72. loading: false,
  73. error: null,
  74. };
  75. }
  76. function virtualItem(index: number): VirtualItem {
  77. const start = index * WORKSPACE_FILE_TREE_VIRTUAL_ROW_HEIGHT_PX;
  78. return {
  79. key: index,
  80. index,
  81. start,
  82. end: start + WORKSPACE_FILE_TREE_VIRTUAL_ROW_HEIGHT_PX,
  83. size: WORKSPACE_FILE_TREE_VIRTUAL_ROW_HEIGHT_PX,
  84. lane: 0,
  85. };
  86. }
  87. describe("workspace file tree helpers", () => {
  88. it("用物理路径计算 compact folder 后代的目录加载深度", () => {
  89. expect(
  90. getWorkspaceFileTreeDirectoryLoadDepth(
  91. "/workspace",
  92. "/workspace/docs/api/architecture/chat",
  93. ),
  94. ).toBe(4);
  95. expect(
  96. getWorkspaceFileTreeDirectoryLoadDepth(
  97. "C:\\workspace",
  98. "C:\\workspace\\docs\\api\\architecture\\chat",
  99. ),
  100. ).toBe(4);
  101. });
  102. it("只为嵌套文件树行生成按层级重复的竖向引导线", () => {
  103. expect(getWorkspaceFileTreeHierarchyGuideStyle(0)).toBeNull();
  104. expect(getWorkspaceFileTreeHierarchyGuideStyle(-1)).toBeNull();
  105. expect(getWorkspaceFileTreeHierarchyGuideStyle(1)).toEqual({
  106. width: "calc(1 * 0.75rem)",
  107. backgroundImage:
  108. "repeating-linear-gradient(to right, transparent 0 calc(0.375rem - 1px), var(--color-border) calc(0.375rem - 1px) 0.375rem, transparent 0.375rem 0.75rem)",
  109. });
  110. expect(getWorkspaceFileTreeHierarchyGuideStyle(3)).toEqual({
  111. width: "calc(3 * 0.75rem)",
  112. backgroundImage:
  113. "repeating-linear-gradient(to right, transparent 0 calc(0.375rem - 1px), var(--color-border) calc(0.375rem - 1px) 0.375rem, transparent 0.375rem 0.75rem)",
  114. });
  115. });
  116. it("keeps QSpace next to Finder in local open-with menus", () => {
  117. const iconDataUrl = "data:image/png;base64,AA==";
  118. expect(
  119. sortInstalledEditorsForFileTree([
  120. { id: "vscode", name: "VS Code", iconDataUrl },
  121. { id: "finder", name: "Finder", iconDataUrl },
  122. { id: "qspace", name: "QSpace", iconDataUrl },
  123. { id: "qspace-pro", name: "QSpace Pro", iconDataUrl },
  124. { id: "cursor", name: "Cursor", iconDataUrl },
  125. ]).map((editor) => editor.id),
  126. ).toEqual(["finder", "qspace", "qspace-pro", "vscode", "cursor"]);
  127. });
  128. it("only treats Git status as available inside a Git repository", () => {
  129. expect(
  130. isWorkspaceFileTreeGitStatusAvailable({
  131. isGitAvailable: true,
  132. isRepository: true,
  133. }),
  134. ).toBe(true);
  135. expect(
  136. isWorkspaceFileTreeGitStatusAvailable({
  137. isGitAvailable: true,
  138. isRepository: false,
  139. }),
  140. ).toBe(false);
  141. expect(
  142. isWorkspaceFileTreeGitStatusAvailable({
  143. isGitAvailable: false,
  144. isRepository: false,
  145. }),
  146. ).toBe(false);
  147. });
  148. it("loads an unavailable Git status state outside Git repositories", async () => {
  149. const gitService = {
  150. refresh: async () => ({
  151. summary: {
  152. workspacePath: "/workspace",
  153. repoRoot: "/workspace",
  154. workspaceInRepoPath: ".",
  155. autoRefreshWatchPaths: [],
  156. branchName: null,
  157. trackingBranchName: null,
  158. headRefType: "branch" as const,
  159. ahead: 0,
  160. behind: 0,
  161. isDirty: false,
  162. isGitAvailable: true,
  163. isRepository: false,
  164. },
  165. identity: null,
  166. unstagedChanges: [],
  167. stagedChanges: [],
  168. branchComparison: null,
  169. }),
  170. } as Pick<IGitService, "refresh"> as IGitService;
  171. await expect(
  172. loadWorkspaceFileTreeGitStatus({
  173. gitService,
  174. workspacePath: "/workspace",
  175. }),
  176. ).resolves.toEqual({
  177. available: false,
  178. statusByPath: new Map(),
  179. });
  180. });
  181. it("detects HTML files that can open in the built-in browser", () => {
  182. expect(
  183. isWorkspaceFileTreeHtmlFile(row("/workspace/index.html", "file", 0)),
  184. ).toBe(true);
  185. expect(
  186. isWorkspaceFileTreeHtmlFile(row("/workspace/preview.HTM", "file", 0)),
  187. ).toBe(true);
  188. expect(
  189. isWorkspaceFileTreeHtmlFile(row("/workspace/src", "directory", 0)),
  190. ).toBe(false);
  191. expect(
  192. isWorkspaceFileTreeHtmlFile(row("/workspace/readme.md", "file", 0)),
  193. ).toBe(false);
  194. });
  195. it("creates file URLs for HTML browser opening", () => {
  196. expect(
  197. createWorkspaceFileTreeHtmlBrowserUrl(
  198. row("/workspace/demo page.html", "file", 0),
  199. ),
  200. ).toBe("file:///workspace/demo%20page.html");
  201. expect(
  202. createWorkspaceFileTreeHtmlBrowserUrl(
  203. row("C:\\workspace\\index.html", "file", 0),
  204. ),
  205. ).toBe("file:///C:/workspace/index.html");
  206. expect(
  207. createWorkspaceFileTreeHtmlBrowserUrl(
  208. row("/workspace/index.ts", "file", 0),
  209. ),
  210. ).toBe(null);
  211. });
  212. it("flattens only visible expanded rows", () => {
  213. const childrenByDirectory = new Map<string, WorkspaceFileTreeNode[]>([
  214. [
  215. "/workspace",
  216. [
  217. node("/workspace/src", "directory", 0),
  218. node("/workspace/README.md", "file", 0),
  219. ],
  220. ],
  221. ["/workspace/src", [node("/workspace/src/index.ts", "file", 1)]],
  222. ]);
  223. expect(
  224. flattenWorkspaceFileTreeRows({
  225. rootPath: "/workspace",
  226. childrenByDirectory,
  227. expandedPaths: new Set(),
  228. loadedDirectoryPaths: new Set(["/workspace"]),
  229. loadingDirectoryPaths: new Set(),
  230. errorByDirectory: new Map(),
  231. }).map((row) => row.path),
  232. ).toEqual(["/workspace/src", "/workspace/README.md"]);
  233. expect(
  234. flattenWorkspaceFileTreeRows({
  235. rootPath: "/workspace",
  236. childrenByDirectory,
  237. expandedPaths: new Set(["/workspace/src"]),
  238. loadedDirectoryPaths: new Set(["/workspace", "/workspace/src"]),
  239. loadingDirectoryPaths: new Set(),
  240. errorByDirectory: new Map(),
  241. }).map((row) => row.path),
  242. ).toEqual([
  243. "/workspace/src",
  244. "/workspace/src/index.ts",
  245. "/workspace/README.md",
  246. ]);
  247. });
  248. it("刷新文件树会覆盖已加载和已展开的子目录", () => {
  249. const refreshPaths = getWorkspaceFileTreeRefreshDirectoryPaths({
  250. workspacePath: "/workspace",
  251. expandedPaths: new Set(["/workspace/src", "/workspace/src/app"]),
  252. loadedDirectoryPaths: new Set([
  253. "/workspace",
  254. "/workspace/src",
  255. "/workspace/docs",
  256. "/outside",
  257. ]),
  258. });
  259. expect(refreshPaths).toEqual([
  260. "/workspace",
  261. "/workspace/src",
  262. "/workspace/docs",
  263. "/workspace/src/app",
  264. ]);
  265. });
  266. it("根目录已有缓存时,刷新错误不会阻塞旧文件树渲染", () => {
  267. const error = new Error("remote host disconnected");
  268. expect(
  269. getWorkspaceFileTreeBlockingRootError({
  270. rootLoaded: true,
  271. rootError: error,
  272. }),
  273. ).toBeNull();
  274. expect(
  275. getWorkspaceFileTreeBlockingRootError({
  276. rootLoaded: false,
  277. rootError: error,
  278. }),
  279. ).toBe(error);
  280. });
  281. it("flattens loaded empty directory chains", () => {
  282. const childrenByDirectory = new Map<string, WorkspaceFileTreeNode[]>([
  283. ["/workspace", [node("/workspace/src", "directory", 0)]],
  284. ["/workspace/src", [node("/workspace/src/features", "directory", 1)]],
  285. [
  286. "/workspace/src/features",
  287. [node("/workspace/src/features/auth", "directory", 2)],
  288. ],
  289. [
  290. "/workspace/src/features/auth",
  291. [node("/workspace/src/features/auth/index.ts", "file", 3)],
  292. ],
  293. ]);
  294. const rows = flattenWorkspaceFileTreeRows({
  295. rootPath: "/workspace",
  296. childrenByDirectory,
  297. expandedPaths: new Set(["/workspace/src"]),
  298. loadedDirectoryPaths: new Set([
  299. "/workspace",
  300. "/workspace/src",
  301. "/workspace/src/features",
  302. "/workspace/src/features/auth",
  303. ]),
  304. loadingDirectoryPaths: new Set(),
  305. errorByDirectory: new Map(),
  306. flattenEmptyDirectories: true,
  307. });
  308. expect(
  309. rows.map((row) => ({ path: row.path, name: row.name, depth: row.depth })),
  310. ).toEqual([
  311. {
  312. path: "/workspace/src/features/auth",
  313. name: "src/features/auth",
  314. depth: 0,
  315. },
  316. {
  317. path: "/workspace/src/features/auth/index.ts",
  318. name: "index.ts",
  319. depth: 1,
  320. },
  321. ]);
  322. expect(rows[0]?.compactedPaths).toEqual([
  323. "/workspace/src",
  324. "/workspace/src/features",
  325. "/workspace/src/features/auth",
  326. ]);
  327. });
  328. it("does not flatten directories that contain files", () => {
  329. const childrenByDirectory = new Map<string, WorkspaceFileTreeNode[]>([
  330. ["/workspace", [node("/workspace/src", "directory", 0)]],
  331. [
  332. "/workspace/src",
  333. [
  334. node("/workspace/src/features", "directory", 1),
  335. node("/workspace/src/index.ts", "file", 1),
  336. ],
  337. ],
  338. ]);
  339. expect(
  340. flattenWorkspaceFileTreeRows({
  341. rootPath: "/workspace",
  342. childrenByDirectory,
  343. expandedPaths: new Set(["/workspace/src"]),
  344. loadedDirectoryPaths: new Set(["/workspace", "/workspace/src"]),
  345. loadingDirectoryPaths: new Set(),
  346. errorByDirectory: new Map(),
  347. flattenEmptyDirectories: true,
  348. }).map((row) => row.path),
  349. ).toEqual([
  350. "/workspace/src",
  351. "/workspace/src/features",
  352. "/workspace/src/index.ts",
  353. ]);
  354. });
  355. it("does not flatten through symlink directories", () => {
  356. const symlinkDirectory = {
  357. ...node("/workspace/src/linked-dir", "directory", 1),
  358. isSymbolicLink: true,
  359. } as WorkspaceFileTreeNode;
  360. const childrenByDirectory = new Map<string, WorkspaceFileTreeNode[]>([
  361. ["/workspace", [node("/workspace/src", "directory", 0)]],
  362. ["/workspace/src", [symlinkDirectory]],
  363. [
  364. "/workspace/src/linked-dir",
  365. [node("/workspace/src/linked-dir/index.ts", "file", 2)],
  366. ],
  367. ]);
  368. const rows = flattenWorkspaceFileTreeRows({
  369. rootPath: "/workspace",
  370. childrenByDirectory,
  371. expandedPaths: new Set(["/workspace/src"]),
  372. loadedDirectoryPaths: new Set([
  373. "/workspace",
  374. "/workspace/src",
  375. "/workspace/src/linked-dir",
  376. ]),
  377. loadingDirectoryPaths: new Set(),
  378. errorByDirectory: new Map(),
  379. flattenEmptyDirectories: true,
  380. });
  381. expect(
  382. rows.map((row) => ({
  383. path: row.path,
  384. name: row.name,
  385. compactedPaths: row.compactedPaths,
  386. })),
  387. ).toEqual([
  388. {
  389. path: "/workspace/src",
  390. name: "src",
  391. compactedPaths: undefined,
  392. },
  393. {
  394. path: "/workspace/src/linked-dir",
  395. name: "linked-dir",
  396. compactedPaths: undefined,
  397. },
  398. ]);
  399. });
  400. it("uses workspace-relative paths for local and Windows separators", () => {
  401. expect(
  402. getWorkspaceFileRelativePath("/workspace", "/workspace/src/index.ts"),
  403. ).toBe("src/index.ts");
  404. expect(
  405. getWorkspaceFileRelativePath("C:\\repo", "C:\\repo\\src\\index.ts"),
  406. ).toBe("src/index.ts");
  407. });
  408. it("builds ancestor directories for revealing active side pane files", () => {
  409. expect(
  410. getWorkspaceFileAncestorDirectories(
  411. "/workspace",
  412. "/workspace/src/app/index.tsx",
  413. ),
  414. ).toEqual(["/workspace/src", "/workspace/src/app"]);
  415. expect(
  416. getWorkspaceFileAncestorDirectories(
  417. "C:\\repo",
  418. "C:\\repo\\src\\app\\index.tsx",
  419. ),
  420. ).toEqual(["C:\\repo\\src", "C:\\repo\\src\\app"]);
  421. expect(
  422. getWorkspaceFileAncestorDirectories("/workspace", "/outside/index.ts"),
  423. ).toEqual([]);
  424. });
  425. it("resolves directory refresh depth and parents across separators", () => {
  426. expect(
  427. getWorkspaceFileDirectoryChildDepth("/workspace", "/workspace"),
  428. ).toBe(0);
  429. expect(
  430. getWorkspaceFileDirectoryChildDepth("/workspace", "/workspace/src"),
  431. ).toBe(1);
  432. expect(
  433. getWorkspaceFileDirectoryChildDepth("/workspace", "/workspace/src/app"),
  434. ).toBe(2);
  435. expect(
  436. getWorkspaceFileDirectoryChildDepth("C:\\repo", "C:\\repo\\src\\app"),
  437. ).toBe(2);
  438. expect(
  439. getWorkspaceFileParentDirectory("/workspace", "/workspace/src/app"),
  440. ).toBe("/workspace/src");
  441. expect(
  442. getWorkspaceFileParentDirectory("/workspace", "/workspace/src"),
  443. ).toBe("/workspace");
  444. expect(
  445. getWorkspaceFileParentDirectory("/workspace", "/workspace"),
  446. ).toBeNull();
  447. expect(
  448. getWorkspaceFileParentDirectory("C:\\repo", "C:\\repo\\src\\app"),
  449. ).toBe("C:\\repo\\src");
  450. });
  451. it("compares paths across Windows and POSIX separators", () => {
  452. expect(
  453. areWorkspaceFilePathsEqual(
  454. "C:\\repo\\src\\app.tsx",
  455. "C:/repo/src/app.tsx",
  456. ),
  457. ).toBe(true);
  458. expect(
  459. areWorkspaceFilePathsEqual("/repo/src/app.tsx", "/repo/src/other.tsx"),
  460. ).toBe(false);
  461. });
  462. it("detects paths inside a workspace across separators", () => {
  463. expect(
  464. isWorkspaceFilePathInside("/workspace", "/workspace/src/index.ts"),
  465. ).toBe(true);
  466. expect(isWorkspaceFilePathInside("C:\\repo", "C:/repo/src/index.ts")).toBe(
  467. true,
  468. );
  469. expect(
  470. isWorkspaceFilePathInside("/workspace", "/workspace-other/index.ts"),
  471. ).toBe(false);
  472. });
  473. it("opens raster images as image previews but keeps svg as file preview", () => {
  474. expect(
  475. createCodeViewerSourceForWorkspaceFile("/workspace/logo.png"),
  476. ).toEqual({
  477. type: "image",
  478. title: "logo.png",
  479. path: "/workspace/logo.png",
  480. mediaType: "image/png",
  481. });
  482. expect(
  483. createCodeViewerSourceForWorkspaceFile("/workspace/logo.svg"),
  484. ).toEqual({
  485. type: "file",
  486. title: "logo.svg",
  487. path: "/workspace/logo.svg",
  488. });
  489. });
  490. it("keeps pdf as file preview and relies on preview pane lazy upgrade", () => {
  491. // pdf 的类型升级收口在 PreviewPane 的 resolvePreviewPanePdfSource,
  492. // 文件树保持 file,让所有入口的 tab key 都是 file:{path},避免同一文件开出两个 tab。
  493. expect(
  494. createCodeViewerSourceForWorkspaceFile("/workspace/report.pdf"),
  495. ).toEqual({
  496. type: "file",
  497. title: "report.pdf",
  498. path: "/workspace/report.pdf",
  499. });
  500. });
  501. it("maps Git changes to file tree status indicators", () => {
  502. const statusByPath = buildWorkspaceFileGitStatusByPath([
  503. {
  504. path: "/workspace/src/index.ts",
  505. section: "unstaged",
  506. isUntracked: false,
  507. kind: "modified",
  508. },
  509. {
  510. path: "/workspace/src/new.ts",
  511. section: "untracked",
  512. isUntracked: true,
  513. kind: "added",
  514. },
  515. {
  516. path: "C:\\repo\\src\\app.tsx",
  517. section: "staged",
  518. isUntracked: false,
  519. kind: "modified",
  520. },
  521. {
  522. path: "/workspace/src/staged-new.ts",
  523. section: "staged",
  524. isUntracked: false,
  525. kind: "added",
  526. },
  527. {
  528. path: "/workspace/src/removed.ts",
  529. section: "unstaged",
  530. isUntracked: false,
  531. kind: "deleted",
  532. },
  533. {
  534. path: "/workspace/src/moved.ts",
  535. section: "staged",
  536. isUntracked: false,
  537. kind: "renamed",
  538. },
  539. ]);
  540. expect(
  541. getWorkspaceFileGitStatus(statusByPath, "/workspace/src/index.ts"),
  542. ).toBe("modified");
  543. expect(
  544. getWorkspaceFileGitStatus(statusByPath, "/workspace/src/new.ts"),
  545. ).toBe("untracked");
  546. expect(getWorkspaceFileGitStatus(statusByPath, "C:/repo/src/app.tsx")).toBe(
  547. "modified",
  548. );
  549. expect(
  550. getWorkspaceFileGitStatus(statusByPath, "/workspace/src/staged-new.ts"),
  551. ).toBe("added");
  552. expect(
  553. getWorkspaceFileGitStatus(statusByPath, "/workspace/src/removed.ts"),
  554. ).toBe("deleted");
  555. expect(
  556. getWorkspaceFileGitStatus(statusByPath, "/workspace/src/moved.ts"),
  557. ).toBe("renamed");
  558. expect(
  559. getWorkspaceFileGitStatus(statusByPath, "/workspace/src/clean.ts"),
  560. ).toBeNull();
  561. });
  562. it("keeps the strongest Git status when staged and unstaged entries share a path", () => {
  563. const statusByPath = buildWorkspaceFileGitStatusByPath([
  564. {
  565. path: "/workspace/src/index.ts",
  566. section: "unstaged",
  567. isUntracked: false,
  568. kind: "modified",
  569. },
  570. {
  571. path: "/workspace/src/index.ts",
  572. section: "staged",
  573. isUntracked: false,
  574. kind: "added",
  575. },
  576. ]);
  577. expect(
  578. getWorkspaceFileGitStatus(statusByPath, "/workspace/src/index.ts"),
  579. ).toBe("added");
  580. });
  581. it("maps Git file tree statuses to dedicated color tokens", () => {
  582. expect(getWorkspaceFileGitStatusTextClassName("modified")).toBe(
  583. "text-git-modified",
  584. );
  585. expect(getWorkspaceFileGitStatusTextClassName("added")).toBe(
  586. "text-git-added",
  587. );
  588. expect(getWorkspaceFileGitStatusTextClassName("deleted")).toBe(
  589. "text-git-deleted",
  590. );
  591. expect(getWorkspaceFileGitStatusTextClassName("renamed")).toBe(
  592. "text-git-renamed",
  593. );
  594. expect(getWorkspaceFileGitStatusTextClassName("untracked")).toBe(
  595. "text-git-untracked",
  596. );
  597. expect(getWorkspaceFileGitStatusTextClassName("ignored")).toBe(
  598. "text-git-ignored",
  599. );
  600. expect(getWorkspaceFileGitStatusIndicatorClassName("untracked")).toBe(
  601. "text-git-untracked/70",
  602. );
  603. expect(getWorkspaceFileGitStatusDotClassName()).toBe(
  604. "bg-git-descendant/60",
  605. );
  606. });
  607. it("uses the resolved descendant priority for directory text color", () => {
  608. expect(
  609. getWorkspaceFileTreeRowDisplayGitStatus({
  610. gitStatus: null,
  611. directoryGitStatuses: ["modified", "untracked"],
  612. }),
  613. ).toBe("modified");
  614. expect(
  615. getWorkspaceFileTreeRowDisplayGitStatus({
  616. gitStatus: "deleted",
  617. directoryGitStatuses: ["modified", "untracked"],
  618. }),
  619. ).toBe("deleted");
  620. });
  621. it("treats deleted file tree rows as non-openable virtual files", () => {
  622. expect(
  623. isWorkspaceFileTreeDeletedFile(
  624. row("/workspace/src/removed.ts", "file", 1),
  625. "deleted",
  626. ),
  627. ).toBe(true);
  628. expect(
  629. isWorkspaceFileTreeDeletedFile(
  630. row("/workspace/src/index.ts", "file", 1),
  631. "modified",
  632. ),
  633. ).toBe(false);
  634. expect(
  635. isWorkspaceFileTreeDeletedFile(
  636. row("/workspace/src", "directory", 0),
  637. "deleted",
  638. ),
  639. ).toBe(false);
  640. });
  641. it("aggregates Git statuses for directories", () => {
  642. const statusByPath = buildWorkspaceFileGitStatusByPath([
  643. {
  644. path: "/workspace/src/index.ts",
  645. section: "unstaged",
  646. isUntracked: false,
  647. kind: "modified",
  648. },
  649. {
  650. path: "/workspace/src/new.ts",
  651. section: "untracked",
  652. isUntracked: true,
  653. kind: "added",
  654. },
  655. {
  656. path: "/workspace/src/deleted.ts",
  657. section: "staged",
  658. isUntracked: false,
  659. kind: "deleted",
  660. },
  661. {
  662. path: "/workspace/docs/readme.md",
  663. section: "unstaged",
  664. isUntracked: false,
  665. kind: "modified",
  666. },
  667. ]);
  668. expect(
  669. getWorkspaceDirectoryGitStatuses(statusByPath, "/workspace/src"),
  670. ).toEqual(["modified", "untracked", "deleted"]);
  671. expect(
  672. getWorkspaceDirectoryGitStatuses(statusByPath, "/workspace/docs"),
  673. ).toEqual(["modified"]);
  674. expect(
  675. getWorkspaceDirectoryGitStatuses(statusByPath, "/workspace/empty"),
  676. ).toEqual([]);
  677. });
  678. it("matches ignored paths across separators without treating them as changed descendants", () => {
  679. const ignoredPathSet = buildWorkspaceFileIgnoredPathSet([
  680. "C:\\repo\\dist\\bundle.js",
  681. "/workspace/build/output.txt",
  682. ]);
  683. expect(
  684. isWorkspaceFileGitIgnored(ignoredPathSet, "C:/repo/dist/bundle.js"),
  685. ).toBe(true);
  686. expect(
  687. isWorkspaceFileGitIgnored(ignoredPathSet, "/workspace/build/output.txt"),
  688. ).toBe(true);
  689. expect(
  690. isWorkspaceFileGitIgnored(ignoredPathSet, "/workspace/src/index.ts"),
  691. ).toBe(false);
  692. });
  693. it("adds deleted Git rows under loaded directories", () => {
  694. const childrenByDirectory = new Map<string, WorkspaceFileTreeNode[]>([
  695. ["/workspace", [node("/workspace/src", "directory", 0)]],
  696. ["/workspace/src", [node("/workspace/src/index.ts", "file", 1)]],
  697. ]);
  698. const statusByPath = new Map([
  699. ["/workspace/src/deleted.ts", "deleted" as const],
  700. ["/workspace/unloaded/deleted.ts", "deleted" as const],
  701. ]);
  702. const nextChildrenByDirectory = addDeletedGitStatusRowsToWorkspaceFileTree({
  703. rootPath: "/workspace",
  704. childrenByDirectory,
  705. statusByPath,
  706. });
  707. expect(
  708. nextChildrenByDirectory.get("/workspace/src")?.map((child) => child.path),
  709. ).toEqual(["/workspace/src/deleted.ts", "/workspace/src/index.ts"]);
  710. expect(nextChildrenByDirectory.get("/workspace/unloaded")).toBeUndefined();
  711. expect(
  712. childrenByDirectory.get("/workspace/src")?.map((child) => child.path),
  713. ).toEqual(["/workspace/src/index.ts"]);
  714. });
  715. it("creates flat file tree rows from workspace search entries", () => {
  716. const rows = createWorkspaceFileTreeRowsFromSearchEntries([
  717. {
  718. name: "UserProfileService.java",
  719. path: "/workspace/src/main/java/com/example/user/UserProfileService.java",
  720. relativePath: "src/main/java/com/example/user/UserProfileService.java",
  721. type: "file",
  722. },
  723. {
  724. name: "user",
  725. path: "/workspace/src/main/java/com/example/user",
  726. relativePath: "src/main/java/com/example/user",
  727. type: "directory",
  728. },
  729. ]);
  730. expect(rows).toEqual([
  731. {
  732. path: "/workspace/src/main/java/com/example/user/UserProfileService.java",
  733. name: "src/main/java/com/example/user/UserProfileService.java",
  734. type: "file",
  735. depth: 0,
  736. expanded: false,
  737. loaded: false,
  738. loading: false,
  739. error: null,
  740. },
  741. {
  742. path: "/workspace/src/main/java/com/example/user",
  743. name: "src/main/java/com/example/user",
  744. type: "directory",
  745. depth: 0,
  746. expanded: false,
  747. loaded: false,
  748. loading: false,
  749. error: null,
  750. },
  751. ]);
  752. });
  753. it("builds the lazy tree reveal chain for directory search results", () => {
  754. expect(
  755. getWorkspaceFileSearchDirectoryRevealPaths({
  756. workspacePath: "/workspace",
  757. directoryPath: "/workspace/src/main/java/com/example/user",
  758. }),
  759. ).toEqual([
  760. "/workspace/src",
  761. "/workspace/src/main",
  762. "/workspace/src/main/java",
  763. "/workspace/src/main/java/com",
  764. "/workspace/src/main/java/com/example",
  765. "/workspace/src/main/java/com/example/user",
  766. ]);
  767. });
  768. it("does not reveal directory search results outside the workspace", () => {
  769. expect(
  770. getWorkspaceFileSearchDirectoryRevealPaths({
  771. workspacePath: "/workspace",
  772. directoryPath: "/outside/src",
  773. }),
  774. ).toEqual([]);
  775. });
  776. it("filters file tree rows by filename while keeping visible ancestors", () => {
  777. const rows: WorkspaceFileTreeRow[] = [
  778. row("/workspace/src", "directory", 0, true),
  779. row("/workspace/src/app", "directory", 1, true),
  780. row("/workspace/src/app/index.ts", "file", 2),
  781. row("/workspace/src/app/view.tsx", "file", 2),
  782. row("/workspace/docs", "directory", 0, true),
  783. row("/workspace/docs/readme.md", "file", 1),
  784. ];
  785. expect(
  786. filterWorkspaceFileTreeRows({
  787. rows,
  788. searchQuery: "index",
  789. changedOnly: false,
  790. statusByPath: new Map(),
  791. }).map((item) => item.path),
  792. ).toEqual([
  793. "/workspace/src",
  794. "/workspace/src/app",
  795. "/workspace/src/app/index.ts",
  796. ]);
  797. });
  798. it("filters file tree rows to changed files and changed descendant directories", () => {
  799. const rows: WorkspaceFileTreeRow[] = [
  800. row("/workspace/src", "directory", 0, true),
  801. row("/workspace/src/app", "directory", 1, true),
  802. row("/workspace/src/app/index.ts", "file", 2),
  803. row("/workspace/src/app/view.tsx", "file", 2),
  804. row("/workspace/docs", "directory", 0, false),
  805. row("/workspace/README.md", "file", 0),
  806. ];
  807. const statusByPath = new Map([
  808. ["/workspace/src/app/view.tsx", "modified" as const],
  809. ["/workspace/docs/readme.md", "added" as const],
  810. ]);
  811. expect(
  812. filterWorkspaceFileTreeRows({
  813. rows,
  814. searchQuery: "",
  815. changedOnly: true,
  816. statusByPath,
  817. }).map((item) => item.path),
  818. ).toEqual([
  819. "/workspace/src",
  820. "/workspace/src/app",
  821. "/workspace/src/app/view.tsx",
  822. "/workspace/docs",
  823. ]);
  824. });
  825. it("combines filename search with changed-only filtering", () => {
  826. const rows: WorkspaceFileTreeRow[] = [
  827. row("/workspace/src", "directory", 0, true),
  828. row("/workspace/src/index.ts", "file", 1),
  829. row("/workspace/src/view.tsx", "file", 1),
  830. ];
  831. const statusByPath = new Map([
  832. ["/workspace/src/view.tsx", "modified" as const],
  833. ]);
  834. expect(
  835. filterWorkspaceFileTreeRows({
  836. rows,
  837. searchQuery: "index",
  838. changedOnly: true,
  839. statusByPath,
  840. }),
  841. ).toEqual([]);
  842. });
  843. it("removes sticky folders after scrolling into a closed sibling directory", () => {
  844. const rows: WorkspaceFileTreeRow[] = [
  845. row("/workspace/src", "directory", 0, true),
  846. row("/workspace/src/index.ts", "file", 1),
  847. row("/workspace/docs", "directory", 0, false),
  848. ];
  849. expect(
  850. getWorkspaceFileTreeStickyFolders({
  851. rows,
  852. virtualItems: [virtualItem(2)],
  853. scrollDirection: "forward",
  854. scrollOffset: 2 * WORKSPACE_FILE_TREE_VIRTUAL_ROW_HEIGHT_PX,
  855. }).map((item) => item.row.path),
  856. ).toEqual([]);
  857. });
  858. it("sticks parent folders when the first visible row is child content", () => {
  859. const rows: WorkspaceFileTreeRow[] = [
  860. row("/workspace/src", "directory", 0, true),
  861. row("/workspace/src/app", "directory", 1, true),
  862. row("/workspace/src/app/index.ts", "file", 2),
  863. ];
  864. expect(
  865. getWorkspaceFileTreeStickyFolders({
  866. rows,
  867. virtualItems: [virtualItem(2)],
  868. scrollDirection: "forward",
  869. scrollOffset: 2 * WORKSPACE_FILE_TREE_VIRTUAL_ROW_HEIGHT_PX,
  870. }).map((item) => item.row.path),
  871. ).toEqual(["/workspace/src", "/workspace/src/app"]);
  872. });
  873. it("sticks each nested folder when it reaches its stacked sticky boundary", () => {
  874. const rows: WorkspaceFileTreeRow[] = [
  875. row("/workspace/src", "directory", 0, true),
  876. row("/workspace/src/app", "directory", 1, true),
  877. row("/workspace/src/app/index.ts", "file", 2),
  878. ];
  879. expect(
  880. getWorkspaceFileTreeStickyFolders({
  881. rows,
  882. virtualItems: [virtualItem(0), virtualItem(1), virtualItem(2)],
  883. scrollDirection: "forward",
  884. scrollOffset: 1,
  885. }).map((item) => item.row.path),
  886. ).toEqual(["/workspace/src", "/workspace/src/app"]);
  887. });
  888. it("masks the virtual list below the combined sticky container", () => {
  889. expect(
  890. getWorkspaceFileTreeListMaskStyle({
  891. stickyFolderCount: 2,
  892. }),
  893. ).toMatchObject({
  894. maskImage: "linear-gradient(to bottom, transparent 0 56px, black 56px)",
  895. maskPosition: "0 var(--workspace-file-tree-mask-offset)",
  896. maskSize: "100% calc(100% - var(--workspace-file-tree-mask-offset))",
  897. maskRepeat: "no-repeat",
  898. });
  899. expect(
  900. getWorkspaceFileTreeListMaskStyle({
  901. stickyFolderCount: 0,
  902. }),
  903. ).toBeUndefined();
  904. });
  905. it("does not stick an expanded folder before scrolling starts", () => {
  906. const rows: WorkspaceFileTreeRow[] = [
  907. row("/workspace/src", "directory", 0, true),
  908. row("/workspace/src/index.ts", "file", 1),
  909. ];
  910. expect(
  911. getWorkspaceFileTreeStickyFolders({
  912. rows,
  913. virtualItems: [virtualItem(0)],
  914. scrollDirection: "forward",
  915. scrollOffset: 0,
  916. }).map((item) => item.row.path),
  917. ).toEqual([]);
  918. });
  919. it("sticks an expanded folder after scrolling it to the boundary", () => {
  920. const rows: WorkspaceFileTreeRow[] = [
  921. row("/workspace/README.md", "file", 0),
  922. row("/workspace/src", "directory", 0, true),
  923. row("/workspace/src/index.ts", "file", 1),
  924. ];
  925. expect(
  926. getWorkspaceFileTreeStickyFolders({
  927. rows,
  928. virtualItems: [virtualItem(1)],
  929. scrollDirection: "forward",
  930. scrollOffset: WORKSPACE_FILE_TREE_VIRTUAL_ROW_HEIGHT_PX,
  931. }).map((item) => item.row.path),
  932. ).toEqual(["/workspace/src"]);
  933. });
  934. it("does not stick folders when the tree is not scrollable", () => {
  935. const rows: WorkspaceFileTreeRow[] = [
  936. row("/workspace/src", "directory", 0, true),
  937. row("/workspace/src/index.ts", "file", 1),
  938. ];
  939. expect(
  940. getWorkspaceFileTreeStickyFolders({
  941. rows,
  942. virtualItems: [virtualItem(0)],
  943. scrollDirection: "forward",
  944. scrollOffset: 0,
  945. enabled: false,
  946. }).map((item) => item.row.path),
  947. ).toEqual([]);
  948. });
  949. it("unsticks a folder while scrolling backward once it crosses its sticky boundary", () => {
  950. const rows: WorkspaceFileTreeRow[] = [
  951. row("/workspace/src", "directory", 0, true),
  952. row("/workspace/src/index.ts", "file", 1),
  953. ];
  954. expect(
  955. getWorkspaceFileTreeStickyFolders({
  956. rows,
  957. virtualItems: [virtualItem(1)],
  958. scrollDirection: "backward",
  959. scrollOffset: WORKSPACE_FILE_TREE_VIRTUAL_ROW_HEIGHT_PX,
  960. }).map((item) => item.row.path),
  961. ).toEqual(["/workspace/src"]);
  962. expect(
  963. getWorkspaceFileTreeStickyFolders({
  964. rows,
  965. virtualItems: [virtualItem(0), virtualItem(1)],
  966. scrollDirection: "backward",
  967. scrollOffset: 0,
  968. }).map((item) => item.row.path),
  969. ).toEqual([]);
  970. });
  971. it("keeps a sticky row until its original row crosses the sticky boundary", () => {
  972. const rows: WorkspaceFileTreeRow[] = [
  973. row("/workspace/README.md", "file", 0),
  974. row("/workspace/src", "directory", 0, true),
  975. row("/workspace/src/index.ts", "file", 1),
  976. ];
  977. expect(
  978. getWorkspaceFileTreeStickyFolders({
  979. rows,
  980. virtualItems: [virtualItem(1), virtualItem(2)],
  981. scrollDirection: "backward",
  982. scrollOffset: 2 * WORKSPACE_FILE_TREE_VIRTUAL_ROW_HEIGHT_PX - 1,
  983. }).map((item) => item.row.path),
  984. ).toEqual(["/workspace/src"]);
  985. expect(
  986. getWorkspaceFileTreeStickyFolders({
  987. rows,
  988. virtualItems: [virtualItem(2)],
  989. scrollDirection: "backward",
  990. scrollOffset: 2 * WORKSPACE_FILE_TREE_VIRTUAL_ROW_HEIGHT_PX,
  991. }).map((item) => item.row.path),
  992. ).toEqual(["/workspace/src"]);
  993. });
  994. });