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

changeRequestUrl.test.ts

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