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