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