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