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

reviewModel.test.ts

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