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

reviewModel.test.ts

350 lines9.3 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 buildReviewListItems,
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("builds renderable rows from a unified patch", () => {
136 const parsed = buildReviewParsedDiff(
137 [
138 "diff --git a/apps/mobile/src/a.ts b/apps/mobile/src/a.ts",
139 "index 1111111..2222222 100644",
140 "--- a/apps/mobile/src/a.ts",
141 "+++ b/apps/mobile/src/a.ts",
142 "@@ -1,2 +1,3 @@",
143 "-const before = 1;",
144 "+const after = 2;",
145 "+console.log(after);",
146 " return true;",
147 ].join("\n"),
148 "unit",
149 );
150​
151 expect(parsed.kind).toBe("files");
152 if (parsed.kind !== "files") {
153 return;
154 }
155​
156 expect(parsed.fileCount).toBe(1);
157 expect(parsed.additions).toBe(2);
158 expect(parsed.deletions).toBe(1);
159 expect(parsed.files[0]).toMatchObject({
160 path: "apps/mobile/src/a.ts",
161 additions: 2,
162 deletions: 1,
163 });
164 expect(parsed.files[0]?.rows).toEqual([
165 expect.objectContaining({ kind: "hunk", header: "@@ -1,2 +1,3 @@" }),
166 expect.objectContaining({
167 kind: "line",
168 change: "delete",
169 oldLineNumber: 1,
170 newLineNumber: null,
171 content: "const before = 1;",
172 comparison: { change: "add", tokenIndex: 0 },
173 }),
174 expect.objectContaining({
175 kind: "line",
176 change: "add",
177 oldLineNumber: null,
178 newLineNumber: 1,
179 content: "const after = 2;",
180 comparison: { change: "delete", tokenIndex: 0 },
181 }),
182 expect.objectContaining({
183 kind: "line",
184 change: "add",
185 oldLineNumber: null,
186 newLineNumber: 2,
187 content: "console.log(after);",
188 comparison: null,
189 }),
190 expect.objectContaining({
191 kind: "line",
192 change: "context",
193 oldLineNumber: 2,
194 newLineNumber: 3,
195 content: "return true;",
196 comparison: null,
197 }),
198 ]);
199 });
200​
201 it("treats truncated patches as partial diffs instead of failing", () => {
202 const parsed = buildReviewParsedDiff(
203 [
204 "diff --git a/apps/mobile/src/a.ts b/apps/mobile/src/a.ts",
205 "index 1111111..2222222 100644",
206 "--- a/apps/mobile/src/a.ts",
207 "+++ b/apps/mobile/src/a.ts",
208 "@@ -1 +1,2 @@",
209 " const before = 1;",
210 "+const after = 2;",
211 "",
212 "[truncated]",
213 ].join("\n"),
214 "unit-truncated",
215 );
216​
217 expect(parsed.kind).toBe("files");
218 if (parsed.kind !== "files") {
219 return;
220 }
221​
222 expect(parsed.notice).toContain("server size cap");
223 expect(parsed.fileCount).toBe(1);
224 expect(parsed.files[0]?.rows[0]).toMatchObject({
225 kind: "hunk",
226 header: "@@ -1,1 +1,2 @@",
227 });
228 });
229​
230 it("suppresses preview for non-text file formats", () => {
231 const preview = getReviewFilePreviewState(
232 makeRenderableFile({
233 path: "apps/mobile/assets/icon.png",
234 }),
235 );
236​
237 expect(preview).toMatchObject({
238 kind: "suppressed",
239 reason: "non-text",
240 title: "Non-text file",
241 actionLabel: null,
242 });
243 });
244​
245 it("suppresses large diffs until explicitly requested", () => {
246 const preview = getReviewFilePreviewState(
247 makeRenderableFile({
248 path: "apps/mobile/src/big.ts",
249 rows: Array.from({ length: 401 }, (_, index) => ({
250 kind: "line" as const,
251 id: `line-${index}`,
252 change: "add" as const,
253 oldLineNumber: null,
254 newLineNumber: index + 1,
255 content: `const line${index} = ${index};`,
256 additionTokenIndex: index,
257 deletionTokenIndex: null,
258 comparison: null,
259 })),
260 }),
261 );
262​
263 expect(preview).toMatchObject({
264 kind: "suppressed",
265 reason: "large",
266 title: "Large diff",
267 actionLabel: "Load diff",
268 });
269 });
270​
271 it("flattens expanded file rows into virtualized review items", () => {
272 const file = makeRenderableFile({
273 path: "apps/mobile/src/a.ts",
274 rows: [
275 {
276 kind: "hunk",
277 id: "hunk-1",
278 header: "@@ -1,1 +1,2 @@",
279 context: null,
280 },
281 {
282 kind: "line",
283 id: "line-1",
284 change: "add",
285 oldLineNumber: null,
286 newLineNumber: 1,
287 content: "const after = 2;",
288 additionTokenIndex: 0,
289 deletionTokenIndex: null,
290 comparison: null,
291 },
292 ],
293 });
294​
295 const items = buildReviewListItems({
296 files: [file],
297 expandedFileIds: [file.id],
298 revealedLargeFileIds: [],
299 });
300​
301 expect(items).toEqual([
302 expect.objectContaining({ kind: "file-header", fileId: file.id, expanded: true }),
303 expect.objectContaining({
304 kind: "hunk",
305 fileId: file.id,
306 file,
307 row: file.rows[0],
308 }),
309 expect.objectContaining({
310 kind: "line",
311 fileId: file.id,
312 file,
313 row: file.rows[1],
314 lineIndex: 0,
315 }),
316 ]);
317 });
318​
319 it("keeps large diffs collapsed into a placeholder item until revealed", () => {
320 const file = makeRenderableFile({
321 path: "apps/mobile/src/big.ts",
322 rows: Array.from({ length: 401 }, (_, index) => ({
323 kind: "line" as const,
324 id: `line-${index}`,
325 change: "add" as const,
326 oldLineNumber: null,
327 newLineNumber: index + 1,
328 content: `const line${index} = ${index};`,
329 additionTokenIndex: index,
330 deletionTokenIndex: null,
331 comparison: null,
332 })),
333 });
334​
335 const items = buildReviewListItems({
336 files: [file],
337 expandedFileIds: [file.id],
338 revealedLargeFileIds: [],
339 });
340​
341 expect(items).toEqual([
342 expect.objectContaining({ kind: "file-header", fileId: file.id, expanded: true }),
343 expect.objectContaining({
344 kind: "file-suppressed",
345 fileId: file.id,
346 actionLabel: "Load diff",
347 }),
348 ]);
349 });
350});