The best way to control Claude Code, Codex and more from any computer or phone

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