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