Open History to see the last commit
1import type { FileDiffMetadata } from "@pierre/diffs";
2import { preloadFileTree } from "@pierre/trees";
3import { describe, expect, it } from "vite-plus/test";
4
5import {
6 buildDiffFileTreeUpdates,
7 compareDiffFileTreeEntries,
8 collectDirectoryPaths,
9 diffFileTreePositions,
10 diffFileTreeEntries,
11} from "./diffFileTree.logic";
12
13function file(type: FileDiffMetadata["type"], name: string, prevName = name): FileDiffMetadata {
14 return { type, name, prevName } as FileDiffMetadata;
15}
16
17describe("diffFileTreeEntries", () => {
18 it("maps each change type to its git status under the file's current path", () => {
19 expect(
20 diffFileTreeEntries([
21 file("new", "a/src/a.ts"),
22 file("deleted", "src/b.ts"),
23 file("rename-pure", "src/c.ts", "src/old-c.ts"),
24 file("rename-changed", "src/d.ts", "src/old-d.ts"),
25 file("change", "README.md"),
26 ]),
27 ).toEqual([
28 { path: "a/src/a.ts", status: "added" },
29 { path: "src/b.ts", status: "deleted" },
30 { path: "src/c.ts", status: "renamed" },
31 { path: "src/d.ts", status: "renamed" },
32 { path: "README.md", status: "modified" },
33 ]);
34 });
35});
36
37describe("collectDirectoryPaths", () => {
38 it("lists every ancestor once, parents first, with Pierre's trailing slash", () => {
39 expect(collectDirectoryPaths(["apps/web/src/a.ts", "apps/web/b.ts", "README.md"])).toEqual([
40 "apps/",
41 "apps/web/",
42 "apps/web/src/",
43 ]);
44 });
45});
46
47describe("diff tree reading order", () => {
48 it("places folders and files where their first diff appears", () => {
49 const paths = [
50 "apps/mobile/src/state/shell.ts",
51 "apps/mobile/src/features/threads/route.ts",
52 "apps/mobile/src/features/threads/screen.tsx",
53 ];
54 const positions = diffFileTreePositions(paths);
55 const tree = preloadFileTree({
56 paths,
57 initialExpansion: "open",
58 flattenEmptyDirectories: true,
59 sort: compareDiffFileTreeEntries(() => positions),
60 });
61 const rows = [...tree.shadowHtml.matchAll(/data-item-path="([^"]+)"/g)].map(
62 (match) => match[1],
63 );
64 expect(rows).toEqual([
65 "apps/mobile/src/",
66 "apps/mobile/src/state/",
67 "apps/mobile/src/state/shell.ts",
68 "apps/mobile/src/features/threads/",
69 "apps/mobile/src/features/threads/route.ts",
70 "apps/mobile/src/features/threads/screen.tsx",
71 ]);
72 });
73});
74
75describe("buildDiffFileTreeUpdates", () => {
76 it("adds a new file's directories before the file", () => {
77 expect(buildDiffFileTreeUpdates(["README.md"], ["README.md", "src/lib/a.ts"])).toEqual([
78 { type: "add", path: "src/" },
79 { type: "add", path: "src/lib/" },
80 { type: "add", path: "src/lib/a.ts" },
81 ]);
82 });
83
84 it("removes files before their now-empty directories, deepest first", () => {
85 expect(buildDiffFileTreeUpdates(["src/lib/a.ts", "src/b.ts"], ["src/b.ts"])).toEqual([
86 { type: "remove", path: "src/lib/a.ts" },
87 { type: "remove", path: "src/lib/", recursive: true },
88 ]);
89 });
90
91 it("keeps a directory that still holds a file", () => {
92 expect(buildDiffFileTreeUpdates(["src/a.ts", "src/b.ts"], ["src/b.ts"])).toEqual([
93 { type: "remove", path: "src/a.ts" },
94 ]);
95 });
96
97 it("produces nothing when the paths are unchanged", () => {
98 expect(buildDiffFileTreeUpdates(["src/a.ts"], ["src/a.ts"])).toEqual([]);
99 });
100});