|
| 1 | +import { |
| 2 | + type ChangeRequestCheckoutInput, |
| 3 | + UNKNOWN_SOURCE_CONTROL_CLIENT, |
| 4 | +} from "@t3tools/source-control-core/client/definition"; |
| 5 | +import { describe, expect, it } from "vite-plus/test"; |
| 6 | + |
| 7 | +import { sourceControlClients } from "./sourceControlClients.ts"; |
| 8 | + |
| 9 | +describe("sourceControlClients", () => { |
| 10 | + it("reads a missing kind as GitHub and an unshipped kind as the generic host", () => { |
| 11 | + expect(sourceControlClients.get(undefined).kind).toBe("github"); |
| 12 | + expect(sourceControlClients.get(null).kind).toBe("github"); |
| 13 | + expect(sourceControlClients.get("unknown")).toBe(UNKNOWN_SOURCE_CONTROL_CLIENT); |
| 14 | + expect(sourceControlClients.get("forkhost")).toBe(UNKNOWN_SOURCE_CONTROL_CLIENT); |
| 15 | + expect(sourceControlClients.find("forkhost")).toBeUndefined(); |
| 16 | + expect(UNKNOWN_SOURCE_CONTROL_CLIENT.changeRequest).toEqual({ |
| 17 | + shortLabel: "change request", |
| 18 | + singular: "change request", |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + it("attributes change request URLs by their path shape", () => { |
| 23 | + const kindOf = (url: string) => sourceControlClients.findByChangeRequestUrl(url)?.kind; |
| 24 | + expect(kindOf("https://github.com/acme/web/pull/7")).toBe("github"); |
| 25 | + expect(kindOf("https://gitlab.example/group/sub/web/-/merge_requests/7")).toBe("gitlab"); |
| 26 | + expect(kindOf("https://codeberg.org/acme/web/pulls/7")).toBe("forgejo"); |
| 27 | + // GitCafe shares Forgejo's `/pulls/` shape, so only its own hosts read as GitCafe. |
| 28 | + expect(kindOf("https://git.cafe/acme/web/pulls/7")).toBe("gitcafe"); |
| 29 | + expect(kindOf("https://staging.git.cafe/acme/web/pulls/7")).toBe("gitcafe"); |
| 30 | + expect(kindOf("https://git.cafe.example/acme/web/pulls/7")).toBe("forgejo"); |
| 31 | + expect(kindOf("https://bitbucket.org/acme/web/pull-requests/7")).toBe("bitbucket"); |
| 32 | + expect(kindOf("https://dev.azure.com/org/project/_git/web/pullrequest/7")).toBe("azure-devops"); |
| 33 | + expect(kindOf("https://example.com/acme/web/issues/7")).toBeUndefined(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("attributes only public hostnames that name exactly one host", () => { |
| 37 | + expect(sourceControlClients.findByPublicHost("GitHub.com")?.kind).toBe("github"); |
| 38 | + expect(sourceControlClients.findByPublicHost("gitlab.com")?.kind).toBe("gitlab"); |
| 39 | + expect(sourceControlClients.findByPublicHost("codeberg.org")).toBeUndefined(); |
| 40 | + expect(sourceControlClients.findByPublicHost("dev.azure.com")).toBeUndefined(); |
| 41 | + expect(sourceControlClients.findByPublicHost("git.cafe")).toBeUndefined(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("builds each host's checkout command", () => { |
| 45 | + const checkout = (kind: string, input: ChangeRequestCheckoutInput) => |
| 46 | + sourceControlClients.get(kind).checkoutCommand(input); |
| 47 | + expect(checkout("github", { number: 42, headBranch: "" })).toBe("gh pr checkout 42"); |
| 48 | + expect(checkout("gitlab", { number: 42, headBranch: "" })).toBe("glab mr checkout 42"); |
| 49 | + expect(checkout("azure-devops", { number: 42, headBranch: "" })).toBe( |
| 50 | + "az repos pr checkout --id 42", |
| 51 | + ); |
| 52 | + expect(checkout("gitcafe", { number: 42, headBranch: "" })).toBe("cafe pr checkout 42"); |
| 53 | + expect(checkout("unknown", { number: 42, headBranch: "feature" })).toBeNull(); |
| 54 | + expect(checkout("forgejo", { number: 42, headBranch: "feature" })).toBeNull(); |
| 55 | + expect( |
| 56 | + checkout("forgejo", { |
| 57 | + number: 42, |
| 58 | + headBranch: "feature", |
| 59 | + repositoryUrl: "https://forgejo.local/maria/repo'$(echo nope)", |
| 60 | + }), |
| 61 | + ).toBe( |
| 62 | + "git fetch 'https://forgejo.local/maria/repo'\\''$(echo nope)' refs/pull/42/head && git checkout -B pulls/42 FETCH_HEAD", |
| 63 | + ); |
| 64 | + expect( |
| 65 | + checkout("bitbucket", { |
| 66 | + number: 42, |
| 67 | + headBranch: "feature/checkout", |
| 68 | + headRepositoryNameWithOwner: "maria/t3code", |
| 69 | + }), |
| 70 | + ).toBe( |
| 71 | + "git clone --single-branch --branch feature/checkout https://bitbucket.org/maria/t3code.git t3code-pr-42", |
| 72 | + ); |
| 73 | + expect( |
| 74 | + checkout("bitbucket", { |
| 75 | + number: 42, |
| 76 | + headBranch: "feature;rm -rf /", |
| 77 | + headRepositoryNameWithOwner: "maria/t3code", |
| 78 | + }), |
| 79 | + ).toBeNull(); |
| 80 | + }); |
| 81 | + |
| 82 | + it("links GitHub author profiles but not bots, and no other host's", () => { |
| 83 | + const github = sourceControlClients.get("github"); |
| 84 | + expect(github.authorProfileUrl("octo cat", "https://ghe.example/acme/web")).toBe( |
| 85 | + "https://ghe.example/octo%20cat", |
| 86 | + ); |
| 87 | + expect(github.authorProfileUrl("renovate[bot]", "https://github.com/acme/web")).toBeNull(); |
| 88 | + expect( |
| 89 | + sourceControlClients.get("gitlab").authorProfileUrl("octocat", "https://gitlab.com/a/b"), |
| 90 | + ).toBeNull(); |
| 91 | + }); |
| 92 | + |
| 93 | + it("asks only Forgejo for a summary when requesting changes", () => { |
| 94 | + expect(sourceControlClients.get("forgejo").reviewSummaryRequired("request-changes")).toBe(true); |
| 95 | + expect(sourceControlClients.get("forgejo").reviewSummaryRequired("approve")).toBe(false); |
| 96 | + expect(sourceControlClients.get("github").reviewSummaryRequired("request-changes")).toBe(false); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe("reference autolinks", () => { |
| 101 | + it("link only under GitHub's repository URL", () => { |
| 102 | + expect( |
| 103 | + sourceControlClients.get("github").referenceAutolinkRepositoryUrl("https://github.com/a/b"), |
| 104 | + ).toBe("https://github.com/a/b"); |
| 105 | + expect( |
| 106 | + sourceControlClients.get("gitlab").referenceAutolinkRepositoryUrl("https://gitlab.com/a/b"), |
| 107 | + ).toBeNull(); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +describe("change request URLs", () => { |
| 112 | + const url = ( |
| 113 | + kind: string, |
| 114 | + input: Parameters<typeof UNKNOWN_SOURCE_CONTROL_CLIENT.changeRequestUrl>[0], |
| 115 | + ) => sourceControlClients.get(kind).changeRequestUrl(input); |
| 116 | + const at = { host: "code.example", repository: "acme/web", number: 7 }; |
| 117 | + |
| 118 | + it("builds each host's path", () => { |
| 119 | + expect(url("github", at)).toBe("https://code.example/acme/web/pull/7"); |
| 120 | + expect(url("gitlab", at)).toBe("https://code.example/acme/web/-/merge_requests/7"); |
| 121 | + expect(url("bitbucket", at)).toBe("https://code.example/acme/web/pull-requests/7"); |
| 122 | + expect(url("gitcafe", { ...at, host: "git.cafe" })).toBe("https://git.cafe/acme/web/pulls/7"); |
| 123 | + expect( |
| 124 | + url("azure-devops", { |
| 125 | + host: "ssh.dev.azure.com", |
| 126 | + repository: "v3/org/project/web", |
| 127 | + number: 7, |
| 128 | + }), |
| 129 | + ).toBe("https://dev.azure.com/org/project/_git/web/pullrequest/7"); |
| 130 | + expect(url("unknown", at)).toBeNull(); |
| 131 | + }); |
| 132 | + |
| 133 | + it("puts Forgejo pages on the server's web origin", () => { |
| 134 | + expect(url("forgejo", { ...at, webUrl: "https://code.example:3000/git/acme/web/" })).toBe( |
| 135 | + "https://code.example:3000/git/acme/web/pulls/7", |
| 136 | + ); |
| 137 | + expect(url("forgejo", { ...at, remoteUrl: "http://code.example:3000/acme/web.git" })).toBe( |
| 138 | + "http://code.example:3000/acme/web/pulls/7", |
| 139 | + ); |
| 140 | + expect(url("forgejo", { ... at, remoteUrl: "[email protected]:acme/web.git" })).toBe( |
| 141 | + "https://code.example/acme/web/pulls/7", |
| 142 | + ); |
| 143 | + expect(url("forgejo", { ...at, remoteUrl: "https://elsewhere.example/acme/web.git" })).toBe( |
| 144 | + "https://code.example/acme/web/pulls/7", |
| 145 | + ); |
| 146 | + }); |
| 147 | +}); |
0 commit comments