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

openPullRequestLink.test.ts

262 lines8.6 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 changeRequestRepositoryUrl,
5 findProjectForChangeRequest,
6 matchesLinkedPullRequestUrl,
7 openPullRequestLink,
8 parseChangeRequestUrl,
9 PullRequestLinkOpenError,
10 shouldOpenPullRequestExternally,
11} from "./openPullRequestLink";
12import { ProjectId } from "@t3tools/contracts";
13​
14describe("changeRequestRepositoryUrl", () => {
15 it("preserves repository path casing", () => {
16 expect(
17 changeRequestRepositoryUrl(
18 "https://gitlab.example.test/Team/Platform/Repo/-/merge_requests/42/diffs#note_1",
19 ),
20 ).toBe("https://gitlab.example.test/Team/Platform/Repo");
21 });
22​
23 it("keeps pull-like segments inside nested GitLab repository paths", () => {
24 expect(
25 changeRequestRepositoryUrl(
26 "https://gitlab.example.test/group/pull/123/repo/-/merge_requests/42",
27 ),
28 ).toBe("https://gitlab.example.test/group/pull/123/repo");
29 });
30});
31​
32describe("matchesLinkedPullRequestUrl", () => {
33 const linkedPullRequest = {
34 projectId: ProjectId.make("project-1"),
35 repository: "pingdotgg/t3code",
36 number: 42,
37 url: "https://github.com/pingdotgg/t3code/pull/42",
38 };
39​
40 it("matches the same pull request without looking up its project", () => {
41 expect(
42 matchesLinkedPullRequestUrl(
43 linkedPullRequest,
44 "https://github.com/PingDotGG/T3Code/pull/42/files",
45 ),
46 ).toBe(true);
47 });
48​
49 it("rejects a different pull request or host", () => {
50 expect(
51 matchesLinkedPullRequestUrl(linkedPullRequest, "https://github.com/pingdotgg/t3code/pull/43"),
52 ).toBe(false);
53 expect(
54 matchesLinkedPullRequestUrl(
55 linkedPullRequest,
56 "https://github.example.com/pingdotgg/t3code/pull/42",
57 ),
58 ).toBe(false);
59 });
60});
61​
62describe("openPullRequestLink", () => {
63 it("opens the requested pull request URL", async () => {
64 const openExternal = vi.fn(async () => undefined);
65 const targetUrl = "https://github.com/pingdotgg/t3code/pull/123";
66​
67 await openPullRequestLink({ openExternal }, targetUrl);
68​
69 expect(openExternal).toHaveBeenCalledExactlyOnceWith(targetUrl);
70 });
71​
72 it("reports bridge failures with a safe target origin", async () => {
73 const cause = new Error("desktop shell unavailable");
74 const targetUrl = "https://github.com/pingdotgg/t3code/pull/123?token=secret";
75 const openExternal = vi.fn(async () => Promise.reject(cause));
76​
77 const result = openPullRequestLink({ openExternal }, targetUrl);
78​
79 await expect(result).rejects.toEqual(
80 new PullRequestLinkOpenError({
81 targetOrigin: "https://github.com",
82 cause,
83 }),
84 );
85 await expect(result).rejects.not.toHaveProperty("message", expect.stringContaining("secret"));
86 });
87});
88​
89describe("shouldOpenPullRequestExternally", () => {
90 it("uses the browser for command-click and control-click", () => {
91 expect(shouldOpenPullRequestExternally({ metaKey: true, ctrlKey: false })).toBe(true);
92 expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: true })).toBe(true);
93 });
94​
95 it("keeps an unmodified click in the pull request view", () => {
96 expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: false })).toBe(false);
97 });
98});
99​
100describe("parseChangeRequestUrl", () => {
101 it("reads a GitHub pull request", () => {
102 expect(parseChangeRequestUrl("https://github.com/T3Tools/T3Code/pull/123")).toEqual({
103 host: "github.com",
104 repository: "t3tools/t3code",
105 number: 123,
106 });
107 });
108​
109 it("reads a pull request on a GitHub Enterprise host", () => {
110 expect(parseChangeRequestUrl("https://github.acme.test/platform/api/pull/7")).toEqual({
111 host: "github.acme.test",
112 repository: "platform/api",
113 number: 7,
114 });
115 });
116​
117 it("reads a GitLab merge request, nested groups and all", () => {
118 expect(
119 parseChangeRequestUrl("https://gitlab.com/t3tools/platform/t3code/-/merge_requests/42"),
120 ).toEqual({
121 host: "gitlab.com",
122 repository: "t3tools/platform/t3code",
123 number: 42,
124 });
125 });
126​
127 it("reads a merge request on a self-hosted GitLab named nothing like GitLab", () => {
128 expect(parseChangeRequestUrl("https://code.acme.test/team/project/-/merge_requests/9")).toEqual(
129 {
130 host: "code.acme.test",
131 repository: "team/project",
132 number: 9,
133 },
134 );
135 });
136​
137 it("reads a Bitbucket pull request", () => {
138 expect(parseChangeRequestUrl("https://bitbucket.org/workspace/repo/pull-requests/5")).toEqual({
139 host: "bitbucket.org",
140 repository: "workspace/repo",
141 number: 5,
142 });
143 });
144​
145 it("reads both Azure DevOps URL forms, keeping `_git` in the repository path", () => {
146 expect(
147 parseChangeRequestUrl("https://dev.azure.com/acme/platform/_git/t3code/pullrequest/17"),
148 ).toEqual({
149 host: "dev.azure.com",
150 repository: "acme/platform/_git/t3code",
151 number: 17,
152 });
153 expect(
154 parseChangeRequestUrl("https://acme.visualstudio.com/platform/_git/t3code/pullrequest/17"),
155 ).toEqual({
156 host: "acme.visualstudio.com",
157 repository: "platform/_git/t3code",
158 number: 17,
159 });
160 });
161​
162 it("survives trailing segments, a trailing slash and a query string", () => {
163 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/files?w=1")).toEqual({
164 host: "github.com",
165 repository: "t3tools/t3code",
166 number: 123,
167 });
168 expect(
169 parseChangeRequestUrl("https://gitlab.com/team/project/-/merge_requests/42/diffs#note_1"),
170 ).toEqual({ host: "gitlab.com", repository: "team/project", number: 42 });
171 expect(
172 parseChangeRequestUrl("https://bitbucket.org/team/repo/pull-requests/5/commits"),
173 ).toEqual({ host: "bitbucket.org", repository: "team/repo", number: 5 });
174 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/")).toEqual({
175 host: "github.com",
176 repository: "t3tools/t3code",
177 number: 123,
178 });
179 });
180​
181 it("claims nothing it cannot be sure of, so the link goes to the browser", () => {
182 for (const link of [
183 "https://github.com/t3tools/t3code/issues/123",
184 "https://github.com/t3tools/t3code/commit/0a1b2c3",
185 "https://github.com/t3tools/t3code",
186 "https://github.com/t3tools/t3code/pull/abc",
187 "https://gitlab.com/t3tools/t3code/-/snippets/12",
188 "https://gitlab.com/t3tools/t3code/-/issues/12",
189 // A path shape that means nothing off its own host.
190 "https://blog.example.test/2026/updates/pull/3",
191 // A lookalike is deliberately not fought here: `github.com.evil.test` reads as a GitHub
192 // Enterprise install and there is no way to tell it from one. It is `findProjectForChange
193 // Request` that refuses it, because no project in the workspace is checked out from it.
194 "javascript:alert(1)//github.com/t3tools/t3code/pull/1",
195 "not a url",
196 ]) {
197 expect(parseChangeRequestUrl(link), link).toBeNull();
198 }
199 });
200});
201​
202describe("findProjectForChangeRequest", () => {
203 const project = (identity: Record<string, unknown>) =>
204 ({ id: "p1", repositoryIdentity: identity }) as never;
205​
206 it("matches a nested GitLab group by the whole path below the host", () => {
207 // The server identifies a repository by `displayName`, which keeps every group segment; the
208 // two-segment owner/name form would look for `t3tools/t3code` and find nothing.
209 const projects = [
210 project({
211 canonicalKey: "gitlab.com/t3tools/platform/t3code",
212 provider: "gitlab",
213 displayName: "t3tools/platform/t3code",
214 owner: "t3tools",
215 name: "t3code",
216 }),
217 ];
218 expect(
219 findProjectForChangeRequest(projects, {
220 host: "gitlab.com",
221 repository: "t3tools/platform/t3code",
222 number: 42,
223 }),
224 ).toBe(projects[0]);
225 });
226​
227 it("keeps two hosts apart, so an Enterprise link does not open the public one", () => {
228 const projects = [
229 project({
230 canonicalKey: "github.com/pingdotgg/t3code",
231 provider: "github",
232 owner: "pingdotgg",
233 name: "t3code",
234 }),
235 ];
236 expect(
237 findProjectForChangeRequest(projects, {
238 host: "github.acme.test",
239 repository: "pingdotgg/t3code",
240 number: 1,
241 }),
242 ).toBeUndefined();
243 });
244​
245 it("claims nothing for a lookalike host, which is what keeps a link a link", () => {
246 const projects = [
247 project({
248 canonicalKey: "github.com/pingdotgg/t3code",
249 provider: "github",
250 owner: "pingdotgg",
251 name: "t3code",
252 }),
253 ];
254 expect(
255 findProjectForChangeRequest(projects, {
256 host: "github.com-evil.test",
257 repository: "pingdotgg/t3code",
258 number: 1,
259 }),
260 ).toBeUndefined();
261 });
262});