Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { changeRequestReferenceAutolinker, splitPullRequestBody } from "./pullRequestMarkdown.ts";
4
5describe("pull request body segmentation", () => {
6 // GitHub writes a dropped image into the body as an `<img>` tag, so a bare attachment link on
7 // its own line is the shape it uses for a video — every one sampled in the wild was one.
8 it("lifts a dropped video out and keeps the prose around it", () => {
9 expect(
10 splitPullRequestBody(
11 "Before\n\nhttps://github.com/user-attachments/assets/2f8c1a90-1b2c-4d5e-8f90-abcdef123456\n\nAfter",
12 ),
13 ).toEqual([
14 { id: "markdown:0", kind: "markdown", text: "Before" },
15 {
16 id: "attachment:1",
17 kind: "attachment",
18 url: "https://github.com/user-attachments/assets/2f8c1a90-1b2c-4d5e-8f90-abcdef123456",
19 media: "video",
20 },
21 { id: "markdown:2", kind: "markdown", text: "After" },
22 ]);
23 });
24
25 it("reads the source out of a video tag, including a multi-line one", () => {
26 expect(
27 splitPullRequestBody(
28 '<video controls>\n <source src="https://example.com/a.webm">\n</video>',
29 ),
30 ).toEqual([
31 {
32 id: "attachment:0",
33 kind: "attachment",
34 url: "https://example.com/a.webm",
35 media: "video",
36 },
37 ]);
38 });
39
40 it("leaves the img tag GitHub writes for a dropped image as markdown", () => {
41 const body =
42 '<img width="1414" alt="image" src="https://github.com/user-attachments/assets/7195f963-51a9-4331-be74-3e06be760422" />';
43 expect(splitPullRequestBody(body)).toEqual([
44 { id: "markdown:0", kind: "markdown", text: body },
45 ]);
46 });
47
48 it("leaves an ordinary link alone, whether it is bare or written as markdown", () => {
49 const body = "https://example.com/page\n\n[the docs](https://example.com/docs)";
50 expect(splitPullRequestBody(body)).toEqual([
51 { id: "markdown:0", kind: "markdown", text: body },
52 ]);
53 });
54
55 it("never lifts a link inside fenced code out of it", () => {
56 const body = "```\nhttps://example.com/demo.mp4\n```";
57 expect(splitPullRequestBody(body)).toEqual([
58 { id: "markdown:0", kind: "markdown", text: body },
59 ]);
60 });
61
62 it("does not let a different fence marker close an open fence", () => {
63 const body = "```\n~~~\nhttps://example.com/demo.mp4\n```";
64 expect(splitPullRequestBody(body)).toEqual([
65 { id: "markdown:0", kind: "markdown", text: body },
66 ]);
67 });
68
69 it("keeps a video tag that shares its line with prose as markdown", () => {
70 const body = 'Before <video src="https://example.com/a.mp4"></video> after';
71 expect(splitPullRequestBody(body)).toEqual([
72 { id: "markdown:0", kind: "markdown", text: body },
73 ]);
74 });
75
76 it("treats an info-string fence as a nested opener, not a close", () => {
77 const body = "```\n```javascript\nhttps://example.com/demo.mp4\n```";
78 expect(splitPullRequestBody(body)).toEqual([
79 { id: "markdown:0", kind: "markdown", text: body },
80 ]);
81 });
82
83 it("leaves an indented code line alone even when it is only a video link", () => {
84 const body = " https://example.com/demo.mp4";
85 expect(splitPullRequestBody(body)).toEqual([
86 { id: "markdown:0", kind: "markdown", text: body },
87 ]);
88 });
89
90 it("leaves an unclosed video tag as prose without eating the rest of the body", () => {
91 const body = ['<video src="https://example.com/a.mp4">', "still prose", "more prose"].join(
92 "\n",
93 );
94 expect(splitPullRequestBody(body)).toEqual([
95 { id: "markdown:0", kind: "markdown", text: body },
96 ]);
97 });
98
99 it("refuses a non-http source rather than making a link out of it", () => {
100 const body = '<video src="javascript:alert(1)"></video>';
101 expect(splitPullRequestBody(body)).toEqual([
102 { id: "markdown:0", kind: "markdown", text: body },
103 ]);
104 });
105});
106
107describe("media on custom hosts", () => {
108 it.each([
109 ["https://drop.example.com/capture.PNG?token=abc#preview", "image"],
110 ["[Recording](https://drop.example.com/recording.mov)", "video"],
111 ["<https://drop.example.com/shot.webp>", "image"],
112 ])("recognizes %s without a request", (body, media) => {
113 expect(splitPullRequestBody(body)).toEqual([
114 expect.objectContaining({ kind: "attachment", media }),
115 ]);
116 });
117
118 it("keeps inline links and media examples in code as prose", () => {
119 const body =
120 "See [recording](https://drop.example.com/a.mp4) for context.\n\n```md\nhttps://drop.example.com/a.png\n```";
121 expect(splitPullRequestBody(body)).toEqual([
122 { id: "markdown:0", kind: "markdown", text: body },
123 ]);
124 });
125});
126
127describe("change request reference autolinks", () => {
128 const urls = (provider: string, url: string, text: string) =>
129 changeRequestReferenceAutolinker({ provider, url })?.(text).map((link) => link.url) ?? null;
130
131 it("links references against the change request's own repository", () => {
132 expect(urls("github", "https://ghe.example.com/acme/web/pull/7", "Fixes #5")).toEqual([
133 "https://ghe.example.com/acme/web/issues/5",
134 ]);
135 });
136
137 // regression: the mobile review guide linked every host's `#5` with GitHub's grammar
138 it("links nothing on a host whose grammar is not applied", () => {
139 expect(urls("gitlab", "https://gitlab.com/group/project/-/merge_requests/7", "#5")).toBeNull();
140 expect(urls("forgejo", "https://codeberg.org/acme/web/pulls/7", "#5")).toBeNull();
141 });
142});