The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import { readEnvCredentials } from "@alchemy.run/github/AuthProvider"; |
| 2 | import { |
| 3 | gitHubBaseUrlChanged, |
| 4 | githubHostname, |
| 5 | normalizeGitHubBaseUrl, |
| 6 | resolveGitHubBaseUrlFromEnv, |
| 7 | withBaseUrl, |
| 8 | } from "@alchemy.run/github/BaseUrl"; |
| 9 | import { Credentials, fromToken } from "@alchemy.run/github/Credentials"; |
| 10 | import { describe, expect, test } from "@alchemy.run/test"; |
| 11 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Result from "effect/Result"; |
| 14 | |
| 15 | const normalize = (input: string) => Effect.runSync(normalizeGitHubBaseUrl(input)); |
| 16 | |
| 17 | describe("normalizeGitHubBaseUrl", { tags: ["unit", "provider:github", "local"] }, () => { |
| 18 | test("github.com hosts normalize to undefined (the github.com default)", () => { |
| 19 | expect(normalize("github.com")).toBeUndefined(); |
| 20 | expect(normalize("https://github.com")).toBeUndefined(); |
| 21 | expect(normalize("https://www.github.com/")).toBeUndefined(); |
| 22 | expect(normalize("https://api.github.com")).toBeUndefined(); |
| 23 | }); |
| 24 | |
| 25 | test("GitHub Enterprise Server hosts get /api/v3 appended", () => { |
| 26 | expect(normalize("github.example.com")).toBe("https://github.example.com/api/v3"); |
| 27 | expect(normalize("https://github.example.com")).toBe("https://github.example.com/api/v3"); |
| 28 | expect(normalize("https://github.example.com/")).toBe("https://github.example.com/api/v3"); |
| 29 | }); |
| 30 | |
| 31 | test("an explicit API path is honored as-is", () => { |
| 32 | expect(normalize("https://github.example.com/api/v3")).toBe( |
| 33 | "https://github.example.com/api/v3", |
| 34 | ); |
| 35 | expect(normalize("https://github.example.com/api/v3/")).toBe( |
| 36 | "https://github.example.com/api/v3", |
| 37 | ); |
| 38 | }); |
| 39 | |
| 40 | test("non-default ports and http protocol are preserved", () => { |
| 41 | expect(normalize("http://github.example.com:8080")).toBe( |
| 42 | "http://github.example.com:8080/api/v3", |
| 43 | ); |
| 44 | }); |
| 45 | |
| 46 | test("data-residency ghe.com hosts get the api. prefix", () => { |
| 47 | expect(normalize("acme.ghe.com")).toBe("https://api.acme.ghe.com"); |
| 48 | expect(normalize("https://acme.ghe.com")).toBe("https://api.acme.ghe.com"); |
| 49 | expect(normalize("https://api.acme.ghe.com")).toBe("https://api.acme.ghe.com"); |
| 50 | }); |
| 51 | |
| 52 | test("invalid input fails with AuthError", () => { |
| 53 | const result = Effect.runSync(Effect.result(normalizeGitHubBaseUrl("https://"))); |
| 54 | expect(Result.isFailure(result)).toBe(true); |
| 55 | }); |
| 56 | }); |
| 57 | |
| 58 | describe("githubHostname", { tags: ["unit", "provider:github", "local"] }, () => { |
| 59 | test("GHES base URL yields the plain host", () => { |
| 60 | expect(githubHostname("https://github.example.com/api/v3")).toBe("github.example.com"); |
| 61 | }); |
| 62 | |
| 63 | test("data-residency base URL drops the api. prefix", () => { |
| 64 | expect(githubHostname("https://api.acme.ghe.com")).toBe("acme.ghe.com"); |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | describe("resolveGitHubBaseUrlFromEnv", { tags: ["unit", "provider:github", "local"] }, () => { |
| 69 | // The default ConfigProvider snapshots process.env, so tests inject their |
| 70 | // environment via a ConfigProvider layer instead of mutating process.env. |
| 71 | const resolveWith = (env: Record<string, string>) => |
| 72 | Effect.runSync( |
| 73 | resolveGitHubBaseUrlFromEnv.pipe( |
| 74 | Effect.provideService(ConfigProvider.ConfigProvider, ConfigProvider.fromEnv({ env })), |
| 75 | ), |
| 76 | ); |
| 77 | |
| 78 | test("GITHUB_BASE_URL wins over GITHUB_API_URL", () => { |
| 79 | expect( |
| 80 | resolveWith({ |
| 81 | GITHUB_BASE_URL: "https://github.example.com", |
| 82 | GITHUB_API_URL: "https://api.github.com", |
| 83 | }), |
| 84 | ).toBe("https://github.example.com/api/v3"); |
| 85 | }); |
| 86 | |
| 87 | test("GITHUB_API_URL pointing at github.com resolves to undefined", () => { |
| 88 | expect(resolveWith({ GITHUB_API_URL: "https://api.github.com" })).toBeUndefined(); |
| 89 | }); |
| 90 | |
| 91 | test("GH_HOST resolves as a bare hostname", () => { |
| 92 | expect(resolveWith({ GH_HOST: "github.example.com" })).toBe( |
| 93 | "https://github.example.com/api/v3", |
| 94 | ); |
| 95 | }); |
| 96 | |
| 97 | test("resolves to undefined when nothing is set", () => { |
| 98 | expect(resolveWith({})).toBeUndefined(); |
| 99 | }); |
| 100 | }); |
| 101 | |
| 102 | describe("readEnvCredentials", { tags: ["unit", "provider:github", "local"] }, () => { |
| 103 | const readWith = (env: Record<string, string>, configBaseUrl?: string) => |
| 104 | Effect.runSync( |
| 105 | readEnvCredentials(configBaseUrl).pipe( |
| 106 | Effect.provideService(ConfigProvider.ConfigProvider, ConfigProvider.fromEnv({ env })), |
| 107 | ), |
| 108 | ); |
| 109 | |
| 110 | test("enterprise token variables win on an enterprise host", () => { |
| 111 | const creds = readWith({ |
| 112 | GITHUB_BASE_URL: "https://github.example.com", |
| 113 | GH_ENTERPRISE_TOKEN: "enterprise-token", |
| 114 | GITHUB_TOKEN: "standard-token", |
| 115 | }); |
| 116 | expect(creds.baseUrl).toBe("https://github.example.com/api/v3"); |
| 117 | expect(creds.source.details).toBe("GH_ENTERPRISE_TOKEN"); |
| 118 | }); |
| 119 | |
| 120 | test("enterprise token variables are ignored on github.com", () => { |
| 121 | const creds = readWith({ |
| 122 | GH_ENTERPRISE_TOKEN: "enterprise-token", |
| 123 | GITHUB_TOKEN: "standard-token", |
| 124 | }); |
| 125 | expect(creds.baseUrl).toBeUndefined(); |
| 126 | expect(creds.source.details).toBe("GITHUB_TOKEN"); |
| 127 | }); |
| 128 | |
| 129 | test("an explicit config baseUrl wins over the environment", () => { |
| 130 | const creds = readWith( |
| 131 | { GITHUB_BASE_URL: "https://other.example.com", GITHUB_ACCESS_TOKEN: "token" }, |
| 132 | "https://github.example.com/api/v3", |
| 133 | ); |
| 134 | expect(creds.baseUrl).toBe("https://github.example.com/api/v3"); |
| 135 | expect(creds.source.details).toBe("GITHUB_ACCESS_TOKEN"); |
| 136 | }); |
| 137 | |
| 138 | test("fails with AuthError when no token variable is set", () => { |
| 139 | const result = Effect.runSync( |
| 140 | Effect.result( |
| 141 | readEnvCredentials().pipe( |
| 142 | Effect.provideService(ConfigProvider.ConfigProvider, ConfigProvider.fromEnv({ env: {} })), |
| 143 | ), |
| 144 | ), |
| 145 | ); |
| 146 | expect(Result.isFailure(result)).toBe(true); |
| 147 | }); |
| 148 | }); |
| 149 | |
| 150 | describe("gitHubBaseUrlChanged", { tags: ["unit", "provider:github", "local"] }, () => { |
| 151 | // The comparison resolves each side through the full fallback chain, so it |
| 152 | // needs the ambient credentials host — `undefined` in props means "use the |
| 153 | // credentials' host", not "github.com". |
| 154 | const changed = (a: string | undefined, b: string | undefined, credsBaseUrl?: string) => |
| 155 | Effect.runSync( |
| 156 | gitHubBaseUrlChanged({ baseUrl: a }, { baseUrl: b }).pipe( |
| 157 | Effect.provide( |
| 158 | fromToken( |
| 159 | "test-token", |
| 160 | credsBaseUrl !== undefined ? { baseUrl: credsBaseUrl } : undefined, |
| 161 | ), |
| 162 | ), |
| 163 | ), |
| 164 | ); |
| 165 | |
| 166 | test("cosmetic rewrites of the same host do not count as a change", () => { |
| 167 | expect(changed("github.example.com", "https://github.example.com/api/v3")).toBe(false); |
| 168 | expect(changed(undefined, "github.com")).toBe(false); |
| 169 | expect(changed("acme.ghe.com", "https://api.acme.ghe.com")).toBe(false); |
| 170 | }); |
| 171 | |
| 172 | test("moving between hosts counts as a change", () => { |
| 173 | expect(changed(undefined, "github.example.com")).toBe(true); |
| 174 | expect(changed("github.example.com", undefined)).toBe(true); |
| 175 | expect(changed("github.example.com", "other.example.com")).toBe(true); |
| 176 | }); |
| 177 | |
| 178 | test("making the ambient enterprise host explicit is not a change", () => { |
| 179 | // providers({ baseUrl: "github.example.com" }): prop undefined and prop |
| 180 | // "github.example.com" resolve to the same effective host. |
| 181 | expect(changed(undefined, "github.example.com", "github.example.com")).toBe(false); |
| 182 | expect(changed("github.example.com", undefined, "github.example.com")).toBe(false); |
| 183 | expect(changed(undefined, "https://github.example.com/api/v3", "github.example.com")).toBe( |
| 184 | false, |
| 185 | ); |
| 186 | }); |
| 187 | |
| 188 | test("explicit github.com under an ambient enterprise host IS a change", () => { |
| 189 | // prop undefined resolves to the enterprise host; prop "github.com" |
| 190 | // pins the resource back to github.com — different physical instance. |
| 191 | expect(changed(undefined, "github.com", "github.example.com")).toBe(true); |
| 192 | expect(changed("github.com", undefined, "github.example.com")).toBe(true); |
| 193 | }); |
| 194 | |
| 195 | test("undefined on both sides is never a change", () => { |
| 196 | expect(changed(undefined, undefined)).toBe(false); |
| 197 | expect(changed(undefined, undefined, "github.example.com")).toBe(false); |
| 198 | }); |
| 199 | }); |
| 200 | |
| 201 | describe("withBaseUrl", { tags: ["unit", "provider:github", "local"] }, () => { |
| 202 | const apiBaseUrlOf = (credsBaseUrl: string | undefined, resourceBaseUrl: string | undefined) => |
| 203 | Effect.runSync( |
| 204 | Effect.gen(function* () { |
| 205 | return (yield* yield* Credentials).apiBaseUrl; |
| 206 | }).pipe( |
| 207 | withBaseUrl(resourceBaseUrl), |
| 208 | Effect.provide( |
| 209 | fromToken( |
| 210 | "test-token", |
| 211 | credsBaseUrl !== undefined ? { baseUrl: credsBaseUrl } : undefined, |
| 212 | ), |
| 213 | ), |
| 214 | ), |
| 215 | ); |
| 216 | |
| 217 | test("falls back to the credentials' host when no override is given", () => { |
| 218 | expect(apiBaseUrlOf("github.example.com", undefined)).toBe("https://github.example.com/api/v3"); |
| 219 | }); |
| 220 | |
| 221 | test("a per-resource baseUrl overrides the credentials' host", () => { |
| 222 | expect(apiBaseUrlOf("github.example.com", "other.example.com")).toBe( |
| 223 | "https://other.example.com/api/v3", |
| 224 | ); |
| 225 | }); |
| 226 | |
| 227 | test("an explicit github.com override wins over an enterprise credential host", () => { |
| 228 | expect(apiBaseUrlOf("github.example.com", "github.com")).toBe("https://api.github.com"); |
| 229 | }); |
| 230 | |
| 231 | test("defaults to api.github.com", () => { |
| 232 | expect(apiBaseUrlOf(undefined, undefined)).toBe("https://api.github.com"); |
| 233 | }); |
| 234 | }); |