The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import { SecretProviderId, VcsProcessTimeoutError } from "@t3tools/contracts"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as Redacted from "effect/Redacted"; |
| 6 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 7 | import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; |
| 8 | import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 9 | |
| 10 | import * as GitCafeCredentials from "./GitCafeCredentials.ts"; |
| 11 | |
| 12 | const processOutput = TestSourceControlHost.processOutput; |
| 13 | |
| 14 | function harness( |
| 15 | run: SourceControlHost.SourceControlHost["Service"]["process"]["run"], |
| 16 | env: NodeJS.ProcessEnv = {}, |
| 17 | ) { |
| 18 | const calls: Array<SourceControlHost.SourceControlProcessInput> = []; |
| 19 | const layer = GitCafeCredentials.layer.pipe( |
| 20 | Layer.provide( |
| 21 | TestSourceControlHost.layer({ |
| 22 | process: { |
| 23 | run: (input) => { |
| 24 | calls.push(input); |
| 25 | return run(input); |
| 26 | }, |
| 27 | }, |
| 28 | }), |
| 29 | ), |
| 30 | Layer.provide(Layer.succeed(HostProcess.Environment, env)), |
| 31 | Layer.provide(Layer.succeed(HostProcess.WorkingDirectory, "/server")), |
| 32 | ); |
| 33 | return { layer, calls }; |
| 34 | } |
| 35 | |
| 36 | describe("GitCafeCredentials", () => { |
| 37 | it("hands CAFE_TOKEN only to the host cafe itself targets", () => { |
| 38 | const token = { CAFE_TOKEN: "env-token" }; |
| 39 | assert.strictEqual(GitCafeCredentials.environmentToken("git.cafe", token), "env-token"); |
| 40 | assert.isNull(GitCafeCredentials.environmentToken("staging.git.cafe", token)); |
| 41 | for (const CAFE_HOST of ["https://staging.git.cafe/api", "staging.git.cafe"]) { |
| 42 | const staging = { ...token, CAFE_HOST }; |
| 43 | assert.strictEqual( |
| 44 | GitCafeCredentials.environmentToken("staging.git.cafe", staging), |
| 45 | "env-token", |
| 46 | ); |
| 47 | assert.isNull(GitCafeCredentials.environmentToken("git.cafe", staging)); |
| 48 | } |
| 49 | assert.isNull(GitCafeCredentials.environmentToken("git.cafe", { CAFE_TOKEN: " " })); |
| 50 | }); |
| 51 | |
| 52 | it.effect("prefers CAFE_TOKEN without running cafe", () => { |
| 53 | const { layer, calls } = harness(() => Effect.die("cafe must not run"), { |
| 54 | CAFE_TOKEN: "env-token", |
| 55 | }); |
| 56 | return Effect.gen(function* () { |
| 57 | const credentials = yield* GitCafeCredentials.GitCafeCredentials; |
| 58 | const credential = yield* credentials.get("git.cafe"); |
| 59 | assert.strictEqual(Redacted.value(credential.token), "env-token"); |
| 60 | assert.strictEqual(credential.source, "env"); |
| 61 | assert.deepStrictEqual(calls, []); |
| 62 | }).pipe(Effect.provide(layer)); |
| 63 | }); |
| 64 | |
| 65 | it.effect("uses a token saved in Settings for git.cafe only, ahead of CAFE_TOKEN", () => { |
| 66 | const calls: Array<SourceControlHost.SourceControlProcessInput> = []; |
| 67 | const layer = GitCafeCredentials.layer.pipe( |
| 68 | Layer.provideMerge( |
| 69 | TestSourceControlHost.layer({ |
| 70 | process: { |
| 71 | run: (input) => { |
| 72 | calls.push(input); |
| 73 | return Effect.succeed(processOutput("password=gct_staging\n")); |
| 74 | }, |
| 75 | }, |
| 76 | }), |
| 77 | ), |
| 78 | Layer.provide(Layer.succeed(HostProcess.Environment, { CAFE_TOKEN: "env-token" })), |
| 79 | Layer.provide(Layer.succeed(HostProcess.WorkingDirectory, "/server")), |
| 80 | ); |
| 81 | return Effect.gen(function* () { |
| 82 | const credentials = yield* GitCafeCredentials.GitCafeCredentials; |
| 83 | const settings = yield* TestSourceControlHost.TestSourceControlHostSettings; |
| 84 | yield* settings.update({ sourceControlHosts: { gitcafe: { token: "saved-token" } } }); |
| 85 | |
| 86 | const saved = yield* credentials.get("git.cafe"); |
| 87 | assert.strictEqual(Redacted.value(saved.token), "saved-token"); |
| 88 | assert.strictEqual(saved.source, "settings"); |
| 89 | assert.deepStrictEqual(yield* credentials.cliEnv("git.cafe"), { CAFE_TOKEN: "saved-token" }); |
| 90 | assert.deepStrictEqual(yield* credentials.cliEnv("staging.git.cafe"), {}); |
| 91 | assert.strictEqual( |
| 92 | Redacted.value((yield* credentials.get("staging.git.cafe")).token), |
| 93 | "gct_staging", |
| 94 | ); |
| 95 | |
| 96 | yield* settings.update({ sourceControlHosts: { gitcafe: { token: "" } } }); |
| 97 | assert.strictEqual((yield* credentials.get("git.cafe")).source, "env"); |
| 98 | |
| 99 | // A reference left in place by getWithSecrets is one its secret provider could not read, |
| 100 | // here one naming a removed provider whose id is also an Object.prototype key. |
| 101 | yield* settings.update({ |
| 102 | sourceControlHosts: { |
| 103 | gitcafe: { |
| 104 | token: { provider: SecretProviderId.make("constructor"), reference: "cafe#token" }, |
| 105 | }, |
| 106 | }, |
| 107 | }); |
| 108 | const error = yield* Effect.flip(credentials.get("git.cafe")); |
| 109 | assert.strictEqual(error._tag, "SettingsSecretUnresolvedError"); |
| 110 | assert.include(error.message, '"constructor"'); |
| 111 | assert.strictEqual( |
| 112 | (yield* Effect.flip(credentials.cliEnv("git.cafe")))._tag, |
| 113 | "SettingsSecretUnresolvedError", |
| 114 | ); |
| 115 | assert.strictEqual(calls.length, 1); |
| 116 | }).pipe(Effect.provide(layer)); |
| 117 | }); |
| 118 | |
| 119 | it.effect("asks cafe's Git credential helper over stdin and reuses the token", () => { |
| 120 | const { layer, calls } = harness(() => |
| 121 | Effect.succeed( |
| 122 | processOutput("protocol=https\nhost=staging.git.cafe\nusername=alice\npassword=gct_1\n"), |
| 123 | ), |
| 124 | ); |
| 125 | return Effect.gen(function* () { |
| 126 | const credentials = yield* GitCafeCredentials.GitCafeCredentials; |
| 127 | const credential = yield* credentials.get("Staging.Git.Cafe"); |
| 128 | assert.strictEqual(Redacted.value(credential.token), "gct_1"); |
| 129 | assert.strictEqual(credential.source, "cafe"); |
| 130 | yield* credentials.get("staging.git.cafe"); |
| 131 | assert.strictEqual(calls.length, 1); |
| 132 | assert.strictEqual(calls[0]?.command, "cafe"); |
| 133 | assert.deepStrictEqual(calls[0]?.args, [ |
| 134 | "auth", |
| 135 | "http", |
| 136 | "credential", |
| 137 | "--host", |
| 138 | "https://staging.git.cafe/api", |
| 139 | "get", |
| 140 | ]); |
| 141 | assert.strictEqual(calls[0]?.stdin, "protocol=https\nhost=staging.git.cafe\n\n"); |
| 142 | yield* credentials.invalidate("staging.git.cafe"); |
| 143 | yield* credentials.get("staging.git.cafe"); |
| 144 | assert.strictEqual(calls.length, 2); |
| 145 | }).pipe(Effect.provide(layer)); |
| 146 | }); |
| 147 | |
| 148 | it.effect("reports a cafe timeout as a timeout and does not cache it", () => { |
| 149 | const { layer, calls } = harness((input) => |
| 150 | calls.length === 1 |
| 151 | ? Effect.fail( |
| 152 | new VcsProcessTimeoutError({ |
| 153 | operation: input.operation, |
| 154 | command: input.command, |
| 155 | cwd: input.cwd, |
| 156 | timeoutMs: 10_000, |
| 157 | }), |
| 158 | ) |
| 159 | : Effect.succeed(processOutput("password=gct_1\n")), |
| 160 | ); |
| 161 | return Effect.gen(function* () { |
| 162 | const credentials = yield* GitCafeCredentials.GitCafeCredentials; |
| 163 | const error = yield* Effect.flip(credentials.get("git.cafe")); |
| 164 | assert.strictEqual(error._tag, "GitCafeCliTimeoutError"); |
| 165 | assert.notInclude(error.message, "cafe auth status"); |
| 166 | assert.strictEqual((yield* credentials.get("git.cafe")).source, "cafe"); |
| 167 | assert.strictEqual(calls.length, 2); |
| 168 | }).pipe(Effect.provide(layer)); |
| 169 | }); |
| 170 | }); |