Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { describe, expect, it } from "vite-plus/test";
3import {
4 ProviderInstanceId,
5 PullRequestReviewGuide,
6 type PullRequestReviewGuideStop,
7} from "@t3tools/contracts";
8import type { ReviewGuideGenerationResult } from "@t3tools/provider-core/server/textGeneration";
9
10import { normalizeReviewGuide, parseReviewGuideChange } from "./pullRequestReviewGuideNormalize.ts";
11
12const patch = [
13 "diff --git a/src/a.ts b/src/a.ts",
14 "index 1111111..2222222 100644",
15 "--- a/src/a.ts",
16 "+++ b/src/a.ts",
17 "@@ -9,0 +10,5 @@ export const x = 1;",
18 "+added",
19 "@@ -30 +34,0 @@ function y() {",
20 "-removed",
21 "@@ -35,0 +40,20 @@",
22 "+added",
23 "diff --git a/src/gone.ts b/src/gone.ts",
24 "deleted file mode 100644",
25 "--- a/src/gone.ts",
26 "+++ /dev/null",
27 "@@ -1,20 +0,0 @@",
28 "-removed",
29 "diff --git a/docs/readme.md b/docs/readme.md",
30 "--- a/docs/readme.md",
31 "+++ b/docs/readme.md",
32 "@@ -2,0 +3 @@",
33 "+added",
34 "",
35].join("\n");
36const change = parseReviewGuideChange("src/a.ts\0src/gone.ts\0docs/readme.md\0", patch);
37
38const stop = (overrides: Partial<PullRequestReviewGuideStop>): PullRequestReviewGuideStop => ({
39 title: "Stop",
40 summary: "",
41 attention: null,
42 excerpts: [],
43 files: [],
44 notes: [],
45 questions: [],
46 ...overrides,
47});
48
49const guide = (overrides: Partial<ReviewGuideGenerationResult>): ReviewGuideGenerationResult => ({
50 stops: [],
51 ...overrides,
52});
53
54const excerpt = (path: string, start: number, end: number, side: "new" | "old" = "new") => ({
55 path,
56 side,
57 start,
58 end,
59 caption: "",
60});
61
62describe("parseReviewGuideChange", () => {
63 it("reads a removed or added line that looks like a file header as a source line", () => {
64 const tricky = [
65 "diff --git a/notes.md b/notes.md",
66 "--- a/notes.md",
67 "+++ b/notes.md",
68 "@@ -3,1 +3,2 @@",
69 "--- a/old-heading",
70 "+++ b/other.ts",
71 "+tail",
72 "@@ -9,0 +10,1 @@",
73 "+later",
74 "",
75 ].join("\n");
76 const parsed = parseReviewGuideChange("notes.md\0", tricky);
77 expect([...parsed.hunks.keys()]).toEqual(["notes.md"]);
78 expect(parsed.hunks.get("notes.md")).toEqual({
79 new: [
80 { start: 3, end: 4 },
81 { start: 10, end: 10 },
82 ],
83 old: [{ start: 3, end: 3 }],
84 });
85 });
86
87 it("reads each side's hunks, keying a deleted file by its old path", () => {
88 expect(change.files).toEqual(["src/a.ts", "src/gone.ts", "docs/readme.md"]);
89 expect(change.hunks.get("src/a.ts")).toEqual({
90 new: [
91 { start: 10, end: 14 },
92 { start: 40, end: 59 },
93 ],
94 old: [{ start: 30, end: 30 }],
95 });
96 expect(change.hunks.get("src/gone.ts")).toEqual({ new: [], old: [{ start: 1, end: 20 }] });
97 expect(change.hunks.get("docs/readme.md")).toEqual({ new: [{ start: 3, end: 3 }], old: [] });
98 });
99
100 it("reads file names git quotes or ends with a tab", () => {
101 const unusual = [
102 "diff --git a/with space.txt b/with space.txt",
103 "--- a/with space.txt\t",
104 "+++ b/with space.txt\t",
105 "@@ -1,0 +2 @@",
106 'diff --git "a/tab\\tname.txt" "b/tab\\tname.txt"',
107 '--- "a/tab\\tname.txt"',
108 '+++ "b/tab\\tname.txt"',
109 "@@ -1,0 +2 @@",
110 'diff --git "a/back\\\\slash.txt" "b/back\\\\slash.txt"',
111 '--- "a/back\\\\slash.txt"',
112 '+++ "b/back\\\\slash.txt"',
113 "@@ -1,0 +2 @@",
114 'diff --git "a/gone\\tfile.txt" "b/gone\\tfile.txt"',
115 '--- "a/gone\\tfile.txt"',
116 "+++ /dev/null",
117 "@@ -1 +0,0 @@",
118 "",
119 ].join("\n");
120 const names = ["with space.txt", "tab\tname.txt", "back\\slash.txt", "gone\tfile.txt"];
121 const parsed = parseReviewGuideChange(names.join("\0"), unusual);
122 expect([...parsed.hunks.keys()]).toEqual(names);
123 expect(parsed.hunks.get("gone\tfile.txt")).toEqual({ new: [], old: [{ start: 1, end: 1 }] });
124 const result = normalizeReviewGuide(
125 guide({ stops: [stop({ excerpts: names.slice(0, 3).map((path) => excerpt(path, 1, 5)) })] }),
126 parsed,
127 );
128 expect(result.stops[0]?.excerpts).toEqual(names.slice(0, 3).map((path) => excerpt(path, 2, 2)));
129 });
130});
131
132describe("normalizeReviewGuide", () => {
133 it("clamps excerpts to changed lines, merges touching ones, and caps their length", () => {
134 const result = normalizeReviewGuide(
135 guide({
136 stops: [
137 stop({
138 excerpts: [
139 excerpt("src/a.ts", 5, 12),
140 excerpt("src/a.ts", 13, 14),
141 excerpt("src/a.ts", 38, 80),
142 excerpt("src/a.ts", 20, 25),
143 excerpt("src/gone.ts", 2, 8, "old"),
144 ],
145 files: [{ path: "docs/readme.md", note: "" }],
146 }),
147 ],
148 }),
149 change,
150 );
151 expect(result.stops[0]?.excerpts).toEqual([
152 excerpt("src/a.ts", 10, 14),
153 excerpt("src/a.ts", 40, 59),
154 excerpt("src/gone.ts", 2, 8, "old"),
155 ]);
156 expect(result.stops[0]?.files).toEqual([{ path: "docs/readme.md" }]);
157 expect(result.stops).toHaveLength(1);
158 });
159
160 it("merges excerpts that a later excerpt bridges into one", () => {
161 const result = normalizeReviewGuide(
162 guide({
163 stops: [
164 stop({
165 excerpts: [
166 excerpt("src/a.ts", 40, 41),
167 { ...excerpt("src/a.ts", 50, 51), caption: "Second" },
168 excerpt("src/a.ts", 41, 50),
169 ],
170 }),
171 ],
172 }),
173 change,
174 );
175 expect(result.stops[0]?.excerpts).toEqual([
176 { ...excerpt("src/a.ts", 40, 51), caption: "Second" },
177 ]);
178
179 const tailNote = { path: "src/a.ts", line: 40, title: "Tail", body: "Keep this note." };
180 const wide = normalizeReviewGuide(
181 guide({
182 stops: [
183 stop({
184 excerpts: [
185 excerpt("src/a.ts", 1, 25),
186 { ...excerpt("src/a.ts", 30, 45), caption: "Second" },
187 excerpt("src/a.ts", 24, 32),
188 ],
189 notes: [tailNote],
190 }),
191 ],
192 }),
193 parseReviewGuideChange(
194 "src/a.ts\0",
195 "diff --git a/src/a.ts b/src/a.ts\n--- a/src/a.ts\n+++ b/src/a.ts\n@@ -0,0 +1,60 @@\n+added",
196 ),
197 );
198 expect(wide.stops[0]?.excerpts).toEqual([{ ...excerpt("src/a.ts", 1, 25), caption: "Second" }]);
199 expect(wide.stops[0]?.notes).toEqual([]);
200 });
201
202 it("drops notes outside the stop's remaining excerpts", () => {
203 const result = normalizeReviewGuide(
204 guide({
205 stops: [
206 stop({
207 excerpts: [excerpt("src/a.ts", 5, 12)],
208 notes: [
209 { path: "src/a.ts", line: 11, title: "Inside", body: "" },
210 { path: "src/a.ts", line: 6, title: "Clamped away", body: "" },
211 { path: "src/other.ts", line: 11, title: "Other file", body: "" },
212 ],
213 }),
214 ],
215 }),
216 change,
217 );
218 expect(result.stops[0]?.notes.map((note) => note.title)).toEqual(["Inside"]);
219 });
220
221 it("drops excerpts and files outside the change", () => {
222 const result = normalizeReviewGuide(
223 guide({
224 stops: [
225 stop({
226 excerpts: [excerpt("src/unchanged.ts", 1, 5)],
227 files: [{ path: "src/a.ts", note: "edited" }, { path: "src/unchanged.ts" }],
228 }),
229 ],
230 }),
231 change,
232 );
233 expect(result.stops[0]?.excerpts).toEqual([]);
234 expect(result.stops[0]?.files).toEqual([{ path: "src/a.ts", note: "edited" }]);
235 });
236
237 it("puts changed files the tour left out into a closing stop", () => {
238 const result = normalizeReviewGuide(
239 guide({ stops: [stop({ excerpts: [excerpt("src/a.ts", 10, 12)] })] }),
240 change,
241 );
242 expect(result.stops.at(-1)).toMatchObject({
243 title: "Other files",
244 files: [{ path: "src/gone.ts" }, { path: "docs/readme.md" }],
245 });
246 });
247
248 it("lets stops show different parts of one file and lists an unshown file once", () => {
249 const result = normalizeReviewGuide(
250 guide({
251 stops: [
252 stop({
253 title: "Shape",
254 excerpts: [excerpt("src/a.ts", 10, 14)],
255 files: [{ path: "docs/readme.md" }],
256 }),
257 stop({
258 title: "Implementation",
259 excerpts: [excerpt("src/a.ts", 40, 50)],
260 files: [{ path: "src/a.ts" }, { path: "docs/readme.md" }, { path: "src/gone.ts" }],
261 }),
262 ],
263 }),
264 change,
265 );
266 expect(result.stops.map((s) => [s.title, s.excerpts.map((e) => e.start), s.files])).toEqual([
267 ["Shape", [10], [{ path: "docs/readme.md" }]],
268 ["Implementation", [40], [{ path: "src/gone.ts" }]],
269 ]);
270 });
271
272 it("keeps a path's edge whitespace through the saved guide codec", () => {
273 const names = ["note.txt ", "other.txt "];
274 const spaced = parseReviewGuideChange(
275 names.map((name) => `${name}\0`).join(""),
276 names
277 .map((name, index) =>
278 [
279 `diff --git a/${name} b/${name}`,
280 `--- a/${name}\t`,
281 `+++ b/${name}\t`,
282 "@@ -1 +1 @@",
283 "-old",
284 `+new ${index}`,
285 ].join("\n"),
286 )
287 .join("\n"),
288 );
289 const normalized = normalizeReviewGuide(
290 guide({
291 stops: [
292 stop({
293 excerpts: [excerpt("note.txt ", 1, 1)],
294 files: [{ path: "other.txt " }],
295 notes: [{ path: "note.txt ", line: 1, title: "Why", body: "" }],
296 }),
297 ],
298 }),
299 spaced,
300 );
301 const codec = Schema.fromJsonString(PullRequestReviewGuide);
302 const saved = Schema.decodeSync(codec)(
303 Schema.encodeSync(codec)({
304 ...normalized,
305 headSha: "abc123",
306 generatedAt: "2026-01-01T00:00:00.000Z",
307 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
308 }),
309 );
310
311 const [savedStop] = saved.stops;
312 expect(savedStop?.excerpts.map((item) => item.path)).toEqual(["note.txt "]);
313 expect(savedStop?.notes.map((item) => item.path)).toEqual(["note.txt "]);
314 expect(savedStop?.files.map((item) => item.path)).toEqual(["other.txt "]);
315 });
316});