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, it } 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 NodeServices from "@effect/platform-node/NodeServices"; |
| 7 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 8 | import { ArtifactStore, createArtifactStore } from "alchemy/Artifacts"; |
| 9 | import { AuthProvidersLive } from "alchemy/Auth/AuthProvider"; |
| 10 | import { ProfileStoreLive } from "alchemy/Auth/Profile"; |
| 11 | import { Stack } from "alchemy/Stack"; |
| 12 | import { Stage } from "alchemy/Stage"; |
| 13 | import { State, type ResourceState } from "alchemy/State"; |
| 14 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 17 | import * as Layer from "effect/Layer"; |
| 18 | import * as Result from "effect/Result"; |
| 19 | import { v4 as uuidv4 } from "uuid"; |
| 20 | |
| 21 | it.live( |
| 22 | "building the AWS provider layers rejects an unknown explicit profile", |
| 23 | () => |
| 24 | Effect.gen(function* () { |
| 25 | // AWSEnvironment is constructed lazily so `alchemy dev` can build |
| 26 | // provider layers without credentials. The unknown-profile rejection |
| 27 | // surfaces on first use, not at `Layer.build`. |
| 28 | const result = yield* Effect.result( |
| 29 | Effect.sandbox(AWSEnvironment.current.pipe(Effect.provide(AWS.providers()))), |
| 30 | ); |
| 31 | expect(Result.isFailure(result)).toBe(true); |
| 32 | if (Result.isFailure(result)) { |
| 33 | expect(String(result.failure)).toContain("does not exist"); |
| 34 | expect(String(result.failure)).toContain("alchemy profile create"); |
| 35 | } |
| 36 | }).pipe( |
| 37 | Effect.provide( |
| 38 | Layer.mergeAll( |
| 39 | AuthProvidersLive, |
| 40 | Layer.sync(ArtifactStore, createArtifactStore), |
| 41 | Layer.succeed(Stage, "test"), |
| 42 | Layer.succeed(Stack, { |
| 43 | name: "test", |
| 44 | stage: "test", |
| 45 | resources: {}, |
| 46 | bindings: {}, |
| 47 | actions: {}, |
| 48 | }), |
| 49 | Layer.succeed(AlchemyContext, { dev: false, adopt: false, dotAlchemy: ".alchemy" }), |
| 50 | ConfigProvider.layer( |
| 51 | ConfigProvider.fromUnknown({ ALCHEMY_PROFILE: `non-existent-${uuidv4()}` }), |
| 52 | ), |
| 53 | ProfileStoreLive, |
| 54 | ).pipe(Layer.provideMerge(Layer.mergeAll(NodeServices.layer, FetchHttpClient.layer))), |
| 55 | ), |
| 56 | ), |
| 57 | { tags: ["unit", "provider:aws", "local"] }, |
| 58 | ); |
| 59 | |
| 60 | // The profile's own environment, moved to another region. Providing it to |
| 61 | // `AWS.providers()` / `AWS.state()` from outside must win over the built-in |
| 62 | // profile default — the region makes it observable. |
| 63 | const PROVIDED_REGION = "us-east-2"; |
| 64 | const providedEnvironment = Layer.effect( |
| 65 | AWSEnvironment, |
| 66 | Effect.gen(function* () { |
| 67 | const profileEnvironment = yield* AWSEnvironment; |
| 68 | return Effect.map(profileEnvironment, (environment) => ({ |
| 69 | ...environment, |
| 70 | region: PROVIDED_REGION, |
| 71 | })); |
| 72 | }), |
| 73 | ).pipe(Layer.provide(AWS.providers())); |
| 74 | |
| 75 | const provided = Test.make({ |
| 76 | providers: AWS.providers().pipe(Layer.provide(providedEnvironment)), |
| 77 | }); |
| 78 | |
| 79 | provided.test.provider( |
| 80 | "a provided AWSEnvironment drives providers() and state()", |
| 81 | (stack) => |
| 82 | Effect.gen(function* () { |
| 83 | yield* stack.destroy(); |
| 84 | |
| 85 | // Resources deploy with the provided environment. |
| 86 | const bucket = yield* stack.deploy( |
| 87 | AWS.S3.Bucket("ProvidedEnvironmentBucket", { forceDestroy: true }), |
| 88 | ); |
| 89 | expect(bucket.region).toBe(PROVIDED_REGION); |
| 90 | const location = yield* s3.getBucketLocation({ Bucket: bucket.bucketName }); |
| 91 | expect(location.LocationConstraint).toBe(PROVIDED_REGION); |
| 92 | |
| 93 | // The state store uses the same provided environment: a round-trip |
| 94 | // against the bucket in that region. |
| 95 | const state = yield* (yield* State).pipe( |
| 96 | Effect.provide( |
| 97 | AWS.state({ bucketName: bucket.bucketName, prefix: "provided-environment" }).pipe( |
| 98 | Layer.provide(providedEnvironment), |
| 99 | ), |
| 100 | ), |
| 101 | ); |
| 102 | const row = { |
| 103 | resourceType: "test:resource", |
| 104 | namespace: undefined, |
| 105 | fqn: "Provided", |
| 106 | logicalId: "Provided", |
| 107 | instanceId: "instance-provided", |
| 108 | providerVersion: 1, |
| 109 | status: "created", |
| 110 | downstream: [], |
| 111 | bindings: [], |
| 112 | props: {}, |
| 113 | attr: { value: "provided" }, |
| 114 | } as unknown as ResourceState; |
| 115 | const key = { stack: "ProvidedEnvironmentStack", stage: "provided" }; |
| 116 | yield* state.set({ ...key, fqn: row.fqn, value: row }); |
| 117 | expect(yield* state.get({ ...key, fqn: row.fqn })).toEqual(row); |
| 118 | yield* state.deleteStack(key); |
| 119 | |
| 120 | yield* stack.destroy(); |
| 121 | }), |
| 122 | { |
| 123 | tags: ["provider:aws", "provider:aws:s3", "provider:aws:statestore", "live"], |
| 124 | timeout: 180_000, |
| 125 | }, |
| 126 | ); |