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

sourceControlClients.test.ts

368 lines13.9 KB

1import { ProjectId, SourceControlProviderKind } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { sourceControlClients } from "./sourceControlClients.ts";
5import { normalizeThreadPullRequestKey } from "./threadPullRequests.ts";
6​
7const parse = (url: string) => {
8 const parsed = sourceControlClients.parseChangeRequestUrl(url);
9 if (parsed === null) return null;
10 const { repositoryUrl: _repositoryUrl, ...link } = parsed.link;
11 return link;
12};
13const kindOf = (url: string) => sourceControlClients.parseChangeRequestUrl(url)?.definition.kind;
14​
15describe("sourceControlClients.parseChangeRequestUrl", () => {
16 it("reads a GitHub pull request, lower-casing the repository", () => {
17 expect(parse("https://github.com/T3Tools/T3Code/pull/123")).toEqual({
18 host: "github.com",
19 repository: "t3tools/t3code",
20 number: 123,
21 });
22 });
23​
24 it("reads a GitCafe pull request at its canonical /pull/ path and the older /pulls/ path", () => {
25 for (const path of ["pull", "pulls"]) {
26 const url = `https://git.cafe/PingDotGG/T3Code/${path}/23897`;
27 expect(parse(url), path).toEqual({
28 host: "git.cafe",
29 repository: "pingdotgg/t3code",
30 number: 23897,
31 });
32 expect(kindOf(url), path).toBe("gitcafe");
33 }
34 expect(parse("https://staging.git.cafe/team/repo/pull/7/files")).toEqual({
35 host: "staging.git.cafe",
36 repository: "team/repo",
37 number: 7,
38 });
39 });
40​
41 it("reads Forgejo URLs even when the hostname contains github", () => {
42 expect(parse("https://github.internal/team/repo/pulls/7")).toEqual({
43 host: "github.internal",
44 authority: "github.internal",
45 repository: "team/repo",
46 number: 7,
47 });
48 });
49​
50 it("reads a GitHub pull request on any host, so GitHub Enterprise works on its own name", () => {
51 expect(parse("https://code.github.example.com/acme/web/pull/42")).toEqual({
52 host: "code.github.example.com",
53 repository: "acme/web",
54 number: 42,
55 });
56 expect(kindOf("https://scm.example.test/acme/web/pull/42")).toBe("github");
57 expect(
58 sourceControlClients.changeRequestUrlFromReferenceAutolink(
59 "https://code.github.example.com/acme/web/issues/42",
60 ),
61 ).toBe("https://code.github.example.com/acme/web/pull/42");
62 });
63​
64 it("reads a GitLab merge request on any host, nested groups and all", () => {
65 expect(parse("https://gitlab.com/t3tools/platform/t3code/-/merge_requests/42")).toEqual({
66 host: "gitlab.com",
67 repository: "t3tools/platform/t3code",
68 number: 42,
69 });
70 expect(parse("https://code.acme.test/team/project/-/merge_requests/9")).toEqual({
71 host: "code.acme.test",
72 repository: "team/project",
73 number: 9,
74 });
75 });
76​
77 it("reads Bitbucket and both Azure DevOps URL forms", () => {
78 expect(parse("https://bitbucket.org/workspace/repo/pull-requests/5")).toEqual({
79 host: "bitbucket.org",
80 repository: "workspace/repo",
81 number: 5,
82 });
83 expect(parse("https://dev.azure.com/acme/platform/_git/t3code/pullrequest/17")).toEqual({
84 host: "dev.azure.com",
85 repository: "acme/platform/_git/t3code",
86 number: 17,
87 });
88 expect(parse("https://acme.visualstudio.com/platform/_git/t3code/pullrequest/17")).toEqual({
89 host: "acme.visualstudio.com",
90 repository: "platform/_git/t3code",
91 number: 17,
92 });
93 });
94​
95 it("reads Azure DevOps Server links and keeps servers on different ports distinct", () => {
96 for (const host of ["scm.example.test", "git.cafe", "code.bitbucket.example.test"]) {
97 const repository = "tfs/collection/project/_git/repo";
98 const linked = {
99 projectId: ProjectId.make("project-1"),
100 host,
101 repository,
102 number: 123,
103 url: `https://${host.toUpperCase()}:8443/tfs/Collection/Project/_git/Repo/pullrequest/123`,
104 };
105 const other = { ...linked, url: linked.url.replace(":8443/", ":9443/") };
106 expect.soft(parse(linked.url), host).toEqual({
107 host,
108 authority: `${host}:8443`,
109 repository,
110 number: 123,
111 });
112 expect
113 .soft(
114 sourceControlClients.isSameChangeRequest(linked.url, `${linked.url}?view=files`),
115 host,
116 )
117 .toBe(true);
118 expect
119 .soft(sourceControlClients.isSameChangeRequest(linked.url, other.url), host)
120 .toBe(false);
121 expect.soft([linked, other].map(normalizeThreadPullRequestKey), host).toEqual([
122 { host: `${host}:8443`, repository, number: 123 },
123 { host: `${host}:9443`, repository, number: 123 },
124 ]);
125 }
126 });
127​
128 it("survives trailing segments, a trailing slash and a query string", () => {
129 expect(parse("https://github.com/t3tools/t3code/pull/123/files?w=1")).toEqual({
130 host: "github.com",
131 repository: "t3tools/t3code",
132 number: 123,
133 });
134 expect(parse("https://github.com/t3tools/t3code/pull/123/")).toEqual({
135 host: "github.com",
136 repository: "t3tools/t3code",
137 number: 123,
138 });
139 });
140​
141 it("claims nothing it cannot be sure of", () => {
142 for (const link of [
143 "https://github.com/t3tools/t3code/issues/123",
144 "https://git.cafe/pingdotgg/t3code/issues/23897",
145 "https://git.cafe/pingdotgg/t3code/pull/abc",
146 "https://git.cafe/pingdotgg/t3code",
147 "https://github.com/t3tools/t3code/commit/0a1b2c3",
148 "https://github.com/t3tools/t3code",
149 "https://github.com/t3tools/t3code/pull/abc",
150 "https://gitlab.com/t3tools/t3code/-/issues/12",
151 "https://blog.example.test/2026/updates/archive/pull/3",
152 "https://scm.example.test/tfs/Collection/Project/_git/Repo",
153 "https://scm.example.test/tfs/Collection/Project/pullrequest/123",
154 "https://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/abc",
155 "https://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/0",
156 "https://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/9007199254740992",
157 "ftp://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/123",
158 "javascript:alert(1)//github.com/t3tools/t3code/pull/1",
159 "not a url",
160 ]) {
161 expect(parse(link), link).toBeNull();
162 }
163 });
164});
165​
166describe("sourceControlClients.isSameChangeRequest", () => {
167 const linked = "https://github.com/pingdotgg/t3code/pull/42";
168​
169 it("matches the same pull request whatever its case or trailing path", () => {
170 expect(
171 sourceControlClients.isSameChangeRequest(
172 linked,
173 "https://github.com/PingDotGG/T3Code/pull/42/files",
174 ),
175 ).toBe(true);
176 });
177​
178 it.each([
179 ["http://forge.example:3000/git/team/repo/pulls/42/files", true],
180 ["http://forge.example:4000/git/team/repo/pulls/42", false],
181 ["http://forge.example/git/team/repo/pulls/42", false],
182 ])("matches Forgejo links by web authority: %s", (url, expected) => {
183 expect(
184 sourceControlClients.isSameChangeRequest(
185 "http://forge.example:3000/git/team/repo/pulls/42",
186 url,
187 ),
188 ).toBe(expected);
189 });
190​
191 it("keeps Forgejo and GitHub path shapes distinct on a GitHub-named host", () => {
192 expect(
193 sourceControlClients.isSameChangeRequest(
194 "https://github.internal/team/repo/pulls/42",
195 "https://github.internal/team/repo/pull/42",
196 ),
197 ).toBe(false);
198 });
199});
200​
201describe("change request URLs built from a parsed link", () => {
202 const sibling = (url: string, number: number) => {
203 const parsed = sourceControlClients.parseChangeRequestUrl(url)!;
204 return parsed.definition.changeRequestUrl({
205 host: parsed.link.host,
206 repository: parsed.link.repository,
207 number,
208 webUrl: parsed.link.repositoryUrl,
209 });
210 };
211​
212 it.each([
213 [
214 "http://git.example.test:3000/team/repo/pulls/42/files",
215 "http://git.example.test:3000/team/repo/pulls/43",
216 ],
217 ["https://github.com/pull/1/pull/42/files", "https://github.com/pull/1/pull/43"],
218 [
219 "https://dev.azure.com/acme/project/_git/web/pullrequest/42?view=files",
220 "https://dev.azure.com/acme/project/_git/web/pullrequest/43",
221 ],
222 ])("builds a canonical sibling of %s", (url, expected) => {
223 expect(sibling(url, 43)).toBe(expected);
224 });
225​
226 it.each([
227 ["ssh.dev.azure.com", "v3/org/project/web"],
228 ["org.visualstudio.com", "defaultcollection/project/_git/web"],
229 ["dev.azure.com", "org/project/_git/web"],
230 ])("reads back the Azure browser URL built from the remote %s/%s", (host, repository) => {
231 const url = sourceControlClients
232 .get("azure-devops")
233 .changeRequestUrl({ host, repository, number: 42 });
234 expect(parse(url!)).toEqual({
235 host: "dev.azure.com",
236 repository: "org/project/_git/web",
237 number: 42,
238 });
239 });
240});
241​
242describe("sourceControlClients.recognizeRemote", () => {
243 const kindOfRemote = (url: string) => sourceControlClients.recognizeRemote(url)?.kind ?? null;
244​
245 it("asks hosts in a fixed order when one hostname carries several hosts' labels", () => {
246 expect(kindOfRemote("https://gitea.github.example/o/r")).toBe("forgejo");
247 expect(kindOfRemote("https://forgejo.gitlab.example/o/r")).toBe("forgejo");
248 expect(kindOfRemote("https://gitlab.github.example/o/r")).toBe("github");
249 expect(kindOfRemote("https://bitbucket.gitlab.example/o/r")).toBe("gitlab");
250 expect(kindOfRemote("https://bitbucket.visualstudio.com/p/_git/r")).toBe("azure-devops");
251 expect(kindOfRemote("git@git.cafe:pingdotgg/t3code.git")).toBe("gitcafe");
252 });
253​
254 it("reads a remote no host claims as an unknown host, and a local path as no host", () => {
255 expect(
256 sourceControlClients.recognizeRemote("https://self-hosted.example.test:8443/group/repo.git"),
257 ).toEqual({
258 kind: "unknown",
259 name: "self-hosted.example.test:8443",
260 baseUrl: "https://self-hosted.example.test:8443",
261 });
262 expect(sourceControlClients.recognizeRemote("git@github-work:owner/repo.git")).toEqual({
263 kind: "unknown",
264 name: "github-work",
265 baseUrl: "https://github-work",
266 });
267 for (const local of ["/srv/git/repo.git", "C:/repos/project", "file:///srv/git/repo.git", ""]) {
268 expect(sourceControlClients.recognizeRemote(local), local).toBeNull();
269 }
270 });
271});
272​
273describe("sourceControlClients repository keys", () => {
274 // Persisted as `repositoryIdentity.canonicalKey` and in thread pull request keys.
275 it.each([
276 ["git@github.com:T3Tools/T3Code.git", "github.com/t3tools/t3code"],
277 ["ssh://git@gitlab.company.com:2222/team/project.git", "gitlab.company.com/team/project"],
278 [
279 "git@ssh.dev.azure.com:v3/T3Tools/Platform/T3Code",
280 "dev.azure.com/t3tools/platform/_git/t3code",
281 ],
282 [
283 "T3Tools@vs-ssh.visualstudio.com:v3/T3Tools/Platform/T3Code",
284 "t3tools.visualstudio.com/platform/_git/t3code",
285 ],
286 [
287 "https://T3Tools.visualstudio.com/DefaultCollection/Platform/_git/T3Code",
288 "t3tools.visualstudio.com/defaultcollection/platform/_git/t3code",
289 ],
290 ["git@ssh.dev.azure.com:v3/o/p", "ssh.dev.azure.com/v3/o/p"],
291 ["ssh.dev.azure.com/v3/o/p/r", "ssh.dev.azure.com/v3/o/p/r"],
292 ["C:/repos/project", "c:/repos/project"],
293 ])("keys %s as %s", (remote, key) => {
294 expect(sourceControlClients.repositoryKeyFromRemote(remote)).toBe(key);
295 });
296​
297 it("compares Azure DevOps spellings on dev.azure.com and leaves other keys alone", () => {
298 expect(
299 sourceControlClients.canonicalRepositoryKey(
300 "org.visualstudio.com/defaultcollection/project/_git/repo",
301 ),
302 ).toBe("dev.azure.com/org/project/_git/repo");
303 expect(sourceControlClients.findByRepositoryKey("dev.azure.com/o/r")?.kind).toBe(
304 "azure-devops",
305 );
306 expect(sourceControlClients.canonicalRepositoryKey("tfs.corp.example/c/p/_git/r")).toBe(
307 "tfs.corp.example/c/p/_git/r",
308 );
309 expect(sourceControlClients.findByRepositoryKey("github.com/o/r")).toBeUndefined();
310 });
311});
312​
313describe("sourceControlClients project hosts", () => {
314 const identity = {
315 canonicalKey: "forge.example/team/repo",
316 locator: { remoteUrl: "http://forge.example:3000/team/repo.git" },
317 };
318​
319 it("keeps an HTTP remote's port for Forgejo only, and falls back to the kind without a key", () => {
320 expect(sourceControlClients.changeRequestHost(identity, "forgejo")).toBe("forge.example:3000");
321 expect(sourceControlClients.changeRequestHost(identity, "gitlab")).toBe("forge.example");
322 expect(sourceControlClients.changeRequestHost(identity, undefined)).toBe("forge.example");
323 expect(
324 sourceControlClients.changeRequestHost(
325 { locator: { remoteUrl: "https://x.example/a/b" } },
326 "github",
327 ),
328 ).toBe("github");
329 });
330​
331 it("selects a repository by its path except where the host's tools take a name", () => {
332 const repository = {
333 displayName: "group/subgroup/service",
334 owner: "group",
335 name: "service",
336 };
337 expect(sourceControlClients.repositorySelector(repository)).toBe("group/subgroup/service");
338 expect(
339 sourceControlClients.repositorySelector({
340 ...repository,
341 provider: SourceControlProviderKind.make("gitlab"),
342 }),
343 ).toBe("group/subgroup/service");
344 expect(
345 sourceControlClients.repositorySelector({
346 ...repository,
347 provider: SourceControlProviderKind.make("azure-devops"),
348 }),
349 ).toBe("service");
350 expect(sourceControlClients.repositorySelector({ owner: "o", name: "r" })).toBe("o/r");
351 });
352});
353​
354describe("sourceControlClients.findByPublicDomain", () => {
355 it("names the host a link is on, subdomains included", () => {
356 const kindOf = (hostname: string) => sourceControlClients.findByPublicDomain(hostname)?.kind;
357 expect(kindOf("gist.github.com")).toBe("github");
358 expect(kindOf("GitLab.com")).toBe("gitlab");
359 expect(kindOf("codeberg.org")).toBe("forgejo");
360 expect(kindOf("git.cafe")).toBe("gitcafe");
361 expect(kindOf("notgithub.com")).toBeUndefined();
362 expect(kindOf("github.com.example")).toBeUndefined();
363 });
364​
365 it("leaves findByPublicHost to the public host alone", () => {
366 expect(sourceControlClients.findByPublicHost("gist.github.com")).toBeUndefined();
367 });
368});