Open History to see the last commit
1import type { FileDiffMetadata } from "@pierre/diffs";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 buildDiffFileTreeUpdates,
6 collectDirectoryPaths,
7 diffFileTreeEntries,
8} from "./diffFileTree.logic";
9
10function file(type: FileDiffMetadata["type"], name: string, prevName = name): FileDiffMetadata {
11 return { type, name: `b/${name}`, prevName: `a/${prevName}` } as FileDiffMetadata;
12}
13
14describe("diffFileTreeEntries", () => {
15 it("maps each change type to its git status under the file's current path", () => {
16 expect(
17 diffFileTreeEntries([
18 file("new", "src/a.ts"),
19 file("deleted", "src/b.ts"),
20 file("rename-pure", "src/c.ts", "src/old-c.ts"),
21 file("rename-changed", "src/d.ts", "src/old-d.ts"),
22 file("change", "README.md"),
23 ]),
24 ).toEqual([
25 { path: "src/a.ts", status: "added" },
26 { path: "src/b.ts", status: "deleted" },
27 { path: "src/c.ts", status: "renamed" },
28 { path: "src/d.ts", status: "renamed" },
29 { path: "README.md", status: "modified" },
30 ]);
31 });
32});
33
34describe("collectDirectoryPaths", () => {
35 it("lists every ancestor once, parents first, with Pierre's trailing slash", () => {
36 expect(collectDirectoryPaths(["apps/web/src/a.ts", "apps/web/b.ts", "README.md"])).toEqual([
37 "apps/",
38 "apps/web/",
39 "apps/web/src/",
40 ]);
41 });
42});
43
44describe("buildDiffFileTreeUpdates", () => {
45 it("adds a new file's directories before the file", () => {
46 expect(buildDiffFileTreeUpdates(["README.md"], ["README.md", "src/lib/a.ts"])).toEqual([
47 { type: "add", path: "src/" },
48 { type: "add", path: "src/lib/" },
49 { type: "add", path: "src/lib/a.ts" },
50 ]);
51 });
52
53 it("removes files before their now-empty directories, deepest first", () => {
54 expect(buildDiffFileTreeUpdates(["src/lib/a.ts", "src/b.ts"], ["src/b.ts"])).toEqual([
55 { type: "remove", path: "src/lib/a.ts" },
56 { type: "remove", path: "src/lib/", recursive: true },
57 ]);
58 });
59
60 it("keeps a directory that still holds a file", () => {
61 expect(buildDiffFileTreeUpdates(["src/a.ts", "src/b.ts"], ["src/b.ts"])).toEqual([
62 { type: "remove", path: "src/a.ts" },
63 ]);
64 });
65
66 it("produces nothing when the paths are unchanged", () => {
67 expect(buildDiffFileTreeUpdates(["src/a.ts"], ["src/a.ts"])).toEqual([]);
68 });
69});