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

changeRequestUrl.test.ts

142 lines5.4 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 changeRequestUrlFor,
5 parseChangeRequestUrl,
6 pullRequestCandidateUrlFromReferenceAutolink,
7 siblingPullRequestUrl,
8} from "./changeRequestUrl.ts";
9​
10describe("parseChangeRequestUrl", () => {
11 it("reads a GitHub pull request, lower-casing the repository", () => {
12 expect(parseChangeRequestUrl("https://github.com/T3Tools/T3Code/pull/123")).toEqual({
13 host: "github.com",
14 repository: "t3tools/t3code",
15 number: 123,
16 });
17 });
18​
19 it("reads a pull request on a GitHub Enterprise host", () => {
20 expect(parseChangeRequestUrl("https://github.acme.test/platform/api/pull/7")).toEqual({
21 host: "github.acme.test",
22 repository: "platform/api",
23 number: 7,
24 });
25 });
26​
27 it("reads a supported GitHub host with a middle DNS label", () => {
28 expect(parseChangeRequestUrl("https://code.github.example.com/acme/web/pull/42")).toEqual({
29 host: "code.github.example.com",
30 repository: "acme/web",
31 number: 42,
32 });
33 expect(
34 pullRequestCandidateUrlFromReferenceAutolink(
35 "https://code.github.example.com/acme/web/issues/42",
36 ),
37 ).toBe("https://code.github.example.com/acme/web/pull/42");
38 });
39​
40 it("reads a GitLab merge request on any host, nested groups and all", () => {
41 expect(
42 parseChangeRequestUrl("https://gitlab.com/t3tools/platform/t3code/-/merge_requests/42"),
43 ).toEqual({ host: "gitlab.com", repository: "t3tools/platform/t3code", number: 42 });
44 expect(parseChangeRequestUrl("https://code.acme.test/team/project/-/merge_requests/9")).toEqual(
45 { host: "code.acme.test", repository: "team/project", number: 9 },
46 );
47 });
48​
49 it("reads Bitbucket and both Azure DevOps URL forms", () => {
50 expect(parseChangeRequestUrl("https://bitbucket.org/workspace/repo/pull-requests/5")).toEqual({
51 host: "bitbucket.org",
52 repository: "workspace/repo",
53 number: 5,
54 });
55 expect(
56 parseChangeRequestUrl("https://dev.azure.com/acme/platform/_git/t3code/pullrequest/17"),
57 ).toEqual({ host: "dev.azure.com", repository: "acme/platform/_git/t3code", number: 17 });
58 expect(
59 parseChangeRequestUrl("https://acme.visualstudio.com/platform/_git/t3code/pullrequest/17"),
60 ).toEqual({ host: "acme.visualstudio.com", repository: "platform/_git/t3code", number: 17 });
61 });
62​
63 it("survives trailing segments, a trailing slash and a query string", () => {
64 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/files?w=1")).toEqual({
65 host: "github.com",
66 repository: "t3tools/t3code",
67 number: 123,
68 });
69 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/")).toEqual({
70 host: "github.com",
71 repository: "t3tools/t3code",
72 number: 123,
73 });
74 });
75​
76 it("claims nothing it cannot be sure of", () => {
77 for (const link of [
78 "https://github.com/t3tools/t3code/issues/123",
79 "https://github.com/t3tools/t3code/commit/0a1b2c3",
80 "https://github.com/t3tools/t3code",
81 "https://github.com/t3tools/t3code/pull/abc",
82 "https://gitlab.com/t3tools/t3code/-/issues/12",
83 "https://blog.example.test/2026/updates/pull/3",
84 "javascript:alert(1)//github.com/t3tools/t3code/pull/1",
85 "not a url",
86 ]) {
87 expect(parseChangeRequestUrl(link), link).toBeNull();
88 }
89 });
90});
91​
92describe("siblingPullRequestUrl", () => {
93 it.each([
94 ["https://github.com/pull/1/pull/42/files", "https://github.com/pull/1/pull/43"],
95 [
96 "https://git.acme.test/team/merge_requests/1/repo/-/merge_requests/42/diffs",
97 "https://git.acme.test/team/merge_requests/1/repo/-/merge_requests/43",
98 ],
99​
100 ["https://github.com/acme/web/pull/42#discussion_r123", "https://github.com/acme/web/pull/43"],
101 ["https://github.com/acme/web/pull/42/files?w=1", "https://github.com/acme/web/pull/43"],
102 [
103 "https://github.acme.test:8443/acme/web/pull/42/",
104 "https://github.acme.test:8443/acme/web/pull/43",
105 ],
106 [
107 "https://git.acme.test/acme/web/-/merge_requests/42/diffs",
108 "https://git.acme.test/acme/web/-/merge_requests/43",
109 ],
110 [
111 "https://bitbucket.org/acme/web/pull-requests/42",
112 "https://bitbucket.org/acme/web/pull-requests/43",
113 ],
114 [
115 "https://dev.azure.com/acme/project/_git/web/pullrequest/42?view=files",
116 "https://dev.azure.com/acme/project/_git/web/pullrequest/43",
117 ],
118 ])("builds a canonical sibling of %s", (url, expected) => {
119 expect(siblingPullRequestUrl(url, 43)).toBe(expected);
120 });
121 it("rejects non-PR URLs and invalid numbers", () => {
122 expect(siblingPullRequestUrl("https://github.com/acme/web/issues/42", 43)).toBeNull();
123 expect(siblingPullRequestUrl("https://github.com/acme/web/pull/42", 0)).toBeNull();
124 });
125});
126​
127describe("changeRequestUrlFor", () => {
128 it.each([
129 ["ssh.dev.azure.com", "v3/org/project/web"],
130 ["vs-ssh.visualstudio.com", "v3/org/project/web"],
131 ["org.visualstudio.com", "defaultcollection/project/_git/web"],
132 ["dev.azure.com", "org/project/_git/web"],
133 ])("builds a browser URL from the Azure remote %s/%s", (host, repository) => {
134 const url = changeRequestUrlFor("azure-devops", host, repository, 42);
135 expect(url).toBe("https://dev.azure.com/org/project/_git/web/pullrequest/42");
136 expect(parseChangeRequestUrl(url!)).toEqual({
137 host: "dev.azure.com",
138 repository: "org/project/_git/web",
139 number: 42,
140 });
141 });
142});