The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import type { Root, RootContent } from "mdast"; |
| 2 | import remarkGfm from "remark-gfm"; |
| 3 | import remarkParse from "remark-parse"; |
| 4 | import { unified } from "unified"; |
| 5 | import { describe, expect, it } from "vite-plus/test"; |
| 6 | |
| 7 | import { |
| 8 | changeRequestReferenceAutolinker, |
| 9 | resolveChangeRequestMediaUrl, |
| 10 | splitPullRequestBody, |
| 11 | type PullRequestBodySegment, |
| 12 | } from "./pullRequestMarkdown.ts"; |
| 13 | |
| 14 | describe("pull request body segmentation", () => { |
| 15 | // GitHub writes a dropped image into the body as an `<img>` tag, so a bare attachment link on |
| 16 | // its own line is the shape it uses for a video — every one sampled in the wild was one. |
| 17 | it("lifts a dropped video out and keeps the prose around it", () => { |
| 18 | expect( |
| 19 | splitPullRequestBody( |
| 20 | "Before\n\nhttps://github.com/user-attachments/assets/2f8c1a90-1b2c-4d5e-8f90-abcdef123456\n\nAfter", |
| 21 | ), |
| 22 | ).toEqual([ |
| 23 | { id: "markdown:0", kind: "markdown", text: "Before", sourceOffset: 0 }, |
| 24 | { |
| 25 | id: "attachment:1", |
| 26 | kind: "attachment", |
| 27 | url: "https://github.com/user-attachments/assets/2f8c1a90-1b2c-4d5e-8f90-abcdef123456", |
| 28 | media: "video", |
| 29 | }, |
| 30 | { id: "markdown:2", kind: "markdown", text: "After", sourceOffset: 89 }, |
| 31 | ]); |
| 32 | }); |
| 33 | |
| 34 | it("places each prose run at its offset in the body, including inside a media table", () => { |
| 35 | const body = |
| 36 | '\n\n- [ ] Before\r\n\nhttps://example.com/shot.png\n\n\n<video controls>\n <source src="https://example.com/a.mp4">\n</video>\n - [ ] code\n- [x] After\n\r\n<table>\r\n<tr><td>\r\n\r\n- [ ] In a cell\r\n\r\n</td><td>\r\n\r\nhttps://example.com/b.mp4\r\n\r\n</td></tr>\r\n</table>\r\n- [ ] Last\n'; |
| 37 | type Prose = Extract<PullRequestBodySegment, { kind: "markdown" }>; |
| 38 | const prose = (segments: ReadonlyArray<PullRequestBodySegment>): Prose[] => |
| 39 | segments.flatMap((segment) => |
| 40 | segment.kind === "markdown" |
| 41 | ? [segment] |
| 42 | : segment.kind === "table" |
| 43 | ? segment.rows.flat().flatMap((cell) => prose(cell.segments)) |
| 44 | : [], |
| 45 | ); |
| 46 | const runs = prose(splitPullRequestBody(body)); |
| 47 | expect(runs.map((segment) => segment.text)).toEqual([ |
| 48 | "- [ ] Before", |
| 49 | " - [ ] code\n- [x] After", |
| 50 | "- [ ] In a cell", |
| 51 | "- [ ] Last", |
| 52 | ]); |
| 53 | for (const segment of runs) { |
| 54 | expect(body.slice(segment.sourceOffset, segment.sourceOffset + segment.text.length)).toBe( |
| 55 | segment.text, |
| 56 | ); |
| 57 | } |
| 58 | }); |
| 59 | |
| 60 | it("reads the source out of a video tag, including a multi-line one", () => { |
| 61 | expect( |
| 62 | splitPullRequestBody( |
| 63 | '<video controls>\n <source src="https://example.com/a.webm">\n</video>', |
| 64 | ), |
| 65 | ).toEqual([ |
| 66 | { |
| 67 | id: "attachment:0", |
| 68 | kind: "attachment", |
| 69 | url: "https://example.com/a.webm", |
| 70 | media: "video", |
| 71 | }, |
| 72 | ]); |
| 73 | }); |
| 74 | |
| 75 | it("leaves the img tag GitHub writes for a dropped image as markdown", () => { |
| 76 | const body = |
| 77 | '<img width="1414" alt="image" src="https://github.com/user-attachments/assets/7195f963-51a9-4331-be74-3e06be760422" />'; |
| 78 | expect(splitPullRequestBody(body)).toEqual([ |
| 79 | { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 }, |
| 80 | ]); |
| 81 | }); |
| 82 | |
| 83 | it("leaves an ordinary link alone, whether it is bare or written as markdown", () => { |
| 84 | const body = "https://example.com/page\n\n[the docs](https://example.com/docs)"; |
| 85 | expect(splitPullRequestBody(body)).toEqual([ |
| 86 | { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 }, |
| 87 | ]); |
| 88 | }); |
| 89 | |
| 90 | it("never lifts a link inside fenced code out of it", () => { |
| 91 | const body = "```\nhttps://example.com/demo.mp4\n```"; |
| 92 | expect(splitPullRequestBody(body)).toEqual([ |
| 93 | { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 }, |
| 94 | ]); |
| 95 | }); |
| 96 | |
| 97 | // GitHub keeps CRLF in a body written in its web editor. |
| 98 | it("recognizes fences in a body with CRLF line endings", () => { |
| 99 | const body = "```\r\nhttps://example.com/demo.mp4\r\n```"; |
| 100 | expect(splitPullRequestBody(body)).toEqual([ |
| 101 | { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 }, |
| 102 | ]); |
| 103 | }); |
| 104 | |
| 105 | it("closes a CRLF fence, so a video after it still lifts out", () => { |
| 106 | const code = '```html\r\n<video src="https://example.com/example.mp4"></video>\r\n```'; |
| 107 | expect(splitPullRequestBody(`${code}\r\n\r\nhttps://example.com/demo.mp4\r\n`)).toEqual([ |
| 108 | { id: "markdown:0", kind: "markdown", text: code, sourceOffset: 0 }, |
| 109 | { |
| 110 | id: "attachment:1", |
| 111 | kind: "attachment", |
| 112 | url: "https://example.com/demo.mp4", |
| 113 | media: "video", |
| 114 | }, |
| 115 | ]); |
| 116 | }); |
| 117 | |
| 118 | it("does not let a different fence marker close an open fence", () => { |
| 119 | const body = "```\n~~~\nhttps://example.com/demo.mp4\n```"; |
| 120 | expect(splitPullRequestBody(body)).toEqual([ |
| 121 | { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 }, |
| 122 | ]); |
| 123 | }); |
| 124 | |
| 125 | it("keeps a video tag that shares its line with prose as markdown", () => { |
| 126 | const body = 'Before <video src="https://example.com/a.mp4"></video> after'; |
| 127 | expect(splitPullRequestBody(body)).toEqual([ |
| 128 | { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 }, |
| 129 | ]); |
| 130 | }); |
| 131 | |
| 132 | it("treats an info-string fence as a nested opener, not a close", () => { |
| 133 | const body = "```\n```javascript\nhttps://example.com/demo.mp4\n```"; |
| 134 | expect(splitPullRequestBody(body)).toEqual([ |
| 135 | { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 }, |
| 136 | ]); |
| 137 | }); |
| 138 | |
| 139 | it("does not open a fence on a line of inline triple-backtick code", () => { |
| 140 | const code = "```\nhttps://example.com/b.png\n```"; |
| 141 | expect( |
| 142 | splitPullRequestBody( |
| 143 | `\`\`\`npm test\`\`\` passes.\n\nhttps://example.com/demo.mp4\n\n${code}`, |
| 144 | ), |
| 145 | ).toEqual([ |
| 146 | { id: "markdown:0", kind: "markdown", text: "```npm test``` passes.", sourceOffset: 0 }, |
| 147 | { |
| 148 | id: "attachment:1", |
| 149 | kind: "attachment", |
| 150 | url: "https://example.com/demo.mp4", |
| 151 | media: "video", |
| 152 | }, |
| 153 | { id: "markdown:2", kind: "markdown", text: code, sourceOffset: 54 }, |
| 154 | ]); |
| 155 | const multilineSpan = "```foo`\nhttps://example.com/demo.mp4\nbar```"; |
| 156 | expect(splitPullRequestBody(multilineSpan)).toEqual([ |
| 157 | { id: "markdown:0", kind: "markdown", text: multilineSpan, sourceOffset: 0 }, |
| 158 | ]); |
| 159 | }); |
| 160 | |
| 161 | it("leaves an indented code line alone even when it is only a video link", () => { |
| 162 | const body = " https://example.com/demo.mp4"; |
| 163 | expect(splitPullRequestBody(body)).toEqual([ |
| 164 | { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 }, |
| 165 | ]); |
| 166 | }); |
| 167 | |
| 168 | it("leaves an unclosed video tag as prose without eating the rest of the body", () => { |
| 169 | const body = ['<video src="https://example.com/a.mp4">', "still prose", "more prose"].join( |
| 170 | "\n", |
| 171 | ); |
| 172 | expect(splitPullRequestBody(body)).toEqual([ |
| 173 | { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 }, |
| 174 | ]); |
| 175 | }); |
| 176 | |
| 177 | it("refuses a non-http source rather than making a link out of it", () => { |
| 178 | const body = '<video src="javascript:alert(1)"></video>'; |
| 179 | expect(splitPullRequestBody(body)).toEqual([ |
| 180 | { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 }, |
| 181 | ]); |
| 182 | }); |
| 183 | }); |
| 184 | |
| 185 | describe("reference links around lifted media", () => { |
| 186 | const recording = |
| 187 | "https://github.com/user-attachments/assets/2f8c1a90-1b2c-4d5e-8f90-abcdef123456"; |
| 188 | const parser = unified().use(remarkParse).use(remarkGfm); |
| 189 | const prose = (segments: ReadonlyArray<PullRequestBodySegment>): string[] => |
| 190 | segments.flatMap((segment) => |
| 191 | segment.kind === "markdown" |
| 192 | ? [segment.text] |
| 193 | : segment.kind === "table" |
| 194 | ? segment.rows.flat().flatMap((cell) => prose(cell.segments)) |
| 195 | : [], |
| 196 | ); |
| 197 | // Each segment renders as its own document; the first definition of a label wins, as in mdast-util-to-hast. |
| 198 | const referenceLinks = (body: string) => |
| 199 | prose(splitPullRequestBody(body)).flatMap((text) => { |
| 200 | const definitions = new Map<string, string>(); |
| 201 | const references: Array<{ identifier: string; text: string }> = []; |
| 202 | const visit = (node: Root | RootContent) => { |
| 203 | if (node.type === "definition" && !definitions.has(node.identifier)) { |
| 204 | definitions.set(node.identifier, node.url); |
| 205 | } |
| 206 | if (node.type === "linkReference") { |
| 207 | references.push({ |
| 208 | identifier: node.identifier, |
| 209 | text: node.children.map((child) => ("value" in child ? child.value : "")).join(""), |
| 210 | }); |
| 211 | } |
| 212 | if ("children" in node) node.children.forEach(visit); |
| 213 | }; |
| 214 | visit(parser.parse(text)); |
| 215 | return references.map((reference) => [reference.text, definitions.get(reference.identifier)]); |
| 216 | }); |
| 217 | |
| 218 | it.each([ |
| 219 | [ |
| 220 | "use, then media, then definition", |
| 221 | `See [the docs][guide].\n\n${recording}\n\n[guide]: https://example.com/docs`, |
| 222 | ], |
| 223 | [ |
| 224 | "definition, then media, then use", |
| 225 | `[guide]: https://example.com/docs\n\n${recording}\n\nSee [the docs][guide].`, |
| 226 | ], |
| 227 | ...["\n", "\r\n", "\r"].map((lineEnding) => [ |
| 228 | `a definition wrapped over quoted lines with ${JSON.stringify(lineEnding)}`, |
| 229 | `> [guide]:${lineEnding}> https://example.com/docs${lineEnding}> "The guide"\n\n${recording}\n\nSee [the docs][guide].`, |
| 230 | ]), |
| 231 | [ |
| 232 | "a use inside a media table cell", |
| 233 | `<table><tr><td>\n\nSee [the docs][guide].\n\n</td><td>\n\n${recording}\n\n</td></tr></table>\n\n[guide]: https://example.com/docs`, |
| 234 | ], |
| 235 | ])("resolves a reference defined across a video: %s", (_case, body) => { |
| 236 | expect(referenceLinks(body)).toEqual([["the docs", "https://example.com/docs"]]); |
| 237 | }); |
| 238 | |
| 239 | it("keeps the body's first definition of a label, wherever the media falls", () => { |
| 240 | expect( |
| 241 | referenceLinks( |
| 242 | `[guide]: https://example.com/first\n\n${recording}\n\nSee [the docs][guide].\n\n[GUIDE]: https://example.com/second`, |
| 243 | ), |
| 244 | ).toEqual([["the docs", "https://example.com/first"]]); |
| 245 | }); |
| 246 | |
| 247 | it("leaves a definition shown in code as code", () => { |
| 248 | expect( |
| 249 | referenceLinks( |
| 250 | `\`\`\`md\n[guide]: https://example.com/docs\n\`\`\`\n\n${recording}\n\nSee [the docs][guide].`, |
| 251 | ), |
| 252 | ).toEqual([]); |
| 253 | }); |
| 254 | |
| 255 | it("still writes a task tick back to its place in the body", () => { |
| 256 | const body = `[guide]: https://example.com/docs\n\n${recording}\n\n- [ ] Read [the docs][guide]`; |
| 257 | const segment = splitPullRequestBody(body).at(-1)!; |
| 258 | if (segment.kind !== "markdown") throw new Error("expected prose last"); |
| 259 | const marker = segment.text.indexOf("[ ]"); |
| 260 | expect(body.slice(segment.sourceOffset + marker)).toBe("[ ] Read [the docs][guide]"); |
| 261 | }); |
| 262 | }); |
| 263 | |
| 264 | describe("media on custom hosts", () => { |
| 265 | it.each([ |
| 266 | ["https://drop.example.com/capture.PNG?token=abc#preview", "image"], |
| 267 | ["[Recording](https://drop.example.com/recording.mov)", "video"], |
| 268 | ["<https://drop.example.com/shot.webp>", "image"], |
| 269 | ])("recognizes %s without a request", (body, media) => { |
| 270 | expect(splitPullRequestBody(body)).toEqual([ |
| 271 | expect.objectContaining({ kind: "attachment", media }), |
| 272 | ]); |
| 273 | }); |
| 274 | |
| 275 | it("keeps inline links and media examples in code as prose", () => { |
| 276 | const body = |
| 277 | "See [recording](https://drop.example.com/a.mp4) for context.\n\n```md\nhttps://drop.example.com/a.png\n```"; |
| 278 | expect(splitPullRequestBody(body)).toEqual([ |
| 279 | { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 }, |
| 280 | ]); |
| 281 | }); |
| 282 | }); |
| 283 | |
| 284 | describe("media tables", () => { |
| 285 | const before = "https://github.com/user-attachments/assets/c69c7c1c-ef2f-47dd-97e8-fe42e40712eb"; |
| 286 | const after = "https://github.com/user-attachments/assets/1867b6ec-4f88-4248-b2df-6b0f9808105b"; |
| 287 | const video = "https://example.com/a.mp4"; |
| 288 | |
| 289 | it("keeps GitHub's before/after video table together, splitting each cell on its own", () => { |
| 290 | const segments = splitPullRequestBody( |
| 291 | `Intro\n\n<table>\n<tr><th>Before</th><th>After</th></tr>\n<tr>\n<td>\n\n${before}\n\n</td>\n<td>\n\n${after}\n\n</td>\n</tr>\n</table>\n\nOutro`, |
| 292 | ); |
| 293 | expect(segments.map((segment) => segment.kind)).toEqual(["markdown", "table", "markdown"]); |
| 294 | expect(segments[0]).toMatchObject({ text: "Intro" }); |
| 295 | expect(segments[2]).toMatchObject({ text: "Outro" }); |
| 296 | const table = segments[1]!; |
| 297 | if (table.kind !== "table") throw new Error("expected a table segment"); |
| 298 | expect( |
| 299 | table.rows.map((row) => |
| 300 | row.map((cell) => |
| 301 | cell.segments.map((segment) => |
| 302 | segment.kind === "attachment" ? `attachment:${segment.url}` : segment, |
| 303 | ), |
| 304 | ), |
| 305 | ), |
| 306 | ).toEqual([ |
| 307 | [ |
| 308 | [{ id: "markdown:0", kind: "markdown", text: "Before", sourceOffset: 23 }], |
| 309 | [{ id: "markdown:0", kind: "markdown", text: "After", sourceOffset: 38 }], |
| 310 | ], |
| 311 | [[`attachment:${before}`], [`attachment:${after}`]], |
| 312 | ]); |
| 313 | }); |
| 314 | |
| 315 | it("keeps a quoted `>` inside a cell's attributes out of its text", () => { |
| 316 | const [table] = splitPullRequestBody( |
| 317 | `<table>\n<tr><td title="Before > after">Caption</td><td>\n\n${video}\n\n</td></tr>\n</table>`, |
| 318 | ); |
| 319 | expect(table).toMatchObject({ |
| 320 | kind: "table", |
| 321 | rows: [[{ segments: [{ kind: "markdown", text: "Caption" }] }, {}]], |
| 322 | }); |
| 323 | }); |
| 324 | |
| 325 | it("leaves a table without media links as markdown", () => { |
| 326 | const body = "<table>\n<tr><td>a</td><td>b</td></tr>\n</table>"; |
| 327 | expect(splitPullRequestBody(body)).toEqual([ |
| 328 | { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 }, |
| 329 | ]); |
| 330 | }); |
| 331 | |
| 332 | it.each([ |
| 333 | ["a caption", "<table>\n<caption>Before upgrading</caption>\n<tr><td>", "</td></tr>\n</table>"], |
| 334 | ["text outside cells", "<table>\nBefore upgrading\n<tr><td>", "</td></tr>\n</table>"], |
| 335 | [ |
| 336 | "text inside a row but outside cells", |
| 337 | "<table>\n<tr>Before upgrading<td>", |
| 338 | "</td></tr>\n</table>", |
| 339 | ], |
| 340 | [ |
| 341 | "a table nested in another", |
| 342 | "<table>\n<tr><td>\n<table>\n<tr><td>", |
| 343 | "</td></tr>\n</table>\n</td></tr>\n</table>", |
| 344 | ], |
| 345 | ["an unclosed table", "<table>\n<tr><td>", ""], |
| 346 | ["a spanning cell", '<table>\n<tr><td colspan="2">', "</td></tr>\n</table>"], |
| 347 | [ |
| 348 | "an indented cell", |
| 349 | "<table>\n <tr>\n <th>\n Before\n </th>\n </tr>\n <tr><td>", |
| 350 | "</td></tr>\n</table>", |
| 351 | ], |
| 352 | [ |
| 353 | "a cell indented by a space and a tab", |
| 354 | "<table>\n<tr><td>\n \tBefore\n</td><td>", |
| 355 | "</td></tr>\n</table>", |
| 356 | ], |
| 357 | ])("splits a media table with %s line by line, as before", (_name, open, close) => { |
| 358 | const body = `${open}\n\n${video}\n\n${close}`; |
| 359 | expect(splitPullRequestBody(body)).toEqual([ |
| 360 | { id: "markdown:0", kind: "markdown", text: open, sourceOffset: 0 }, |
| 361 | { id: "attachment:1", kind: "attachment", url: video, media: "video" }, |
| 362 | ...(close |
| 363 | ? [ |
| 364 | { |
| 365 | id: "markdown:2", |
| 366 | kind: "markdown", |
| 367 | text: close, |
| 368 | sourceOffset: body.length - close.length, |
| 369 | }, |
| 370 | ] |
| 371 | : []), |
| 372 | ]); |
| 373 | }); |
| 374 | }); |
| 375 | |
| 376 | describe("change request reference autolinks", () => { |
| 377 | const urls = (provider: string, url: string, text: string) => |
| 378 | changeRequestReferenceAutolinker({ provider, url })?.(text).map((link) => link.url) ?? null; |
| 379 | |
| 380 | it("links references against the change request's own repository", () => { |
| 381 | expect(urls("github", "https://ghe.example.com/acme/web/pull/7", "Fixes #5")).toEqual([ |
| 382 | "https://ghe.example.com/acme/web/issues/5", |
| 383 | ]); |
| 384 | }); |
| 385 | |
| 386 | // regression: the mobile review guide linked every host's `#5` with GitHub's grammar |
| 387 | it("links nothing on a host whose grammar is not applied", () => { |
| 388 | expect(urls("forgejo", "https://codeberg.org/acme/web/pulls/7", "#5")).toBeNull(); |
| 389 | }); |
| 390 | }); |
| 391 | |
| 392 | describe("change request media urls", () => { |
| 393 | // regression: GitCafe's `/api/attachments/<id>` uploads rendered as missing local files |
| 394 | it.each([ |
| 395 | ["/api/attachments/attach_1", "https://git.cafe/api/attachments/attach_1"], |
| 396 | ["//cdn.example.com/a.png", "//cdn.example.com/a.png"], |
| 397 | ["/\\evil.example.com/a.png", "/\\evil.example.com/a.png"], |
| 398 | ["https://example.com/a.png", "https://example.com/a.png"], |
| 399 | ["docs/a.png", "docs/a.png"], |
| 400 | ])("resolves %s against the forge", (url, expected) => { |
| 401 | expect(resolveChangeRequestMediaUrl(url, "https://git.cafe/pingdotgg/t3code/pulls/31251")).toBe( |
| 402 | expected, |
| 403 | ); |
| 404 | }); |
| 405 | }); |