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

pullRequestAutolinks.test.ts

73 lines3.4 KB

1import { changeRequestReferenceAutolinker } from "@t3tools/client-runtime/pull-request-references";
2import { setMarkdownTaskChecked } from "@t3tools/shared/markdownTaskEdits";
3import { markdownTaskMarkerOffset } from "@t3tools/shared/markdownTaskList";
4import { describe, expect, it } from "vite-plus/test";
5​
6import { nativePullRequestMarkdown } from "./pullRequestAutolinks";
7​
8const github = changeRequestReferenceAutolinker({
9 provider: "github",
10 url: "https://github.com/team/repo/pull/8",
11});
12​
13describe("nativePullRequestMarkdown", () => {
14 it("links the host's references in prose", () => {
15 expect(nativePullRequestMarkdown("Closes #123, see @julius", github, false).markdown).toBe(
16 "Closes [#123](https://github.com/team/repo/issues/123), see [@julius](https://github.com/julius)",
17 );
18 });
19​
20 it("leaves code, links, images, escapes and definitions alone", () => {
21 const text =
22 "`#123 @person abc1234`\n\n```\n#123\n```\n\n[#123](https://example.com) ![#123](image.png) https://example.com/#123 user@example.com \\#123 1234567\n\n[ref]: https://example.com/#123";
23 expect(nativePullRequestMarkdown(text, github, false).markdown).toBe(text);
24 });
25​
26 it("does not nest links inside HTML anchors or code", () => {
27 const text = 'See <a href="https://example.com">#12</a> and <code>#13</code>, then #14.';
28 expect(nativePullRequestMarkdown(text, github, false).markdown).toBe(
29 'See <a href="https://example.com">#12</a> and <code>#13</code>, then [#14](https://github.com/team/repo/issues/14).',
30 );
31 });
32​
33 it("leaves markdown unchanged for a host that links nothing", () => {
34 const forgejo = changeRequestReferenceAutolinker({
35 provider: "forgejo",
36 url: "https://codeberg.org/acme/web/pulls/7",
37 });
38 expect(nativePullRequestMarkdown("Closes #123", forgejo, false).markdown).toBe("Closes #123");
39 });
40​
41 it("expands GitHub emoji aliases in prose and link labels, mapping offsets back", () => {
42 const text =
43 "- [ ] Ship :tada: #1 `:tada:`\n- [ ] [:+1: docs](https://x.dev/:tada:) https://x.dev/:tada: :asterisk:";
44 const native = nativePullRequestMarkdown(text, github, true);
45 expect(native.markdown).toBe(
46 "- [ ] Ship 🎉 [#1](https://github.com/team/repo/issues/1) `:tada:`\n- [ ] [👍 docs](https://x.dev/:tada:) https://x.dev/:tada: \\*️⃣",
47 );
48 const marker = native.markdown.lastIndexOf("[ ]");
49 expect(native.sourceOffset(marker)).toBe(text.lastIndexOf("[ ]"));
50 });
51​
52 it("keeps rewrites and task offsets aligned after a leading BOM", () => {
53 const text = "\uFEFF[:+1: see](https://x.test) Fixes #12 :tada:\n- [ ] next";
54 const native = nativePullRequestMarkdown(text, github, true);
55 expect(native.markdown).toBe(
56 "[👍 see](https://x.test) Fixes [#12](https://github.com/team/repo/issues/12) 🎉\n- [ ] next",
57 );
58 const marker = native.markdown.lastIndexOf("[ ]");
59 expect(native.sourceOffset(marker)).toBe(text.lastIndexOf("[ ]"));
60 });
61​
62 it("hands the native renderer a BOM-prefixed task list it parses as tasks", () => {
63 const body = "\uFEFF- [ ] a\n- [ ] b\n";
64 for (const autolinks of [github, null]) {
65 const native = nativePullRequestMarkdown(body, autolinks, false);
66 expect(native.markdown).toBe("- [ ] a\n- [ ] b\n");
67 const offset = markdownTaskMarkerOffset(native.markdown, 1, "b", true);
68 expect(setMarkdownTaskChecked(body, native.sourceOffset(offset ?? -1), true)).toBe(
69 "\uFEFF- [ ] a\n- [x] b\n",
70 );
71 }
72 });
73});