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