Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { upgradeLegacyContextMessage } from "@t3tools/shared/composerContextLegacy";
3import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences";
4
5import { pullRequestComposerContext } from "../../lib/composerContext";
6
7import {
8 clearReviewCommentTarget,
9 completeReviewCommentRange,
10 formatReviewCommentContext,
11 formatReviewSelectedRangeLabel,
12 getReviewCommentSelection,
13 reviewCommentSelectionScope,
14 selectReviewCommentSelection,
15 setReviewCommentTarget,
16 startReviewCommentRange,
17 parseReviewCommentMessageSegments,
18 parseReviewInlineComments,
19 type ReviewCommentTarget,
20} from "./reviewCommentSelection";
21import { setReviewSelectedSectionId } from "./reviewState";
22
23function makeTarget(): ReviewCommentTarget {
24 return {
25 sectionId: "section-1",
26 sectionTitle: "Working tree",
27 filePath: "apps/demo/src/main.ts",
28 startIndex: 0,
29 endIndex: 1,
30 lines: [
31 {
32 kind: "line",
33 id: "line-1",
34 change: "delete",
35 oldLineNumber: 7,
36 newLineNumber: null,
37 content: "const retryLimit = 2;",
38 additionTokenIndex: null,
39 deletionTokenIndex: 0,
40 comparison: null,
41 },
42 {
43 kind: "line",
44 id: "line-2",
45 change: "add",
46 oldLineNumber: null,
47 newLineNumber: 7,
48 content: "const retryLimit = 4;",
49 additionTokenIndex: 0,
50 deletionTokenIndex: null,
51 comparison: null,
52 },
53 ],
54 };
55}
56
57describe("review comment serialization", () => {
58 it("excludes a linked pull request chip from draft review comments", () => {
59 const record = pullRequestComposerContext(
60 {
61 number: 42,
62 title: "Fix checkout",
63 url: "https://github.com/example/repo/pull/42",
64 headBranch: "fix-checkout",
65 baseBranch: "main",
66 state: "open",
67 isDraft: false,
68 },
69 "pr-42",
70 );
71
72 expect(
73 parseReviewInlineComments(formatComposerContextReference(record), {
74 version: 1,
75 records: [record],
76 }),
77 ).toEqual([]);
78 });
79
80 it("reads an added review comment after the composer upgrades it to a context chip", () => {
81 const upgraded = upgradeLegacyContextMessage(
82 formatReviewCommentContext(makeTarget(), "Please keep this configurable."),
83 );
84 const comments = parseReviewInlineComments(upgraded.text, {
85 version: 1,
86 records: upgraded.records,
87 });
88
89 expect(comments).toEqual([
90 {
91 id: upgraded.records[0]!.contextId,
92 sectionId: "section-1",
93 sectionTitle: "Working tree",
94 filePath: "apps/demo/src/main.ts",
95 startIndex: 0,
96 endIndex: 1,
97 rangeLabel: "7",
98 text: "Please keep this configurable.",
99 diff: "@@ -7,1 +7,1 @@\n-const retryLimit = 2;\n+const retryLimit = 4;",
100 fenceLanguage: "diff",
101 },
102 ]);
103 });
104
105 it("keeps legacy comments and counts a typed comment only while its chip is present", () => {
106 const upgraded = upgradeLegacyContextMessage(
107 formatReviewCommentContext(makeTarget(), "Typed comment"),
108 );
109 const context = { version: 1, records: upgraded.records } as const;
110 const legacy = formatReviewCommentContext(makeTarget(), "Legacy comment");
111 const comments = parseReviewInlineComments(
112 `${legacy}\n${upgraded.text}\n${upgraded.text}`,
113 context,
114 );
115
116 expect(comments.map((comment) => comment.text)).toEqual(["Legacy comment", "Typed comment"]);
117 expect(parseReviewInlineComments(legacy, context).map((comment) => comment.text)).toEqual([
118 "Legacy comment",
119 ]);
120 expect(parseReviewInlineComments("", context)).toEqual([]);
121 expect(
122 parseReviewInlineComments(upgraded.text.replace("/review-comment/", "/mention/"), context),
123 ).toEqual([]);
124 });
125
126 it("keeps closing-tag text inside a chip label within a real review body", () => {
127 const body = "Before [</review_comment>](t3-context://v1/mention/context-1) after";
128 const serialized = `<review_comment sectionId="s" filePath="app.ts" startIndex="0" endIndex="0">${body}</review_comment>`;
129 const segments = parseReviewCommentMessageSegments(`${serialized} tail`);
130 expect(segments).toEqual([
131 { kind: "review-comment", comment: expect.objectContaining({ text: body }) },
132 { kind: "text", id: `review-comment-text:${serialized.length}`, text: " tail" },
133 ]);
134 });
135
136 it("treats legacy markup inside a context label as opaque text", () => {
137 const text =
138 '[<review_comment sectionId="s" filePath="app.ts" startIndex="0" endIndex="0">Review this</review_comment>](t3-context://v1/mention/context-1)';
139 expect(parseReviewCommentMessageSegments(text)).toEqual([
140 { kind: "text", id: "review-comment-text:0", text },
141 ]);
142 });
143
144 it("splits chat text into review comment segments", () => {
145 const serialized = `Before\n${formatReviewCommentContext(makeTarget(), "Please keep this configurable.")}\nAfter`;
146 const segments = parseReviewCommentMessageSegments(serialized);
147
148 expect(segments).toHaveLength(3);
149 expect(segments[0]).toEqual(expect.objectContaining({ kind: "text", text: "Before\n" }));
150 expect(segments[1]).toEqual(
151 expect.objectContaining({
152 kind: "review-comment",
153 comment: expect.objectContaining({
154 filePath: "apps/demo/src/main.ts",
155 text: "Please keep this configurable.",
156 diff: expect.stringContaining("+const retryLimit = 4;"),
157 }),
158 }),
159 );
160 expect(segments[2]).toEqual(expect.objectContaining({ kind: "text", text: "\nAfter" }));
161 });
162
163 it("parses source-language review comments created by the web file viewer", () => {
164 const [segment] = parseReviewCommentMessageSegments(
165 [
166 '<review_comment sectionId="file:docs/plan.md" sectionTitle="File comment" filePath="docs/plan.md" startIndex="0" endIndex="1" rangeLabel="L1 to L2">',
167 "Clarify this.",
168 "```md",
169 "# Plan",
170 "- Step one",
171 "```",
172 "</review_comment>",
173 ].join("\n"),
174 );
175
176 expect(segment).toEqual(
177 expect.objectContaining({
178 kind: "review-comment",
179 comment: expect.objectContaining({
180 filePath: "docs/plan.md",
181 fenceLanguage: "md",
182 diff: "# Plan\n- Step one",
183 }),
184 }),
185 );
186 });
187
188 it("keeps fenced examples in comment prose separate from the context fence", () => {
189 const [segment] = parseReviewCommentMessageSegments(
190 [
191 '<review_comment sectionId="section-1" sectionTitle="Working tree" filePath="src/app.ts" startIndex="0" endIndex="0" rangeLabel="+1">',
192 "Try this:",
193 "```ts",
194 "const value = 1;",
195 "```",
196 "Then retry.",
197 "```diff",
198 "@@ -0,0 +1,1 @@",
199 "+one",
200 "```",
201 "</review_comment>",
202 ].join("\n"),
203 );
204
205 expect(segment).toEqual(
206 expect.objectContaining({
207 kind: "review-comment",
208 comment: expect.objectContaining({
209 text: ["Try this:", "```ts", "const value = 1;", "```", "Then retry."].join("\n"),
210 diff: "@@ -0,0 +1,1 @@\n+one",
211 }),
212 }),
213 );
214 });
215
216 it("round-trips greater-than signs in review attributes", () => {
217 const serialized = formatReviewCommentContext(
218 { ...makeTarget(), sectionTitle: "Changes > 5" },
219 "Check this.",
220 );
221 const [comment] = parseReviewInlineComments(serialized);
222
223 expect(serialized).toContain('sectionTitle="Changes > 5"');
224 expect(comment?.sectionTitle).toBe("Changes > 5");
225 });
226});
227
228describe("review selected range label", () => {
229 it("falls back to line count when unified endpoints decrease across old and new numbers", () => {
230 expect(
231 formatReviewSelectedRangeLabel({
232 ...makeTarget(),
233 lines: [
234 {
235 kind: "line",
236 id: "line-1",
237 change: "context",
238 oldLineNumber: 10,
239 newLineNumber: 13,
240 content: "line 10 of the original file",
241 additionTokenIndex: null,
242 deletionTokenIndex: null,
243 comparison: null,
244 },
245 {
246 kind: "line",
247 id: "line-2",
248 change: "delete",
249 oldLineNumber: 11,
250 newLineNumber: null,
251 content: "line 11 of the original file",
252 additionTokenIndex: null,
253 deletionTokenIndex: 0,
254 comparison: null,
255 },
256 ],
257 }),
258 ).toBe("2 lines");
259 });
260});
261
262describe("review comment selection store", () => {
263 const changes = { id: "git:branch-range", content: "hash-1" };
264 const scope = reviewCommentSelectionScope(1, "env:thread", changes)!;
265 const otherScope = reviewCommentSelectionScope(1, "env:thread", {
266 id: "turn:2",
267 content: "diff",
268 })!;
269 const target = makeTarget();
270 const pending = {
271 sectionId: target.sectionId,
272 sectionTitle: target.sectionTitle,
273 filePath: target.filePath,
274 lines: target.lines,
275 lineIndex: 0,
276 rowId: "row-1",
277 };
278
279 it("keeps a long-pressed range start when replacing an earlier target", () => {
280 setReviewCommentTarget(scope, target);
281 startReviewCommentRange(scope, pending);
282
283 expect(getReviewCommentSelection()).toEqual({ scope, target: null, pending });
284 });
285
286 it("keeps the range start after a same-line range end and drops it on a plain tap", () => {
287 startReviewCommentRange(scope, pending);
288 const sameLine = { ...target, startIndex: 0, endIndex: 0 };
289 completeReviewCommentRange(scope, sameLine);
290 expect(getReviewCommentSelection().pending).toEqual(pending);
291
292 setReviewCommentTarget(scope, sameLine);
293 expect(getReviewCommentSelection().pending).toBeNull();
294 });
295
296 it("clears the target and the range start together", () => {
297 startReviewCommentRange(scope, pending);
298 clearReviewCommentTarget();
299
300 expect(getReviewCommentSelection()).toEqual({ scope: null, target: null, pending: null });
301 });
302
303 it("reads as empty from another thread or section", () => {
304 setReviewCommentTarget(scope, target);
305
306 expect(selectReviewCommentSelection(getReviewCommentSelection(), scope).target).toBe(target);
307 expect(selectReviewCommentSelection(getReviewCommentSelection(), otherScope).target).toBeNull();
308 expect(selectReviewCommentSelection(getReviewCommentSelection(), null).target).toBeNull();
309 });
310
311 it("reads as empty in a reopened review sheet", () => {
312 setReviewCommentTarget(scope, target);
313 const reopenedScope = reviewCommentSelectionScope(2, "env:thread", changes);
314
315 expect(
316 selectReviewCommentSelection(getReviewCommentSelection(), reopenedScope).target,
317 ).toBeNull();
318 });
319
320 it("reads as empty when the section disappears and comes back over a different diff", () => {
321 setReviewCommentTarget(scope, target);
322 // The working tree went clean, so the default showed; the section later returns.
323 const returned = reviewCommentSelectionScope(1, "env:thread", {
324 ...changes,
325 content: "hash-2",
326 });
327 const unchanged = reviewCommentSelectionScope(1, "env:thread", { ...changes });
328
329 expect(selectReviewCommentSelection(getReviewCommentSelection(), returned).target).toBeNull();
330 expect(selectReviewCommentSelection(getReviewCommentSelection(), unchanged).target).toBe(
331 target,
332 );
333 });
334
335 it("drops the selection when the section changes, so switching back starts empty", () => {
336 setReviewCommentTarget(scope, target);
337 setReviewSelectedSectionId("env:thread", "turn:2");
338 setReviewSelectedSectionId("env:thread", "git:branch-range");
339
340 expect(selectReviewCommentSelection(getReviewCommentSelection(), scope).target).toBeNull();
341 });
342});