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