The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { stateStoreScope } from "@alchemy.run/cloudflare/Alchemist/StateStore"; |
| 2 | import { expect, it } from "@alchemy.run/test"; |
| 3 | import * as Alchemist from "alchemy/Alchemist"; |
| 4 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 5 | import { ArtifactStore, createArtifactStore } from "alchemy/Artifacts"; |
| 6 | import { CredentialsStoreLive } from "alchemy/Auth/Credentials"; |
| 7 | import { ProfileStore, ProfileStoreLive } from "alchemy/Auth/Profile"; |
| 8 | import { CredentialsUnavailable } from "alchemy/Auth/Resolve"; |
| 9 | import * as Contributions from "alchemy/Contributions"; |
| 10 | import * as Interaction from "alchemy/Interaction"; |
| 11 | import { PlatformServices } from "alchemy/Util/PlatformServices"; |
| 12 | import * as Cause from "effect/Cause"; |
| 13 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 14 | import * as Effect from "effect/Effect"; |
| 15 | import * as Exit from "effect/Exit"; |
| 16 | import * as FileSystem from "effect/FileSystem"; |
| 17 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 18 | import * as Layer from "effect/Layer"; |
| 19 | import * as Path from "effect/Path"; |
| 20 | |
| 21 | const STAGING_ACCOUNT = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 22 | const DEFAULT_ACCOUNT = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; |
| 23 | |
| 24 | const CLOUDFLARE_ENV_KEYS = [ |
| 25 | "CI", |
| 26 | "ALCHEMY_PROFILE", |
| 27 | "CLOUDFLARE_ACCOUNT_ID", |
| 28 | "CLOUDFLARE_API_TOKEN", |
| 29 | "CLOUDFLARE_API_KEY", |
| 30 | "CLOUDFLARE_EMAIL", |
| 31 | "CLOUDFLARE_ACCOUNT_EMAIL", |
| 32 | ] as const; |
| 33 | |
| 34 | /** |
| 35 | * Isolate `ALCHEMY_HOME` and strip env credentials / `CI` so |
| 36 | * `resolveProviderConfig` actually reads the profile store. The alchemy-test |
| 37 | * runner sets `CI=true` and `--profile testing` exports `ALCHEMY_PROFILE`, |
| 38 | * both of which would otherwise skip the named-profile path under test. |
| 39 | * |
| 40 | * An empty `--env-file` in the temp home is passed through so |
| 41 | * `loadConfigProvider` never falls back to the checkout's cwd `.env`. |
| 42 | */ |
| 43 | const withIsolatedHome = <A, E, R>(effect: (envFile: string) => Effect.Effect<A, E, R>) => |
| 44 | Effect.gen(function* () { |
| 45 | const fs = yield* FileSystem.FileSystem; |
| 46 | const path = yield* Path.Path; |
| 47 | const dir = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-cf-bootstrap-profile-" }); |
| 48 | const envFile = path.join(dir, "empty.env"); |
| 49 | yield* fs.writeFileString(envFile, ""); |
| 50 | const previous: Record<string, string | undefined> = { ALCHEMY_HOME: process.env.ALCHEMY_HOME }; |
| 51 | for (const key of CLOUDFLARE_ENV_KEYS) { |
| 52 | previous[key] = process.env[key]; |
| 53 | } |
| 54 | yield* Effect.acquireRelease( |
| 55 | Effect.sync(() => { |
| 56 | process.env.ALCHEMY_HOME = dir; |
| 57 | for (const key of CLOUDFLARE_ENV_KEYS) { |
| 58 | delete process.env[key]; |
| 59 | } |
| 60 | }), |
| 61 | () => |
| 62 | Effect.sync(() => { |
| 63 | for (const [key, value] of Object.entries(previous)) { |
| 64 | if (value === undefined) delete process.env[key]; |
| 65 | else process.env[key] = value; |
| 66 | } |
| 67 | }), |
| 68 | ); |
| 69 | return yield* effect(envFile).pipe( |
| 70 | Effect.provide( |
| 71 | Layer.mergeAll( |
| 72 | Layer.provide(ProfileStoreLive, PlatformServices), |
| 73 | Layer.provide(CredentialsStoreLive, PlatformServices), |
| 74 | Layer.succeed(AlchemyContext, { dotAlchemy: dir, dev: false, adopt: false }), |
| 75 | Interaction.layerNonInteractive(), |
| 76 | ConfigProvider.layer(ConfigProvider.fromUnknown({})), |
| 77 | FetchHttpClient.layer, |
| 78 | Layer.succeed(ArtifactStore, createArtifactStore()), |
| 79 | ), |
| 80 | ), |
| 81 | ); |
| 82 | }).pipe(Effect.scoped, Effect.provide(PlatformServices)); |
| 83 | |
| 84 | /** |
| 85 | * The Cloudflare store's scope as `alchemy state … --backend cloudflare` |
| 86 | * resolves it: under the context core builds from `--profile` and |
| 87 | * `--env-file`, with the provider's auth registered. |
| 88 | */ |
| 89 | const resolveStateStoreScope = (target: { readonly profile?: string; readonly envFile: string }) => |
| 90 | Effect.gen(function* () { |
| 91 | const cloudflare = (yield* Contributions.load()).find( |
| 92 | (provider) => provider.name === "cloudflare", |
| 93 | ); |
| 94 | if (cloudflare === undefined) return yield* Effect.die(new Error("no cloudflare provider")); |
| 95 | return yield* stateStoreScope(undefined).pipe( |
| 96 | Effect.provide(yield* Alchemist.State.providerContext(cloudflare, target)), |
| 97 | ); |
| 98 | }); |
| 99 | |
| 100 | const storedToken = (accountId: string, apiToken: string) => ({ |
| 101 | method: "stored" as const, |
| 102 | credentialType: "apiToken" as const, |
| 103 | apiToken, |
| 104 | accountId, |
| 105 | }); |
| 106 | |
| 107 | it.live( |
| 108 | "cloudflare bootstrap --profile resolves that profile's credentials, not default (#252)", |
| 109 | () => |
| 110 | withIsolatedHome((envFile) => |
| 111 | Effect.gen(function* () { |
| 112 | const profiles = yield* ProfileStore; |
| 113 | yield* profiles.createProfile("staging"); |
| 114 | yield* profiles.setProviderConfig( |
| 115 | "staging", |
| 116 | "Cloudflare", |
| 117 | storedToken(STAGING_ACCOUNT, "staging-token"), |
| 118 | ); |
| 119 | yield* profiles.setProviderConfig( |
| 120 | "default", |
| 121 | "Cloudflare", |
| 122 | storedToken(DEFAULT_ACCOUNT, "default-token"), |
| 123 | ); |
| 124 | |
| 125 | const scoped = yield* resolveStateStoreScope({ profile: "staging", envFile }); |
| 126 | expect(scoped.profile).toBe("staging"); |
| 127 | expect(scoped.accountId).toBe(STAGING_ACCOUNT); |
| 128 | }), |
| 129 | ), |
| 130 | { |
| 131 | tags: ["unit", "provider:cloudflare", "provider:cloudflare:statestore", "local"], |
| 132 | exclusive: true, |
| 133 | }, |
| 134 | ); |
| 135 | |
| 136 | it.live( |
| 137 | "cloudflare bootstrap without --profile uses the default profile", |
| 138 | () => |
| 139 | withIsolatedHome((envFile) => |
| 140 | Effect.gen(function* () { |
| 141 | const profiles = yield* ProfileStore; |
| 142 | yield* profiles.setProviderConfig( |
| 143 | "default", |
| 144 | "Cloudflare", |
| 145 | storedToken(DEFAULT_ACCOUNT, "default-token"), |
| 146 | ); |
| 147 | |
| 148 | const scoped = yield* resolveStateStoreScope({ envFile }); |
| 149 | expect(scoped.profile).toBe("default"); |
| 150 | expect(scoped.accountId).toBe(DEFAULT_ACCOUNT); |
| 151 | }), |
| 152 | ), |
| 153 | { |
| 154 | tags: ["unit", "provider:cloudflare", "provider:cloudflare:statestore", "local"], |
| 155 | exclusive: true, |
| 156 | }, |
| 157 | ); |
| 158 | |
| 159 | it.live( |
| 160 | "cloudflare bootstrap --profile does not fall back to an unconfigured default", |
| 161 | () => |
| 162 | withIsolatedHome((envFile) => |
| 163 | Effect.gen(function* () { |
| 164 | const profiles = yield* ProfileStore; |
| 165 | yield* profiles.createProfile("staging"); |
| 166 | yield* profiles.setProviderConfig( |
| 167 | "staging", |
| 168 | "Cloudflare", |
| 169 | storedToken(STAGING_ACCOUNT, "staging-token"), |
| 170 | ); |
| 171 | |
| 172 | const scoped = yield* resolveStateStoreScope({ profile: "staging", envFile }); |
| 173 | expect(scoped.accountId).toBe(STAGING_ACCOUNT); |
| 174 | |
| 175 | // Environment resolution is lazy: missing auth surfaces when the |
| 176 | // account is demanded, through the environment's defect channel. |
| 177 | const missing = yield* Effect.exit(resolveStateStoreScope({ envFile })); |
| 178 | expect(Exit.isFailure(missing)).toBe(true); |
| 179 | if (Exit.isFailure(missing)) { |
| 180 | const error = Cause.squash(missing.cause); |
| 181 | expect(error).toBeInstanceOf(CredentialsUnavailable); |
| 182 | expect(String(error)).toContain("profile 'default'"); |
| 183 | } |
| 184 | }), |
| 185 | ), |
| 186 | { |
| 187 | tags: ["unit", "provider:cloudflare", "provider:cloudflare:statestore", "local"], |
| 188 | exclusive: true, |
| 189 | }, |
| 190 | ); |