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

openPullRequestLink.test.ts

211 lines7.1 KB

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