Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 buildFileDiffContentVersion,
4 buildFileDiffIdentityKey,
5 buildFileDiffRenderKey,
6 buildPatchCacheKey,
7 getDiffLineStat,
8 getRenderablePatch,
9 resolveFileDiffPath,
10 resolveFileDiffPreviousPath,
11} from "./diffRendering";
12
13describe("buildPatchCacheKey", () => {
14 it("normalizes outer whitespace before hashing", () => {
15 const patch = "diff --git a/a.ts b/a.ts\n+console.log('hello')";
16
17 expect(buildPatchCacheKey(`\n${patch}\n`)).toBe(buildPatchCacheKey(patch));
18 });
19
20 it("changes when diff content changes", () => {
21 const before = "diff --git a/a.ts b/a.ts\n+console.log('hello')";
22 const after = "diff --git a/a.ts b/a.ts\n+console.log('hello world')";
23
24 expect(buildPatchCacheKey(before)).not.toBe(buildPatchCacheKey(after));
25 });
26
27 it("changes when cache scope changes", () => {
28 const patch = "diff --git a/a.ts b/a.ts\n+console.log('hello')";
29
30 expect(buildPatchCacheKey(patch, "diff-panel:light")).not.toBe(
31 buildPatchCacheKey(patch, "diff-panel:dark"),
32 );
33 });
34});
35
36describe("getRenderablePatch", () => {
37 it.each([
38 ["a/example.ts", "a/example.ts", "change"],
39 ["b/example.ts", "b/example.ts", "change"],
40 ["a/before.ts", "b/after.ts", "rename-changed"],
41 ])("preserves repository paths from %s to %s", (previousPath, path, type) => {
42 const parsed = getRenderablePatch(
43 [
44 `diff --git a/${previousPath} b/${path}`,
45 ...(previousPath === path
46 ? []
47 : ["similarity index 50%", `rename from ${previousPath}`, `rename to ${path}`]),
48 `--- a/${previousPath}`,
49 `+++ b/${path}`,
50 "@@ -1 +1 @@",
51 "-before",
52 "+after",
53 ].join("\n"),
54 );
55 expect(parsed?.kind).toBe("files");
56 if (parsed?.kind !== "files") return;
57 const file = parsed.files[0];
58 expect(file).toBeDefined();
59 if (!file) return;
60 expect(resolveFileDiffPath(file)).toBe(path);
61 expect(resolveFileDiffPreviousPath(file)).toBe(previousPath);
62 expect(buildFileDiffIdentityKey(file)).toBe(`${previousPath}\0${path}\0${type}`);
63 });
64
65 it("compacts partial hunk render offsets for virtualized review diffs", () => {
66 const patch = [
67 "diff --git a/example.ts b/example.ts",
68 "index 1111111..2222222 100644",
69 "--- a/example.ts",
70 "+++ b/example.ts",
71 "@@ -48,4 +48,4 @@",
72 " context",
73 "-before",
74 "+after",
75 " context",
76 " context",
77 "@@ -80,3 +80,4 @@",
78 " context",
79 "+added",
80 " context",
81 " context",
82 ].join("\n");
83
84 const parsed = getRenderablePatch(patch, "review", {
85 compactPartialHunkOffsets: true,
86 });
87 expect(parsed?.kind).toBe("files");
88 if (parsed?.kind !== "files") return;
89
90 const file = parsed.files[0];
91 expect(file?.hunks[0]?.collapsedBefore).toBe(47);
92 expect(file?.hunks[0]?.unifiedLineStart).toBe(0);
93 expect(file?.hunks[1]?.collapsedBefore).toBeGreaterThan(0);
94 expect(file?.hunks[1]?.unifiedLineStart).toBe(file?.hunks[0]?.unifiedLineCount);
95 expect(file?.unifiedLineCount).toBe(
96 file?.hunks.reduce((total, hunk) => total + hunk.unifiedLineCount, 0),
97 );
98 });
99
100 it("retains source-file offsets for checkpoint diffs", () => {
101 const patch = [
102 "diff --git a/example.ts b/example.ts",
103 "--- a/example.ts",
104 "+++ b/example.ts",
105 "@@ -48,1 +48,1 @@",
106 "-before",
107 "+after",
108 ].join("\n");
109
110 const parsed = getRenderablePatch(patch, "checkpoint");
111 expect(parsed?.kind).toBe("files");
112 if (parsed?.kind !== "files") return;
113 expect(parsed.files[0]?.hunks[0]?.unifiedLineStart).toBe(47);
114 });
115});
116
117describe("diff file reconciliation", () => {
118 it("keeps Pierre's render key stable when a partial diff hydrates", () => {
119 const patch = [
120 "diff --git a/example.ts b/example.ts",
121 "--- a/example.ts",
122 "+++ b/example.ts",
123 "@@ -1 +1 @@",
124 "-before",
125 "+after",
126 ].join("\n");
127 const parsed = getRenderablePatch(patch, "hydrated-key");
128 expect(parsed?.kind).toBe("files");
129 if (parsed?.kind !== "files") return;
130
131 const file = parsed.files[0];
132 expect(file).toBeDefined();
133 if (!file) return;
134 const key = buildFileDiffRenderKey(file);
135 file.cacheKey = `${file.cacheKey}:hydrated`;
136
137 expect(buildFileDiffRenderKey(file)).toBe(key);
138 });
139
140 it("gives a type change its own identity per block", () => {
141 const patch = [
142 "diff --git a/AGENTS.md b/AGENTS.md",
143 "deleted file mode 100644",
144 "--- a/AGENTS.md",
145 "+++ /dev/null",
146 "@@ -1 +0,0 @@",
147 "-duplicated instructions",
148 "diff --git a/AGENTS.md b/AGENTS.md",
149 "new file mode 120000",
150 "--- /dev/null",
151 "+++ b/AGENTS.md",
152 "@@ -0,0 +1 @@",
153 "+CLAUDE.md",
154 ].join("\n");
155 const parsed = getRenderablePatch(patch, "type-change");
156 expect(parsed?.kind).toBe("files");
157 if (parsed?.kind !== "files") return;
158 const [deleted, added] = parsed.files;
159 expect(deleted?.type).toBe("deleted");
160 expect(added?.type).toBe("new");
161 if (!deleted || !added) return;
162
163 expect(buildFileDiffIdentityKey(deleted)).not.toBe(buildFileDiffIdentityKey(added));
164 expect(new Set(parsed.files.map(buildFileDiffIdentityKey)).size).toBe(parsed.files.length);
165 });
166
167 it("keeps identities stable and versions local to the changed file", () => {
168 const patch = (secondLine: string) =>
169 [
170 "diff --git a/unchanged.ts b/unchanged.ts",
171 "--- a/unchanged.ts",
172 "+++ b/unchanged.ts",
173 "@@ -1 +1 @@",
174 "-before",
175 "+after",
176 "diff --git a/changed.ts b/changed.ts",
177 "--- a/changed.ts",
178 "+++ b/changed.ts",
179 "@@ -1 +1 @@",
180 "-old",
181 `+${secondLine}`,
182 ].join("\n");
183 const before = getRenderablePatch(patch("new"), "before");
184 const after = getRenderablePatch(patch("newer"), "after");
185 expect(before?.kind).toBe("files");
186 expect(after?.kind).toBe("files");
187 if (before?.kind !== "files" || after?.kind !== "files") return;
188
189 const [beforeUnchanged, beforeChanged] = before.files;
190 const [afterUnchanged, afterChanged] = after.files;
191 expect(beforeUnchanged).toBeDefined();
192 expect(beforeChanged).toBeDefined();
193 expect(afterUnchanged).toBeDefined();
194 expect(afterChanged).toBeDefined();
195 if (!beforeUnchanged || !beforeChanged || !afterUnchanged || !afterChanged) return;
196
197 expect(buildFileDiffIdentityKey(afterUnchanged)).toBe(
198 buildFileDiffIdentityKey(beforeUnchanged),
199 );
200 expect(buildFileDiffIdentityKey(afterChanged)).toBe(buildFileDiffIdentityKey(beforeChanged));
201 expect(buildFileDiffContentVersion(afterUnchanged)).toBe(
202 buildFileDiffContentVersion(beforeUnchanged),
203 );
204 expect(buildFileDiffContentVersion(afterChanged)).not.toBe(
205 buildFileDiffContentVersion(beforeChanged),
206 );
207 });
208});
209
210describe("getDiffLineStat", () => {
211 it("totals additions and deletions across every file and hunk", () => {
212 const patch = [
213 "diff --git a/example.ts b/example.ts",
214 "--- a/example.ts",
215 "+++ b/example.ts",
216 "@@ -1,2 +1,3 @@",
217 "-before",
218 "+after",
219 "+added",
220 " context",
221 "@@ -10,2 +11,1 @@",
222 "-removed",
223 " context",
224 "diff --git a/README.md b/README.md",
225 "--- a/README.md",
226 "+++ b/README.md",
227 "@@ -1 +1,2 @@",
228 " title",
229 "+description",
230 ].join("\n");
231
232 const parsed = getRenderablePatch(patch);
233 expect(parsed?.kind).toBe("files");
234 if (parsed?.kind !== "files") return;
235
236 expect(getDiffLineStat(parsed.files)).toEqual({ additions: 3, deletions: 2 });
237 });
238});
239
240describe("a file whose name a patch header cannot carry plainly", () => {
241 /** How git writes such a name, and so how every provider's patch arrives here. */
242 const quotedPatch = (written: string) =>
243 [
244 `diff --git "a/${written}" "b/${written}"`,
245 "index 1111111..2222222 100644",
246 `--- "a/${written}"`,
247 `+++ "b/${written}"`,
248 "@@ -1 +1 @@",
249 "-before",
250 "+after",
251 "",
252 ].join("\n");
253
254 const pathOf = (patch: string) => {
255 const parsed = getRenderablePatch(patch, "review");
256 expect(parsed?.kind).toBe("files");
257 if (parsed?.kind !== "files") throw new Error("patch did not parse as files");
258 const file = parsed.files[0];
259 expect(file).toBeDefined();
260 if (!file) throw new Error("patch carried no file");
261 return resolveFileDiffPath(file);
262 };
263
264 it("is the name the host knows, not the part of it before the tab", () => {
265 // The path is what a viewed mark, a review comment and a file read are all asked for by, so a
266 // name read short is a mark put on a path the host has never heard of.
267 expect(pathOf(quotedPatch("tab\\tfile.txt"))).toBe("tab\tfile.txt");
268 });
269
270 it("is the name the host knows, not the part of it before the newline", () => {
271 expect(pathOf(quotedPatch("line\\nfile.txt"))).toBe("line\nfile.txt");
272 });
273
274 it("reads the octal a host with core.quotePath on writes for a name outside ASCII", () => {
275 expect(pathOf(quotedPatch("caf\\303\\251/r\\303\\251sum\\303\\251.ts"))).toBe("café/résumé.ts");
276 });
277
278 it("reads both sides of a rename under the names they really have", () => {
279 const patch = [
280 'diff --git "a/old\\tname.ts" "b/new\\tname.ts"',
281 "similarity index 90%",
282 'rename from "old\\tname.ts"',
283 'rename to "new\\tname.ts"',
284 "",
285 ].join("\n");
286
287 const parsed = getRenderablePatch(patch, "review");
288 expect(parsed?.kind).toBe("files");
289 if (parsed?.kind !== "files") return;
290 const file = parsed.files[0];
291 expect(file).toBeDefined();
292 if (!file) return;
293
294 expect(resolveFileDiffPath(file)).toBe("new\tname.ts");
295 expect(resolveFileDiffPreviousPath(file)).toBe("old\tname.ts");
296 });
297});