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

gitHubRepositoryResolution.test.ts

86 lines2.8 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 assert.deepStrictEqual(
35 select({
36 remotes: remotes(
37 ["origin", "git@github.com:me/web.git"],
38 ["Upstream", "git@github.com:acme/web.git"],
39 ),
40 }),
41 { owner: "acme", name: "web" },
42 );
43 });
44​
45 it("leaves gh to choose when it might weigh the remotes differently", () => {
46 for (const input of [
47 { remotes: "" },
48 {
49 remotes: remotes(["a", "git@github.com:me/web.git"], ["b", "git@github.com:acme/web.git"]),
50 },
51 {
52 remotes: remotes(
53 ["origin", "git@github.com:me/web.git"],
54 ["mirror", "git@gitlab.com:me/web.git"],
55 ),
56 },
57 { remotes: remotes(["origin", "git@github-work:me/web.git"]) },
58 {
59 remotes: remotes(
60 ["origin", "git@github.com:me/web.git"],
61 ["Origin", "git@github.com:acme/web.git"],
62 ),
63 },
64 {
65 remotes: remotes(
66 ["origin", "git@github.com:me/web.git"],
67 ["upstream", "git@github.com:acme/web.git"],
68 ),
69 resolved: "remote.origin.gh-resolved base\nremote.upstream.gh-resolved base",
70 },
71 ]) {
72 assert.strictEqual(select(input), null);
73 }
74 });
75});
76​
77describe("gitHubApiHostForRemote", () => {
78 it("reads an SSH alias through github.com and a real host as itself", () => {
79 assert.strictEqual(gitHubApiHostForRemote("git@github:acme/web.git"), "github.com");
80 assert.strictEqual(
81 gitHubApiHostForRemote("git@github.example.com:a/b.git"),
82 "github.example.com",
83 );
84 assert.strictEqual(gitHubApiHostForRemote("git@gitlab.com:a/b.git"), null);
85 });
86});