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

openPullRequestLink.test.ts

363 lines11.5 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 changeRequestRepositoryUrl,
5 findProjectForChangeRequest,
6 gitHubPullRequestBrowserUrl,
7 matchesLinkedPullRequestUrl,
8 openPullRequestLink,
9 parseChangeRequestUrl,
10 PullRequestLinkOpenError,
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("matchesLinkedPullRequestUrl", () => {
134 const linkedPullRequest = {
135 projectId: ProjectId.make("project-1"),
136 repository: "pingdotgg/t3code",
137 number: 42,
138 url: "https://github.com/pingdotgg/t3code/pull/42",
139 };
140​
141 it("matches the same pull request without looking up its project", () => {
142 expect(
143 matchesLinkedPullRequestUrl(
144 linkedPullRequest,
145 "https://github.com/PingDotGG/T3Code/pull/42/files",
146 ),
147 ).toBe(true);
148 });
149​
150 it("rejects a different pull request or host", () => {
151 expect(
152 matchesLinkedPullRequestUrl(linkedPullRequest, "https://github.com/pingdotgg/t3code/pull/43"),
153 ).toBe(false);
154 expect(
155 matchesLinkedPullRequestUrl(
156 linkedPullRequest,
157 "https://github.example.com/pingdotgg/t3code/pull/42",
158 ),
159 ).toBe(false);
160 });
161});
162​
163describe("openPullRequestLink", () => {
164 it("opens the requested pull request URL", async () => {
165 const openExternal = vi.fn(async () => undefined);
166 const targetUrl = "https://github.com/pingdotgg/t3code/pull/123";
167​
168 await openPullRequestLink({ openExternal }, targetUrl);
169​
170 expect(openExternal).toHaveBeenCalledExactlyOnceWith(targetUrl);
171 });
172​
173 it("reports bridge failures with a safe target origin", async () => {
174 const cause = new Error("desktop shell unavailable");
175 const targetUrl = "https://github.com/pingdotgg/t3code/pull/123?token=secret";
176 const openExternal = vi.fn(async () => Promise.reject(cause));
177​
178 const result = openPullRequestLink({ openExternal }, targetUrl);
179​
180 await expect(result).rejects.toEqual(
181 new PullRequestLinkOpenError({
182 targetOrigin: "https://github.com",
183 cause,
184 }),
185 );
186 await expect(result).rejects.not.toHaveProperty("message", expect.stringContaining("secret"));
187 });
188});
189​
190describe("shouldOpenPullRequestExternally", () => {
191 it("uses the browser for command-click and control-click", () => {
192 expect(shouldOpenPullRequestExternally({ metaKey: true, ctrlKey: false })).toBe(true);
193 expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: true })).toBe(true);
194 });
195​
196 it("keeps an unmodified click in the pull request view", () => {
197 expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: false })).toBe(false);
198 });
199});
200​
201describe("parseChangeRequestUrl", () => {
202 it("reads a GitHub pull request", () => {
203 expect(parseChangeRequestUrl("https://github.com/T3Tools/T3Code/pull/123")).toEqual({
204 host: "github.com",
205 repository: "t3tools/t3code",
206 number: 123,
207 });
208 });
209​
210 it("reads a pull request on a GitHub Enterprise host", () => {
211 expect(parseChangeRequestUrl("https://github.acme.test/platform/api/pull/7")).toEqual({
212 host: "github.acme.test",
213 repository: "platform/api",
214 number: 7,
215 });
216 });
217​
218 it("reads a GitLab merge request, nested groups and all", () => {
219 expect(
220 parseChangeRequestUrl("https://gitlab.com/t3tools/platform/t3code/-/merge_requests/42"),
221 ).toEqual({
222 host: "gitlab.com",
223 repository: "t3tools/platform/t3code",
224 number: 42,
225 });
226 });
227​
228 it("reads a merge request on a self-hosted GitLab named nothing like GitLab", () => {
229 expect(parseChangeRequestUrl("https://code.acme.test/team/project/-/merge_requests/9")).toEqual(
230 {
231 host: "code.acme.test",
232 repository: "team/project",
233 number: 9,
234 },
235 );
236 });
237​
238 it("reads a Bitbucket pull request", () => {
239 expect(parseChangeRequestUrl("https://bitbucket.org/workspace/repo/pull-requests/5")).toEqual({
240 host: "bitbucket.org",
241 repository: "workspace/repo",
242 number: 5,
243 });
244 });
245​
246 it("reads both Azure DevOps URL forms, keeping `_git` in the repository path", () => {
247 expect(
248 parseChangeRequestUrl("https://dev.azure.com/acme/platform/_git/t3code/pullrequest/17"),
249 ).toEqual({
250 host: "dev.azure.com",
251 repository: "acme/platform/_git/t3code",
252 number: 17,
253 });
254 expect(
255 parseChangeRequestUrl("https://acme.visualstudio.com/platform/_git/t3code/pullrequest/17"),
256 ).toEqual({
257 host: "acme.visualstudio.com",
258 repository: "platform/_git/t3code",
259 number: 17,
260 });
261 });
262​
263 it("survives trailing segments, a trailing slash and a query string", () => {
264 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/files?w=1")).toEqual({
265 host: "github.com",
266 repository: "t3tools/t3code",
267 number: 123,
268 });
269 expect(
270 parseChangeRequestUrl("https://gitlab.com/team/project/-/merge_requests/42/diffs#note_1"),
271 ).toEqual({ host: "gitlab.com", repository: "team/project", number: 42 });
272 expect(
273 parseChangeRequestUrl("https://bitbucket.org/team/repo/pull-requests/5/commits"),
274 ).toEqual({ host: "bitbucket.org", repository: "team/repo", number: 5 });
275 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/")).toEqual({
276 host: "github.com",
277 repository: "t3tools/t3code",
278 number: 123,
279 });
280 });
281​
282 it("claims nothing it cannot be sure of, so the link goes to the browser", () => {
283 for (const link of [
284 "https://github.com/t3tools/t3code/issues/123",
285 "https://github.com/t3tools/t3code/commit/0a1b2c3",
286 "https://github.com/t3tools/t3code",
287 "https://github.com/t3tools/t3code/pull/abc",
288 "https://gitlab.com/t3tools/t3code/-/snippets/12",
289 "https://gitlab.com/t3tools/t3code/-/issues/12",
290 // A path shape that means nothing off its own host.
291 "https://blog.example.test/2026/updates/pull/3",
292 // A lookalike is deliberately not fought here: `github.com.evil.test` reads as a GitHub
293 // Enterprise install and there is no way to tell it from one. It is `findProjectForChange
294 // Request` that refuses it, because no project in the workspace is checked out from it.
295 "javascript:alert(1)//github.com/t3tools/t3code/pull/1",
296 "not a url",
297 ]) {
298 expect(parseChangeRequestUrl(link), link).toBeNull();
299 }
300 });
301});
302​
303describe("findProjectForChangeRequest", () => {
304 const project = (identity: Record<string, unknown>) =>
305 ({ id: "p1", repositoryIdentity: identity }) as never;
306​
307 it("matches a nested GitLab group by the whole path below the host", () => {
308 // The server identifies a repository by `displayName`, which keeps every group segment; the
309 // two-segment owner/name form would look for `t3tools/t3code` and find nothing.
310 const projects = [
311 project({
312 canonicalKey: "gitlab.com/t3tools/platform/t3code",
313 provider: "gitlab",
314 displayName: "t3tools/platform/t3code",
315 owner: "t3tools",
316 name: "t3code",
317 }),
318 ];
319 expect(
320 findProjectForChangeRequest(projects, {
321 host: "gitlab.com",
322 repository: "t3tools/platform/t3code",
323 number: 42,
324 }),
325 ).toBe(projects[0]);
326 });
327​
328 it("keeps two hosts apart, so an Enterprise link does not open the public one", () => {
329 const projects = [
330 project({
331 canonicalKey: "github.com/pingdotgg/t3code",
332 provider: "github",
333 owner: "pingdotgg",
334 name: "t3code",
335 }),
336 ];
337 expect(
338 findProjectForChangeRequest(projects, {
339 host: "github.acme.test",
340 repository: "pingdotgg/t3code",
341 number: 1,
342 }),
343 ).toBeUndefined();
344 });
345​
346 it("claims nothing for a lookalike host, which is what keeps a link a link", () => {
347 const projects = [
348 project({
349 canonicalKey: "github.com/pingdotgg/t3code",
350 provider: "github",
351 owner: "pingdotgg",
352 name: "t3code",
353 }),
354 ];
355 expect(
356 findProjectForChangeRequest(projects, {
357 host: "github.com-evil.test",
358 repository: "pingdotgg/t3code",
359 number: 1,
360 }),
361 ).toBeUndefined();
362 });
363});