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

diffRendering.test.ts

143 lines4.2 KB

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