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

referenceAutolinks.test.ts

57 lines2.3 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { gitHubReferenceAutolinks } from "./referenceAutolinks.ts";
4​
5const repositoryUrl = "https://github.com/team/repo";
6const linked = (text: string) =>
7 gitHubReferenceAutolinks(text, repositoryUrl).map(({ label, url, kind }) => [label, url, kind]);
8​
9describe("gitHubReferenceAutolinks", () => {
10 it("links references, cross-repository references, commits and mentions", () => {
11 expect(
12 linked(
13 "Closes #123, fixes other/project#9; other#4 @julius @team/mobile abc1234 other/project@abcdef1",
14 ),
15 ).toEqual([
16 ["#123", "https://github.com/team/repo/issues/123", "reference"],
17 ["other/project#9", "https://github.com/other/project/issues/9", "reference"],
18 ["other#4", "https://github.com/other/repo/issues/4", "reference"],
19 ["@julius", "https://github.com/julius", "mention"],
20 ["@team/mobile", "https://github.com/orgs/team/teams/mobile", "mention"],
21 ["abc1234", "https://github.com/team/repo/commit/abc1234", "commit"],
22 ["other/project@abcdef1", "https://github.com/other/project/commit/abcdef1", "commit"],
23 ]);
24 });
25​
26 it("shows a full SHA by its first seven characters", () => {
27 const sha = "0123456789abcdef0123456789abcdef01234567";
28 expect(linked(`Reverts ${sha}.`)).toEqual([
29 ["0123456", `https://github.com/team/repo/commit/${sha}`, "commit"],
30 ]);
31 });
32​
33 it("leaves URLs, escapes, emails, words and plain numbers alone", () => {
34 expect(linked("https://example.com/#123 user@example.com \\#123 1234567 issue#x a#1b")).toEqual(
35 [],
36 );
37 });
38​
39 it("scans long runs of periods and dashes in linear time", () => {
40 expect(linked(`${".".repeat(50_000)} ${".-".repeat(25_000)} #1`)).toEqual([
41 ["#1", "https://github.com/team/repo/issues/1", "reference"],
42 ]);
43 });
44​
45 it("links references that follow punctuation", () => {
46 expect(linked("(#5), owner.name/repo-x#3")).toEqual([
47 ["#5", "https://github.com/team/repo/issues/5", "reference"],
48 ["owner.name/repo-x#3", "https://github.com/owner.name/repo-x/issues/3", "reference"],
49 ]);
50 });
51​
52 it("links against the repository's own host", () => {
53 expect(
54 gitHubReferenceAutolinks("See #5", "https://ghe.example.com/acme/web").map(({ url }) => url),
55 ).toEqual(["https://ghe.example.com/acme/web/issues/5"]);
56 });
57});