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} from "./diffRendering";
10
11describe("buildPatchCacheKey", () => {
12 it("normalizes outer whitespace before hashing", () => {
13 const patch = "diff --git a/a.ts b/a.ts\n+console.log('hello')";
14
15 expect(buildPatchCacheKey(`\n${patch}\n`)).toBe(buildPatchCacheKey(patch));
16 });
17
18 it("changes when diff content changes", () => {
19 const before = "diff --git a/a.ts b/a.ts\n+console.log('hello')";
20 const after = "diff --git a/a.ts b/a.ts\n+console.log('hello world')";
21
22 expect(buildPatchCacheKey(before)).not.toBe(buildPatchCacheKey(after));
23 });
24
25 it("changes when cache scope changes", () => {
26 const patch = "diff --git a/a.ts b/a.ts\n+console.log('hello')";
27
28 expect(buildPatchCacheKey(patch, "diff-panel:light")).not.toBe(
29 buildPatchCacheKey(patch, "diff-panel:dark"),
30 );
31 });
32});
33
34describe("getRenderablePatch", () => {
35 it("compacts partial hunk render offsets for virtualized review diffs", () => {
36 const patch = [
37 "diff --git a/example.ts b/example.ts",
38 "index 1111111..2222222 100644",
39 "--- a/example.ts",
40 "+++ b/example.ts",
41 "@@ -48,4 +48,4 @@",
42 " context",
43 "-before",
44 "+after",
45 " context",
46 " context",
47 "@@ -80,3 +80,4 @@",
48 " context",
49 "+added",
50 " context",
51 " context",
52 ].join("\n");
53
54 const parsed = getRenderablePatch(patch, "review", {
55 compactPartialHunkOffsets: true,
56 });
57 expect(parsed?.kind).toBe("files");
58 if (parsed?.kind !== "files") return;
59
60 const file = parsed.files[0];
61 expect(file?.hunks[0]?.collapsedBefore).toBe(47);
62 expect(file?.hunks[0]?.unifiedLineStart).toBe(0);
63 expect(file?.hunks[1]?.collapsedBefore).toBeGreaterThan(0);
64 expect(file?.hunks[1]?.unifiedLineStart).toBe(file?.hunks[0]?.unifiedLineCount);
65 expect(file?.unifiedLineCount).toBe(
66 file?.hunks.reduce((total, hunk) => total + hunk.unifiedLineCount, 0),
67 );
68 });
69
70 it("retains source-file offsets for checkpoint diffs", () => {
71 const patch = [
72 "diff --git a/example.ts b/example.ts",
73 "--- a/example.ts",
74 "+++ b/example.ts",
75 "@@ -48,1 +48,1 @@",
76 "-before",
77 "+after",
78 ].join("\n");
79
80 const parsed = getRenderablePatch(patch, "checkpoint");
81 expect(parsed?.kind).toBe("files");
82 if (parsed?.kind !== "files") return;
83 expect(parsed.files[0]?.hunks[0]?.unifiedLineStart).toBe(47);
84 });
85});
86
87describe("diff file reconciliation", () => {
88 it("keeps Pierre's render key stable when a partial diff hydrates", () => {
89 const patch = [
90 "diff --git a/example.ts b/example.ts",
91 "--- a/example.ts",
92 "+++ b/example.ts",
93 "@@ -1 +1 @@",
94 "-before",
95 "+after",
96 ].join("\n");
97 const parsed = getRenderablePatch(patch, "hydrated-key");
98 expect(parsed?.kind).toBe("files");
99 if (parsed?.kind !== "files") return;
100
101 const file = parsed.files[0];
102 expect(file).toBeDefined();
103 if (!file) return;
104 const key = buildFileDiffRenderKey(file);
105 file.cacheKey = `${file.cacheKey}:hydrated`;
106
107 expect(buildFileDiffRenderKey(file)).toBe(key);
108 });
109
110 it("keeps identities stable and versions local to the changed file", () => {
111 const patch = (secondLine: string) =>
112 [
113 "diff --git a/unchanged.ts b/unchanged.ts",
114 "--- a/unchanged.ts",
115 "+++ b/unchanged.ts",
116 "@@ -1 +1 @@",
117 "-before",
118 "+after",
119 "diff --git a/changed.ts b/changed.ts",
120 "--- a/changed.ts",
121 "+++ b/changed.ts",
122 "@@ -1 +1 @@",
123 "-old",
124 `+${secondLine}`,
125 ].join("\n");
126 const before = getRenderablePatch(patch("new"), "before");
127 const after = getRenderablePatch(patch("newer"), "after");
128 expect(before?.kind).toBe("files");
129 expect(after?.kind).toBe("files");
130 if (before?.kind !== "files" || after?.kind !== "files") return;
131
132 const [beforeUnchanged, beforeChanged] = before.files;
133 const [afterUnchanged, afterChanged] = after.files;
134 expect(beforeUnchanged).toBeDefined();
135 expect(beforeChanged).toBeDefined();
136 expect(afterUnchanged).toBeDefined();
137 expect(afterChanged).toBeDefined();
138 if (!beforeUnchanged || !beforeChanged || !afterUnchanged || !afterChanged) return;
139
140 expect(buildFileDiffIdentityKey(afterUnchanged)).toBe(
141 buildFileDiffIdentityKey(beforeUnchanged),
142 );
143 expect(buildFileDiffIdentityKey(afterChanged)).toBe(buildFileDiffIdentityKey(beforeChanged));
144 expect(buildFileDiffContentVersion(afterUnchanged)).toBe(
145 buildFileDiffContentVersion(beforeUnchanged),
146 );
147 expect(buildFileDiffContentVersion(afterChanged)).not.toBe(
148 buildFileDiffContentVersion(beforeChanged),
149 );
150 });
151});
152
153describe("getDiffLineStat", () => {
154 it("totals additions and deletions across every file and hunk", () => {
155 const patch = [
156 "diff --git a/example.ts b/example.ts",
157 "--- a/example.ts",
158 "+++ b/example.ts",
159 "@@ -1,2 +1,3 @@",
160 "-before",
161 "+after",
162 "+added",
163 " context",
164 "@@ -10,2 +11,1 @@",
165 "-removed",
166 " context",
167 "diff --git a/README.md b/README.md",
168 "--- a/README.md",
169 "+++ b/README.md",
170 "@@ -1 +1,2 @@",
171 " title",
172 "+description",
173 ].join("\n");
174
175 const parsed = getRenderablePatch(patch);
176 expect(parsed?.kind).toBe("files");
177 if (parsed?.kind !== "files") return;
178
179 expect(getDiffLineStat(parsed.files)).toEqual({ additions: 3, deletions: 2 });
180 });
181});