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

reviewModel.test.ts

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