Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 buildFileDiffRenderKey,
4 buildPatchCacheKey,
5 getDiffLineStat,
6 getRenderablePatch,
7} from "./diffRendering";
8
9describe("buildPatchCacheKey", () => {
10 it("normalizes outer whitespace before hashing", () => {
11 const patch = "diff --git a/a.ts b/a.ts\n+console.log('hello')";
12
13 expect(buildPatchCacheKey(`\n${patch}\n`)).toBe(buildPatchCacheKey(patch));
14 });
15
16 it("changes when diff content changes", () => {
17 const before = "diff --git a/a.ts b/a.ts\n+console.log('hello')";
18 const after = "diff --git a/a.ts b/a.ts\n+console.log('hello world')";
19
20 expect(buildPatchCacheKey(before)).not.toBe(buildPatchCacheKey(after));
21 });
22
23 it("changes when cache scope changes", () => {
24 const patch = "diff --git a/a.ts b/a.ts\n+console.log('hello')";
25
26 expect(buildPatchCacheKey(patch, "diff-panel:light")).not.toBe(
27 buildPatchCacheKey(patch, "diff-panel:dark"),
28 );
29 });
30});
31
32describe("getRenderablePatch", () => {
33 it("compacts partial hunk render offsets for virtualized review diffs", () => {
34 const patch = [
35 "diff --git a/example.ts b/example.ts",
36 "index 1111111..2222222 100644",
37 "--- a/example.ts",
38 "+++ b/example.ts",
39 "@@ -48,4 +48,4 @@",
40 " context",
41 "-before",
42 "+after",
43 " context",
44 " context",
45 "@@ -80,3 +80,4 @@",
46 " context",
47 "+added",
48 " context",
49 " context",
50 ].join("\n");
51
52 const parsed = getRenderablePatch(patch, "review", {
53 compactPartialHunkOffsets: true,
54 });
55 expect(parsed?.kind).toBe("files");
56 if (parsed?.kind !== "files") return;
57
58 const file = parsed.files[0];
59 expect(file?.hunks[0]?.collapsedBefore).toBe(47);
60 expect(file?.hunks[0]?.unifiedLineStart).toBe(0);
61 expect(file?.hunks[1]?.collapsedBefore).toBeGreaterThan(0);
62 expect(file?.hunks[1]?.unifiedLineStart).toBe(file?.hunks[0]?.unifiedLineCount);
63 expect(file?.unifiedLineCount).toBe(
64 file?.hunks.reduce((total, hunk) => total + hunk.unifiedLineCount, 0),
65 );
66 });
67
68 it("retains source-file offsets for checkpoint diffs", () => {
69 const patch = [
70 "diff --git a/example.ts b/example.ts",
71 "--- a/example.ts",
72 "+++ b/example.ts",
73 "@@ -48,1 +48,1 @@",
74 "-before",
75 "+after",
76 ].join("\n");
77
78 const parsed = getRenderablePatch(patch, "checkpoint");
79 expect(parsed?.kind).toBe("files");
80 if (parsed?.kind !== "files") return;
81 expect(parsed.files[0]?.hunks[0]?.unifiedLineStart).toBe(47);
82 });
83});
84
85describe("buildFileDiffRenderKey", () => {
86 it("keeps file identity stable when Pierre hydrates a partial diff", () => {
87 const patch = [
88 "diff --git a/example.ts b/example.ts",
89 "--- a/example.ts",
90 "+++ b/example.ts",
91 "@@ -1 +1 @@",
92 "-before",
93 "+after",
94 ].join("\n");
95 const parsed = getRenderablePatch(patch, "hydrated-key");
96 expect(parsed?.kind).toBe("files");
97 if (parsed?.kind !== "files") return;
98
99 const file = parsed.files[0];
100 expect(file).toBeDefined();
101 if (!file) return;
102 const key = buildFileDiffRenderKey(file);
103 file.cacheKey = `${file.cacheKey}:hydrated`;
104
105 expect(buildFileDiffRenderKey(file)).toBe(key);
106 });
107});
108
109describe("getDiffLineStat", () => {
110 it("totals additions and deletions across every file and hunk", () => {
111 const patch = [
112 "diff --git a/example.ts b/example.ts",
113 "--- a/example.ts",
114 "+++ b/example.ts",
115 "@@ -1,2 +1,3 @@",
116 "-before",
117 "+after",
118 "+added",
119 " context",
120 "@@ -10,2 +11,1 @@",
121 "-removed",
122 " context",
123 "diff --git a/README.md b/README.md",
124 "--- a/README.md",
125 "+++ b/README.md",
126 "@@ -1 +1,2 @@",
127 " title",
128 "+description",
129 ].join("\n");
130
131 const parsed = getRenderablePatch(patch);
132 expect(parsed?.kind).toBe("files");
133 if (parsed?.kind !== "files") return;
134
135 expect(getDiffLineStat(parsed.files)).toEqual({ additions: 3, deletions: 2 });
136 });
137});