Repository navigation
Expand file tree
/
Copy pathdiffRendering.test.ts
More file actions
407 lines (378 loc) · 13.6 KB
/
Copy pathdiffRendering.test.ts
File metadata and controls
407 lines (378 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import { hydratePartialDiff } from "@pierre/diffs";
import { describe, expect, it } from "vite-plus/test";
import { resolveDiffReviewPosition } from "../reviewCommentContext";
import {
buildFileDiffContentVersion,
buildFileDiffIdentityKey,
buildFileDiffRenderKey,
getDiffLineStat,
getRenderablePatch,
resolveFileDiffPath,
resolveFileDiffPreviousPath,
} from "./diffRendering";
describe("getRenderablePatch", () => {
it("hides indentation changes around inserted JSX without moving review lines", () => {
const patch = [
"diff --git a/item.tsx b/item.tsx",
"--- a/item.tsx",
"+++ b/item.tsx",
"@@ -40,5 +40,7 @@",
' <ItemContent className="min-w-0">',
"- <ItemTitle>",
'- <h4 className="wrap-break-word">{name}</h4>',
"- </ItemTitle>",
"+ {showName && (",
"+ <ItemTitle>",
'+ <h4 className="wrap-break-word">{name}</h4>',
"+ </ItemTitle>",
"+ )}",
" </ItemContent>",
"@@ -80 +82 @@",
"-const value = 1;",
"+const value = 2;",
].join("\n");
const shown = getRenderablePatch(patch, "pr", { compactPartialHunkOffsets: true });
const hidden = getRenderablePatch(patch, "pr", {
compactPartialHunkOffsets: true,
ignoreWhitespace: true,
});
expect(shown?.kind).toBe("files");
expect(hidden?.kind).toBe("files");
if (shown?.kind !== "files" || hidden?.kind !== "files") return;
expect(getDiffLineStat(shown.files)).toEqual({ additions: 6, deletions: 4 });
expect(getDiffLineStat(hidden.files)).toEqual({ additions: 3, deletions: 1 });
const file = hidden.files[0]!;
expect(file.additionLines).toEqual(shown.files[0]!.additionLines);
expect(file.deletionLines).toEqual(shown.files[0]!.deletionLines);
expect(file.cacheKey).not.toBe(shown.files[0]!.cacheKey);
expect(resolveDiffReviewPosition(hidden.sourceFiles[0]!, 43, "additions")).toEqual({
kind: "added",
newLine: 43,
});
expect(resolveDiffReviewPosition(hidden.sourceFiles[0]!, 42, "deletions")).toEqual({
kind: "deleted",
oldLine: 42,
});
expect(file.hunks[0]?.hunkContent).toContainEqual({
type: "context",
lines: 3,
additionLineIndex: 2,
deletionLineIndex: 1,
});
expect(resolveDiffReviewPosition(file, 41, "additions")).toEqual({
kind: "added",
newLine: 41,
});
expect(resolveDiffReviewPosition(file, 43, "additions")).toEqual({
kind: "context",
oldLine: 42,
newLine: 43,
side: "right",
});
expect(resolveDiffReviewPosition(file, 42, "deletions")).toEqual({
kind: "context",
oldLine: 42,
newLine: 43,
side: "left",
});
expect(file.hunks[1]).toMatchObject({
additionStart: 82,
deletionStart: 80,
splitLineStart: 7,
unifiedLineStart: 7,
});
const prefix = "unchanged\n".repeat(39);
const gap = "unchanged\n".repeat(35);
const hydrated = hydratePartialDiff("clone", file, {
oldFile: {
name: file.name,
contents: prefix + file.deletionLines.slice(0, 5).join("") + gap + "const value = 1;\n",
},
newFile: {
name: file.name,
contents: prefix + file.additionLines.slice(0, 7).join("") + gap + "const value = 2;\n",
},
});
expect(getDiffLineStat([hydrated])).toEqual({ additions: 3, deletions: 1 });
expect(resolveDiffReviewPosition(hydrated, 43, "additions")).toEqual(
resolveDiffReviewPosition(file, 43, "additions"),
);
});
it.each([
[" const x = 1;\t", "\tconst x=1;", 0, 0],
["const x = 1;", "const x = 2;", 1, 1],
["const x = 1;", "const x = 1;\n", 1, 0],
])("filters whitespace in %j to %j", (before, after, additions, deletions) => {
const patch = [
"diff --git a/example.ts b/example.ts",
"--- a/example.ts",
"+++ b/example.ts",
`@@ -1 +1,${after.split("\n").length} @@`,
`-${before}`,
...after.split("\n").map((line) => `+${line}`),
"",
].join("\n");
const filtered = getRenderablePatch(patch, "pr", { ignoreWhitespace: true });
expect(filtered?.kind).toBe("files");
if (filtered?.kind !== "files") return;
expect(getDiffLineStat(filtered.files)).toEqual({ additions, deletions });
});
it.each(["+", "-"])("keeps %s blank lines without a final newline", (sign) => {
const parsed = getRenderablePatch(
[
"diff --git a/blank.txt b/blank.txt",
"--- a/blank.txt",
"+++ b/blank.txt",
sign === "+" ? "@@ -0,0 +1 @@" : "@@ -1 +0,0 @@",
`${sign} `,
"\\ No newline at end of file",
].join("\n"),
"pr",
{ ignoreWhitespace: true },
);
expect(parsed?.kind).toBe("files");
if (parsed?.kind !== "files") return;
expect(getDiffLineStat(parsed.files)).toEqual({
additions: sign === "+" ? 1 : 0,
deletions: sign === "-" ? 1 : 0,
});
});
it.each([
["a/example.ts", "a/example.ts", "change"],
["b/example.ts", "b/example.ts", "change"],
["a/before.ts", "b/after.ts", "rename-changed"],
])("preserves repository paths from %s to %s", (previousPath, path, type) => {
const parsed = getRenderablePatch(
[
`diff --git a/${previousPath} b/${path}`,
...(previousPath === path
? []
: ["similarity index 50%", `rename from ${previousPath}`, `rename to ${path}`]),
`--- a/${previousPath}`,
`+++ b/${path}`,
"@@ -1 +1 @@",
"-before",
"+after",
].join("\n"),
);
expect(parsed?.kind).toBe("files");
if (parsed?.kind !== "files") return;
const file = parsed.files[0];
expect(file).toBeDefined();
if (!file) return;
expect(resolveFileDiffPath(file)).toBe(path);
expect(resolveFileDiffPreviousPath(file)).toBe(previousPath);
expect(buildFileDiffIdentityKey(file)).toBe(`${previousPath}\0${path}\0${type}`);
});
it("compacts partial hunk render offsets for virtualized review diffs", () => {
const patch = [
"diff --git a/example.ts b/example.ts",
"index 1111111..2222222 100644",
"--- a/example.ts",
"+++ b/example.ts",
"@@ -48,4 +48,4 @@",
" context",
"-before",
"+after",
" context",
" context",
"@@ -80,3 +80,4 @@",
" context",
"+added",
" context",
" context",
].join("\n");
const parsed = getRenderablePatch(patch, "review", {
compactPartialHunkOffsets: true,
});
expect(parsed?.kind).toBe("files");
if (parsed?.kind !== "files") return;
const file = parsed.files[0];
expect(file?.hunks[0]?.collapsedBefore).toBe(47);
expect(file?.hunks[0]?.unifiedLineStart).toBe(0);
expect(file?.hunks[1]?.collapsedBefore).toBeGreaterThan(0);
expect(file?.hunks[1]?.unifiedLineStart).toBe(file?.hunks[0]?.unifiedLineCount);
expect(file?.unifiedLineCount).toBe(
file?.hunks.reduce((total, hunk) => total + hunk.unifiedLineCount, 0),
);
});
it("retains source-file offsets for checkpoint diffs", () => {
const patch = [
"diff --git a/example.ts b/example.ts",
"--- a/example.ts",
"+++ b/example.ts",
"@@ -48,1 +48,1 @@",
"-before",
"+after",
].join("\n");
const parsed = getRenderablePatch(patch, "checkpoint");
expect(parsed?.kind).toBe("files");
if (parsed?.kind !== "files") return;
expect(parsed.files[0]?.hunks[0]?.unifiedLineStart).toBe(47);
});
it("keeps whitespace at the end of the last hunk", () => {
const patch = (lastLines: string[]) =>
[
"diff --git a/example.ts b/example.ts",
"--- a/example.ts",
"+++ b/example.ts",
...lastLines,
"",
].join("\n");
const trailing = getRenderablePatch(patch(["@@ -1 +1 @@", "-a", "+ b \t"]), "review");
expect(trailing?.kind).toBe("files");
if (trailing?.kind !== "files") return;
expect(trailing.files[0]?.additionLines).toEqual([" b \t\n"]);
const blankContext = getRenderablePatch(patch(["@@ -1,2 +1,2 @@", "-a", "+b", " "]), "review");
expect(blankContext?.kind).toBe("files");
if (blankContext?.kind !== "files") return;
expect(blankContext.files[0]?.additionLines).toEqual(["b\n", "\n"]);
expect(blankContext.files[0]?.hunks[0]?.unifiedLineCount).toBe(3);
expect(getRenderablePatch(" \n\t\n", "review")).toBeNull();
});
it("caches patches that differ only in trailing whitespace separately", () => {
const patch = (added: string) =>
[
"diff --git a/example.ts b/example.ts",
"--- a/example.ts",
"+++ b/example.ts",
"@@ -1 +1 @@",
"-a",
`+${added}`,
"",
].join("\n");
const plain = getRenderablePatch(patch("b"), "review");
const tabbed = getRenderablePatch(patch("b\t"), "review");
expect(plain?.kind).toBe("files");
expect(tabbed?.kind).toBe("files");
if (plain?.kind !== "files" || tabbed?.kind !== "files") return;
expect(tabbed.files[0]?.cacheKey).not.toBe(plain.files[0]?.cacheKey);
});
});
describe("diff file reconciliation", () => {
it("keeps Pierre's render key stable when a partial diff hydrates", () => {
const patch = [
"diff --git a/example.ts b/example.ts",
"--- a/example.ts",
"+++ b/example.ts",
"@@ -1 +1 @@",
"-before",
"+after",
].join("\n");
const parsed = getRenderablePatch(patch, "hydrated-key");
expect(parsed?.kind).toBe("files");
if (parsed?.kind !== "files") return;
const file = parsed.files[0];
expect(file).toBeDefined();
if (!file) return;
const key = buildFileDiffRenderKey(file);
file.cacheKey = `${file.cacheKey}:hydrated`;
expect(buildFileDiffRenderKey(file)).toBe(key);
});
it("gives a type change its own identity per block", () => {
const patch = [
"diff --git a/AGENTS.md b/AGENTS.md",
"deleted file mode 100644",
"--- a/AGENTS.md",
"+++ /dev/null",
"@@ -1 +0,0 @@",
"-duplicated instructions",
"diff --git a/AGENTS.md b/AGENTS.md",
"new file mode 120000",
"--- /dev/null",
"+++ b/AGENTS.md",
"@@ -0,0 +1 @@",
"+CLAUDE.md",
].join("\n");
const parsed = getRenderablePatch(patch, "type-change");
expect(parsed?.kind).toBe("files");
if (parsed?.kind !== "files") return;
const [deleted, added] = parsed.files;
expect(deleted?.type).toBe("deleted");
expect(added?.type).toBe("new");
if (!deleted || !added) return;
expect(buildFileDiffIdentityKey(deleted)).not.toBe(buildFileDiffIdentityKey(added));
expect(new Set(parsed.files.map(buildFileDiffIdentityKey)).size).toBe(parsed.files.length);
});
it("keeps identities stable and versions local to the changed file", () => {
const patch = (secondLine: string) =>
[
"diff --git a/unchanged.ts b/unchanged.ts",
"--- a/unchanged.ts",
"+++ b/unchanged.ts",
"@@ -1 +1 @@",
"-before",
"+after",
"diff --git a/changed.ts b/changed.ts",
"--- a/changed.ts",
"+++ b/changed.ts",
"@@ -1 +1 @@",
"-old",
`+${secondLine}`,
].join("\n");
const before = getRenderablePatch(patch("new"), "before");
const after = getRenderablePatch(patch("newer"), "after");
expect(before?.kind).toBe("files");
expect(after?.kind).toBe("files");
if (before?.kind !== "files" || after?.kind !== "files") return;
const [beforeUnchanged, beforeChanged] = before.files;
const [afterUnchanged, afterChanged] = after.files;
expect(beforeUnchanged).toBeDefined();
expect(beforeChanged).toBeDefined();
expect(afterUnchanged).toBeDefined();
expect(afterChanged).toBeDefined();
if (!beforeUnchanged || !beforeChanged || !afterUnchanged || !afterChanged) return;
expect(buildFileDiffIdentityKey(afterUnchanged)).toBe(
buildFileDiffIdentityKey(beforeUnchanged),
);
expect(buildFileDiffIdentityKey(afterChanged)).toBe(buildFileDiffIdentityKey(beforeChanged));
expect(buildFileDiffContentVersion(afterUnchanged)).toBe(
buildFileDiffContentVersion(beforeUnchanged),
);
expect(buildFileDiffContentVersion(afterChanged)).not.toBe(
buildFileDiffContentVersion(beforeChanged),
);
});
});
describe("a file whose name a patch header cannot carry plainly", () => {
const quotedPatch = (written: string) =>
[
`diff --git "a/${written}" "b/${written}"`,
"index 1111111..2222222 100644",
`--- "a/${written}"`,
`+++ "b/${written}"`,
"@@ -1 +1 @@",
"-before",
"+after",
"",
].join("\n");
const pathOf = (patch: string) => {
const parsed = getRenderablePatch(patch, "review");
expect(parsed?.kind).toBe("files");
if (parsed?.kind !== "files") throw new Error("patch did not parse as files");
const file = parsed.files[0];
expect(file).toBeDefined();
if (!file) throw new Error("patch carried no file");
return resolveFileDiffPath(file);
};
it("reads the octal a host with core.quotePath on writes for a name outside ASCII", () => {
expect(pathOf(quotedPatch("caf\\303\\251/r\\303\\251sum\\303\\251.ts"))).toBe("café/résumé.ts");
});
it("reads both sides of a rename under the names they really have", () => {
const patch = [
'diff --git "a/old\\tname.ts" "b/new\\tname.ts"',
"similarity index 90%",
'rename from "old\\tname.ts"',
'rename to "new\\tname.ts"',
"",
].join("\n");
const parsed = getRenderablePatch(patch, "review");
expect(parsed?.kind).toBe("files");
if (parsed?.kind !== "files") return;
const file = parsed.files[0];
expect(file).toBeDefined();
if (!file) return;
expect(resolveFileDiffPath(file)).toBe("new\tname.ts");
expect(resolveFileDiffPreviousPath(file)).toBe("old\tname.ts");
});
});