Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

diffRendering.test.ts

211 lines6.6 KB

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"],
39 ["b/example.ts", "b/example.ts"],
40 ["a/before.ts", "b/after.ts"],
41 ])("preserves repository paths from %s to %s", (previousPath, path) => {
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}`);
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("keeps identities stable and versions local to the changed file", () => {
141 const patch = (secondLine: string) =>
142 [
143 "diff --git a/unchanged.ts b/unchanged.ts",
144 "--- a/unchanged.ts",
145 "+++ b/unchanged.ts",
146 "@@ -1 +1 @@",
147 "-before",
148 "+after",
149 "diff --git a/changed.ts b/changed.ts",
150 "--- a/changed.ts",
151 "+++ b/changed.ts",
152 "@@ -1 +1 @@",
153 "-old",
154 `+${secondLine}`,
155 ].join("\n");
156 const before = getRenderablePatch(patch("new"), "before");
157 const after = getRenderablePatch(patch("newer"), "after");
158 expect(before?.kind).toBe("files");
159 expect(after?.kind).toBe("files");
160 if (before?.kind !== "files" || after?.kind !== "files") return;
161​
162 const [beforeUnchanged, beforeChanged] = before.files;
163 const [afterUnchanged, afterChanged] = after.files;
164 expect(beforeUnchanged).toBeDefined();
165 expect(beforeChanged).toBeDefined();
166 expect(afterUnchanged).toBeDefined();
167 expect(afterChanged).toBeDefined();
168 if (!beforeUnchanged || !beforeChanged || !afterUnchanged || !afterChanged) return;
169​
170 expect(buildFileDiffIdentityKey(afterUnchanged)).toBe(
171 buildFileDiffIdentityKey(beforeUnchanged),
172 );
173 expect(buildFileDiffIdentityKey(afterChanged)).toBe(buildFileDiffIdentityKey(beforeChanged));
174 expect(buildFileDiffContentVersion(afterUnchanged)).toBe(
175 buildFileDiffContentVersion(beforeUnchanged),
176 );
177 expect(buildFileDiffContentVersion(afterChanged)).not.toBe(
178 buildFileDiffContentVersion(beforeChanged),
179 );
180 });
181});
182​
183describe("getDiffLineStat", () => {
184 it("totals additions and deletions across every file and hunk", () => {
185 const patch = [
186 "diff --git a/example.ts b/example.ts",
187 "--- a/example.ts",
188 "+++ b/example.ts",
189 "@@ -1,2 +1,3 @@",
190 "-before",
191 "+after",
192 "+added",
193 " context",
194 "@@ -10,2 +11,1 @@",
195 "-removed",
196 " context",
197 "diff --git a/README.md b/README.md",
198 "--- a/README.md",
199 "+++ b/README.md",
200 "@@ -1 +1,2 @@",
201 " title",
202 "+description",
203 ].join("\n");
204​
205 const parsed = getRenderablePatch(patch);
206 expect(parsed?.kind).toBe("files");
207 if (parsed?.kind !== "files") return;
208​
209 expect(getDiffLineStat(parsed.files)).toEqual({ additions: 3, deletions: 2 });
210 });
211});