Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 clearReviewCommentTarget,
5 completeReviewCommentRange,
6 formatReviewCommentContext,
7 getReviewCommentSelection,
8 reviewCommentSelectionScope,
9 selectReviewCommentSelection,
10 setReviewCommentTarget,
11 startReviewCommentRange,
12 parseReviewCommentMessageSegments,
13 parseReviewInlineComments,
14 type ReviewCommentTarget,
15} from "./reviewCommentSelection";
16import { setReviewSelectedSectionId } from "./reviewState";
17
18function makeTarget(): ReviewCommentTarget {
19 return {
20 sectionId: "section-1",
21 sectionTitle: "Working tree",
22 filePath: "apps/demo/src/main.ts",
23 startIndex: 0,
24 endIndex: 1,
25 lines: [
26 {
27 kind: "line",
28 id: "line-1",
29 change: "delete",
30 oldLineNumber: 7,
31 newLineNumber: null,
32 content: "const retryLimit = 2;",
33 additionTokenIndex: null,
34 deletionTokenIndex: 0,
35 comparison: null,
36 },
37 {
38 kind: "line",
39 id: "line-2",
40 change: "add",
41 oldLineNumber: null,
42 newLineNumber: 7,
43 content: "const retryLimit = 4;",
44 additionTokenIndex: 0,
45 deletionTokenIndex: null,
46 comparison: null,
47 },
48 ],
49 };
50}
51
52describe("review comment serialization", () => {
53 it("keeps closing-tag text inside a chip label within a real review body", () => {
54 const body = "Before [</review_comment>](t3-context://v1/mention/context-1) after";
55 const serialized = `<review_comment sectionId="s" filePath="app.ts" startIndex="0" endIndex="0">${body}</review_comment>`;
56 const segments = parseReviewCommentMessageSegments(`${serialized} tail`);
57 expect(segments).toEqual([
58 { kind: "review-comment", comment: expect.objectContaining({ text: body }) },
59 { kind: "text", id: `review-comment-text:${serialized.length}`, text: " tail" },
60 ]);
61 });
62
63 it("treats legacy markup inside a context label as opaque text", () => {
64 const text =
65 '[<review_comment sectionId="s" filePath="app.ts" startIndex="0" endIndex="0">Review this</review_comment>](t3-context://v1/mention/context-1)';
66 expect(parseReviewCommentMessageSegments(text)).toEqual([
67 { kind: "text", id: "review-comment-text:0", text },
68 ]);
69 });
70
71 it("splits chat text into review comment segments", () => {
72 const serialized = `Before\n${formatReviewCommentContext(makeTarget(), "Please keep this configurable.")}\nAfter`;
73 const segments = parseReviewCommentMessageSegments(serialized);
74
75 expect(segments).toHaveLength(3);
76 expect(segments[0]).toEqual(expect.objectContaining({ kind: "text", text: "Before\n" }));
77 expect(segments[1]).toEqual(
78 expect.objectContaining({
79 kind: "review-comment",
80 comment: expect.objectContaining({
81 filePath: "apps/demo/src/main.ts",
82 text: "Please keep this configurable.",
83 diff: expect.stringContaining("+const retryLimit = 4;"),
84 }),
85 }),
86 );
87 expect(segments[2]).toEqual(expect.objectContaining({ kind: "text", text: "\nAfter" }));
88 });
89
90 it("parses source-language review comments created by the web file viewer", () => {
91 const [segment] = parseReviewCommentMessageSegments(
92 [
93 '<review_comment sectionId="file:docs/plan.md" sectionTitle="File comment" filePath="docs/plan.md" startIndex="0" endIndex="1" rangeLabel="L1 to L2">',
94 "Clarify this.",
95 "```md",
96 "# Plan",
97 "- Step one",
98 "```",
99 "</review_comment>",
100 ].join("\n"),
101 );
102
103 expect(segment).toEqual(
104 expect.objectContaining({
105 kind: "review-comment",
106 comment: expect.objectContaining({
107 filePath: "docs/plan.md",
108 fenceLanguage: "md",
109 diff: "# Plan\n- Step one",
110 }),
111 }),
112 );
113 });
114
115 it("keeps fenced examples in comment prose separate from the context fence", () => {
116 const [segment] = parseReviewCommentMessageSegments(
117 [
118 '<review_comment sectionId="section-1" sectionTitle="Working tree" filePath="src/app.ts" startIndex="0" endIndex="0" rangeLabel="+1">',
119 "Try this:",
120 "```ts",
121 "const value = 1;",
122 "```",
123 "Then retry.",
124 "```diff",
125 "@@ -0,0 +1,1 @@",
126 "+one",
127 "```",
128 "</review_comment>",
129 ].join("\n"),
130 );
131
132 expect(segment).toEqual(
133 expect.objectContaining({
134 kind: "review-comment",
135 comment: expect.objectContaining({
136 text: ["Try this:", "```ts", "const value = 1;", "```", "Then retry."].join("\n"),
137 diff: "@@ -0,0 +1,1 @@\n+one",
138 }),
139 }),
140 );
141 });
142
143 it("round-trips greater-than signs in review attributes", () => {
144 const serialized = formatReviewCommentContext(
145 { ...makeTarget(), sectionTitle: "Changes > 5" },
146 "Check this.",
147 );
148 const [comment] = parseReviewInlineComments(serialized);
149
150 expect(serialized).toContain('sectionTitle="Changes > 5"');
151 expect(comment?.sectionTitle).toBe("Changes > 5");
152 });
153});
154
155describe("review comment selection store", () => {
156 const changes = { id: "git:branch-range", content: "hash-1" };
157 const scope = reviewCommentSelectionScope(1, "env:thread", changes)!;
158 const otherScope = reviewCommentSelectionScope(1, "env:thread", {
159 id: "turn:2",
160 content: "diff",
161 })!;
162 const target = makeTarget();
163 const pending = {
164 sectionId: target.sectionId,
165 sectionTitle: target.sectionTitle,
166 filePath: target.filePath,
167 lines: target.lines,
168 lineIndex: 0,
169 rowId: "row-1",
170 };
171
172 it("keeps a long-pressed range start when replacing an earlier target", () => {
173 setReviewCommentTarget(scope, target);
174 startReviewCommentRange(scope, pending);
175
176 expect(getReviewCommentSelection()).toEqual({ scope, target: null, pending });
177 });
178
179 it("keeps the range start after a same-line range end and drops it on a plain tap", () => {
180 startReviewCommentRange(scope, pending);
181 const sameLine = { ...target, startIndex: 0, endIndex: 0 };
182 completeReviewCommentRange(scope, sameLine);
183 expect(getReviewCommentSelection().pending).toEqual(pending);
184
185 setReviewCommentTarget(scope, sameLine);
186 expect(getReviewCommentSelection().pending).toBeNull();
187 });
188
189 it("clears the target and the range start together", () => {
190 startReviewCommentRange(scope, pending);
191 clearReviewCommentTarget();
192
193 expect(getReviewCommentSelection()).toEqual({ scope: null, target: null, pending: null });
194 });
195
196 it("reads as empty from another thread or section", () => {
197 setReviewCommentTarget(scope, target);
198
199 expect(selectReviewCommentSelection(getReviewCommentSelection(), scope).target).toBe(target);
200 expect(selectReviewCommentSelection(getReviewCommentSelection(), otherScope).target).toBeNull();
201 expect(selectReviewCommentSelection(getReviewCommentSelection(), null).target).toBeNull();
202 });
203
204 it("reads as empty in a reopened review sheet", () => {
205 setReviewCommentTarget(scope, target);
206 const reopenedScope = reviewCommentSelectionScope(2, "env:thread", changes);
207
208 expect(
209 selectReviewCommentSelection(getReviewCommentSelection(), reopenedScope).target,
210 ).toBeNull();
211 });
212
213 it("reads as empty when the section disappears and comes back over a different diff", () => {
214 setReviewCommentTarget(scope, target);
215 // The working tree went clean, so the default showed; the section later returns.
216 const returned = reviewCommentSelectionScope(1, "env:thread", {
217 ...changes,
218 content: "hash-2",
219 });
220 const unchanged = reviewCommentSelectionScope(1, "env:thread", { ...changes });
221
222 expect(selectReviewCommentSelection(getReviewCommentSelection(), returned).target).toBeNull();
223 expect(selectReviewCommentSelection(getReviewCommentSelection(), unchanged).target).toBe(
224 target,
225 );
226 });
227
228 it("drops the selection when the section changes, so switching back starts empty", () => {
229 setReviewCommentTarget(scope, target);
230 setReviewSelectedSectionId("env:thread", "turn:2");
231 setReviewSelectedSectionId("env:thread", "git:branch-range");
232
233 expect(selectReviewCommentSelection(getReviewCommentSelection(), scope).target).toBeNull();
234 });
235});