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

openPullRequestLink.test.ts

230 lines7.7 KB

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