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

reviewModel.test.ts

354 lines9.3 KB

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