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

reviewModel.test.ts

276 lines9.2 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { applyReviewDiffMetadata, buildReviewParsedDiff } from "./reviewModel";
4​
5describe("buildReviewParsedDiff", () => {
6 it("keeps UI identities compact, stable, and isolated from parser cache keys", () => {
7 const patch =
8 "diff --git a/a.ts b/a.ts\n--- a/a.ts\n+++ b/a.ts\n@@ -1 +1 @@\n-before\n+after\n";
9 const scope = JSON.stringify({ target: 'remote\\"scope'.repeat(100) });
10 const parsed = buildReviewParsedDiff(patch + patch, scope);
11 const repeated = buildReviewParsedDiff(patch + patch, scope);
12 const otherSection = buildReviewParsedDiff(patch + patch, `${scope}:other`);
13 const changed = buildReviewParsedDiff((patch + patch).replaceAll("after", "changed"), scope);
14 if (
15 parsed.kind !== "files" ||
16 repeated.kind !== "files" ||
17 otherSection.kind !== "files" ||
18 changed.kind !== "files"
19 )
20 throw new Error("Expected structured diffs");
21​
22 expect(parsed.files).toHaveLength(2);
23 expect(parsed.files.map((file) => file.id)).toEqual(repeated.files.map((file) => file.id));
24 expect(parsed.files.map((file) => file.cacheKey)).toEqual(
25 repeated.files.map((file) => file.cacheKey),
26 );
27 const ids = parsed.files.flatMap((file) => [file.id, ...file.rows.map((row) => row.id)]);
28 expect(new Set(ids).size).toBe(ids.length);
29 for (const id of ids) expect(id.length).toBeLessThan(48);
30 for (const variant of [otherSection, changed]) {
31 const otherIds = variant.files.flatMap((file) => [
32 file.id,
33 ...file.rows.map((row) => row.id),
34 ]);
35 expect(otherIds.some((id) => ids.includes(id))).toBe(false);
36 }
37 expect(parsed.files[0]!.cacheKey.length).toBeGreaterThan(scope.length);
38 expect(parsed.files[0]!.id).not.toBe(parsed.files[0]!.cacheKey);
39 expect(repeated.files[0]!.rows.map((row) => row.id)).toEqual(
40 parsed.files[0]!.rows.map((row) => row.id),
41 );
42 });
43​
44 it.each(["", "\n\n[truncated]"])(
45 "preserves the final blank context in the resourceTelemetry excerpt with suffix %j",
46 (suffix) => {
47 const parsed = buildReviewParsedDiff(
48 [
49 "diff --git a/packages/contracts/src/resourceTelemetry.ts b/packages/contracts/src/resourceTelemetry.ts",
50 "--- a/packages/contracts/src/resourceTelemetry.ts",
51 "+++ b/packages/contracts/src/resourceTelemetry.ts",
52 "@@ -350,6 +371,7 @@",
53 " DesktopTelemetryRequestDesktopUpdate,",
54 " DesktopTelemetryCommitDesktopUpdate,",
55 " DesktopTelemetryCancelDesktopUpdate,",
56 "+ DesktopUpdateRestartPolicyResult,",
57 " ]);",
58 " export type DesktopTelemetryControlMessage = typeof DesktopTelemetryControlMessage.Type;",
59 " ",
60 "",
61 ].join("\n") + suffix,
62 "resource-telemetry",
63 );
64 expect(parsed.kind).toBe("files");
65 if (parsed.kind !== "files") return;
66 expect(parsed.files[0]?.rows.at(-1)).toMatchObject({
67 kind: "line",
68 change: "context",
69 oldLineNumber: 355,
70 newLineNumber: 377,
71 content: "",
72 });
73 expect(parsed.files[0]?.additionLines.at(-1)).toBe("\n");
74 expect(parsed.files[0]?.deletionLines.at(-1)).toBe("\n");
75 },
76 );
77​
78 it("preserves final content whitespace and gives distinct patches distinct cache keys", () => {
79 const patch = "diff --git a/a.ts b/a.ts\n--- a/a.ts\n+++ b/a.ts\n@@ -0,0 +1 @@\n+value \t\n";
80 const parsed = buildReviewParsedDiff(patch, "whitespace");
81 const withoutWhitespace = buildReviewParsedDiff(
82 patch.replace("value \t", "value"),
83 "whitespace",
84 );
85 expect(parsed.kind).toBe("files");
86 expect(withoutWhitespace.kind).toBe("files");
87 if (parsed.kind !== "files" || withoutWhitespace.kind !== "files") return;
88 expect(parsed.files[0]?.rows.at(-1)).toMatchObject({ content: "value \t" });
89 expect(parsed.files[0]?.additionLines).toEqual(["value \t\n"]);
90 expect(parsed.files[0]?.cacheKey).not.toBe(withoutWhitespace.files[0]?.cacheKey);
91 });
92​
93 it.each([
94 [true, false],
95 [false, true],
96 [true, true],
97 [false, false],
98 ])("preserves missing-newline flags for old=%s and new=%s", (oldMissing, newMissing) => {
99 const parsed = buildReviewParsedDiff(
100 [
101 "diff --git a/a.ts b/a.ts",
102 "--- a/a.ts",
103 "+++ b/a.ts",
104 "@@ -1 +1 @@",
105 "-value",
106 ...(oldMissing ? ["\\ No newline at end of file"] : []),
107 "+value",
108 ...(newMissing ? ["\\ No newline at end of file"] : []),
109 "",
110 ].join("\n"),
111 "eof",
112 );
113 expect(parsed.kind).toBe("files");
114 if (parsed.kind !== "files") return;
115 expect(parsed.files[0]).toMatchObject({
116 noEOFCRDeletions: oldMissing,
117 noEOFCRAdditions: newMissing,
118 deletionLines: [oldMissing ? "value" : "value\n"],
119 additionLines: [newMissing ? "value" : "value\n"],
120 });
121 });
122​
123 it.each([
124 ["a/example.ts", "a/example.ts"],
125 ["a/before.ts", "b/after.ts"],
126 ])("preserves repository paths from %s to %s", (previousPath, path) => {
127 const parsed = buildReviewParsedDiff(
128 [
129 `diff --git a/${previousPath} b/${path}`,
130 ...(previousPath === path
131 ? []
132 : ["similarity index 50%", `rename from ${previousPath}`, `rename to ${path}`]),
133 `--- a/${previousPath}`,
134 `+++ b/${path}`,
135 "@@ -1 +1 @@",
136 "-before",
137 "+after",
138 ].join("\n"),
139 "repository-paths",
140 );
141 expect(parsed.kind).toBe("files");
142 if (parsed.kind !== "files") return;
143 expect(parsed.files[0]).toMatchObject({
144 path,
145 previousPath: previousPath === path ? null : previousPath,
146 additions: 1,
147 deletions: 1,
148 });
149 });
150​
151 it("builds renderable rows from a unified patch", () => {
152 const parsed = buildReviewParsedDiff(
153 [
154 "diff --git a/apps/mobile/src/a.ts b/apps/mobile/src/a.ts",
155 "index 1111111..2222222 100644",
156 "--- a/apps/mobile/src/a.ts",
157 "+++ b/apps/mobile/src/a.ts",
158 "@@ -1,2 +1,3 @@",
159 "-const before = 1;",
160 "+const after = 2;",
161 "+console.log(after);",
162 " return true;",
163 ].join("\n"),
164 "unit",
165 );
166​
167 expect(parsed.kind).toBe("files");
168 if (parsed.kind !== "files") {
169 return;
170 }
171​
172 expect(parsed.fileCount).toBe(1);
173 expect(parsed.additions).toBe(2);
174 expect(parsed.deletions).toBe(1);
175 expect(parsed.files[0]).toMatchObject({
176 path: "apps/mobile/src/a.ts",
177 additions: 2,
178 deletions: 1,
179 });
180 expect(parsed.files[0]?.rows).toEqual([
181 expect.objectContaining({ kind: "hunk", header: "@@ -1,2 +1,3 @@" }),
182 expect.objectContaining({
183 kind: "line",
184 change: "delete",
185 oldLineNumber: 1,
186 newLineNumber: null,
187 content: "const before = 1;",
188 comparison: { change: "add", tokenIndex: 0 },
189 }),
190 expect.objectContaining({
191 kind: "line",
192 change: "add",
193 oldLineNumber: null,
194 newLineNumber: 1,
195 content: "const after = 2;",
196 comparison: { change: "delete", tokenIndex: 0 },
197 }),
198 expect.objectContaining({
199 kind: "line",
200 change: "add",
201 oldLineNumber: null,
202 newLineNumber: 2,
203 content: "console.log(after);",
204 comparison: null,
205 }),
206 expect.objectContaining({
207 kind: "line",
208 change: "context",
209 oldLineNumber: 2,
210 newLineNumber: 3,
211 content: "return true;",
212 comparison: null,
213 }),
214 ]);
215 });
216​
217 it("treats truncated patches as partial diffs instead of failing", () => {
218 const parsed = buildReviewParsedDiff(
219 [
220 "diff --git a/apps/mobile/src/a.ts b/apps/mobile/src/a.ts",
221 "index 1111111..2222222 100644",
222 "--- a/apps/mobile/src/a.ts",
223 "+++ b/apps/mobile/src/a.ts",
224 "@@ -1 +1,2 @@",
225 " const before = 1;",
226 "+const after = 2;",
227 "",
228 "[truncated]",
229 ].join("\n"),
230 "unit-truncated",
231 );
232​
233 expect(parsed.kind).toBe("files");
234 if (parsed.kind !== "files") {
235 return;
236 }
237​
238 expect(parsed.notice).toContain("server size cap");
239 expect(parsed.fileCount).toBe(1);
240 expect(parsed.files[0]?.rows[0]).toMatchObject({
241 kind: "hunk",
242 header: "@@ -1,1 +1,2 @@",
243 });
244 });
245});
246​
247describe("applyReviewDiffMetadata", () => {
248 it("uses complete counts even when the preview contains only part of one file", () => {
249 const parsed = buildReviewParsedDiff(
250 [
251 "diff --git a/large.txt b/large.txt",
252 "--- a/large.txt",
253 "+++ b/large.txt",
254 "@@ -1 +1 @@",
255 "-before",
256 "+after",
257 ].join("\n"),
258 "partial",
259 );
260 const result = applyReviewDiffMetadata(parsed, {
261 truncated: true,
262 files: [
263 { path: "large.txt", previousPath: null, additions: 4000, deletions: 3000 },
264 { path: "unseen.txt", previousPath: null, additions: 100, deletions: 20 },
265 ],
266 });
267 expect(result.kind).toBe("files");
268 if (result.kind !== "files") return;
269 expect(result.fileCount).toBe(2);
270 expect(result.additions).toBe(4100);
271 expect(result.deletions).toBe(3020);
272 expect(result.files[0]?.additions).toBe(4000);
273 expect(result.notice).toContain("Counts include all changes");
274 expect(applyReviewDiffMetadata(parsed, null)).toEqual(parsed);
275 });
276});