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

openPullRequestLink.test.ts

518 lines17.2 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 changeRequestRepositoryUrl,
5 findProjectForChangeRequest,
6 findProjectOnChangeRequestHost,
7 gitHubPullRequestBrowserUrl,
8 matchesLinkedPullRequestUrl,
9 parseChangeRequestUrl,
10 pullRequestCandidateUrlFromReferenceAutolink,
11 shouldOpenPullRequestExternally,
12} from "./openPullRequestLink";
13import { ProjectId, type RepositoryIdentity } from "@t3tools/contracts";
14​
15function repositoryIdentity(
16 provider: string,
17 canonicalKey: string,
18 remoteUrl: string,
19): RepositoryIdentity {
20 return {
21 canonicalKey,
22 provider,
23 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
24 };
26​
27describe("gitHubPullRequestBrowserUrl", () => {
28 it("uses the requested GitHub repository instead of the project's default repository", () => {
29 const identity = repositoryIdentity(
30 "github",
31 "github.com/acme/default",
32 "https://github.com/acme/default.git",
33 );
34​
35 expect(gitHubPullRequestBrowserUrl(identity, "acme/other", 42)).toBe(
36 "https://github.com/acme/other/pull/42",
37 );
38 });
39​
40 it("preserves a custom GitHub HTTP origin without its credentials", () => {
41 const identity = repositoryIdentity(
42 "github",
43 "github.acme.test/team/default",
44 "http://token@github.acme.test:8443/team/default.git",
45 );
46​
47 expect(gitHubPullRequestBrowserUrl(identity, "platform/api", 7)).toBe(
48 "http://github.acme.test:8443/platform/api/pull/7",
49 );
50 });
51​
52 it.each([
53 {
54 name: "SSH",
55 remoteUrl: "git@github.acme.test:team/default.git",
56 },
57 {
58 name: "git protocol",
59 remoteUrl: "git://github.acme.test/team/default.git",
60 },
61 ])("uses the normalized host for a $name remote", ({ remoteUrl }) => {
62 const identity = repositoryIdentity("github", "github.acme.test/team/default", remoteUrl);
63​
64 expect(gitHubPullRequestBrowserUrl(identity, "platform/api", 9)).toBe(
65 "https://github.acme.test/platform/api/pull/9",
66 );
67 });
68​
69 it("returns null for missing or invalid GitHub data", () => {
70 expect(gitHubPullRequestBrowserUrl(null, "acme/repository", 1)).toBeNull();
71 expect(
72 gitHubPullRequestBrowserUrl(
73 repositoryIdentity("github", "github.com/acme/repository", "https://github.com/a/b"),
74 "acme",
75 1,
76 ),
77 ).toBeNull();
78 expect(
79 gitHubPullRequestBrowserUrl(
80 repositoryIdentity("github", "github.com/acme/repository", "https://github.com/a/b"),
81 "../repository",
82 1,
83 ),
84 ).toBeNull();
85 expect(
86 gitHubPullRequestBrowserUrl(
87 repositoryIdentity("github", "github.com/acme/repository", "https://github.com/a/b"),
88 "acme/repository",
89 0,
90 ),
91 ).toBeNull();
92 expect(
93 gitHubPullRequestBrowserUrl(
94 repositoryIdentity("github", "bad host/acme/repository", "not a remote"),
95 "acme/repository",
96 1,
97 ),
98 ).toBeNull();
99 });
100​
101 it.each(["gitlab", "bitbucket", "azure-devops", "unknown"])(
102 "does not build a fallback for %s",
103 (provider) => {
104 expect(
105 gitHubPullRequestBrowserUrl(
106 repositoryIdentity(provider, "github.com/acme/repository", "https://github.com/a/b"),
107 "acme/repository",
108 1,
109 ),
110 ).toBeNull();
111 },
112 );
113});
114​
115describe("changeRequestRepositoryUrl", () => {
116 it("preserves repository path casing", () => {
117 expect(
118 changeRequestRepositoryUrl(
119 "https://gitlab.example.test/Team/Platform/Repo/-/merge_requests/42/diffs#note_1",
120 ),
121 ).toBe("https://gitlab.example.test/Team/Platform/Repo");
122 });
123​
124 it("keeps pull-like segments inside nested GitLab repository paths", () => {
125 expect(
126 changeRequestRepositoryUrl(
127 "https://gitlab.example.test/group/pull/123/repo/-/merge_requests/42",
128 ),
129 ).toBe("https://gitlab.example.test/group/pull/123/repo");
130 });
131});
132​
133describe("pullRequestCandidateUrlFromReferenceAutolink", () => {
134 it("turns GitHub's shared issue route into a pull request candidate", () => {
135 expect(
136 pullRequestCandidateUrlFromReferenceAutolink(
137 "https://github.com/pingdotgg/t3code/issues/8600#issuecomment-1",
138 ),
139 ).toBe("https://github.com/pingdotgg/t3code/pull/8600#issuecomment-1");
140 });
141​
142 it("does not reinterpret other issue hosts or malformed references", () => {
143 expect(
144 pullRequestCandidateUrlFromReferenceAutolink(
145 "https://gitlab.com/pingdotgg/t3code/-/issues/8600",
146 ),
147 ).toBeNull();
148 expect(
149 pullRequestCandidateUrlFromReferenceAutolink(
150 "https://github.com/pingdotgg/t3code/issues/not-a-number",
151 ),
152 ).toBeNull();
153 });
154});
155​
156describe("matchesLinkedPullRequestUrl", () => {
157 const linkedPullRequest = {
158 projectId: ProjectId.make("project-1"),
159 repository: "pingdotgg/t3code",
160 number: 42,
161 url: "https://github.com/pingdotgg/t3code/pull/42",
162 };
163​
164 it("matches the same pull request without looking up its project", () => {
165 expect(
166 matchesLinkedPullRequestUrl(
167 linkedPullRequest,
168 "https://github.com/PingDotGG/T3Code/pull/42/files",
169 ),
170 ).toBe(true);
171 });
172​
173 it.each([
174 ["http://forge.example:3000/git/team/repo/pulls/42/files", true],
175 ["http://forge.example:4000/git/team/repo/pulls/42", false],
176 ["http://forge.example/git/team/repo/pulls/42", false],
177 ])("matches Forgejo links by web authority: %s", (url, expected) => {
178 expect(
179 matchesLinkedPullRequestUrl(
180 { ...linkedPullRequest, url: "http://forge.example:3000/git/team/repo/pulls/42" },
181 url,
182 ),
183 ).toBe(expected);
184 });
185​
186 it("keeps other providers' existing port normalization", () => {
187 expect(
188 matchesLinkedPullRequestUrl(
189 linkedPullRequest,
190 "https://github.com:8443/pingdotgg/t3code/pull/42",
191 ),
192 ).toBe(true);
193 });
194​
195 it("keeps Forgejo and GitHub path shapes distinct on a GitHub-named host", () => {
196 expect(
197 matchesLinkedPullRequestUrl(
198 { ...linkedPullRequest, url: "https://github.internal/team/repo/pulls/42" },
199 "https://github.internal/team/repo/pull/42",
200 ),
201 ).toBe(false);
202 });
203​
204 it("rejects a different pull request or host", () => {
205 expect(
206 matchesLinkedPullRequestUrl(linkedPullRequest, "https://github.com/pingdotgg/t3code/pull/43"),
207 ).toBe(false);
208 expect(
209 matchesLinkedPullRequestUrl(
210 linkedPullRequest,
211 "https://github.example.com/pingdotgg/t3code/pull/42",
212 ),
213 ).toBe(false);
214 });
215});
216​
217describe("shouldOpenPullRequestExternally", () => {
218 it("uses the browser for command-click and control-click", () => {
219 expect(shouldOpenPullRequestExternally({ metaKey: true, ctrlKey: false })).toBe(true);
220 expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: true })).toBe(true);
221 });
222​
223 it("keeps an unmodified click in the pull request view", () => {
224 expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: false })).toBe(false);
225 });
226});
227​
228describe("parseChangeRequestUrl", () => {
229 it("reads a GitHub pull request", () => {
230 expect(parseChangeRequestUrl("https://github.com/T3Tools/T3Code/pull/123")).toEqual({
231 host: "github.com",
232 repository: "t3tools/t3code",
233 number: 123,
234 });
235 });
236​
237 it("reads a pull request on a GitHub Enterprise host", () => {
238 expect(parseChangeRequestUrl("https://github.acme.test/platform/api/pull/7")).toEqual({
239 host: "github.acme.test",
240 repository: "platform/api",
241 number: 7,
242 });
243 });
244​
245 it("reads a GitLab merge request, nested groups and all", () => {
246 expect(
247 parseChangeRequestUrl("https://gitlab.com/t3tools/platform/t3code/-/merge_requests/42"),
248 ).toEqual({
249 host: "gitlab.com",
250 repository: "t3tools/platform/t3code",
251 number: 42,
252 });
253 });
254​
255 it("reads a merge request on a self-hosted GitLab named nothing like GitLab", () => {
256 expect(parseChangeRequestUrl("https://code.acme.test/team/project/-/merge_requests/9")).toEqual(
257 {
258 host: "code.acme.test",
259 repository: "team/project",
260 number: 9,
261 },
262 );
263 });
264​
265 it("reads a Bitbucket pull request", () => {
266 expect(parseChangeRequestUrl("https://bitbucket.org/workspace/repo/pull-requests/5")).toEqual({
267 host: "bitbucket.org",
268 repository: "workspace/repo",
269 number: 5,
270 });
271 });
272​
273 it("reads both Azure DevOps URL forms, keeping `_git` in the repository path", () => {
274 expect(
275 parseChangeRequestUrl("https://dev.azure.com/acme/platform/_git/t3code/pullrequest/17"),
276 ).toEqual({
277 host: "dev.azure.com",
278 repository: "acme/platform/_git/t3code",
279 number: 17,
280 });
281 expect(
282 parseChangeRequestUrl("https://acme.visualstudio.com/platform/_git/t3code/pullrequest/17"),
283 ).toEqual({
284 host: "acme.visualstudio.com",
285 repository: "platform/_git/t3code",
286 number: 17,
287 });
288 });
289​
290 it("survives trailing segments, a trailing slash and a query string", () => {
291 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/files?w=1")).toEqual({
292 host: "github.com",
293 repository: "t3tools/t3code",
294 number: 123,
295 });
296 expect(
297 parseChangeRequestUrl("https://gitlab.com/team/project/-/merge_requests/42/diffs#note_1"),
298 ).toEqual({ host: "gitlab.com", repository: "team/project", number: 42 });
299 expect(
300 parseChangeRequestUrl("https://bitbucket.org/team/repo/pull-requests/5/commits"),
301 ).toEqual({ host: "bitbucket.org", repository: "team/repo", number: 5 });
302 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/")).toEqual({
303 host: "github.com",
304 repository: "t3tools/t3code",
305 number: 123,
306 });
307 });
308​
309 it("claims nothing it cannot be sure of, so the link goes to the browser", () => {
310 for (const link of [
311 "https://github.com/t3tools/t3code/issues/123",
312 "https://github.com/t3tools/t3code/commit/0a1b2c3",
313 "https://github.com/t3tools/t3code",
314 "https://github.com/t3tools/t3code/pull/abc",
315 "https://gitlab.com/t3tools/t3code/-/snippets/12",
316 "https://gitlab.com/t3tools/t3code/-/issues/12",
317 // A path shape that means nothing off its own host.
318 "https://blog.example.test/2026/updates/pull/3",
319 // A lookalike is deliberately not fought here: `github.com.evil.test` reads as a GitHub
320 // Enterprise install and there is no way to tell it from one. It is `findProjectForChange
321 // Request` that refuses it, because no project in the workspace is checked out from it.
322 "javascript:alert(1)//github.com/t3tools/t3code/pull/1",
323 "not a url",
324 ]) {
325 expect(parseChangeRequestUrl(link), link).toBeNull();
326 }
327 });
328});
329​
330describe("findProjectOnChangeRequestHost", () => {
331 const project = (id: string, identity: Record<string, unknown>) =>
332 ({ id, repositoryIdentity: identity }) as never;
333 const frontend = project("frontend", {
334 canonicalKey: "github.com/acme/frontend",
335 provider: "github",
336 owner: "acme",
337 name: "frontend",
338 });
339 const backend = project("backend", {
340 canonicalKey: "github.com/acme/backend",
341 provider: "github",
342 owner: "acme",
343 name: "backend",
344 });
345​
346 it.each([
347 "ssh.dev.azure.com/v3/org-a/project/web",
348 "vs-ssh.visualstudio.com/v3/org-a/project/web",
349 "org-a.visualstudio.com/defaultcollection/project/_git/web",
350 "dev.azure.com/org-a/project/_git/web",
351 ])("matches Azure browser references against %s", (canonicalKey) => {
352 const checkout = project("azure", {
353 canonicalKey,
354 provider: "azure-devops",
355 displayName: canonicalKey.split("/").slice(1).join("/"),
356 });
357 const reference = { host: "dev.azure.com", repository: "org-a/project/_git/web", number: 42 };
358 expect(findProjectForChangeRequest([checkout], reference)).toBe(checkout);
359 expect(findProjectOnChangeRequestHost([checkout], reference)).toBe(checkout);
360 expect(
361 findProjectOnChangeRequestHost([checkout], {
362 ...reference,
363 repository: "org-b/project/_git/web",
364 }),
365 ).toBeUndefined();
366 expect(
367 findProjectOnChangeRequestHost([checkout], {
368 ...reference,
369 repository: "org-a/other-project/_git/web",
370 }),
371 ).toBeUndefined();
372 });
373​
374 it("prefers the project checked out from the link's own repository", () => {
375 expect(
376 findProjectOnChangeRequestHost([frontend, backend], {
377 host: "github.com",
378 repository: "acme/backend",
379 number: 7,
380 }),
381 ).toBe(backend);
382 });
383​
384 it("selects the Forgejo HTTP port for repository and host-level matches", () => {
385 const projects = [3000, 4000].map((port) =>
386 project(`forgejo-${port}`, {
387 canonicalKey: "forge.example/git/team/repo",
388 provider: "forgejo",
389 displayName: "git/team/repo",
390 locator: { remoteUrl: `http://forge.example:${port}/git/team/repo.git` },
391 }),
392 );
393 const reference = parseChangeRequestUrl("http://forge.example:4000/git/team/repo/pulls/42")!;
394 expect(findProjectForChangeRequest(projects, reference)).toBe(projects[1]);
395 expect(findProjectOnChangeRequestHost(projects, reference)).toBe(projects[1]);
396 expect(
397 findProjectOnChangeRequestHost(projects, { ...reference, repository: "git/team/other" }),
398 ).toBe(projects[1]);
399 expect(findProjectForChangeRequest([projects[0]!], reference)).toBeUndefined();
400 expect(findProjectOnChangeRequestHost([projects[0]!], reference)).toBeUndefined();
401 });
402​
403 it("lets tea resolve the web port for Forgejo SSH remotes", () => {
404 const checkout = project("forgejo-ssh", {
405 canonicalKey: "forge.example/git/team/repo",
406 provider: "forgejo",
407 displayName: "git/team/repo",
408 locator: { remoteUrl: "git@forge.example:git/team/repo.git" },
409 });
410 const reference = parseChangeRequestUrl("http://forge.example:4000/git/team/repo/pulls/42")!;
411 expect(findProjectForChangeRequest([checkout], reference)).toBe(checkout);
412 const aliased = project("forgejo-alias", {
413 canonicalKey: "ssh.forge.example/team/repo",
414 provider: "forgejo",
415 displayName: "team/repo",
416 locator: { remoteUrl: "git@ssh.forge.example:team/repo.git" },
417 webUrl: "http://forge.example:4000/git/team/repo",
418 });
419 expect(findProjectForChangeRequest([aliased], reference)).toBe(aliased);
420 expect(findProjectOnChangeRequestHost([aliased], reference)).toBe(aliased);
421 expect(
422 findProjectOnChangeRequestHost([aliased], { ...reference, repository: "git/team/other" }),
423 ).toBe(aliased);
424 for (const url of [
425 "http://other.example:4000/git/team/repo/pulls/42",
426 "http://forge.example:3000/git/team/repo/pulls/42",
427 ]) {
428 const other = parseChangeRequestUrl(url)!;
429 expect(findProjectForChangeRequest([aliased], other)).toBeUndefined();
430 expect(findProjectOnChangeRequestHost([aliased], other)).toBeUndefined();
431 }
432 const otherMount = parseChangeRequestUrl("http://forge.example:4000/other/team/repo/pulls/42")!;
433 expect(findProjectForChangeRequest([aliased], otherMount)).toBeUndefined();
434 expect(findProjectOnChangeRequestHost([aliased], otherMount)).toBeUndefined();
435 });
436​
437 it("lends any project on the host to a repository nobody has checked out", () => {
438 expect(
439 findProjectOnChangeRequestHost([frontend], {
440 host: "github.com",
441 repository: "acme/backend",
442 number: 7,
443 }),
444 ).toBe(frontend);
445 });
446​
447 it("finds nothing on a host nothing is checked out from", () => {
448 expect(
449 findProjectOnChangeRequestHost([frontend], {
450 host: "gitlab.com",
451 repository: "acme/backend",
452 number: 7,
453 }),
454 ).toBeUndefined();
455 });
456});
457​
458describe("findProjectForChangeRequest", () => {
459 const project = (identity: Record<string, unknown>) =>
460 ({ id: "p1", repositoryIdentity: identity }) as never;
461​
462 it("matches a nested GitLab group by the whole path below the host", () => {
463 // The server identifies a repository by `displayName`, which keeps every group segment; the
464 // two-segment owner/name form would look for `t3tools/t3code` and find nothing.
465 const projects = [
466 project({
467 canonicalKey: "gitlab.com/t3tools/platform/t3code",
468 provider: "gitlab",
469 displayName: "t3tools/platform/t3code",
470 owner: "t3tools",
471 name: "t3code",
472 }),
473 ];
474 expect(
475 findProjectForChangeRequest(projects, {
476 host: "gitlab.com",
477 repository: "t3tools/platform/t3code",
478 number: 42,
479 }),
480 ).toBe(projects[0]);
481 });
482​
483 it("keeps two hosts apart, so an Enterprise link does not open the public one", () => {
484 const projects = [
485 project({
486 canonicalKey: "github.com/pingdotgg/t3code",
487 provider: "github",
488 owner: "pingdotgg",
489 name: "t3code",
490 }),
491 ];
492 expect(
493 findProjectForChangeRequest(projects, {
494 host: "github.acme.test",
495 repository: "pingdotgg/t3code",
496 number: 1,
497 }),
498 ).toBeUndefined();
499 });
500​
501 it("claims nothing for a lookalike host, which is what keeps a link a link", () => {
502 const projects = [
503 project({
504 canonicalKey: "github.com/pingdotgg/t3code",
505 provider: "github",
506 owner: "pingdotgg",
507 name: "t3code",
508 }),
509 ];
510 expect(
511 findProjectForChangeRequest(projects, {
512 host: "github.com-evil.test",
513 repository: "pingdotgg/t3code",
514 number: 1,
515 }),
516 ).toBeUndefined();
517 });
518});