Open History to see the last commit
1import type { Element, Root } from "hast";
2import { describe, expect, it } from "vite-plus/test";
3
4import { wordsHast } from "~/markdown/wordsHast";
5import {
6 pullRequestCommentPreview,
7 pullRequestMarkdownExtensions,
8} from "./pullRequestMarkdown.logic";
9
10function links(markdown: string) {
11 const extensions = pullRequestMarkdownExtensions({
12 provider: "github",
13 url: "https://github.com/team/repo/pull/8",
14 });
15 const tree = wordsHast(markdown, {
16 math: false,
17 breaks: false,
18 escapeHtml: false,
19 insights: false,
20 extensions,
21 });
22 const found: Array<[string, unknown, unknown]> = [];
23 const visit = (node: Root | Element) => {
24 for (const child of node.children) {
25 if (child.type !== "element") continue;
26 if (child.tagName === "a") {
27 const label = child.children[0];
28 found.push([
29 label?.type === "text" ? label.value : "",
30 child.properties.href,
31 child.properties.dataPullRequestAutolink,
32 ]);
33 }
34 visit(child);
35 }
36 };
37 visit(tree);
38 return found;
39}
40
41describe("pullRequestMarkdownExtensions", () => {
42 it("links the host's references in prose and leaves links and code alone", () => {
43 expect(links("Fixes #12 in **abcdef1**. [#13](https://example.com) `#14`")).toEqual([
44 ["#12", "https://github.com/team/repo/issues/12", "reference"],
45 ["abcdef1", "https://github.com/team/repo/commit/abcdef1", "commit"],
46 ["#13", "https://example.com", undefined],
47 ]);
48 });
49 it("renders GitHub emoji shortcodes in prose and leaves code literal", () => {
50 const extensions = pullRequestMarkdownExtensions({
51 provider: "github",
52 url: "https://github.com/team/repo/pull/8",
53 });
54 const tree = wordsHast("Ship it :tada: :+1: `:tada:`", {
55 math: false,
56 breaks: false,
57 escapeHtml: false,
58 insights: false,
59 extensions,
60 });
61 const text = (node: Root | Element | Root["children"][number]): string => {
62 if (node.type === "text") return node.value;
63 if (node.type === "element" && node.tagName === "code") {
64 return `\`${node.children.map(text).join("")}\``;
65 }
66 return "children" in node ? node.children.map(text).join("") : "";
67 };
68 expect(text(tree)).toBe("Ship it 🎉 👍 `:tada:`");
69 });
70});
71
72describe("pullRequestCommentPreview", () => {
73 it.each([
74 ["<!-- hidden -->\n> [!WARNING]\n**Guard** [this](https://example.com).", "Guard this."],
75 [
76 '<a href="https://example.com"><img src="badge.svg" /></a>\n<strong>Tasks keep the wrong computer</strong>',
77 "Tasks keep the wrong computer",
78 ],
79 [
80 "<details><summary>Review & fixes</summary><p>First<br>Second</p></details>",
81 "Review & fixes First Second",
82 ],
83 ['<p title="value > limit">Keep <strong>this</strong> readable.</p>', "Keep this readable."],
84 [
85 "Use `<div>` and `Promise<void>`.\n\n```html\n<span>example</span>\n```",
86 "Use <div> and Promise<void>. <span>example</span>",
87 ],
88 [" **Review**", "badge Review"],
89 ["```md\n[!TIP]\n> [!WARNING]\n```\n\n [!NOTE]", "[!TIP] > [!WARNING] [!NOTE]"],
90 [
91 "Review :codex-file-citation{purpose='output'} safely.",
92 "Review :codex-file-citation{purpose='output'} safely.",
93 ],
94 [":codex-followup[Prepare **print** version]", ":codex-followup[Prepare **print** version]"],
95 ["Approved :thumbsup: `:warning:`", "Approved 👍 :warning:"],
96 ["Approved :thumbsup:", "Approved :thumbsup:", "gitlab"],
97 ])("extracts readable text from %s", (body, expected, provider = "github") => {
98 expect(pullRequestCommentPreview(body, provider)).toBe(expected);
99 });
100});