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

definition.test.ts

131 lines5.2 KB

1import type { ChangeRequestProjectIdentity } from "@t3tools/source-control-core/shared/definition";
2import { recognizeRemoteWith } from "@t3tools/source-control-core/shared/definition";
3import { describe, expect, it } from "vite-plus/test";
4​
5import { definition } from "./definition.ts";
6​
7const parse = (url: string) => definition.parseChangeRequestUrl(new URL(url))!;
8​
9const identity = (
10 remoteUrl: string,
11 fields: Partial<ChangeRequestProjectIdentity> = {},
12): ChangeRequestProjectIdentity => ({
13 canonicalKey: "forge.example/git/team/repo",
14 displayName: "git/team/repo",
15 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
16 ...fields,
17});
18​
19const onHost = (project: ChangeRequestProjectIdentity, link: ReturnType<typeof parse>) =>
20 definition.isChangeRequestInRepository(project, link) ||
21 definition.canReadChangeRequestOnHost(project, link);
22​
23describe("Forgejo change request URLs", () => {
24 it("reads nested paths and keeps the server's port as the authority", () => {
25 expect(parse("http://git.example.test:3000/Team/Repo/pulls/42/files")).toEqual({
26 host: "git.example.test",
27 authority: "git.example.test:3000",
28 repository: "team/repo",
29 number: 42,
30 repositoryUrl: "http://git.example.test:3000/Team/Repo",
31 });
32 expect(parse("https://github.internal/team/repo/pulls/7")).toMatchObject({
33 host: "github.internal",
34 authority: "github.internal",
35 });
36 });
37​
38 it("claims only its own route", () => {
39 for (const url of [
40 "https://forge.example/team/repo/pull/7",
41 "https://forge.example/repo/pulls/7",
42 "https://forge.example/team/repo/pulls/abc",
43 ]) {
44 expect(definition.parseChangeRequestUrl(new URL(url)), url).toBeNull();
45 }
46 });
47​
48 it.each([
49 ["http://forge.example:3000/git/owner/repo.git", "http://forge.example:3000"],
50 ["git@forge.example:git/owner/repo.git", "https://forge.example"],
51 ["http://other.example:3000/git/owner/repo.git", "https://forge.example"],
52 ])("keeps the matching web origin from %s", (remoteUrl, origin) => {
53 expect(
54 definition.changeRequestUrl({
55 host: "forge.example",
56 repository: "git/owner/repo",
57 number: 42,
58 remoteUrl,
59 }),
60 ).toBe(`${origin}/git/owner/repo/pulls/42`);
61 });
62​
63 it("keeps the origin when the host already carries its port", () => {
64 expect(
65 definition.changeRequestUrl({
66 host: "forge.example:3000",
67 repository: "team/repo",
68 number: 42,
69 remoteUrl: "http://forge.example:3000/team/repo.git",
70 }),
71 ).toBe("http://forge.example:3000/team/repo/pulls/42");
72 });
73​
74 it("matches projects by HTTP port at repository and host level", () => {
75 const [atThree, atFour] = [3000, 4000].map((port) =>
76 identity(`http://forge.example:${port}/git/team/repo.git`),
77 );
78 const link = parse("http://forge.example:4000/git/team/repo/pulls/42");
79 expect(definition.isChangeRequestInRepository(atFour!, link)).toBe(true);
80 expect(onHost(atFour!, { ...link, repository: "git/team/other" })).toBe(true);
81 expect(definition.isChangeRequestInRepository(atThree!, link)).toBe(false);
82 expect(onHost(atThree!, link)).toBe(false);
83 });
84​
85 it("lets tea resolve the web port for SSH remotes, and honours a resolved web URL", () => {
86 const link = parse("http://forge.example:4000/git/team/repo/pulls/42");
87 expect(
88 definition.isChangeRequestInRepository(identity("git@forge.example:git/team/repo.git"), link),
89 ).toBe(true);
90 const aliased = identity("git@ssh.forge.example:team/repo.git", {
91 canonicalKey: "ssh.forge.example/team/repo",
92 displayName: "team/repo",
93 webUrl: "http://forge.example:4000/git/team/repo",
94 });
95 expect(definition.isChangeRequestInRepository(aliased, link)).toBe(true);
96 expect(onHost(aliased, { ...link, repository: "git/team/other" })).toBe(true);
97 for (const url of [
98 "http://other.example:4000/git/team/repo/pulls/42",
99 "http://forge.example:3000/git/team/repo/pulls/42",
100 "http://forge.example:4000/other/team/repo/pulls/42",
101 ]) {
102 expect(onHost(aliased, parse(url)), url).toBe(false);
103 }
104 });
105});
106​
107describe("Forgejo remotes", () => {
108 it("recognises Codeberg and Forgejo or Gitea hosts, keeping an HTTP remote's origin", () => {
109 for (const host of ["codeberg.org", "forgejo.example.test", "gitea.example.test"]) {
110 expect(recognizeRemoteWith(definition, `http://${host}:3000/team/repo.git`)).toEqual({
111 kind: "forgejo",
112 name: "Forgejo",
113 baseUrl: `http://${host}:3000`,
114 });
115 }
116 expect(
117 recognizeRemoteWith(definition, "ssh://git@forgejo.example.test:2222/team/repo.git")?.baseUrl,
118 ).toBe("https://forgejo.example.test");
119 expect(recognizeRemoteWith(definition, "https://git.example.test/team/repo")).toBeNull();
120 });
121});
122​
123describe("Forgejo change request host", () => {
124 it("keeps an HTTP remote's port, because a server's identity includes it", () => {
125 const project = identity("http://forge.example:3000/git/team/repo.git");
126 expect(definition.changeRequestHost?.(project)).toBe("forge.example:3000");
127 expect(
128 definition.changeRequestHost?.(identity("ssh://git@forge.example:2222/git/team/repo.git")),
129 ).toBe("forge.example");
130 });
131});