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