The best way to control Claude Code, Codex and more from any computer or phone

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