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 crypto from "node:crypto"; |
| 2 | import { fileURLToPath } from "node:url"; |
| 3 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 4 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 5 | import { STATE_STORE_VERSION } from "@alchemy.run/cloudflare/StateStore/Api"; |
| 6 | import { |
| 7 | bootstrap, |
| 8 | loginWithCloudflare, |
| 9 | teardownStateStore, |
| 10 | } from "@alchemy.run/cloudflare/StateStore/State"; |
| 11 | import { |
| 12 | AuthTokenSecretName, |
| 13 | EncryptionKeySecretName, |
| 14 | stateStoreSecretName, |
| 15 | } from "@alchemy.run/cloudflare/StateStore/Token"; |
| 16 | import { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup"; |
| 17 | import { expect } from "@alchemy.run/test"; |
| 18 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 19 | import * as secretsStore from "@distilled.cloud/cloudflare/secrets-store"; |
| 20 | import * as workers from "@distilled.cloud/cloudflare/workers"; |
| 21 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 22 | import * as Alchemist from "alchemy/Alchemist"; |
| 23 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 24 | import { currentProfileName } from "alchemy/Auth/Profile"; |
| 25 | import { checkHttpStateStoreAuth, makeHttpStateStore } from "alchemy/State/HttpStateStore"; |
| 26 | import { makeLocalState } from "alchemy/State/LocalState"; |
| 27 | import { State } from "alchemy/State/State"; |
| 28 | import { exec } from "alchemy/Util/exec"; |
| 29 | import * as Cause from "effect/Cause"; |
| 30 | import * as Clock from "effect/Clock"; |
| 31 | import * as Effect from "effect/Effect"; |
| 32 | import * as Exit from "effect/Exit"; |
| 33 | import * as FileSystem from "effect/FileSystem"; |
| 34 | import * as HttpClient from "effect/http/HttpClient"; |
| 35 | import * as Layer from "effect/Layer"; |
| 36 | import * as Option from "effect/Option"; |
| 37 | import * as Path from "effect/Path"; |
| 38 | import { ChildProcess } from "effect/process"; |
| 39 | import * as Redacted from "effect/Redacted"; |
| 40 | import * as Schedule from "effect/Schedule"; |
| 41 | import * as Stream from "effect/Stream"; |
| 42 | import { testName } from "../../names.ts"; |
| 43 | import { workerName as configuredWorkerName } from "./fixtures/configured-store.ts"; |
| 44 | |
| 45 | /** |
| 46 | * Every way a Cloudflare State Store deploy can start, and what it must keep. |
| 47 | * Whatever the starting point, a deploy never changes a secret that exists: |
| 48 | * replacing the auth token or the encryption key loses every stack's state. |
| 49 | * Each case checks the secrets' ids and modification times out of band. |
| 50 | */ |
| 51 | |
| 52 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 53 | |
| 54 | const tags = ["provider:cloudflare", "provider:cloudflare:statestore", "live"]; |
| 55 | const stage = Test.defaultStage().replaceAll("_", "-"); |
| 56 | /** A store Worker name, unique per stage and short enough for previews (54). */ |
| 57 | const storeWorker = (name: string) => testName(`alchemy-state-store-${name}`); |
| 58 | /** A store name for the old release's `--name`, which allows 20 characters: unique per stage. */ |
| 59 | const storeName = (prefix: string) => |
| 60 | `${prefix}-${crypto.createHash("sha256").update(stage).digest("hex").slice(0, 8)}`; |
| 61 | |
| 62 | const ZONE = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 63 | |
| 64 | const record = (fqn: string) => ({ |
| 65 | kind: "resource" as const, |
| 66 | resourceType: "Test.Resource", |
| 67 | namespace: undefined, |
| 68 | fqn, |
| 69 | logicalId: fqn, |
| 70 | instanceId: "kept", |
| 71 | providerVersion: 1, |
| 72 | status: "created" as const, |
| 73 | downstream: [], |
| 74 | bindings: [], |
| 75 | props: { hello: "world" }, |
| 76 | attr: { id: "kept" }, |
| 77 | }); |
| 78 | |
| 79 | /** Open a store the way a stack does, building its layer anew. */ |
| 80 | const open = (options: Cloudflare.CloudflareStateOptions) => |
| 81 | State.pipe(Effect.flatten, Effect.provide(Layer.fresh(Cloudflare.state(options)))); |
| 82 | |
| 83 | /** A store's secrets by name: every match, so a duplicate shows up too. */ |
| 84 | const observeSecrets = (accountId: string, workerName: string) => |
| 85 | Effect.gen(function* () { |
| 86 | const store = yield* secretsStore.listStores.items({ accountId }).pipe(Stream.runHead); |
| 87 | if (Option.isNone(store)) return { token: [], key: [] }; |
| 88 | const secrets = Array.from( |
| 89 | yield* secretsStore.listStoreSecrets |
| 90 | .items({ accountId, storeId: store.value.id }) |
| 91 | .pipe(Stream.runCollect), |
| 92 | ); |
| 93 | const named = (base: string) => |
| 94 | secrets |
| 95 | .filter((secret) => secret.name === stateStoreSecretName(base, workerName)) |
| 96 | .map((secret) => ({ id: secret.id, modified: secret.modified })); |
| 97 | return { token: named(AuthTokenSecretName), key: named(EncryptionKeySecretName) }; |
| 98 | }); |
| 99 | |
| 100 | const deleteSecret = (accountId: string, workerName: string, base: string) => |
| 101 | Effect.gen(function* () { |
| 102 | const store = yield* secretsStore.listStores.items({ accountId }).pipe(Stream.runHead); |
| 103 | if (Option.isNone(store)) return; |
| 104 | const secret = yield* secretsStore.listStoreSecrets |
| 105 | .items({ accountId, storeId: store.value.id }) |
| 106 | .pipe( |
| 107 | Stream.filter((secret) => secret.name === stateStoreSecretName(base, workerName)), |
| 108 | Stream.runHead, |
| 109 | ); |
| 110 | if (Option.isSome(secret)) { |
| 111 | yield* secretsStore.deleteStoreSecret({ |
| 112 | accountId, |
| 113 | storeId: store.value.id, |
| 114 | secretId: secret.value.id, |
| 115 | }); |
| 116 | } |
| 117 | }); |
| 118 | |
| 119 | /** When the Worker's script was last uploaded, or `undefined` without one. */ |
| 120 | const scriptModifiedOn = (accountId: string, workerName: string) => |
| 121 | workers.listScripts.items({ accountId }).pipe( |
| 122 | Stream.filter((script) => script.id === workerName), |
| 123 | Stream.runHead, |
| 124 | Effect.map((script) => (Option.isSome(script) ? script.value.modifiedOn : undefined)), |
| 125 | ); |
| 126 | |
| 127 | const scriptExists = (accountId: string, workerName: string) => |
| 128 | Effect.map(scriptModifiedOn(accountId, workerName), (modified) => modified !== undefined); |
| 129 | |
| 130 | /** The version a store's Worker reports. */ |
| 131 | const servedVersion = (url: string) => |
| 132 | Effect.gen(function* () { |
| 133 | const response = yield* HttpClient.get(`${url}/version`); |
| 134 | return ((yield* response.json) as { version: number }).version; |
| 135 | }); |
| 136 | |
| 137 | /** The version a store's Worker reports once the edge serves `version`. */ |
| 138 | const eventuallyServes = (url: string, version: number) => |
| 139 | servedVersion(url).pipe( |
| 140 | Effect.repeat({ |
| 141 | until: (served) => served === version, |
| 142 | schedule: Schedule.spaced("1 second"), |
| 143 | times: 30, |
| 144 | }), |
| 145 | ); |
| 146 | |
| 147 | const attachedDomains = (accountId: string, hostname: string) => |
| 148 | workers |
| 149 | .listDomains({ accountId, hostname }) |
| 150 | .pipe( |
| 151 | Effect.map(({ result }) => |
| 152 | (result ?? []).filter((d) => d.hostname === hostname).map((d) => d.service), |
| 153 | ), |
| 154 | ); |
| 155 | |
| 156 | const expectFailure = <A, E>(exit: Exit.Exit<A, E>, message: string) => { |
| 157 | expect(Exit.isFailure(exit)).toBe(true); |
| 158 | if (Exit.isFailure(exit)) expect(Cause.pretty(exit.cause)).toContain(message); |
| 159 | }; |
| 160 | |
| 161 | test.provider( |
| 162 | "a store named by workerName deploys once, then adopts and redeploys without touching its secrets", |
| 163 | () => |
| 164 | Effect.gen(function* () { |
| 165 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 166 | const workerName = storeWorker("deploy"); |
| 167 | yield* teardownStateStore({ workerName }); |
| 168 | |
| 169 | // First use deploys the Worker under exactly `workerName`, with one |
| 170 | // token and one key named after it. |
| 171 | const state = yield* open({ workerName }); |
| 172 | yield* state.set({ stack: "deploy", stage: "test", fqn: "a", value: record("a") }); |
| 173 | const secrets = yield* observeSecrets(accountId, workerName); |
| 174 | expect(secrets.token).toHaveLength(1); |
| 175 | expect(secrets.key).toHaveLength(1); |
| 176 | const suffix = workerName.replaceAll("-", "_"); |
| 177 | expect(stateStoreSecretName(AuthTokenSecretName, workerName)).toBe( |
| 178 | `AlchemyStateStoreToken_${suffix}`, |
| 179 | ); |
| 180 | expect(stateStoreSecretName(EncryptionKeySecretName, workerName)).toBe( |
| 181 | `AlchemyStateStoreEncryptionKey_${suffix}`, |
| 182 | ); |
| 183 | const uploaded = yield* scriptModifiedOn(accountId, workerName); |
| 184 | expect(uploaded).toBeDefined(); |
| 185 | |
| 186 | // Opening it again adopts it as it is. |
| 187 | const reopened = yield* open({ workerName }); |
| 188 | expect(yield* reopened.get({ stack: "deploy", stage: "test", fqn: "a" })).toEqual( |
| 189 | record("a"), |
| 190 | ); |
| 191 | expect(yield* scriptModifiedOn(accountId, workerName)).toEqual(uploaded); |
| 192 | |
| 193 | // So does a bootstrap, which refreshes the credentials. |
| 194 | const adopted = yield* bootstrap({ workerName }); |
| 195 | expect(yield* adopted.get({ stack: "deploy", stage: "test", fqn: "a" })).toEqual(record("a")); |
| 196 | expect(yield* scriptModifiedOn(accountId, workerName)).toEqual(uploaded); |
| 197 | |
| 198 | // A forced redeploy uploads the Worker again over the same secrets. |
| 199 | const redeployed = yield* bootstrap({ workerName, force: true }); |
| 200 | expect(yield* scriptModifiedOn(accountId, workerName)).not.toEqual(uploaded); |
| 201 | expect(yield* redeployed.get({ stack: "deploy", stage: "test", fqn: "a" })).toEqual( |
| 202 | record("a"), |
| 203 | ); |
| 204 | expect(yield* observeSecrets(accountId, workerName)).toEqual(secrets); |
| 205 | |
| 206 | yield* teardownStateStore({ workerName }); |
| 207 | expect(yield* scriptExists(accountId, workerName)).toBe(false); |
| 208 | expect(yield* observeSecrets(accountId, workerName)).toEqual({ token: [], key: [] }); |
| 209 | }), |
| 210 | { tags, timeout: 300_000 }, |
| 211 | ); |
| 212 | |
| 213 | test.provider( |
| 214 | "a Worker name workers.dev can't serve fails before anything is created", |
| 215 | () => |
| 216 | Effect.gen(function* () { |
| 217 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 218 | // The store enables workers.dev previews, and Cloudflare rejects a |
| 219 | // script name over 54 characters with previews enabled. |
| 220 | const workerName = `${storeWorker("too-long")}-`.padEnd(55, "x"); |
| 221 | const message = `Invalid Cloudflare State Store Worker name '${workerName}'`; |
| 222 | expectFailure(yield* Effect.exit(open({ workerName })), message); |
| 223 | expectFailure(yield* Effect.exit(bootstrap({ workerName })), message); |
| 224 | expect(yield* scriptExists(accountId, workerName)).toBe(false); |
| 225 | expect(yield* observeSecrets(accountId, workerName)).toEqual({ token: [], key: [] }); |
| 226 | }), |
| 227 | { tags, timeout: 60_000 }, |
| 228 | ); |
| 229 | |
| 230 | test.provider( |
| 231 | "a store a newer Alchemy deployed is left untouched", |
| 232 | (stack) => |
| 233 | Effect.gen(function* () { |
| 234 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 235 | const workerName = storeWorker("newer"); |
| 236 | yield* stack.destroy(); |
| 237 | yield* teardownStateStore({ workerName }); |
| 238 | |
| 239 | // Stands in for a store from a future release: it serves a later version. |
| 240 | yield* stack.deploy( |
| 241 | Cloudflare.Worker("NewerStore", { |
| 242 | name: workerName, |
| 243 | script: `export default { fetch() { return Response.json({ version: ${STATE_STORE_VERSION + 1} }); } };`, |
| 244 | workersDev: true, |
| 245 | }), |
| 246 | ); |
| 247 | const uploaded = yield* scriptModifiedOn(accountId, workerName); |
| 248 | |
| 249 | const message = `runs v${STATE_STORE_VERSION + 1}, newer than this Alchemy's v${STATE_STORE_VERSION}`; |
| 250 | expectFailure(yield* Effect.exit(open({ workerName })), message); |
| 251 | expectFailure(yield* Effect.exit(bootstrap({ workerName })), message); |
| 252 | expectFailure(yield* Effect.exit(bootstrap({ workerName, force: true })), message); |
| 253 | expect(yield* scriptModifiedOn(accountId, workerName)).toEqual(uploaded); |
| 254 | expect(yield* observeSecrets(accountId, workerName)).toEqual({ token: [], key: [] }); |
| 255 | |
| 256 | yield* stack.destroy(); |
| 257 | }), |
| 258 | { tags, timeout: 300_000 }, |
| 259 | ); |
| 260 | |
| 261 | test.provider( |
| 262 | "a store whose Worker is gone is redeployed onto its existing secrets", |
| 263 | () => |
| 264 | Effect.gen(function* () { |
| 265 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 266 | const workerName = storeWorker("orphaned"); |
| 267 | yield* teardownStateStore({ workerName }); |
| 268 | |
| 269 | yield* open({ workerName }); |
| 270 | const secrets = yield* observeSecrets(accountId, workerName); |
| 271 | const profile = yield* currentProfileName; |
| 272 | const credentials = yield* loginWithCloudflare(profile, true, workerName, accountId); |
| 273 | |
| 274 | // The Worker is deleted, as after an interrupted teardown, and bootstrap |
| 275 | // runs at once, while the edge can still answer for the deleted Worker. |
| 276 | // Each round must redeploy rather than adopt a store that isn't there. |
| 277 | for (const round of [1, 2, 3]) { |
| 278 | yield* workers.deleteScript({ accountId, scriptName: workerName, force: true }); |
| 279 | const redeployed = yield* bootstrap({ workerName }); |
| 280 | expect(yield* scriptExists(accountId, workerName)).toBe(true); |
| 281 | yield* redeployed.set({ |
| 282 | stack: "orphaned", |
| 283 | stage: "test", |
| 284 | fqn: `round-${round}`, |
| 285 | value: record(`round-${round}`), |
| 286 | }); |
| 287 | expect(yield* observeSecrets(accountId, workerName)).toEqual(secrets); |
| 288 | // The token from before still authenticates. |
| 289 | expect(yield* checkHttpStateStoreAuth(credentials)).toBe(true); |
| 290 | } |
| 291 | |
| 292 | yield* teardownStateStore({ workerName }); |
| 293 | }), |
| 294 | { tags, timeout: 400_000 }, |
| 295 | ); |
| 296 | |
| 297 | test.provider( |
| 298 | "a store missing its auth token gets a new one and keeps its encryption key and state", |
| 299 | () => |
| 300 | Effect.gen(function* () { |
| 301 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 302 | const workerName = storeWorker("no-token"); |
| 303 | yield* teardownStateStore({ workerName }); |
| 304 | |
| 305 | const state = yield* open({ workerName }); |
| 306 | yield* state.set({ stack: "no-token", stage: "test", fqn: "a", value: record("a") }); |
| 307 | const { key } = yield* observeSecrets(accountId, workerName); |
| 308 | |
| 309 | yield* deleteSecret(accountId, workerName, AuthTokenSecretName); |
| 310 | |
| 311 | const healed = yield* bootstrap({ workerName }); |
| 312 | const secrets = yield* observeSecrets(accountId, workerName); |
| 313 | expect(secrets.token).toHaveLength(1); |
| 314 | expect(secrets.key).toEqual(key); |
| 315 | expect(yield* healed.get({ stack: "no-token", stage: "test", fqn: "a" })).toEqual( |
| 316 | record("a"), |
| 317 | ); |
| 318 | |
| 319 | yield* teardownStateStore({ workerName }); |
| 320 | }), |
| 321 | { tags, timeout: 300_000 }, |
| 322 | ); |
| 323 | |
| 324 | test.provider( |
| 325 | "a store that lost its encryption key gets a new one only when approved", |
| 326 | () => |
| 327 | Effect.gen(function* () { |
| 328 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 329 | const workerName = storeWorker("no-key"); |
| 330 | yield* teardownStateStore({ workerName }); |
| 331 | |
| 332 | const state = yield* open({ workerName }); |
| 333 | yield* state.set({ stack: "no-key", stage: "test", fqn: "a", value: record("a") }); |
| 334 | const { token } = yield* observeSecrets(accountId, workerName); |
| 335 | |
| 336 | yield* deleteSecret(accountId, workerName, EncryptionKeySecretName); |
| 337 | const uploaded = yield* scriptModifiedOn(accountId, workerName); |
| 338 | |
| 339 | // The store held state under the lost key: with nobody to confirm, |
| 340 | // nothing changes. |
| 341 | expectFailure( |
| 342 | yield* Effect.exit(bootstrap({ workerName })), |
| 343 | `Cloudflare State Store '${workerName}' has lost its encryption key`, |
| 344 | ); |
| 345 | expect(yield* observeSecrets(accountId, workerName)).toEqual({ token, key: [] }); |
| 346 | expect(yield* scriptModifiedOn(accountId, workerName)).toEqual(uploaded); |
| 347 | |
| 348 | // `force` approves a new key; the token is kept. |
| 349 | const restarted = yield* bootstrap({ workerName, force: true }); |
| 350 | const secrets = yield* observeSecrets(accountId, workerName); |
| 351 | expect(secrets.key).toHaveLength(1); |
| 352 | expect(secrets.token).toEqual(token); |
| 353 | yield* restarted.set({ stack: "no-key", stage: "test", fqn: "b", value: record("b") }); |
| 354 | expect(yield* restarted.get({ stack: "no-key", stage: "test", fqn: "b" })).toEqual( |
| 355 | record("b"), |
| 356 | ); |
| 357 | |
| 358 | yield* teardownStateStore({ workerName }); |
| 359 | }), |
| 360 | { tags, timeout: 300_000 }, |
| 361 | ); |
| 362 | |
| 363 | test.provider( |
| 364 | "concurrent first deploys of one store create a single pair of secrets", |
| 365 | () => |
| 366 | Effect.gen(function* () { |
| 367 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 368 | const workerName = storeWorker("concurrent"); |
| 369 | yield* teardownStateStore({ workerName }); |
| 370 | |
| 371 | const [first, second] = yield* Effect.all( |
| 372 | [bootstrap({ workerName }), bootstrap({ workerName })], |
| 373 | { concurrency: 2 }, |
| 374 | ); |
| 375 | const secrets = yield* observeSecrets(accountId, workerName); |
| 376 | expect(secrets.token).toHaveLength(1); |
| 377 | expect(secrets.key).toHaveLength(1); |
| 378 | |
| 379 | // Both hold working credentials for the same store. |
| 380 | yield* first.set({ stack: "concurrent", stage: "test", fqn: "a", value: record("a") }); |
| 381 | expect(yield* second.get({ stack: "concurrent", stage: "test", fqn: "a" })).toEqual( |
| 382 | record("a"), |
| 383 | ); |
| 384 | |
| 385 | yield* teardownStateStore({ workerName }); |
| 386 | }), |
| 387 | { tags, timeout: 300_000 }, |
| 388 | ); |
| 389 | |
| 390 | test.provider( |
| 391 | "a store whose name differs from another's only by '-' and '_' is refused, and tearing it down keeps the other's secrets", |
| 392 | () => |
| 393 | Effect.gen(function* () { |
| 394 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 395 | const workerName = storeWorker("shared"); |
| 396 | const lookalike = workerName.replaceAll("-", "_"); |
| 397 | yield* teardownStateStore({ workerName: lookalike }); |
| 398 | yield* teardownStateStore({ workerName }); |
| 399 | |
| 400 | const state = yield* open({ workerName }); |
| 401 | yield* state.set({ stack: "shared", stage: "test", fqn: "a", value: record("a") }); |
| 402 | const secrets = yield* observeSecrets(accountId, workerName); |
| 403 | |
| 404 | const refused = yield* Effect.exit(bootstrap({ workerName: lookalike })); |
| 405 | expectFailure(refused, `its secrets would be shared with Worker '${workerName}'`); |
| 406 | expect(yield* scriptExists(accountId, lookalike)).toBe(false); |
| 407 | |
| 408 | yield* teardownStateStore({ workerName: lookalike }); |
| 409 | expect(yield* observeSecrets(accountId, workerName)).toEqual(secrets); |
| 410 | const kept = yield* bootstrap({ workerName }); |
| 411 | expect(yield* kept.get({ stack: "shared", stage: "test", fqn: "a" })).toEqual(record("a")); |
| 412 | |
| 413 | yield* teardownStateStore({ workerName }); |
| 414 | expect(yield* observeSecrets(accountId, workerName)).toEqual({ token: [], key: [] }); |
| 415 | }), |
| 416 | { tags, timeout: 300_000 }, |
| 417 | ); |
| 418 | |
| 419 | test.provider( |
| 420 | "a domain another Worker owns fails before changing anything, and a free one attaches to the existing store", |
| 421 | (stack) => |
| 422 | Effect.gen(function* () { |
| 423 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 424 | const workerName = storeWorker("domain"); |
| 425 | const domain = `state-taken-${stage}.${ZONE}`; |
| 426 | yield* stack.destroy(); |
| 427 | yield* teardownStateStore({ workerName }); |
| 428 | |
| 429 | const state = yield* open({ workerName }); |
| 430 | yield* state.set({ stack: "domain", stage: "test", fqn: "a", value: record("a") }); |
| 431 | const secrets = yield* observeSecrets(accountId, workerName); |
| 432 | const uploaded = yield* scriptModifiedOn(accountId, workerName); |
| 433 | |
| 434 | const holder = yield* stack.deploy( |
| 435 | Cloudflare.Worker("DomainHolder", { |
| 436 | script: `export default { fetch() { return new Response("holder"); } };`, |
| 437 | domain, |
| 438 | }), |
| 439 | ); |
| 440 | const taken = yield* Effect.exit(bootstrap({ workerName, domain })); |
| 441 | expectFailure(taken, `already attached to Worker '${holder.workerName}'`); |
| 442 | expect(yield* attachedDomains(accountId, domain)).toEqual([holder.workerName]); |
| 443 | expect(yield* scriptModifiedOn(accountId, workerName)).toEqual(uploaded); |
| 444 | expect(yield* observeSecrets(accountId, workerName)).toEqual(secrets); |
| 445 | |
| 446 | // Once the hostname is free, the existing store moves onto it. |
| 447 | yield* stack.destroy(); |
| 448 | const moved = yield* bootstrap({ workerName, domain }); |
| 449 | expect(yield* attachedDomains(accountId, domain)).toEqual([workerName]); |
| 450 | expect(yield* observeSecrets(accountId, workerName)).toEqual(secrets); |
| 451 | expect(yield* moved.get({ stack: "domain", stage: "test", fqn: "a" })).toEqual(record("a")); |
| 452 | |
| 453 | yield* teardownStateStore({ workerName }); |
| 454 | expect(yield* attachedDomains(accountId, domain)).toEqual([]); |
| 455 | }), |
| 456 | { tags, timeout: 400_000 }, |
| 457 | ); |
| 458 | |
| 459 | test.provider( |
| 460 | "a store that can't be reached after a deploy fails within 2 minutes and keeps its secrets and state", |
| 461 | (stack) => |
| 462 | Effect.gen(function* () { |
| 463 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 464 | const workerName = storeWorker("blocked"); |
| 465 | const domain = `state-blocked-${stage}.${ZONE}`; |
| 466 | yield* stack.destroy(); |
| 467 | yield* teardownStateStore({ workerName }); |
| 468 | |
| 469 | const state = yield* open({ workerName }); |
| 470 | yield* state.set({ stack: "blocked", stage: "test", fqn: "a", value: record("a") }); |
| 471 | const secrets = yield* observeSecrets(accountId, workerName); |
| 472 | |
| 473 | // Only an address nobody uses may reach the store's hostname, so every |
| 474 | // request to it is refused once the store moves there. |
| 475 | const zone = yield* findZoneByName({ accountId, name: ZONE }); |
| 476 | expect(zone).toBeDefined(); |
| 477 | yield* stack.deploy( |
| 478 | Cloudflare.Firewall.Lockdown("BlockStore", { |
| 479 | zoneId: zone!.id, |
| 480 | urls: [`${domain}/*`], |
| 481 | configurations: [{ target: "ip", value: "192.0.2.1" }], |
| 482 | description: "alchemy state store test: unreachable store", |
| 483 | }), |
| 484 | ); |
| 485 | |
| 486 | const started = yield* Clock.currentTimeMillis; |
| 487 | expectFailure( |
| 488 | yield* Effect.exit(bootstrap({ workerName, domain })), |
| 489 | `did not serve v${STATE_STORE_VERSION} within 2 minutes`, |
| 490 | ); |
| 491 | expect((yield* Clock.currentTimeMillis) - started).toBeLessThan(200_000); |
| 492 | expect(yield* observeSecrets(accountId, workerName)).toEqual(secrets); |
| 493 | // Its state is still there on workers.dev. |
| 494 | const kept = yield* bootstrap({ workerName }); |
| 495 | expect(yield* kept.get({ stack: "blocked", stage: "test", fqn: "a" })).toEqual(record("a")); |
| 496 | |
| 497 | yield* stack.destroy(); |
| 498 | yield* teardownStateStore({ workerName }); |
| 499 | expect(yield* attachedDomains(accountId, domain)).toEqual([]); |
| 500 | }), |
| 501 | { tags, timeout: 400_000 }, |
| 502 | ); |
| 503 | |
| 504 | /** Run with `CI` and the given env set, as a CI deploy would. */ |
| 505 | const inCI = <A, E, R>(env: Readonly<Record<string, string>>, effect: Effect.Effect<A, E, R>) => |
| 506 | Effect.acquireUseRelease( |
| 507 | Effect.sync(() => { |
| 508 | const vars = { CI: "true", ...env }; |
| 509 | const previous = Object.fromEntries(Object.keys(vars).map((key) => [key, process.env[key]])); |
| 510 | Object.assign(process.env, vars); |
| 511 | return previous; |
| 512 | }), |
| 513 | () => effect, |
| 514 | (previous) => |
| 515 | Effect.sync(() => { |
| 516 | for (const [key, value] of Object.entries(previous)) { |
| 517 | if (value === undefined) delete process.env[key]; |
| 518 | else process.env[key] = value; |
| 519 | } |
| 520 | }), |
| 521 | ); |
| 522 | |
| 523 | test.provider( |
| 524 | "in CI, `alchemy state bootstrap` deploys the store the config names", |
| 525 | () => |
| 526 | Effect.gen(function* () { |
| 527 | const environment = yield* yield* CloudflareEnvironment; |
| 528 | const { accountId } = environment; |
| 529 | const workerName = configuredWorkerName; |
| 530 | const entrypoint = fileURLToPath(new URL("./fixtures/configured-store.ts", import.meta.url)); |
| 531 | const source = { type: "configured", entrypoint } as const; |
| 532 | const alchemist = Alchemist.layer(); |
| 533 | yield* teardownStateStore({ workerName }); |
| 534 | |
| 535 | expect(environment.type).toBe("apiToken"); |
| 536 | const env: Record<string, string> = |
| 537 | environment.type === "apiToken" |
| 538 | ? { |
| 539 | CLOUDFLARE_API_TOKEN: Redacted.value(environment.apiToken), |
| 540 | CLOUDFLARE_ACCOUNT_ID: accountId, |
| 541 | } |
| 542 | : {}; |
| 543 | |
| 544 | // A CI run of the stack can't deploy it, and names the command that can: |
| 545 | // `--name` is the store's full Worker name. |
| 546 | const missing = yield* inCI( |
| 547 | env, |
| 548 | Effect.exit(Alchemist.State.store(source).pipe(Effect.provide(alchemist))), |
| 549 | ); |
| 550 | expectFailure( |
| 551 | missing, |
| 552 | `Run 'alchemy state bootstrap --backend cloudflare --name ${workerName} --profile`, |
| 553 | ); |
| 554 | expect(yield* scriptExists(accountId, workerName)).toBe(false); |
| 555 | |
| 556 | const { version } = yield* inCI( |
| 557 | env, |
| 558 | Alchemist.State.bootstrapConfigured(source).pipe(Effect.provide(alchemist)), |
| 559 | ); |
| 560 | expect(version).toBe(STATE_STORE_VERSION); |
| 561 | expect(yield* scriptExists(accountId, workerName)).toBe(true); |
| 562 | const secrets = yield* observeSecrets(accountId, workerName); |
| 563 | expect(secrets.token).toHaveLength(1); |
| 564 | expect(secrets.key).toHaveLength(1); |
| 565 | |
| 566 | yield* teardownStateStore({ workerName }); |
| 567 | }), |
| 568 | // Exclusive: sets `CI` on the process. |
| 569 | { tags, timeout: 300_000, exclusive: true }, |
| 570 | ); |
| 571 | |
| 572 | /** |
| 573 | * The last release before this store's direct deploy: it deploys the store |
| 574 | * through a `CloudflareStateStore` stack, and redeploys any store whose |
| 575 | * version differs from its own. |
| 576 | */ |
| 577 | const OLD_ALCHEMY = "2.0.0-beta.84"; |
| 578 | /** The store version that release deploys. */ |
| 579 | const OLD_STATE_STORE_VERSION = 7; |
| 580 | |
| 581 | const oldAlchemyDir = fileURLToPath( |
| 582 | new URL(`../../../.alchemy/old-alchemy/${OLD_ALCHEMY}/`, import.meta.url), |
| 583 | ); |
| 584 | |
| 585 | /** Install the old release once, into a directory of its own. */ |
| 586 | const installOldAlchemy = Effect.gen(function* () { |
| 587 | const fs = yield* FileSystem.FileSystem; |
| 588 | const path = yield* Path.Path; |
| 589 | const cli = path.join(oldAlchemyDir, "node_modules", "alchemy", "bin", "cli.js"); |
| 590 | if (yield* fs.exists(cli)) return cli; |
| 591 | yield* fs.makeDirectory(oldAlchemyDir, { recursive: true }); |
| 592 | yield* fs.writeFileString( |
| 593 | path.join(oldAlchemyDir, "package.json"), |
| 594 | JSON.stringify({ name: "old-alchemy", private: true, type: "module" }), |
| 595 | ); |
| 596 | const { exitCode, stderr } = yield* exec( |
| 597 | ChildProcess.make( |
| 598 | "npm", |
| 599 | [ |
| 600 | "install", |
| 601 | "--no-audit", |
| 602 | "--no-fund", |
| 603 | `alchemy@${OLD_ALCHEMY}`, |
| 604 | `@alchemy.run/cloudflare@${OLD_ALCHEMY}`, |
| 605 | "effect@4.0.7", |
| 606 | "@effect/platform-node@4.0.7", |
| 607 | // platform-node asks for `^4.0.7`, and 4.1 needs effect 4.1. |
| 608 | "@effect/platform-node-shared@4.0.7", |
| 609 | "@effect/platform-bun@4.0.7", |
| 610 | ], |
| 611 | { cwd: oldAlchemyDir, stdout: "pipe", stderr: "pipe" }, |
| 612 | ), |
| 613 | ); |
| 614 | expect({ exitCode, stderr: exitCode === 0 ? "" : stderr }).toEqual({ exitCode: 0, stderr: "" }); |
| 615 | return cli; |
| 616 | }).pipe(Effect.scoped); |
| 617 | |
| 618 | const oldAlchemyCli = Effect.runSync(Effect.cached(installOldAlchemy)); |
| 619 | |
| 620 | /** `alchemy state bootstrap --backend cloudflare --name <name>` from the old release. */ |
| 621 | const oldBootstrap = (name: string) => |
| 622 | Effect.gen(function* () { |
| 623 | const environment = yield* yield* CloudflareEnvironment; |
| 624 | if (environment.type !== "apiToken") { |
| 625 | return yield* Effect.die(new Error("The old CLI needs an API token.")); |
| 626 | } |
| 627 | const cli = yield* oldAlchemyCli; |
| 628 | const home = `${oldAlchemyDir}home-${name}`; |
| 629 | return yield* exec( |
| 630 | ChildProcess.make( |
| 631 | "node", |
| 632 | [cli, "state", "bootstrap", "--backend", "cloudflare", "--name", name, "--no-input"], |
| 633 | { |
| 634 | cwd: oldAlchemyDir, |
| 635 | env: { |
| 636 | HOME: home, |
| 637 | ALCHEMY_PROFILE: "old-alchemy", |
| 638 | CLOUDFLARE_API_TOKEN: Redacted.value(environment.apiToken), |
| 639 | CLOUDFLARE_ACCOUNT_ID: environment.accountId, |
| 640 | }, |
| 641 | extendEnv: true, |
| 642 | stdout: "pipe", |
| 643 | stderr: "pipe", |
| 644 | }, |
| 645 | ), |
| 646 | ).pipe(Effect.scoped); |
| 647 | }).pipe(Effect.provide(NodeServices.layer)); |
| 648 | |
| 649 | test.provider( |
| 650 | "an older Alchemy can't redeploy a store this version deployed", |
| 651 | () => |
| 652 | Effect.gen(function* () { |
| 653 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 654 | const name = storeName("new"); |
| 655 | const workerName = `alchemy-state-store-${name}`; |
| 656 | yield* teardownStateStore({ workerName }); |
| 657 | |
| 658 | const state = yield* open({ workerName }); |
| 659 | yield* state.set({ stack: "new", stage: "test", fqn: "a", value: record("a") }); |
| 660 | const secrets = yield* observeSecrets(accountId, workerName); |
| 661 | const uploaded = yield* scriptModifiedOn(accountId, workerName); |
| 662 | |
| 663 | // It sees a version it doesn't know and redeploys through its bootstrap |
| 664 | // stack, which the store refuses before any secret is touched. |
| 665 | const old = yield* oldBootstrap(name); |
| 666 | expect(old.exitCode).not.toBe(0); |
| 667 | expect(`${old.stdout}${old.stderr}`).toContain("409"); |
| 668 | expect(yield* observeSecrets(accountId, workerName)).toEqual(secrets); |
| 669 | expect(yield* scriptModifiedOn(accountId, workerName)).toEqual(uploaded); |
| 670 | const kept = yield* bootstrap({ workerName }); |
| 671 | expect(yield* kept.get({ stack: "new", stage: "test", fqn: "a" })).toEqual(record("a")); |
| 672 | |
| 673 | yield* teardownStateStore({ workerName }); |
| 674 | }), |
| 675 | { tags, timeout: 400_000 }, |
| 676 | ); |
| 677 | |
| 678 | test.provider( |
| 679 | "a store an older Alchemy deployed is upgraded in place with its secrets and state, and the old version can't take it back", |
| 680 | () => |
| 681 | Effect.gen(function* () { |
| 682 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 683 | const name = storeName("old"); |
| 684 | const workerName = `alchemy-state-store-${name}`; |
| 685 | yield* teardownStateStore({ workerName }); |
| 686 | |
| 687 | const deployed = yield* oldBootstrap(name); |
| 688 | expect({ |
| 689 | exitCode: deployed.exitCode, |
| 690 | output: deployed.exitCode === 0 ? "" : deployed.stderr, |
| 691 | }).toEqual({ |
| 692 | exitCode: 0, |
| 693 | output: "", |
| 694 | }); |
| 695 | const secrets = yield* observeSecrets(accountId, workerName); |
| 696 | expect(secrets.token).toHaveLength(1); |
| 697 | expect(secrets.key).toHaveLength(1); |
| 698 | |
| 699 | // State written to the old Worker. |
| 700 | const profile = yield* currentProfileName; |
| 701 | const credentials = yield* loginWithCloudflare(profile, true, workerName, accountId); |
| 702 | expect(yield* servedVersion(credentials.url)).toBe(OLD_STATE_STORE_VERSION); |
| 703 | const legacy = yield* makeHttpStateStore({ ...credentials, id: "legacy" }); |
| 704 | yield* legacy.set({ stack: "old", stage: "test", fqn: "a", value: record("a") }); |
| 705 | // The old version deployed the store through a stack recorded in it. |
| 706 | expect([...(yield* legacy.listStacks())].sort()).toEqual(["CloudflareStateStore", "old"]); |
| 707 | |
| 708 | // An interrupted old bootstrap leaves that stack in local state, under |
| 709 | // `<profile>_<worker>`. Another store's leftovers are its own business. |
| 710 | const local = yield* makeLocalState(); |
| 711 | const leftover = { stack: "CloudflareStateStore", fqn: "StateStoreSecrets" }; |
| 712 | yield* local.set({ ...leftover, stage: `${profile}_${workerName}`, value: record("s") }); |
| 713 | yield* local.set({ ...leftover, stage: `${profile}_other-store`, value: record("s") }); |
| 714 | |
| 715 | const upgraded = yield* bootstrap({ workerName }); |
| 716 | const upgradedOn = yield* scriptModifiedOn(accountId, workerName); |
| 717 | // Stacks opening it right away, while edge locations may still serve |
| 718 | // the old version, neither fail as out of date nor redeploy it. |
| 719 | for (const _ of [1, 2, 3]) { |
| 720 | const reopened = yield* open({ workerName }); |
| 721 | expect(yield* reopened.get({ stack: "old", stage: "test", fqn: "a" })).toEqual(record("a")); |
| 722 | } |
| 723 | expect(yield* scriptModifiedOn(accountId, workerName)).toEqual(upgradedOn); |
| 724 | // A redeploy reaches every edge location within seconds. |
| 725 | expect(yield* eventuallyServes(credentials.url, STATE_STORE_VERSION)).toBe( |
| 726 | STATE_STORE_VERSION, |
| 727 | ); |
| 728 | expect(yield* observeSecrets(accountId, workerName)).toEqual(secrets); |
| 729 | expect(yield* upgraded.get({ stack: "old", stage: "test", fqn: "a" })).toEqual(record("a")); |
| 730 | // The upgrade deletes that stack's records, so nobody can destroy the |
| 731 | // store through them. |
| 732 | expect(yield* upgraded.listStacks()).toEqual(["old"]); |
| 733 | expect(yield* local.listStages("CloudflareStateStore")).toEqual([`${profile}_other-store`]); |
| 734 | yield* local.deleteStack({ stack: "CloudflareStateStore" }); |
| 735 | |
| 736 | const old = yield* oldBootstrap(name); |
| 737 | expect(old.exitCode).not.toBe(0); |
| 738 | expect(`${old.stdout}${old.stderr}`).toContain("409"); |
| 739 | expect(yield* observeSecrets(accountId, workerName)).toEqual(secrets); |
| 740 | expect(yield* scriptModifiedOn(accountId, workerName)).toEqual(upgradedOn); |
| 741 | |
| 742 | yield* teardownStateStore({ workerName }); |
| 743 | }), |
| 744 | { tags, timeout: 600_000 }, |
| 745 | ); |
| 746 | |
| 747 | test.provider( |
| 748 | "a stack opening a store an older Alchemy deployed upgrades it in place only when approved", |
| 749 | () => |
| 750 | Effect.gen(function* () { |
| 751 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 752 | const name = storeName("v7"); |
| 753 | const workerName = `alchemy-state-store-${name}`; |
| 754 | yield* teardownStateStore({ workerName }); |
| 755 | |
| 756 | const deployed = yield* oldBootstrap(name); |
| 757 | expect({ |
| 758 | exitCode: deployed.exitCode, |
| 759 | output: deployed.exitCode === 0 ? "" : deployed.stderr, |
| 760 | }).toEqual({ exitCode: 0, output: "" }); |
| 761 | const secrets = yield* observeSecrets(accountId, workerName); |
| 762 | const profile = yield* currentProfileName; |
| 763 | const credentials = yield* loginWithCloudflare(profile, true, workerName, accountId); |
| 764 | expect(yield* servedVersion(credentials.url)).toBe(OLD_STATE_STORE_VERSION); |
| 765 | const legacy = yield* makeHttpStateStore({ ...credentials, id: "legacy" }); |
| 766 | yield* legacy.set({ stack: "v7", stage: "test", fqn: "a", value: record("a") }); |
| 767 | |
| 768 | // Without `--yes` (and nobody to confirm), the deploy stops and says so. |
| 769 | const unapproved = yield* Effect.exit(open({ workerName })); |
| 770 | expectFailure( |
| 771 | unapproved, |
| 772 | `is out of date (expected v${STATE_STORE_VERSION}, observed v${OLD_STATE_STORE_VERSION})`, |
| 773 | ); |
| 774 | expect(yield* servedVersion(credentials.url)).toBe(OLD_STATE_STORE_VERSION); |
| 775 | expect(yield* observeSecrets(accountId, workerName)).toEqual(secrets); |
| 776 | |
| 777 | // With `--yes`, it upgrades the store in place. |
| 778 | const context = yield* Effect.serviceOption(AlchemyContext); |
| 779 | const upgraded = yield* open({ workerName }).pipe( |
| 780 | Effect.provideService(AlchemyContext, { |
| 781 | dotAlchemy: Option.isSome(context) ? context.value.dotAlchemy : ".alchemy", |
| 782 | dev: false, |
| 783 | adopt: false, |
| 784 | updateStateStore: true, |
| 785 | }), |
| 786 | ); |
| 787 | expect(yield* eventuallyServes(credentials.url, STATE_STORE_VERSION)).toBe( |
| 788 | STATE_STORE_VERSION, |
| 789 | ); |
| 790 | expect(yield* observeSecrets(accountId, workerName)).toEqual(secrets); |
| 791 | expect(yield* upgraded.get({ stack: "v7", stage: "test", fqn: "a" })).toEqual(record("a")); |
| 792 | expect(yield* upgraded.listStacks()).toEqual(["v7"]); |
| 793 | |
| 794 | yield* teardownStateStore({ workerName }); |
| 795 | }), |
| 796 | { tags, timeout: 600_000 }, |
| 797 | ); |