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

reviewCommentSelection.test.ts

145 lines4.4 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 countReviewCommentContexts,
5 formatReviewCommentContext,
6 parseReviewCommentMessageSegments,
7 parseReviewInlineComments,
8 type ReviewCommentTarget,
9} from "./reviewCommentSelection";
10​
11function makeTarget(): ReviewCommentTarget {
12 return {
13 sectionId: "section-1",
14 sectionTitle: "Working tree",
15 filePath: "apps/demo/src/main.ts",
16 startIndex: 0,
17 endIndex: 1,
18 lines: [
19 {
20 kind: "line",
21 id: "line-1",
22 change: "delete",
23 oldLineNumber: 7,
24 newLineNumber: null,
25 content: "const retryLimit = 2;",
26 additionTokenIndex: null,
27 deletionTokenIndex: 0,
28 comparison: null,
29 },
30 {
31 kind: "line",
32 id: "line-2",
33 change: "add",
34 oldLineNumber: null,
35 newLineNumber: 7,
36 content: "const retryLimit = 4;",
37 additionTokenIndex: 0,
38 deletionTokenIndex: null,
39 comparison: null,
40 },
41 ],
42 };
44​
45describe("review comment serialization", () => {
46 it("preserves enough metadata for inline diff rendering", () => {
47 const serialized = formatReviewCommentContext(makeTarget(), "Please keep this configurable.");
48​
49 expect(countReviewCommentContexts(serialized)).toBe(1);
50 expect(parseReviewInlineComments(serialized)).toEqual([
51 expect.objectContaining({
52 sectionId: "section-1",
53 sectionTitle: "Working tree",
54 filePath: "apps/demo/src/main.ts",
55 startIndex: 0,
56 endIndex: 1,
57 text: "Please keep this configurable.",
58 diff: expect.stringContaining("-const retryLimit = 2;"),
59 }),
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});