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

gitHubRepositoryResolution.test.ts

91 lines3.1 KB

1import { assert, describe, it } from "@effect/vitest";
2​
3import {
4 gitHubApiHostForRemote,
5 selectGitHubBaseRepository,
6} from "./gitHubRepositoryResolution.ts";
7​
8describe("selectGitHubBaseRepository", () => {
9 const remotes = (...entries: ReadonlyArray<readonly [string, string]>) =>
10 entries
11 .flatMap(([name, url]) => [`${name}\t${url} (fetch)`, `${name}\t${url} (push)`])
12 .join("\n");
13 const select = (input: { remotes: string; resolved?: string }) =>
14 selectGitHubBaseRepository({ resolved: "", host: "github.com", ...input });
15​
16 it("picks the repository gh reads without a prompt", () => {
17 assert.deepStrictEqual(select({ remotes: remotes(["fork", "git@github.com:me/web.git"]) }), {
18 owner: "me",
19 name: "web",
20 });
21 const fork = remotes(
22 ["origin", "git@github.com:me/web.git"],
23 ["upstream", "https://github.com/Acme/Web.git"],
24 );
25 assert.deepStrictEqual(select({ remotes: fork }), { owner: "acme", name: "web" });
26 assert.deepStrictEqual(select({ remotes: fork, resolved: "remote.origin.gh-resolved base" }), {
27 owner: "me",
28 name: "web",
29 });
30 assert.deepStrictEqual(
31 select({ remotes: fork, resolved: "remote.origin.gh-resolved acme/other" }),
32 { owner: "acme", name: "other" },
33 );
34 const partialClone = fork.replaceAll("(fetch)", "(fetch) [blob:none]");
35 assert.deepStrictEqual(
36 select({ remotes: partialClone, resolved: "remote.origin.gh-resolved base" }),
37 { owner: "me", name: "web" },
38 );
39 assert.deepStrictEqual(
40 select({
41 remotes: remotes(
42 ["origin", "git@github.com:me/web.git"],
43 ["Upstream", "git@github.com:acme/web.git"],
44 ),
45 }),
46 { owner: "acme", name: "web" },
47 );
48 });
49​
50 it("leaves gh to choose when it might weigh the remotes differently", () => {
51 for (const input of [
52 { remotes: "" },
53 {
54 remotes: remotes(["a", "git@github.com:me/web.git"], ["b", "git@github.com:acme/web.git"]),
55 },
56 {
57 remotes: remotes(
58 ["origin", "git@github.com:me/web.git"],
59 ["mirror", "git@gitlab.com:me/web.git"],
60 ),
61 },
62 { remotes: remotes(["origin", "git@github-work:me/web.git"]) },
63 {
64 remotes: remotes(
65 ["origin", "git@github.com:me/web.git"],
66 ["Origin", "git@github.com:acme/web.git"],
67 ),
68 },
69 {
70 remotes: remotes(
71 ["origin", "git@github.com:me/web.git"],
72 ["upstream", "git@github.com:acme/web.git"],
73 ),
74 resolved: "remote.origin.gh-resolved base\nremote.upstream.gh-resolved base",
75 },
76 ]) {
77 assert.strictEqual(select(input), null);
78 }
79 });
80});
81​
82describe("gitHubApiHostForRemote", () => {
83 it("reads an SSH alias through github.com and a real host as itself", () => {
84 assert.strictEqual(gitHubApiHostForRemote("git@github:acme/web.git"), "github.com");
85 assert.strictEqual(
86 gitHubApiHostForRemote("git@github.example.com:a/b.git"),
87 "github.example.com",
88 );
89 assert.strictEqual(gitHubApiHostForRemote("git@gitlab.com:a/b.git"), null);
90 });
91});