Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 changeRequestReferenceAutolinker,
5 resolveChangeRequestMediaUrl,
6 splitPullRequestBody,
7} from "./pullRequestMarkdown.ts";
8
9describe("pull request body segmentation", () => {
10 // GitHub writes a dropped image into the body as an `<img>` tag, so a bare attachment link on
11 // its own line is the shape it uses for a video — every one sampled in the wild was one.
12 it("lifts a dropped video out and keeps the prose around it", () => {
13 expect(
14 splitPullRequestBody(
15 "Before\n\nhttps://github.com/user-attachments/assets/2f8c1a90-1b2c-4d5e-8f90-abcdef123456\n\nAfter",
16 ),
17 ).toEqual([
18 { id: "markdown:0", kind: "markdown", text: "Before", sourceOffset: 0 },
19 {
20 id: "attachment:1",
21 kind: "attachment",
22 url: "https://github.com/user-attachments/assets/2f8c1a90-1b2c-4d5e-8f90-abcdef123456",
23 media: "video",
24 },
25 { id: "markdown:2", kind: "markdown", text: "After", sourceOffset: 89 },
26 ]);
27 });
28
29 it("places each prose run at its offset in the body", () => {
30 const body =
31 '\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';
32 const prose = splitPullRequestBody(body).flatMap((segment) =>
33 segment.kind === "markdown" ? [segment] : [],
34 );
35 expect(prose.map((segment) => segment.text)).toEqual([
36 "- [ ] Before",
37 " - [ ] code\n- [x] After",
38 ]);
39 for (const segment of prose) {
40 expect(body.slice(segment.sourceOffset, segment.sourceOffset + segment.text.length)).toBe(
41 segment.text,
42 );
43 }
44 });
45
46 it("reads the source out of a video tag, including a multi-line one", () => {
47 expect(
48 splitPullRequestBody(
49 '<video controls>\n <source src="https://example.com/a.webm">\n</video>',
50 ),
51 ).toEqual([
52 {
53 id: "attachment:0",
54 kind: "attachment",
55 url: "https://example.com/a.webm",
56 media: "video",
57 },
58 ]);
59 });
60
61 it("leaves the img tag GitHub writes for a dropped image as markdown", () => {
62 const body =
63 '<img width="1414" alt="image" src="https://github.com/user-attachments/assets/7195f963-51a9-4331-be74-3e06be760422" />';
64 expect(splitPullRequestBody(body)).toEqual([
65 { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 },
66 ]);
67 });
68
69 it("leaves an ordinary link alone, whether it is bare or written as markdown", () => {
70 const body = "https://example.com/page\n\n[the docs](https://example.com/docs)";
71 expect(splitPullRequestBody(body)).toEqual([
72 { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 },
73 ]);
74 });
75
76 it("never lifts a link inside fenced code out of it", () => {
77 const body = "```\nhttps://example.com/demo.mp4\n```";
78 expect(splitPullRequestBody(body)).toEqual([
79 { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 },
80 ]);
81 });
82
83 // GitHub keeps CRLF in a body written in its web editor.
84 it("recognizes fences in a body with CRLF line endings", () => {
85 const body = "```\r\nhttps://example.com/demo.mp4\r\n```";
86 expect(splitPullRequestBody(body)).toEqual([
87 { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 },
88 ]);
89 });
90
91 it("closes a CRLF fence, so a video after it still lifts out", () => {
92 const code = '```html\r\n<video src="https://example.com/example.mp4"></video>\r\n```';
93 expect(splitPullRequestBody(`${code}\r\n\r\nhttps://example.com/demo.mp4\r\n`)).toEqual([
94 { id: "markdown:0", kind: "markdown", text: code, sourceOffset: 0 },
95 {
96 id: "attachment:1",
97 kind: "attachment",
98 url: "https://example.com/demo.mp4",
99 media: "video",
100 },
101 ]);
102 });
103
104 it("does not let a different fence marker close an open fence", () => {
105 const body = "```\n~~~\nhttps://example.com/demo.mp4\n```";
106 expect(splitPullRequestBody(body)).toEqual([
107 { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 },
108 ]);
109 });
110
111 it("keeps a video tag that shares its line with prose as markdown", () => {
112 const body = 'Before <video src="https://example.com/a.mp4"></video> after';
113 expect(splitPullRequestBody(body)).toEqual([
114 { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 },
115 ]);
116 });
117
118 it("treats an info-string fence as a nested opener, not a close", () => {
119 const body = "```\n```javascript\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("leaves an indented code line alone even when it is only a video link", () => {
126 const body = " https://example.com/demo.mp4";
127 expect(splitPullRequestBody(body)).toEqual([
128 { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 },
129 ]);
130 });
131
132 it("leaves an unclosed video tag as prose without eating the rest of the body", () => {
133 const body = ['<video src="https://example.com/a.mp4">', "still prose", "more prose"].join(
134 "\n",
135 );
136 expect(splitPullRequestBody(body)).toEqual([
137 { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 },
138 ]);
139 });
140
141 it("refuses a non-http source rather than making a link out of it", () => {
142 const body = '<video src="javascript:alert(1)"></video>';
143 expect(splitPullRequestBody(body)).toEqual([
144 { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 },
145 ]);
146 });
147});
148
149describe("media on custom hosts", () => {
150 it.each([
151 ["https://drop.example.com/capture.PNG?token=abc#preview", "image"],
152 ["[Recording](https://drop.example.com/recording.mov)", "video"],
153 ["<https://drop.example.com/shot.webp>", "image"],
154 ])("recognizes %s without a request", (body, media) => {
155 expect(splitPullRequestBody(body)).toEqual([
156 expect.objectContaining({ kind: "attachment", media }),
157 ]);
158 });
159
160 it("keeps inline links and media examples in code as prose", () => {
161 const body =
162 "See [recording](https://drop.example.com/a.mp4) for context.\n\n```md\nhttps://drop.example.com/a.png\n```";
163 expect(splitPullRequestBody(body)).toEqual([
164 { id: "markdown:0", kind: "markdown", text: body, sourceOffset: 0 },
165 ]);
166 });
167});
168
169describe("change request reference autolinks", () => {
170 const urls = (provider: string, url: string, text: string) =>
171 changeRequestReferenceAutolinker({ provider, url })?.(text).map((link) => link.url) ?? null;
172
173 it("links references against the change request's own repository", () => {
174 expect(urls("github", "https://ghe.example.com/acme/web/pull/7", "Fixes #5")).toEqual([
175 "https://ghe.example.com/acme/web/issues/5",
176 ]);
177 });
178
179 // regression: the mobile review guide linked every host's `#5` with GitHub's grammar
180 it("links nothing on a host whose grammar is not applied", () => {
181 expect(urls("forgejo", "https://codeberg.org/acme/web/pulls/7", "#5")).toBeNull();
182 });
183});
184
185describe("change request media urls", () => {
186 // regression: GitCafe's `/api/attachments/<id>` uploads rendered as missing local files
187 it.each([
188 ["/api/attachments/attach_1", "https://git.cafe/api/attachments/attach_1"],
189 ["//cdn.example.com/a.png", "//cdn.example.com/a.png"],
190 ["/\\evil.example.com/a.png", "/\\evil.example.com/a.png"],
191 ["https://example.com/a.png", "https://example.com/a.png"],
192 ["docs/a.png", "docs/a.png"],
193 ])("resolves %s against the forge", (url, expected) => {
194 expect(resolveChangeRequestMediaUrl(url, "https://git.cafe/pingdotgg/t3code/pulls/31251")).toBe(
195 expected,
196 );
197 });
198});