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

changeRequestUrl.test.ts

197 lines7.7 KB

1import { ProjectId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 changeRequestUrlFor,
6 matchesLinkedPullRequestUrl,
7 parseChangeRequestUrl,
8 pullRequestCandidateUrlFromReferenceAutolink,
9 siblingPullRequestUrl,
10} from "./changeRequestUrl.ts";
11import { normalizeThreadPullRequestKey } from "./threadPullRequests.ts";
12​
13describe("parseChangeRequestUrl", () => {
14 it("reads a GitHub pull request, lower-casing the repository", () => {
15 expect(parseChangeRequestUrl("https://github.com/T3Tools/T3Code/pull/123")).toEqual({
16 host: "github.com",
17 repository: "t3tools/t3code",
18 number: 123,
19 });
20 });
21​
22 it("reads a GitCafe pull request at its canonical /pull/ path and the older /pulls/ path", () => {
23 for (const path of ["pull", "pulls"]) {
24 expect(
25 parseChangeRequestUrl(`https://git.cafe/PingDotGG/T3Code/${path}/23897`),
26 path,
27 ).toEqual({ host: "git.cafe", repository: "pingdotgg/t3code", number: 23897 });
28 }
29 expect(parseChangeRequestUrl("https://staging.git.cafe/team/repo/pull/7/files")).toEqual({
30 host: "staging.git.cafe",
31 repository: "team/repo",
32 number: 7,
33 });
34 });
35​
36 it("reads Forgejo URLs even when the hostname contains github", () => {
37 expect(parseChangeRequestUrl("https://github.internal/team/repo/pulls/7")).toEqual({
38 host: "github.internal",
39 authority: "github.internal",
40 repository: "team/repo",
41 number: 7,
42 });
43 });
44​
45 it("reads a supported GitHub host with a middle DNS label", () => {
46 expect(parseChangeRequestUrl("https://code.github.example.com/acme/web/pull/42")).toEqual({
47 host: "code.github.example.com",
48 repository: "acme/web",
49 number: 42,
50 });
51 expect(
52 pullRequestCandidateUrlFromReferenceAutolink(
53 "https://code.github.example.com/acme/web/issues/42",
54 ),
55 ).toBe("https://code.github.example.com/acme/web/pull/42");
56 });
57​
58 it("reads a GitLab merge request on any host, nested groups and all", () => {
59 expect(
60 parseChangeRequestUrl("https://gitlab.com/t3tools/platform/t3code/-/merge_requests/42"),
61 ).toEqual({ host: "gitlab.com", repository: "t3tools/platform/t3code", number: 42 });
62 expect(parseChangeRequestUrl("https://code.acme.test/team/project/-/merge_requests/9")).toEqual(
63 { host: "code.acme.test", repository: "team/project", number: 9 },
64 );
65 });
66​
67 it("reads Bitbucket and both Azure DevOps URL forms", () => {
68 expect(parseChangeRequestUrl("https://bitbucket.org/workspace/repo/pull-requests/5")).toEqual({
69 host: "bitbucket.org",
70 repository: "workspace/repo",
71 number: 5,
72 });
73 expect(
74 parseChangeRequestUrl("https://dev.azure.com/acme/platform/_git/t3code/pullrequest/17"),
75 ).toEqual({ host: "dev.azure.com", repository: "acme/platform/_git/t3code", number: 17 });
76 expect(
77 parseChangeRequestUrl("https://acme.visualstudio.com/platform/_git/t3code/pullrequest/17"),
78 ).toEqual({ host: "acme.visualstudio.com", repository: "platform/_git/t3code", number: 17 });
79 });
80​
81 it("reads Azure DevOps Server links and keeps servers on different ports distinct", () => {
82 for (const host of ["scm.example.test", "git.cafe", "code.bitbucket.example.test"]) {
83 const repository = "tfs/collection/project/_git/repo";
84 const linked = {
85 projectId: ProjectId.make("project-1"),
86 host,
87 repository,
88 number: 123,
89 url: `https://${host.toUpperCase()}:8443/tfs/Collection/Project/_git/Repo/pullrequest/123`,
90 };
91 const other = { ...linked, url: linked.url.replace(":8443/", ":9443/") };
92 expect.soft(parseChangeRequestUrl(linked.url), host).toEqual({
93 host,
94 authority: `${host}:8443`,
95 repository,
96 number: 123,
97 });
98 expect.soft(matchesLinkedPullRequestUrl(linked, `${linked.url}?view=files`), host).toBe(true);
99 expect.soft(matchesLinkedPullRequestUrl(linked, other.url), host).toBe(false);
100 expect.soft([linked, other].map(normalizeThreadPullRequestKey), host).toEqual([
101 { host: `${host}:8443`, repository, number: 123 },
102 { host: `${host}:9443`, repository, number: 123 },
103 ]);
104 }
105 });
106​
107 it("survives trailing segments, a trailing slash and a query string", () => {
108 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/files?w=1")).toEqual({
109 host: "github.com",
110 repository: "t3tools/t3code",
111 number: 123,
112 });
113 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/")).toEqual({
114 host: "github.com",
115 repository: "t3tools/t3code",
116 number: 123,
117 });
118 });
119​
120 it("claims nothing it cannot be sure of", () => {
121 for (const link of [
122 "https://github.com/t3tools/t3code/issues/123",
123 "https://git.cafe/pingdotgg/t3code/issues/23897",
124 "https://git.cafe/pingdotgg/t3code/pull/abc",
125 "https://git.cafe/pingdotgg/t3code",
126 "https://github.com/t3tools/t3code/commit/0a1b2c3",
127 "https://github.com/t3tools/t3code",
128 "https://github.com/t3tools/t3code/pull/abc",
129 "https://gitlab.com/t3tools/t3code/-/issues/12",
130 "https://blog.example.test/2026/updates/pull/3",
131 "https://scm.example.test/tfs/Collection/Project/_git/Repo",
132 "https://scm.example.test/tfs/Collection/Project/pullrequest/123",
133 "https://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/abc",
134 "https://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/0",
135 "https://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/9007199254740992",
136 "ftp://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/123",
137 "javascript:alert(1)//github.com/t3tools/t3code/pull/1",
138 "not a url",
139 ]) {
140 expect(parseChangeRequestUrl(link), link).toBeNull();
141 }
142 });
143});
144​
145describe("siblingPullRequestUrl", () => {
146 it.each([
147 [
148 "http://git.example.test:3000/team/repo/pulls/42/files",
149 "http://git.example.test:3000/team/repo/pulls/43",
150 ],
151 ["https://github.com/pull/1/pull/42/files", "https://github.com/pull/1/pull/43"],
152 [
153 "https://dev.azure.com/acme/project/_git/web/pullrequest/42?view=files",
154 "https://dev.azure.com/acme/project/_git/web/pullrequest/43",
155 ],
156 ])("builds a canonical sibling of %s", (url, expected) => {
157 expect(siblingPullRequestUrl(url, 43)).toBe(expected);
158 });
159});
160​
161describe("changeRequestUrlFor", () => {
162 it("preserves the origin when the Forgejo host already contains its port", () => {
163 expect(
164 changeRequestUrlFor(
165 "forgejo",
166 "forge.example:3000",
167 "team/repo",
168 42,
169 "http://forge.example:3000/team/repo.git",
170 ),
171 ).toBe("http://forge.example:3000/team/repo/pulls/42");
172 });
173​
174 it.each([
175 ["http://forge.example:3000/git/owner/repo.git", "http://forge.example:3000"],
176 ["git@forge.example:git/owner/repo.git", "https://forge.example"],
177 ["http://other.example:3000/git/owner/repo.git", "https://forge.example"],
178 ])("preserves the matching Forgejo web origin from %s", (remoteUrl, origin) => {
179 expect(changeRequestUrlFor("forgejo", "forge.example", "git/owner/repo", 42, remoteUrl)).toBe(
180 `${origin}/git/owner/repo/pulls/42`,
181 );
182 });
183​
184 it.each([
185 ["ssh.dev.azure.com", "v3/org/project/web"],
186 ["org.visualstudio.com", "defaultcollection/project/_git/web"],
187 ["dev.azure.com", "org/project/_git/web"],
188 ])("builds a browser URL from the Azure remote %s/%s", (host, repository) => {
189 const url = changeRequestUrlFor("azure-devops", host, repository, 42);
190 expect(url).toBe("https://dev.azure.com/org/project/_git/web/pullrequest/42");
191 expect(parseChangeRequestUrl(url!)).toEqual({
192 host: "dev.azure.com",
193 repository: "org/project/_git/web",
194 number: 42,
195 });
196 });
197});