The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { AWSEnvironment } from "@alchemy.run/aws/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as s3 from "@distilled.cloud/aws/s3"; |
| 6 | import * as Alchemist from "alchemy/Alchemist"; |
| 7 | import * as Cause from "effect/Cause"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Exit from "effect/Exit"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: AWS.providers() }); |
| 12 | |
| 13 | /** Run with `CI` set, as a CI deploy would. */ |
| 14 | const inCI = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 15 | Effect.acquireUseRelease( |
| 16 | Effect.sync(() => { |
| 17 | const previous = process.env.CI; |
| 18 | process.env.CI = "true"; |
| 19 | return previous; |
| 20 | }), |
| 21 | () => effect, |
| 22 | (previous) => |
| 23 | Effect.sync(() => { |
| 24 | if (previous === undefined) delete process.env.CI; |
| 25 | else process.env.CI = previous; |
| 26 | }), |
| 27 | ); |
| 28 | |
| 29 | const bucketExists = (bucket: string) => |
| 30 | s3.headBucket({ Bucket: bucket }).pipe( |
| 31 | Effect.as(true), |
| 32 | Effect.catchTag(["NotFound", "NoSuchBucket"], () => Effect.succeed(false)), |
| 33 | ); |
| 34 | |
| 35 | test.provider( |
| 36 | "a named S3 state store bootstraps, serves state and tears down", |
| 37 | () => |
| 38 | Effect.gen(function* () { |
| 39 | const { accountId, region } = yield* AWSEnvironment.current; |
| 40 | // Never the default store, which every other run on the account uses. |
| 41 | const name = "lifecycle"; |
| 42 | const bucket = `alchemy-state-lifecycle-${accountId}-${region}-an`; |
| 43 | const target = { type: "provider", backend: "aws-s3", name } as const; |
| 44 | const alchemist = Alchemist.layer(); |
| 45 | |
| 46 | yield* Alchemist.State.teardown(target).pipe(Effect.provide(alchemist)); |
| 47 | |
| 48 | const created = yield* Alchemist.State.bootstrap(target).pipe(Effect.provide(alchemist)); |
| 49 | expect(created.status).toBe("created"); |
| 50 | expect(yield* bucketExists(bucket)).toBe(true); |
| 51 | expect((yield* s3.getBucketVersioning({ Bucket: bucket })).Status).toBe("Enabled"); |
| 52 | |
| 53 | const adopted = yield* Alchemist.State.bootstrap(target).pipe(Effect.provide(alchemist)); |
| 54 | expect(adopted.status).toBe("adopted"); |
| 55 | |
| 56 | const store = yield* Alchemist.State.store(target).pipe(Effect.provide(alchemist)); |
| 57 | expect(yield* store.listStacks()).toEqual([]); |
| 58 | |
| 59 | const torn = yield* Alchemist.State.teardown(target).pipe(Effect.provide(alchemist)); |
| 60 | expect(torn.deleted).toEqual([bucket]); |
| 61 | expect(yield* bucketExists(bucket)).toBe(false); |
| 62 | |
| 63 | const again = yield* Alchemist.State.teardown(target).pipe(Effect.provide(alchemist)); |
| 64 | expect(again.deleted).toEqual([]); |
| 65 | |
| 66 | // In CI, a missing store fails instead of being created on first use. |
| 67 | const inCi = yield* inCI( |
| 68 | Effect.exit( |
| 69 | Alchemist.State.store(target).pipe( |
| 70 | Effect.flatMap((store) => store.listStacks()), |
| 71 | Effect.provide(alchemist), |
| 72 | ), |
| 73 | ), |
| 74 | ); |
| 75 | expect(Exit.isFailure(inCi)).toBe(true); |
| 76 | if (Exit.isFailure(inCi)) { |
| 77 | expect(Cause.pretty(inCi.cause)).toContain( |
| 78 | `Run 'alchemy state bootstrap --backend aws-s3 --name ${name}`, |
| 79 | ); |
| 80 | } |
| 81 | expect(yield* bucketExists(bucket)).toBe(false); |
| 82 | }), |
| 83 | // Exclusive: sets `CI` on the process. |
| 84 | { |
| 85 | tags: ["provider:aws", "provider:aws:s3", "provider:aws:statestore", "live"], |
| 86 | timeout: 180_000, |
| 87 | exclusive: true, |
| 88 | }, |
| 89 | ); |