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, 04:45 UTC
| 1 | import { ServerSettings, SourceControlProviderKind } from "@t3tools/contracts"; |
| 2 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 3 | import { assert, it } from "@effect/vitest"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import * as Redacted from "effect/Redacted"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 11 | import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 12 | import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; |
| 13 | |
| 14 | import * as GitHubCredentials from "./GitHubCredentials.ts"; |
| 15 | import * as GitHubAppSignIn from "./GitHubAppSignIn.ts"; |
| 16 | import * as GitHubRest from "./GitHubRest.ts"; |
| 17 | import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts"; |
| 18 | |
| 19 | const unknownContext = (baseUrl: string) => ({ |
| 20 | provider: { kind: SourceControlProviderKind.make("unknown"), name: "Unknown", baseUrl }, |
| 21 | remoteName: "origin", |
| 22 | remoteUrl: "git@code.example.test:team/project.git", |
| 23 | }); |
| 24 | |
| 25 | // The default host dies on any command, so refinement never reaches `gh`. |
| 26 | const makeDiscovery = (credential: GitHubRest.GitHubRest["Service"]["credential"]) => |
| 27 | GitHubSourceControlProvider.makeDiscovery.pipe( |
| 28 | Effect.provide( |
| 29 | Layer.mergeAll( |
| 30 | Layer.mock(GitHubRest.GitHubRest)({ credential }), |
| 31 | Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ enabled: false }), |
| 32 | TestSourceControlHost.layer(), |
| 33 | FileSystem.layerNoop({}), |
| 34 | ), |
| 35 | ), |
| 36 | Effect.provideService(HostProcess.Environment, {}), |
| 37 | ); |
| 38 | |
| 39 | it.effect.each([ |
| 40 | { name: "a credential", credential: "found", claimed: true }, |
| 41 | { name: "a host turned off in Settings", credential: "disabled", claimed: true }, |
| 42 | { name: "no credential", credential: "missing", claimed: false }, |
| 43 | ] as const)("claims a custom-host remote with $name", (scenario) => |
| 44 | Effect.gen(function* () { |
| 45 | const hosts: Array<string> = []; |
| 46 | const discovery = yield* makeDiscovery((host) => { |
| 47 | hosts.push(host); |
| 48 | return scenario.credential === "found" |
| 49 | ? Effect.succeed({ token: Redacted.make("token"), fingerprint: `${host}:fingerprint` }) |
| 50 | : scenario.credential === "disabled" |
| 51 | ? Effect.fail(new GitHubCredentials.GitHubHostDisabledError({ host })) |
| 52 | : Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host })); |
| 53 | }); |
| 54 | if (discovery.type !== "managed-cli") return assert.fail("expected a managed CLI"); |
| 55 | const provider = yield* discovery.refineUnknownRemote({ |
| 56 | cwd: "/repo", |
| 57 | context: unknownContext("https://code.example.test"), |
| 58 | }); |
| 59 | assert.deepStrictEqual( |
| 60 | provider, |
| 61 | scenario.claimed |
| 62 | ? { |
| 63 | kind: SourceControlProviderKind.make("github"), |
| 64 | name: "GitHub Self-Hosted", |
| 65 | baseUrl: "https://code.example.test", |
| 66 | } |
| 67 | : null, |
| 68 | ); |
| 69 | assert.deepStrictEqual(hosts, ["code.example.test"]); |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | it.effect("asks for no credential when the identity resolver left the base URL empty", () => |
| 74 | Effect.gen(function* () { |
| 75 | const hosts: Array<string> = []; |
| 76 | const discovery = yield* makeDiscovery((host) => { |
| 77 | hosts.push(host); |
| 78 | return Effect.succeed({ token: Redacted.make("token"), fingerprint: `${host}:fingerprint` }); |
| 79 | }); |
| 80 | if (discovery.type !== "managed-cli") return assert.fail("expected a managed CLI"); |
| 81 | assert.strictEqual( |
| 82 | yield* discovery.refineUnknownRemote({ cwd: "/repo", context: unknownContext("") }), |
| 83 | null, |
| 84 | ); |
| 85 | assert.deepStrictEqual(hosts, []); |
| 86 | }), |
| 87 | ); |
| 88 | |
| 89 | it.effect.each([ |
| 90 | ["github-work", "git@github-work:team/project.git"], |
| 91 | ["github-ci", "git@github-ci:team/project.git"], |
| 92 | ["github-work", "ssh://git@github-work:22/team/project.git"], |
| 93 | ["github.com-work", "git@github.com-work:team/project.git"], |
| 94 | ] as const)("recognizes a GitHub SSH alias on %s (%s)", ([host, remoteUrl]) => |
| 95 | Effect.gen(function* () { |
| 96 | const discovery = yield* makeDiscovery((host) => |
| 97 | host === "github.com" |
| 98 | ? Effect.succeed({ token: Redacted.make("token"), fingerprint: `${host}:fingerprint` }) |
| 99 | : Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host })), |
| 100 | ); |
| 101 | if (discovery.type !== "managed-cli") return assert.fail("expected a managed CLI"); |
| 102 | const provider = yield* discovery.refineUnknownRemote({ |
| 103 | cwd: "/repo", |
| 104 | context: { ...unknownContext(`https://${host}`), remoteUrl }, |
| 105 | }); |
| 106 | assert.deepStrictEqual(provider, { |
| 107 | kind: SourceControlProviderKind.make("github"), |
| 108 | name: "GitHub", |
| 109 | baseUrl: "https://github.com", |
| 110 | }); |
| 111 | }), |
| 112 | ); |
| 113 | |
| 114 | const decodeSettings = Schema.decodeSync(ServerSettings); |
| 115 | const referencedTokenSettings = decodeSettings({ |
| 116 | sourceControlHosts: { |
| 117 | github: { tokens: { "github.com": { provider: "vault", reference: "op://v/gh" } } }, |
| 118 | }, |
| 119 | }); |
| 120 | |
| 121 | it.effect.each([ |
| 122 | { |
| 123 | name: "a readable", |
| 124 | token: "resolved-token", |
| 125 | status: "authenticated", |
| 126 | detail: "the token saved in Settings", |
| 127 | }, |
| 128 | { |
| 129 | name: "no", |
| 130 | token: "", |
| 131 | status: "authenticated", |
| 132 | detail: "GH_TOKEN from the server environment", |
| 133 | }, |
| 134 | // getWithSecrets leaves a reference its secret provider could not read in place. |
| 135 | { |
| 136 | name: "an unreadable", |
| 137 | token: { provider: "vault", reference: "op://v/gh" }, |
| 138 | status: "unauthenticated", |
| 139 | detail: 'secret provider "vault"', |
| 140 | }, |
| 141 | ])("reports $name saved GitHub token instead of another credential", (scenario) => |
| 142 | Effect.gen(function* () { |
| 143 | const paths: Array<string> = []; |
| 144 | const discovery = yield* GitHubSourceControlProvider.makeDiscovery.pipe( |
| 145 | Effect.provide( |
| 146 | Layer.mergeAll( |
| 147 | Layer.mock(GitHubRest.GitHubRest)({ |
| 148 | rest: (input) => { |
| 149 | paths.push(input.path); |
| 150 | return Effect.succeed({ |
| 151 | status: 200, |
| 152 | headers: {}, |
| 153 | body: JSON.stringify({ login: "octocat" }), |
| 154 | truncated: false, |
| 155 | invalidUtf8: false, |
| 156 | }); |
| 157 | }, |
| 158 | }), |
| 159 | Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ enabled: false }), |
| 160 | FileSystem.layerNoop({}), |
| 161 | Layer.effect( |
| 162 | SourceControlHost.SourceControlHost, |
| 163 | Effect.gen(function* () { |
| 164 | const host = yield* SourceControlHost.SourceControlHost; |
| 165 | return { |
| 166 | ...host, |
| 167 | settings: { |
| 168 | ...host.settings, |
| 169 | getWithSecrets: () => |
| 170 | Effect.succeed( |
| 171 | decodeSettings({ |
| 172 | sourceControlHosts: { |
| 173 | github: { tokens: { "github.com": scenario.token } }, |
| 174 | }, |
| 175 | }), |
| 176 | ), |
| 177 | }, |
| 178 | }; |
| 179 | }), |
| 180 | ).pipe( |
| 181 | Layer.provide( |
| 182 | TestSourceControlHost.layer({ |
| 183 | settings: referencedTokenSettings, |
| 184 | process: { |
| 185 | run: () => |
| 186 | Effect.succeed( |
| 187 | TestSourceControlHost.processOutput("", { |
| 188 | exitCode: ChildProcessSpawner.ExitCode(1), |
| 189 | }), |
| 190 | ), |
| 191 | }, |
| 192 | }), |
| 193 | ), |
| 194 | ), |
| 195 | ), |
| 196 | ), |
| 197 | Effect.provideService(HostProcess.Environment, { GH_TOKEN: "environment-token" }), |
| 198 | ); |
| 199 | if (discovery.type !== "managed-cli") return assert.fail("expected a managed CLI"); |
| 200 | const item = yield* discovery.probe("/repo"); |
| 201 | const authenticated = scenario.status === "authenticated"; |
| 202 | assert.deepStrictEqual(paths, authenticated ? ["user"] : []); |
| 203 | assert.strictEqual(item.auth.status, scenario.status); |
| 204 | assert.deepStrictEqual( |
| 205 | item.auth.account, |
| 206 | authenticated ? Option.some("octocat") : Option.none(), |
| 207 | ); |
| 208 | assert.include(Option.getOrThrow(item.auth.detail), scenario.detail); |
| 209 | }), |
| 210 | ); |