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