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

openPullRequestLink.test.ts

358 lines11.3 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 changeRequestRepositoryUrl,
5 findProjectForChangeRequest,
6 gitHubPullRequestBrowserUrl,
7 matchesLinkedPullRequestUrl,
8 parseChangeRequestUrl,
9 pullRequestCandidateUrlFromReferenceAutolink,
10 shouldOpenPullRequestExternally,
11} from "./openPullRequestLink";
12import { ProjectId, type RepositoryIdentity } from "@t3tools/contracts";
13​
14function repositoryIdentity(
15 provider: string,
16 canonicalKey: string,
17 remoteUrl: string,
18): RepositoryIdentity {
19 return {
20 canonicalKey,
21 provider,
22 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
23 };
25​
26describe("gitHubPullRequestBrowserUrl", () => {
27 it("uses the requested GitHub repository instead of the project's default repository", () => {
28 const identity = repositoryIdentity(
29 "github",
30 "github.com/acme/default",
31 "https://github.com/acme/default.git",
32 );
33​
34 expect(gitHubPullRequestBrowserUrl(identity, "acme/other", 42)).toBe(
35 "https://github.com/acme/other/pull/42",
36 );
37 });
38​
39 it("preserves a custom GitHub HTTP origin without its credentials", () => {
40 const identity = repositoryIdentity(
41 "github",
42 "github.acme.test/team/default",
43 "http://token@github.acme.test:8443/team/default.git",
44 );
45​
46 expect(gitHubPullRequestBrowserUrl(identity, "platform/api", 7)).toBe(
47 "http://github.acme.test:8443/platform/api/pull/7",
48 );
49 });
50​
51 it.each([
52 {
53 name: "SSH",
54 remoteUrl: "git@github.acme.test:team/default.git",
55 },
56 {
57 name: "git protocol",
58 remoteUrl: "git://github.acme.test/team/default.git",
59 },
60 ])("uses the normalized host for a $name remote", ({ remoteUrl }) => {
61 const identity = repositoryIdentity("github", "github.acme.test/team/default", remoteUrl);
62​
63 expect(gitHubPullRequestBrowserUrl(identity, "platform/api", 9)).toBe(
64 "https://github.acme.test/platform/api/pull/9",
65 );
66 });
67​
68 it("returns null for missing or invalid GitHub data", () => {
69 expect(gitHubPullRequestBrowserUrl(null, "acme/repository", 1)).toBeNull();
70 expect(
71 gitHubPullRequestBrowserUrl(
72 repositoryIdentity("github", "github.com/acme/repository", "https://github.com/a/b"),
73 "acme",
74 1,
75 ),
76 ).toBeNull();
77 expect(
78 gitHubPullRequestBrowserUrl(
79 repositoryIdentity("github", "github.com/acme/repository", "https://github.com/a/b"),
80 "../repository",
81 1,
82 ),
83 ).toBeNull();
84 expect(
85 gitHubPullRequestBrowserUrl(
86 repositoryIdentity("github", "github.com/acme/repository", "https://github.com/a/b"),
87 "acme/repository",
88 0,
89 ),
90 ).toBeNull();
91 expect(
92 gitHubPullRequestBrowserUrl(
93 repositoryIdentity("github", "bad host/acme/repository", "not a remote"),
94 "acme/repository",
95 1,
96 ),
97 ).toBeNull();
98 });
99​
100 it.each(["gitlab", "bitbucket", "azure-devops", "unknown"])(
101 "does not build a fallback for %s",
102 (provider) => {
103 expect(
104 gitHubPullRequestBrowserUrl(
105 repositoryIdentity(provider, "github.com/acme/repository", "https://github.com/a/b"),
106 "acme/repository",
107 1,
108 ),
109 ).toBeNull();
110 },
111 );
112});
113​
114describe("changeRequestRepositoryUrl", () => {
115 it("preserves repository path casing", () => {
116 expect(
117 changeRequestRepositoryUrl(
118 "https://gitlab.example.test/Team/Platform/Repo/-/merge_requests/42/diffs#note_1",
119 ),
120 ).toBe("https://gitlab.example.test/Team/Platform/Repo");
121 });
122​
123 it("keeps pull-like segments inside nested GitLab repository paths", () => {
124 expect(
125 changeRequestRepositoryUrl(
126 "https://gitlab.example.test/group/pull/123/repo/-/merge_requests/42",
127 ),
128 ).toBe("https://gitlab.example.test/group/pull/123/repo");
129 });
130});
131​
132describe("pullRequestCandidateUrlFromReferenceAutolink", () => {
133 it("turns GitHub's shared issue route into a pull request candidate", () => {
134 expect(
135 pullRequestCandidateUrlFromReferenceAutolink(
136 "https://github.com/pingdotgg/t3code/issues/8600#issuecomment-1",
137 ),
138 ).toBe("https://github.com/pingdotgg/t3code/pull/8600#issuecomment-1");
139 });
140​
141 it("does not reinterpret other issue hosts or malformed references", () => {
142 expect(
143 pullRequestCandidateUrlFromReferenceAutolink(
144 "https://gitlab.com/pingdotgg/t3code/-/issues/8600",
145 ),
146 ).toBeNull();
147 expect(
148 pullRequestCandidateUrlFromReferenceAutolink(
149 "https://github.com/pingdotgg/t3code/issues/not-a-number",
150 ),
151 ).toBeNull();
152 });
153});
154​
155describe("matchesLinkedPullRequestUrl", () => {
156 const linkedPullRequest = {
157 projectId: ProjectId.make("project-1"),
158 repository: "pingdotgg/t3code",
159 number: 42,
160 url: "https://github.com/pingdotgg/t3code/pull/42",
161 };
162​
163 it("matches the same pull request without looking up its project", () => {
164 expect(
165 matchesLinkedPullRequestUrl(
166 linkedPullRequest,
167 "https://github.com/PingDotGG/T3Code/pull/42/files",
168 ),
169 ).toBe(true);
170 });
171​
172 it("rejects a different pull request or host", () => {
173 expect(
174 matchesLinkedPullRequestUrl(linkedPullRequest, "https://github.com/pingdotgg/t3code/pull/43"),
175 ).toBe(false);
176 expect(
177 matchesLinkedPullRequestUrl(
178 linkedPullRequest,
179 "https://github.example.com/pingdotgg/t3code/pull/42",
180 ),
181 ).toBe(false);
182 });
183});
184​
185describe("shouldOpenPullRequestExternally", () => {
186 it("uses the browser for command-click and control-click", () => {
187 expect(shouldOpenPullRequestExternally({ metaKey: true, ctrlKey: false })).toBe(true);
188 expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: true })).toBe(true);
189 });
190​
191 it("keeps an unmodified click in the pull request view", () => {
192 expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: false })).toBe(false);
193 });
194});
195​
196describe("parseChangeRequestUrl", () => {
197 it("reads a GitHub pull request", () => {
198 expect(parseChangeRequestUrl("https://github.com/T3Tools/T3Code/pull/123")).toEqual({
199 host: "github.com",
200 repository: "t3tools/t3code",
201 number: 123,
202 });
203 });
204​
205 it("reads a pull request on a GitHub Enterprise host", () => {
206 expect(parseChangeRequestUrl("https://github.acme.test/platform/api/pull/7")).toEqual({
207 host: "github.acme.test",
208 repository: "platform/api",
209 number: 7,
210 });
211 });
212​
213 it("reads a GitLab merge request, nested groups and all", () => {
214 expect(
215 parseChangeRequestUrl("https://gitlab.com/t3tools/platform/t3code/-/merge_requests/42"),
216 ).toEqual({
217 host: "gitlab.com",
218 repository: "t3tools/platform/t3code",
219 number: 42,
220 });
221 });
222​
223 it("reads a merge request on a self-hosted GitLab named nothing like GitLab", () => {
224 expect(parseChangeRequestUrl("https://code.acme.test/team/project/-/merge_requests/9")).toEqual(
225 {
226 host: "code.acme.test",
227 repository: "team/project",
228 number: 9,
229 },
230 );
231 });
232​
233 it("reads a Bitbucket pull request", () => {
234 expect(parseChangeRequestUrl("https://bitbucket.org/workspace/repo/pull-requests/5")).toEqual({
235 host: "bitbucket.org",
236 repository: "workspace/repo",
237 number: 5,
238 });
239 });
240​
241 it("reads both Azure DevOps URL forms, keeping `_git` in the repository path", () => {
242 expect(
243 parseChangeRequestUrl("https://dev.azure.com/acme/platform/_git/t3code/pullrequest/17"),
244 ).toEqual({
245 host: "dev.azure.com",
246 repository: "acme/platform/_git/t3code",
247 number: 17,
248 });
249 expect(
250 parseChangeRequestUrl("https://acme.visualstudio.com/platform/_git/t3code/pullrequest/17"),
251 ).toEqual({
252 host: "acme.visualstudio.com",
253 repository: "platform/_git/t3code",
254 number: 17,
255 });
256 });
257​
258 it("survives trailing segments, a trailing slash and a query string", () => {
259 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/files?w=1")).toEqual({
260 host: "github.com",
261 repository: "t3tools/t3code",
262 number: 123,
263 });
264 expect(
265 parseChangeRequestUrl("https://gitlab.com/team/project/-/merge_requests/42/diffs#note_1"),
266 ).toEqual({ host: "gitlab.com", repository: "team/project", number: 42 });
267 expect(
268 parseChangeRequestUrl("https://bitbucket.org/team/repo/pull-requests/5/commits"),
269 ).toEqual({ host: "bitbucket.org", repository: "team/repo", number: 5 });
270 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/")).toEqual({
271 host: "github.com",
272 repository: "t3tools/t3code",
273 number: 123,
274 });
275 });
276​
277 it("claims nothing it cannot be sure of, so the link goes to the browser", () => {
278 for (const link of [
279 "https://github.com/t3tools/t3code/issues/123",
280 "https://github.com/t3tools/t3code/commit/0a1b2c3",
281 "https://github.com/t3tools/t3code",
282 "https://github.com/t3tools/t3code/pull/abc",
283 "https://gitlab.com/t3tools/t3code/-/snippets/12",
284 "https://gitlab.com/t3tools/t3code/-/issues/12",
285 // A path shape that means nothing off its own host.
286 "https://blog.example.test/2026/updates/pull/3",
287 // A lookalike is deliberately not fought here: `github.com.evil.test` reads as a GitHub
288 // Enterprise install and there is no way to tell it from one. It is `findProjectForChange
289 // Request` that refuses it, because no project in the workspace is checked out from it.
290 "javascript:alert(1)//github.com/t3tools/t3code/pull/1",
291 "not a url",
292 ]) {
293 expect(parseChangeRequestUrl(link), link).toBeNull();
294 }
295 });
296});
297​
298describe("findProjectForChangeRequest", () => {
299 const project = (identity: Record<string, unknown>) =>
300 ({ id: "p1", repositoryIdentity: identity }) as never;
301​
302 it("matches a nested GitLab group by the whole path below the host", () => {
303 // The server identifies a repository by `displayName`, which keeps every group segment; the
304 // two-segment owner/name form would look for `t3tools/t3code` and find nothing.
305 const projects = [
306 project({
307 canonicalKey: "gitlab.com/t3tools/platform/t3code",
308 provider: "gitlab",
309 displayName: "t3tools/platform/t3code",
310 owner: "t3tools",
311 name: "t3code",
312 }),
313 ];
314 expect(
315 findProjectForChangeRequest(projects, {
316 host: "gitlab.com",
317 repository: "t3tools/platform/t3code",
318 number: 42,
319 }),
320 ).toBe(projects[0]);
321 });
322​
323 it("keeps two hosts apart, so an Enterprise link does not open the public one", () => {
324 const projects = [
325 project({
326 canonicalKey: "github.com/pingdotgg/t3code",
327 provider: "github",
328 owner: "pingdotgg",
329 name: "t3code",
330 }),
331 ];
332 expect(
333 findProjectForChangeRequest(projects, {
334 host: "github.acme.test",
335 repository: "pingdotgg/t3code",
336 number: 1,
337 }),
338 ).toBeUndefined();
339 });
340​
341 it("claims nothing for a lookalike host, which is what keeps a link a link", () => {
342 const projects = [
343 project({
344 canonicalKey: "github.com/pingdotgg/t3code",
345 provider: "github",
346 owner: "pingdotgg",
347 name: "t3code",
348 }),
349 ];
350 expect(
351 findProjectForChangeRequest(projects, {
352 host: "github.com-evil.test",
353 repository: "pingdotgg/t3code",
354 number: 1,
355 }),
356 ).toBeUndefined();
357 });
358});