Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import type { PullRequestReviewGuideStop } from "@t3tools/contracts";
3import type { ReviewGuideGenerationResult } from "@t3tools/provider-core/server/textGeneration";
4
5import { normalizeReviewGuide, parseReviewGuideChange } from "./pullRequestReviewGuideNormalize.ts";
6
7const patch = [
8 "diff --git a/src/a.ts b/src/a.ts",
9 "index 1111111..2222222 100644",
10 "--- a/src/a.ts",
11 "+++ b/src/a.ts",
12 "@@ -9,0 +10,5 @@ export const x = 1;",
13 "+added",
14 "@@ -30 +34,0 @@ function y() {",
15 "-removed",
16 "@@ -35,0 +40,20 @@",
17 "+added",
18 "diff --git a/src/gone.ts b/src/gone.ts",
19 "deleted file mode 100644",
20 "--- a/src/gone.ts",
21 "+++ /dev/null",
22 "@@ -1,20 +0,0 @@",
23 "-removed",
24 "diff --git a/docs/readme.md b/docs/readme.md",
25 "--- a/docs/readme.md",
26 "+++ b/docs/readme.md",
27 "@@ -2,0 +3 @@",
28 "+added",
29 "",
30].join("\n");
31const change = parseReviewGuideChange("src/a.ts\0src/gone.ts\0docs/readme.md\0", patch);
32
33const stop = (overrides: Partial<PullRequestReviewGuideStop>): PullRequestReviewGuideStop => ({
34 title: "Stop",
35 summary: "",
36 attention: null,
37 excerpts: [],
38 files: [],
39 notes: [],
40 questions: [],
41 ...overrides,
42});
43
44const guide = (overrides: Partial<ReviewGuideGenerationResult>): ReviewGuideGenerationResult => ({
45 stops: [],
46 ...overrides,
47});
48
49const excerpt = (path: string, start: number, end: number, side: "new" | "old" = "new") => ({
50 path,
51 side,
52 start,
53 end,
54 caption: "",
55});
56
57describe("parseReviewGuideChange", () => {
58 it("reads a removed or added line that looks like a file header as a source line", () => {
59 const tricky = [
60 "diff --git a/notes.md b/notes.md",
61 "--- a/notes.md",
62 "+++ b/notes.md",
63 "@@ -3,1 +3,2 @@",
64 "--- a/old-heading",
65 "+++ b/other.ts",
66 "+tail",
67 "@@ -9,0 +10,1 @@",
68 "+later",
69 "",
70 ].join("\n");
71 const parsed = parseReviewGuideChange("notes.md\0", tricky);
72 expect([...parsed.hunks.keys()]).toEqual(["notes.md"]);
73 expect(parsed.hunks.get("notes.md")).toEqual({
74 new: [
75 { start: 3, end: 4 },
76 { start: 10, end: 10 },
77 ],
78 old: [{ start: 3, end: 3 }],
79 });
80 });
81
82 it("reads each side's hunks, keying a deleted file by its old path", () => {
83 expect(change.files).toEqual(["src/a.ts", "src/gone.ts", "docs/readme.md"]);
84 expect(change.hunks.get("src/a.ts")).toEqual({
85 new: [
86 { start: 10, end: 14 },
87 { start: 40, end: 59 },
88 ],
89 old: [{ start: 30, end: 30 }],
90 });
91 expect(change.hunks.get("src/gone.ts")).toEqual({ new: [], old: [{ start: 1, end: 20 }] });
92 expect(change.hunks.get("docs/readme.md")).toEqual({ new: [{ start: 3, end: 3 }], old: [] });
93 });
94});
95
96describe("normalizeReviewGuide", () => {
97 it("clamps excerpts to changed lines, merges touching ones, and caps their length", () => {
98 const result = normalizeReviewGuide(
99 guide({
100 stops: [
101 stop({
102 excerpts: [
103 excerpt("src/a.ts", 5, 12),
104 excerpt("src/a.ts", 13, 14),
105 excerpt("src/a.ts", 38, 80),
106 excerpt("src/a.ts", 20, 25),
107 excerpt("src/gone.ts", 2, 8, "old"),
108 ],
109 files: [{ path: "docs/readme.md", note: "" }],
110 }),
111 ],
112 }),
113 change,
114 );
115 expect(result.stops[0]?.excerpts).toEqual([
116 excerpt("src/a.ts", 10, 14),
117 excerpt("src/a.ts", 40, 59),
118 excerpt("src/gone.ts", 2, 8, "old"),
119 ]);
120 expect(result.stops[0]?.files).toEqual([{ path: "docs/readme.md" }]);
121 expect(result.stops).toHaveLength(1);
122 });
123
124 it("keeps a long excerpt to its first 25 lines", () => {
125 const long = parseReviewGuideChange(
126 "big.ts\0",
127 "--- a/big.ts\n+++ b/big.ts\n@@ -0,0 +1,100 @@\n",
128 );
129 const result = normalizeReviewGuide(
130 guide({ stops: [stop({ excerpts: [excerpt("big.ts", 1, 100)] })] }),
131 long,
132 );
133 expect(result.stops[0]?.excerpts).toEqual([excerpt("big.ts", 1, 25)]);
134 });
135
136 it("drops notes outside the stop's remaining excerpts", () => {
137 const result = normalizeReviewGuide(
138 guide({
139 stops: [
140 stop({
141 excerpts: [excerpt("src/a.ts", 5, 12)],
142 notes: [
143 { path: "src/a.ts", line: 11, title: "Inside", body: "" },
144 { path: "src/a.ts", line: 6, title: "Clamped away", body: "" },
145 { path: "src/other.ts", line: 11, title: "Other file", body: "" },
146 ],
147 }),
148 ],
149 }),
150 change,
151 );
152 expect(result.stops[0]?.notes.map((note) => note.title)).toEqual(["Inside"]);
153 });
154
155 it("drops excerpts and files outside the change", () => {
156 const result = normalizeReviewGuide(
157 guide({
158 stops: [
159 stop({
160 excerpts: [excerpt("src/unchanged.ts", 1, 5)],
161 files: [{ path: "src/a.ts", note: "edited" }, { path: "src/unchanged.ts" }],
162 }),
163 ],
164 }),
165 change,
166 );
167 expect(result.stops[0]?.excerpts).toEqual([]);
168 expect(result.stops[0]?.files).toEqual([{ path: "src/a.ts", note: "edited" }]);
169 });
170
171 it("puts changed files the tour left out into a closing stop", () => {
172 const result = normalizeReviewGuide(
173 guide({ stops: [stop({ excerpts: [excerpt("src/a.ts", 10, 12)] })] }),
174 change,
175 );
176 expect(result.stops.at(-1)).toMatchObject({
177 title: "Other files",
178 files: [{ path: "src/gone.ts" }, { path: "docs/readme.md" }],
179 });
180 });
181
182 it("lets stops show different parts of one file and lists an unshown file once", () => {
183 const result = normalizeReviewGuide(
184 guide({
185 stops: [
186 stop({
187 title: "Shape",
188 excerpts: [excerpt("src/a.ts", 10, 14)],
189 files: [{ path: "docs/readme.md" }],
190 }),
191 stop({
192 title: "Implementation",
193 excerpts: [excerpt("src/a.ts", 40, 50)],
194 files: [{ path: "src/a.ts" }, { path: "docs/readme.md" }, { path: "src/gone.ts" }],
195 }),
196 ],
197 }),
198 change,
199 );
200 expect(result.stops.map((s) => [s.title, s.excerpts.map((e) => e.start), s.files])).toEqual([
201 ["Shape", [10], [{ path: "docs/readme.md" }]],
202 ["Implementation", [40], [{ path: "src/gone.ts" }]],
203 ]);
204 });
205});