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

reviewModel.test.ts

330 lines9.2 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { MessageId, RunId, type ReviewDiffPreviewSource } from "@t3tools/contracts";
4import type { ThreadCheckpointSummary } from "@t3tools/client-runtime/state/thread-checkpoints";
5​
6import {
7 applyReviewDiffMetadata,
8 buildReviewParsedDiff,
9 buildReviewSectionItems,
10 getDefaultReviewSectionId,
11 getReviewFilePreviewState,
12 getReviewSectionIdForCheckpoint,
13 type ReviewRenderableFile,
14} from "./reviewModel";
15​
16function makeCheckpoint(
17 input: Partial<ThreadCheckpointSummary> &
18 Pick<ThreadCheckpointSummary, "runId" | "checkpointTurnCount" | "completedAt">,
19): ThreadCheckpointSummary {
20 return {
21 checkpointRef: `refs/t3/checkpoints/thread/${input.checkpointTurnCount}` as any,
22 status: "ready",
23 files: [],
24 assistantMessageId: MessageId.make(`msg-${input.checkpointTurnCount}`),
25 ...input,
26 };
28​
29function makeRenderableFile(
30 input: Partial<ReviewRenderableFile> & Pick<ReviewRenderableFile, "path">,
31): ReviewRenderableFile {
32 return {
33 id: input.path,
34 cacheKey: input.path,
35 previousPath: null,
36 changeType: "new",
37 additions: 0,
38 deletions: 0,
39 languageHint: null,
40 additionLines: [],
41 deletionLines: [],
42 rows: [],
43 ...input,
44 };
46​
47describe("buildReviewSectionItems", () => {
48 it("keeps one chip per checkpoint and appends git sources", () => {
49 const checkpoints = [
50 makeCheckpoint({
51 runId: RunId.make("run-1"),
52 checkpointTurnCount: 1,
53 completedAt: "2026-04-01T00:00:00.000Z",
54 }),
55 makeCheckpoint({
56 runId: RunId.make("run-2"),
57 checkpointTurnCount: 2,
58 completedAt: "2026-04-02T00:00:00.000Z",
59 }),
60 ];
61 const gitSections: ReviewDiffPreviewSource[] = [
62 {
63 id: "working-tree",
64 kind: "working-tree",
65 title: "Dirty worktree",
66 baseRef: "HEAD",
67 headRef: null,
68 diff: "diff --git a/a.ts b/a.ts",
69 diffHash: "hash-dirty",
70 truncated: false,
71 },
72 {
73 id: "branch-range",
74 kind: "branch-range",
75 title: "Against main",
76 baseRef: "main",
77 headRef: "feature",
78 diff: "diff --git a/a.ts b/a.ts",
79 diffHash: "hash-base",
80 truncated: false,
81 },
82 ];
83​
84 const loadedTurnId = getReviewSectionIdForCheckpoint(checkpoints[0]!);
85 const items = buildReviewSectionItems({
86 checkpoints,
87 gitSections,
88 turnDiffById: {
89 [loadedTurnId]: "diff --git a/loaded.ts b/loaded.ts",
90 },
91 loadingTurnIds: {
92 [getReviewSectionIdForCheckpoint(checkpoints[1]!)]: true,
93 },
94 loadingGitSections: false,
95 });
96​
97 expect(items.map((item) => item.id)).toEqual([
98 "turn:2",
99 "turn:1",
100 "git:working-tree",
101 "git:branch-range",
102 ]);
103 expect(items[0]).toMatchObject({ isLoading: true, diff: null });
104 expect(items[1]).toMatchObject({
105 isLoading: false,
106 diff: expect.stringContaining("loaded.ts"),
107 });
108 expect(getDefaultReviewSectionId(items)).toBe("turn:2");
109 });
110​
111 it("shows dirty worktree while git preview is loading", () => {
112 const items = buildReviewSectionItems({
113 checkpoints: [],
114 gitSections: [],
115 turnDiffById: {},
116 loadingTurnIds: {},
117 loadingGitSections: true,
118 });
119​
120 expect(items).toEqual([
121 expect.objectContaining({
122 id: "git:working-tree",
123 kind: "working-tree",
124 title: "Dirty worktree",
125 subtitle: "Tracked, staged, and untracked worktree changes",
126 diff: null,
127 isLoading: true,
128 }),
129 ]);
130 expect(getDefaultReviewSectionId(items)).toBe("git:working-tree");
131 });
132});
133​
134describe("buildReviewParsedDiff", () => {
135 it.each([
136 ["a/example.ts", "a/example.ts"],
137 ["b/example.ts", "b/example.ts"],
138 ["a/before.ts", "b/after.ts"],
139 ])("preserves repository paths from %s to %s", (previousPath, path) => {
140 const parsed = buildReviewParsedDiff(
141 [
142 `diff --git a/${previousPath} b/${path}`,
143 ...(previousPath === path
144 ? []
145 : ["similarity index 50%", `rename from ${previousPath}`, `rename to ${path}`]),
146 `--- a/${previousPath}`,
147 `+++ b/${path}`,
148 "@@ -1 +1 @@",
149 "-before",
150 "+after",
151 ].join("\n"),
152 "repository-paths",
153 );
154 expect(parsed.kind).toBe("files");
155 if (parsed.kind !== "files") return;
156 expect(parsed.files[0]).toMatchObject({
157 path,
158 previousPath: previousPath === path ? null : previousPath,
159 additions: 1,
160 deletions: 1,
161 });
162 });
163​
164 it("builds renderable rows from a unified patch", () => {
165 const parsed = buildReviewParsedDiff(
166 [
167 "diff --git a/apps/mobile/src/a.ts b/apps/mobile/src/a.ts",
168 "index 1111111..2222222 100644",
169 "--- a/apps/mobile/src/a.ts",
170 "+++ b/apps/mobile/src/a.ts",
171 "@@ -1,2 +1,3 @@",
172 "-const before = 1;",
173 "+const after = 2;",
174 "+console.log(after);",
175 " return true;",
176 ].join("\n"),
177 "unit",
178 );
179​
180 expect(parsed.kind).toBe("files");
181 if (parsed.kind !== "files") {
182 return;
183 }
184​
185 expect(parsed.fileCount).toBe(1);
186 expect(parsed.additions).toBe(2);
187 expect(parsed.deletions).toBe(1);
188 expect(parsed.files[0]).toMatchObject({
189 path: "apps/mobile/src/a.ts",
190 additions: 2,
191 deletions: 1,
192 });
193 expect(parsed.files[0]?.rows).toEqual([
194 expect.objectContaining({ kind: "hunk", header: "@@ -1,2 +1,3 @@" }),
195 expect.objectContaining({
196 kind: "line",
197 change: "delete",
198 oldLineNumber: 1,
199 newLineNumber: null,
200 content: "const before = 1;",
201 comparison: { change: "add", tokenIndex: 0 },
202 }),
203 expect.objectContaining({
204 kind: "line",
205 change: "add",
206 oldLineNumber: null,
207 newLineNumber: 1,
208 content: "const after = 2;",
209 comparison: { change: "delete", tokenIndex: 0 },
210 }),
211 expect.objectContaining({
212 kind: "line",
213 change: "add",
214 oldLineNumber: null,
215 newLineNumber: 2,
216 content: "console.log(after);",
217 comparison: null,
218 }),
219 expect.objectContaining({
220 kind: "line",
221 change: "context",
222 oldLineNumber: 2,
223 newLineNumber: 3,
224 content: "return true;",
225 comparison: null,
226 }),
227 ]);
228 });
229​
230 it("treats truncated patches as partial diffs instead of failing", () => {
231 const parsed = buildReviewParsedDiff(
232 [
233 "diff --git a/apps/mobile/src/a.ts b/apps/mobile/src/a.ts",
234 "index 1111111..2222222 100644",
235 "--- a/apps/mobile/src/a.ts",
236 "+++ b/apps/mobile/src/a.ts",
237 "@@ -1 +1,2 @@",
238 " const before = 1;",
239 "+const after = 2;",
240 "",
241 "[truncated]",
242 ].join("\n"),
243 "unit-truncated",
244 );
245​
246 expect(parsed.kind).toBe("files");
247 if (parsed.kind !== "files") {
248 return;
249 }
250​
251 expect(parsed.notice).toContain("server size cap");
252 expect(parsed.fileCount).toBe(1);
253 expect(parsed.files[0]?.rows[0]).toMatchObject({
254 kind: "hunk",
255 header: "@@ -1,1 +1,2 @@",
256 });
257 });
258​
259 it("suppresses preview for non-text file formats", () => {
260 const preview = getReviewFilePreviewState(
261 makeRenderableFile({
262 path: "apps/mobile/assets/icon.png",
263 }),
264 );
265​
266 expect(preview).toMatchObject({
267 kind: "suppressed",
268 reason: "non-text",
269 title: "Non-text file",
270 actionLabel: null,
271 });
272 });
273​
274 it("suppresses large diffs until explicitly requested", () => {
275 const preview = getReviewFilePreviewState(
276 makeRenderableFile({
277 path: "apps/mobile/src/big.ts",
278 rows: Array.from({ length: 401 }, (_, index) => ({
279 kind: "line" as const,
280 id: `line-${index}`,
281 change: "add" as const,
282 oldLineNumber: null,
283 newLineNumber: index + 1,
284 content: `const line${index} = ${index};`,
285 additionTokenIndex: index,
286 deletionTokenIndex: null,
287 comparison: null,
288 })),
289 }),
290 );
291​
292 expect(preview).toMatchObject({
293 kind: "suppressed",
294 reason: "large",
295 title: "Large diff",
296 actionLabel: "Load diff",
297 });
298 });
299});
300​
301describe("applyReviewDiffMetadata", () => {
302 it("uses complete counts even when the preview contains only part of one file", () => {
303 const parsed = buildReviewParsedDiff(
304 [
305 "diff --git a/large.txt b/large.txt",
306 "--- a/large.txt",
307 "+++ b/large.txt",
308 "@@ -1 +1 @@",
309 "-before",
310 "+after",
311 ].join("\n"),
312 "partial",
313 );
314 const result = applyReviewDiffMetadata(parsed, {
315 truncated: true,
316 files: [
317 { path: "large.txt", previousPath: null, additions: 4000, deletions: 3000 },
318 { path: "unseen.txt", previousPath: null, additions: 100, deletions: 20 },
319 ],
320 });
321 expect(result.kind).toBe("files");
322 if (result.kind !== "files") return;
323 expect(result.fileCount).toBe(2);
324 expect(result.additions).toBe(4100);
325 expect(result.deletions).toBe(3020);
326 expect(result.files[0]?.additions).toBe(4000);
327 expect(result.notice).toContain("Counts include all changes");
328 expect(applyReviewDiffMetadata(parsed, null)).toEqual(parsed);
329 });
330});