Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { applyReviewDiffMetadata, buildReviewParsedDiff } from "./reviewModel";
4
5describe("buildReviewParsedDiff", () => {
6 it.each([
7 ["a/example.ts", "a/example.ts"],
8 ["a/before.ts", "b/after.ts"],
9 ])("preserves repository paths from %s to %s", (previousPath, path) => {
10 const parsed = buildReviewParsedDiff(
11 [
12 `diff --git a/${previousPath} b/${path}`,
13 ...(previousPath === path
14 ? []
15 : ["similarity index 50%", `rename from ${previousPath}`, `rename to ${path}`]),
16 `--- a/${previousPath}`,
17 `+++ b/${path}`,
18 "@@ -1 +1 @@",
19 "-before",
20 "+after",
21 ].join("\n"),
22 "repository-paths",
23 );
24 expect(parsed.kind).toBe("files");
25 if (parsed.kind !== "files") return;
26 expect(parsed.files[0]).toMatchObject({
27 path,
28 previousPath: previousPath === path ? null : previousPath,
29 additions: 1,
30 deletions: 1,
31 });
32 });
33
34 it("builds renderable rows from a unified patch", () => {
35 const parsed = buildReviewParsedDiff(
36 [
37 "diff --git a/apps/mobile/src/a.ts b/apps/mobile/src/a.ts",
38 "index 1111111..2222222 100644",
39 "--- a/apps/mobile/src/a.ts",
40 "+++ b/apps/mobile/src/a.ts",
41 "@@ -1,2 +1,3 @@",
42 "-const before = 1;",
43 "+const after = 2;",
44 "+console.log(after);",
45 " return true;",
46 ].join("\n"),
47 "unit",
48 );
49
50 expect(parsed.kind).toBe("files");
51 if (parsed.kind !== "files") {
52 return;
53 }
54
55 expect(parsed.fileCount).toBe(1);
56 expect(parsed.additions).toBe(2);
57 expect(parsed.deletions).toBe(1);
58 expect(parsed.files[0]).toMatchObject({
59 path: "apps/mobile/src/a.ts",
60 additions: 2,
61 deletions: 1,
62 });
63 expect(parsed.files[0]?.rows).toEqual([
64 expect.objectContaining({ kind: "hunk", header: "@@ -1,2 +1,3 @@" }),
65 expect.objectContaining({
66 kind: "line",
67 change: "delete",
68 oldLineNumber: 1,
69 newLineNumber: null,
70 content: "const before = 1;",
71 comparison: { change: "add", tokenIndex: 0 },
72 }),
73 expect.objectContaining({
74 kind: "line",
75 change: "add",
76 oldLineNumber: null,
77 newLineNumber: 1,
78 content: "const after = 2;",
79 comparison: { change: "delete", tokenIndex: 0 },
80 }),
81 expect.objectContaining({
82 kind: "line",
83 change: "add",
84 oldLineNumber: null,
85 newLineNumber: 2,
86 content: "console.log(after);",
87 comparison: null,
88 }),
89 expect.objectContaining({
90 kind: "line",
91 change: "context",
92 oldLineNumber: 2,
93 newLineNumber: 3,
94 content: "return true;",
95 comparison: null,
96 }),
97 ]);
98 });
99
100 it("treats truncated patches as partial diffs instead of failing", () => {
101 const parsed = buildReviewParsedDiff(
102 [
103 "diff --git a/apps/mobile/src/a.ts b/apps/mobile/src/a.ts",
104 "index 1111111..2222222 100644",
105 "--- a/apps/mobile/src/a.ts",
106 "+++ b/apps/mobile/src/a.ts",
107 "@@ -1 +1,2 @@",
108 " const before = 1;",
109 "+const after = 2;",
110 "",
111 "[truncated]",
112 ].join("\n"),
113 "unit-truncated",
114 );
115
116 expect(parsed.kind).toBe("files");
117 if (parsed.kind !== "files") {
118 return;
119 }
120
121 expect(parsed.notice).toContain("server size cap");
122 expect(parsed.fileCount).toBe(1);
123 expect(parsed.files[0]?.rows[0]).toMatchObject({
124 kind: "hunk",
125 header: "@@ -1,1 +1,2 @@",
126 });
127 });
128});
129
130describe("applyReviewDiffMetadata", () => {
131 it("uses complete counts even when the preview contains only part of one file", () => {
132 const parsed = buildReviewParsedDiff(
133 [
134 "diff --git a/large.txt b/large.txt",
135 "--- a/large.txt",
136 "+++ b/large.txt",
137 "@@ -1 +1 @@",
138 "-before",
139 "+after",
140 ].join("\n"),
141 "partial",
142 );
143 const result = applyReviewDiffMetadata(parsed, {
144 truncated: true,
145 files: [
146 { path: "large.txt", previousPath: null, additions: 4000, deletions: 3000 },
147 { path: "unseen.txt", previousPath: null, additions: 100, deletions: 20 },
148 ],
149 });
150 expect(result.kind).toBe("files");
151 if (result.kind !== "files") return;
152 expect(result.fileCount).toBe(2);
153 expect(result.additions).toBe(4100);
154 expect(result.deletions).toBe(3020);
155 expect(result.files[0]?.additions).toBe(4000);
156 expect(result.notice).toContain("Counts include all changes");
157 expect(applyReviewDiffMetadata(parsed, null)).toEqual(parsed);
158 });
159});