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

reviewCommentSelection.test.ts

145 lines4.7 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 formatReviewCommentContext,
5 parseReviewCommentMessageSegments,
6 parseReviewInlineComments,
7 type ReviewCommentTarget,
8} from "./reviewCommentSelection";
9​
10function makeTarget(): ReviewCommentTarget {
11 return {
12 sectionId: "section-1",
13 sectionTitle: "Working tree",
14 filePath: "apps/demo/src/main.ts",
15 startIndex: 0,
16 endIndex: 1,
17 lines: [
18 {
19 kind: "line",
20 id: "line-1",
21 change: "delete",
22 oldLineNumber: 7,
23 newLineNumber: null,
24 content: "const retryLimit = 2;",
25 additionTokenIndex: null,
26 deletionTokenIndex: 0,
27 comparison: null,
28 },
29 {
30 kind: "line",
31 id: "line-2",
32 change: "add",
33 oldLineNumber: null,
34 newLineNumber: 7,
35 content: "const retryLimit = 4;",
36 additionTokenIndex: 0,
37 deletionTokenIndex: null,
38 comparison: null,
39 },
40 ],
41 };
43​
44describe("review comment serialization", () => {
45 it("keeps closing-tag text inside a chip label within a real review body", () => {
46 const body = "Before [</review_comment>](t3-context://v1/mention/context-1) after";
47 const serialized = `<review_comment sectionId="s" filePath="app.ts" startIndex="0" endIndex="0">${body}</review_comment>`;
48 const segments = parseReviewCommentMessageSegments(`${serialized} tail`);
49 expect(segments).toEqual([
50 { kind: "review-comment", comment: expect.objectContaining({ text: body }) },
51 { kind: "text", id: `review-comment-text:${serialized.length}`, text: " tail" },
52 ]);
53 });
54​
55 it("treats legacy markup inside a context label as opaque text", () => {
56 const text =
57 '[<review_comment sectionId="s" filePath="app.ts" startIndex="0" endIndex="0">Review this</review_comment>](t3-context://v1/mention/context-1)';
58 expect(parseReviewCommentMessageSegments(text)).toEqual([
59 { kind: "text", id: "review-comment-text:0", text },
60 ]);
61 });
62​
63 it("splits chat text into review comment segments", () => {
64 const serialized = `Before\n${formatReviewCommentContext(makeTarget(), "Please keep this configurable.")}\nAfter`;
65 const segments = parseReviewCommentMessageSegments(serialized);
66​
67 expect(segments).toHaveLength(3);
68 expect(segments[0]).toEqual(expect.objectContaining({ kind: "text", text: "Before\n" }));
69 expect(segments[1]).toEqual(
70 expect.objectContaining({
71 kind: "review-comment",
72 comment: expect.objectContaining({
73 filePath: "apps/demo/src/main.ts",
74 text: "Please keep this configurable.",
75 diff: expect.stringContaining("+const retryLimit = 4;"),
76 }),
77 }),
78 );
79 expect(segments[2]).toEqual(expect.objectContaining({ kind: "text", text: "\nAfter" }));
80 });
81​
82 it("parses source-language review comments created by the web file viewer", () => {
83 const [segment] = parseReviewCommentMessageSegments(
84 [
85 '<review_comment sectionId="file:docs/plan.md" sectionTitle="File comment" filePath="docs/plan.md" startIndex="0" endIndex="1" rangeLabel="L1 to L2">',
86 "Clarify this.",
87 "```md",
88 "# Plan",
89 "- Step one",
90 "```",
91 "</review_comment>",
92 ].join("\n"),
93 );
94​
95 expect(segment).toEqual(
96 expect.objectContaining({
97 kind: "review-comment",
98 comment: expect.objectContaining({
99 filePath: "docs/plan.md",
100 fenceLanguage: "md",
101 diff: "# Plan\n- Step one",
102 }),
103 }),
104 );
105 });
106​
107 it("keeps fenced examples in comment prose separate from the context fence", () => {
108 const [segment] = parseReviewCommentMessageSegments(
109 [
110 '<review_comment sectionId="section-1" sectionTitle="Working tree" filePath="src/app.ts" startIndex="0" endIndex="0" rangeLabel="+1">',
111 "Try this:",
112 "```ts",
113 "const value = 1;",
114 "```",
115 "Then retry.",
116 "```diff",
117 "@@ -0,0 +1,1 @@",
118 "+one",
119 "```",
120 "</review_comment>",
121 ].join("\n"),
122 );
123​
124 expect(segment).toEqual(
125 expect.objectContaining({
126 kind: "review-comment",
127 comment: expect.objectContaining({
128 text: ["Try this:", "```ts", "const value = 1;", "```", "Then retry."].join("\n"),
129 diff: "@@ -0,0 +1,1 @@\n+one",
130 }),
131 }),
132 );
133 });
134​
135 it("round-trips greater-than signs in review attributes", () => {
136 const serialized = formatReviewCommentContext(
137 { ...makeTarget(), sectionTitle: "Changes > 5" },
138 "Check this.",
139 );
140 const [comment] = parseReviewInlineComments(serialized);
141​
142 expect(serialized).toContain('sectionTitle="Changes &gt; 5"');
143 expect(comment?.sectionTitle).toBe("Changes > 5");
144 });
145});