Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { parseTurnDiffFilesFromNumstat, prefixNumstatPaths, prefixPatchPaths } from "./Diffs.ts";
4
5describe("parseTurnDiffFilesFromNumstat", () => {
6 it("sorts files and preserves addition and deletion counts", () => {
7 const numstat = ["0\t2\tsrc/b.ts", "2\t1\ta.txt", ""].join("\0");
8 expect(parseTurnDiffFilesFromNumstat(numstat)).toEqual([
9 { path: "a.txt", additions: 2, deletions: 1 },
10 { path: "src/b.ts", additions: 0, deletions: 2 },
11 ]);
12 });
13
14 it("preserves both paths for renames and copies", () => {
15 const numstat = [
16 "0\t0\t",
17 "src/old.ts",
18 "src/new.ts",
19 "2\t1\t",
20 "src/source.ts",
21 "src/copied.ts",
22 "1\t0\tother.ts",
23 "",
24 ].join("\0");
25
26 expect(parseTurnDiffFilesFromNumstat(numstat)).toEqual([
27 { path: "other.ts", additions: 1, deletions: 0 },
28 { path: "src/copied.ts", previousPath: "src/source.ts", additions: 2, deletions: 1 },
29 { path: "src/new.ts", previousPath: "src/old.ts", additions: 0, deletions: 0 },
30 ]);
31 });
32
33 it("keeps binary files and empty files with zero line changes", () => {
34 const numstat = ["-\t-\timage.png", "0\t0\tempty.txt", ""].join("\0");
35 expect(parseTurnDiffFilesFromNumstat(numstat)).toEqual([
36 { path: "empty.txt", additions: 0, deletions: 0 },
37 { path: "image.png", additions: 0, deletions: 0 },
38 ]);
39 });
40
41 it("preserves Unicode, tabs, line endings, and spaces in paths", () => {
42 const path = " café\tline\r\nname.txt ";
43 const numstat = `3\t2\t\0old\tname\n.txt\0${path}\0`;
44
45 expect(parseTurnDiffFilesFromNumstat(numstat)).toEqual([
46 { path, previousPath: "old\tname\n.txt", additions: 3, deletions: 2 },
47 ]);
48 expect(parseTurnDiffFilesFromNumstat(`1\t0\t${path}\0`)).toEqual([
49 { path, additions: 1, deletions: 0 },
50 ]);
51 });
52});
53
54describe("prefixNumstatPaths", () => {
55 it("moves plain, renamed and copied paths under the prefix", () => {
56 const numstat = ["1\t0\tsrc/a.ts", "0\t0\t", "old.ts", "new.ts", "-\t-\timage.png", ""].join(
57 "\0",
58 );
59
60 expect(parseTurnDiffFilesFromNumstat(prefixNumstatPaths(numstat, "api"))).toEqual([
61 { path: "api/image.png", additions: 0, deletions: 0 },
62 { path: "api/new.ts", previousPath: "api/old.ts", additions: 0, deletions: 0 },
63 { path: "api/src/a.ts", additions: 1, deletions: 0 },
64 ]);
65 expect(prefixNumstatPaths("", "api")).toBe("");
66 });
67});
68
69describe("prefixPatchPaths", () => {
70 it("rewrites file headers and leaves hunk content alone", () => {
71 const patch = [
72 "diff --git a/src/a b/c.ts b/src/a b/c.ts",
73 "index 1111111..2222222 100644",
74 "--- a/src/a b/c.ts",
75 "+++ b/src/a b/c.ts",
76 "@@ -1,2 +1,2 @@",
77 "--- a/looks-like-a-header",
78 "+++ b/also-content",
79 "diff --git a/old.ts b/new.ts",
80 "similarity index 90%",
81 "rename from old.ts",
82 "rename to new.ts",
83 "diff --git a/added.ts b/added.ts",
84 "new file mode 100644",
85 "--- /dev/null",
86 '+++ "b/caf\\303\\251.ts"',
87 "diff --git a/logo.png b/logo.png",
88 "Binary files a/logo.png and b/logo.png differ",
89 "",
90 ].join("\n");
91
92 expect(prefixPatchPaths(patch, "web")).toBe(
93 [
94 "diff --git a/web/src/a b/c.ts b/web/src/a b/c.ts",
95 "index 1111111..2222222 100644",
96 "--- a/web/src/a b/c.ts",
97 "+++ b/web/src/a b/c.ts",
98 "@@ -1,2 +1,2 @@",
99 "--- a/looks-like-a-header",
100 "+++ b/also-content",
101 "diff --git a/web/old.ts b/web/new.ts",
102 "similarity index 90%",
103 "rename from web/old.ts",
104 "rename to web/new.ts",
105 "diff --git a/web/added.ts b/web/added.ts",
106 "new file mode 100644",
107 "--- /dev/null",
108 '+++ "b/web/caf\\303\\251.ts"',
109 "diff --git a/web/logo.png b/web/logo.png",
110 "Binary files a/web/logo.png and b/web/logo.png differ",
111 "",
112 ].join("\n"),
113 );
114 });
115});