Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestMarkdown.logic.test.ts

74 lines2.7 KB

1import { changeRequestReferenceAutolinker } from "@t3tools/client-runtime/pull-request-markdown";
2import remarkParse from "remark-parse";
3import { unified } from "unified";
4import { describe, expect, it } from "vite-plus/test";
5​
6import { pullRequestCommentPreview, remarkPullRequestAutolinks } from "./pullRequestMarkdown.logic";
7​
8type Node = {
9 type: string;
10 value?: string;
11 url?: string;
12 data?: { hProperties?: Record<string, unknown> };
13 children?: Node[];
14};
15​
16function links(markdown: string) {
17 const autolinks = changeRequestReferenceAutolinker({
18 provider: "github",
19 url: "https://github.com/team/repo/pull/8",
20 })!;
21 const tree = unified().use(remarkParse).parse(markdown) as Node;
22 remarkPullRequestAutolinks({ autolinks })(tree as never);
23 const found: Array<[string | undefined, string | undefined, unknown]> = [];
24 const visit = (node: Node) => {
25 if (node.type === "link") {
26 found.push([
27 node.children?.[0]?.value,
28 node.url,
29 node.data?.hProperties?.["dataPullRequestAutolink"],
30 ]);
31 }
32 node.children?.forEach(visit);
33 };
34 visit(tree);
35 return found;
37​
38describe("remarkPullRequestAutolinks", () => {
39 it("links the host's references in prose and leaves links and code alone", () => {
40 expect(links("Fixes #12 in **abcdef1**. [#13](https://example.com) `#14`")).toEqual([
41 ["#12", "https://github.com/team/repo/issues/12", "reference"],
42 ["abcdef1", "https://github.com/team/repo/commit/abcdef1", "commit"],
43 ["#13", "https://example.com", undefined],
44 ]);
45 });
46});
47​
48describe("pullRequestCommentPreview", () => {
49 it.each([
50 ["<!-- hidden -->\n> [!WARNING]\n**Guard** [this](https://example.com).", "Guard this."],
51 [
52 '<a href="https://example.com"><img src="badge.svg" /></a>\n<strong>Tasks keep the wrong computer</strong>',
53 "Tasks keep the wrong computer",
54 ],
55 [
56 "<details><summary>Review &amp; fixes</summary><p>First<br>Second</p></details>",
57 "Review & fixes First Second",
58 ],
59 ['<p title="value > limit">Keep <strong>this</strong> readable.</p>', "Keep this readable."],
60 [
61 "Use `<div>` and `Promise<void>`.\n\n```html\n<span>example</span>\n```",
62 "Use <div> and Promise<void>. <span>example</span>",
63 ],
64 ["![badge](https://example.com/badge.svg) **Review**", "badge Review"],
65 ["```md\n[!TIP]\n> [!WARNING]\n```\n\n [!NOTE]", "[!TIP] > [!WARNING] [!NOTE]"],
66 [
67 "Review :codex-file-citation{purpose='output'} safely.",
68 "Review :codex-file-citation{purpose='output'} safely.",
69 ],
70 [":codex-followup[Prepare **print** version]", ":codex-followup[Prepare **print** version]"],
71 ])("extracts readable text from %s", (body, expected) => {
72 expect(pullRequestCommentPreview(body)).toBe(expected);
73 });
74});