The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Railway from "@alchemy.run/railway"; |
| 2 | import { projectBuckets } from "@alchemy.run/railway/GraphQL"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import { fromCredentials } from "@distilled.cloud/aws/Credentials"; |
| 6 | import * as AwsEndpoint from "@distilled.cloud/aws/Endpoint"; |
| 7 | import type { RegionName } from "@distilled.cloud/aws/Region"; |
| 8 | import * as S3 from "@distilled.cloud/aws/s3"; |
| 9 | import { Query } from "@distilled.cloud/core/query"; |
| 10 | import { Railway as RailwayApi } from "@distilled.cloud/railway"; |
| 11 | import * as Provider from "alchemy/Provider"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Layer from "effect/Layer"; |
| 14 | import * as Redacted from "effect/Redacted"; |
| 15 | import { MinimumLogLevel } from "effect/References"; |
| 16 | import * as Schedule from "effect/Schedule"; |
| 17 | import * as Stream from "effect/Stream"; |
| 18 | import { suitePartition } from "./suiteProject.ts"; |
| 19 | |
| 20 | const { test } = Test.make({ providers: Railway.providers() }); |
| 21 | |
| 22 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 23 | |
| 24 | const OBJECT_KEY = "alchemy-marker.txt"; |
| 25 | const OBJECT_BODY = "hello-from-railway"; |
| 26 | |
| 27 | const readBucketCredentials = Query.fn( |
| 28 | (bucketId: string, environmentId: string, projectId: string) => |
| 29 | RailwayApi.bucketS3Credentials({ bucketId, environmentId, projectId }).pipe( |
| 30 | Query.map((creds) => ({ |
| 31 | bucketName: creds.bucketName, |
| 32 | endpoint: creds.endpoint, |
| 33 | accessKeyId: creds.accessKeyId, |
| 34 | secretAccessKey: creds.secretAccessKey, |
| 35 | region: creds.region, |
| 36 | })), |
| 37 | ), |
| 38 | ); |
| 39 | |
| 40 | const readEnvironmentConfig = Query.fn((id: string, projectId: string) => ({ |
| 41 | config: RailwayApi.environment({ id, projectId }).config, |
| 42 | })); |
| 43 | |
| 44 | const listProjectBuckets = (projectId: string) => |
| 45 | projectBuckets(projectId, (bucket) => ({ |
| 46 | id: bucket.id, |
| 47 | name: bucket.name, |
| 48 | projectId: bucket.projectId, |
| 49 | })).pipe(Effect.catchTag("RailwayNotFound", () => Effect.succeed([]))); |
| 50 | |
| 51 | const findBucket = (projectId: string, bucketId: string, name: string) => |
| 52 | listProjectBuckets(projectId).pipe( |
| 53 | Effect.map( |
| 54 | (buckets) => |
| 55 | buckets.find((bucket) => bucket.id === bucketId) ?? |
| 56 | buckets.find((bucket) => bucket.name === name), |
| 57 | ), |
| 58 | ); |
| 59 | |
| 60 | const firstCredentials = (bucketId: string, environmentId: string, projectId: string) => |
| 61 | readBucketCredentials(bucketId, environmentId, projectId).pipe( |
| 62 | Effect.flatMap((items) => { |
| 63 | const first = items[0]; |
| 64 | return first !== undefined |
| 65 | ? Effect.succeed(first) |
| 66 | : Effect.fail(new Error("missing bucket credentials")); |
| 67 | }), |
| 68 | Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 8 }), |
| 69 | ); |
| 70 | |
| 71 | const waitUntilBucketGone = (environmentId: string, projectId: string, bucketId: string) => |
| 72 | readEnvironmentConfig(environmentId, projectId).pipe( |
| 73 | Effect.map((env) => { |
| 74 | const buckets = |
| 75 | env.config !== null && typeof env.config === "object" && !Array.isArray(env.config) |
| 76 | ? (env.config as { buckets?: Record<string, { isDeleted?: boolean | null } | null> }) |
| 77 | .buckets |
| 78 | : undefined; |
| 79 | const row = buckets?.[bucketId]; |
| 80 | return row == null || row.isDeleted === true ? ("gone" as const) : ("found" as const); |
| 81 | }), |
| 82 | Effect.catchTag("RailwayNotFound", () => Effect.succeed("gone" as const)), |
| 83 | Effect.repeat({ |
| 84 | schedule: Schedule.spaced("2 seconds"), |
| 85 | until: (status) => status === "gone", |
| 86 | times: 10, |
| 87 | }), |
| 88 | ); |
| 89 | |
| 90 | const withBucketS3 = <A, E, R>( |
| 91 | creds: { |
| 92 | accessKeyId: string; |
| 93 | secretAccessKey: Redacted.Redacted<string>; |
| 94 | endpoint: string; |
| 95 | region: string; |
| 96 | }, |
| 97 | operation: Effect.Effect<A, E, R>, |
| 98 | ) => |
| 99 | operation.pipe( |
| 100 | Effect.provide( |
| 101 | Layer.mergeAll( |
| 102 | fromCredentials( |
| 103 | { |
| 104 | accessKeyId: Redacted.make(creds.accessKeyId), |
| 105 | secretAccessKey: creds.secretAccessKey, |
| 106 | }, |
| 107 | creds.region as RegionName, |
| 108 | ), |
| 109 | AwsEndpoint.of(creds.endpoint), |
| 110 | ), |
| 111 | ), |
| 112 | ); |
| 113 | |
| 114 | test.provider( |
| 115 | "create, update, list, put/get/delete objects, and delete a bucket", |
| 116 | (stack) => |
| 117 | Effect.gen(function* () { |
| 118 | yield* stack.destroy(); |
| 119 | |
| 120 | const created = yield* stack.deploy( |
| 121 | Effect.gen(function* () { |
| 122 | const { project, environment } = yield* suitePartition; |
| 123 | const bucket = yield* Railway.Bucket("Data", { project, environment }); |
| 124 | return { project, environment, bucket }; |
| 125 | }), |
| 126 | ); |
| 127 | |
| 128 | expect(created.bucket.bucketId).toEqual(expect.any(String)); |
| 129 | expect(created.bucket.bucketId.length).toBeGreaterThan(0); |
| 130 | expect(created.bucket.projectId).toEqual(created.project.projectId); |
| 131 | expect(created.bucket.environmentId).toEqual(created.environment.environmentId); |
| 132 | expect(created.bucket.name).toEqual(expect.any(String)); |
| 133 | expect(created.bucket.name.length).toBeGreaterThan(0); |
| 134 | expect(created.bucket.name.length).toBeLessThanOrEqual(32); |
| 135 | expect(created.bucket.name).toMatch(/^[a-z][a-z0-9-]*$/); |
| 136 | expect(created.bucket.region).toEqual("sjc"); |
| 137 | expect(created.bucket.s3BucketName).toEqual(expect.any(String)); |
| 138 | expect((created.bucket.s3BucketName ?? "").length).toBeGreaterThan(0); |
| 139 | expect(created.bucket.endpoint).toEqual(expect.any(String)); |
| 140 | expect(created.bucket.createdAt).toEqual(expect.any(String)); |
| 141 | |
| 142 | const fetched = yield* findBucket( |
| 143 | created.project.projectId, |
| 144 | created.bucket.bucketId, |
| 145 | created.bucket.name, |
| 146 | ); |
| 147 | expect(fetched).toBeDefined(); |
| 148 | expect(fetched?.id).toEqual(created.bucket.bucketId); |
| 149 | expect(fetched?.name).toEqual(created.bucket.name); |
| 150 | expect(fetched?.projectId).toEqual(created.project.projectId); |
| 151 | |
| 152 | const provider = yield* Provider.findProvider(Railway.Bucket); |
| 153 | const listed = yield* provider.list(); |
| 154 | const found = listed.find((bucket) => bucket.bucketId === created.bucket.bucketId); |
| 155 | expect(found).toBeDefined(); |
| 156 | expect(found?.name).toEqual(created.bucket.name); |
| 157 | expect(found?.projectId).toEqual(created.project.projectId); |
| 158 | expect(found?.environmentId).toEqual(created.bucket.environmentId); |
| 159 | |
| 160 | const creds = yield* firstCredentials( |
| 161 | created.bucket.bucketId, |
| 162 | created.bucket.environmentId, |
| 163 | created.project.projectId, |
| 164 | ); |
| 165 | expect(creds.bucketName.length).toBeGreaterThan(0); |
| 166 | expect(creds.endpoint.length).toBeGreaterThan(0); |
| 167 | expect(creds.accessKeyId.length).toBeGreaterThan(0); |
| 168 | expect(Redacted.value(creds.secretAccessKey).length).toBeGreaterThan(0); |
| 169 | |
| 170 | yield* withBucketS3( |
| 171 | creds, |
| 172 | S3.putObject({ |
| 173 | Bucket: creds.bucketName, |
| 174 | Key: OBJECT_KEY, |
| 175 | Body: OBJECT_BODY, |
| 176 | ContentType: "text/plain", |
| 177 | }), |
| 178 | ); |
| 179 | |
| 180 | const got = yield* withBucketS3( |
| 181 | creds, |
| 182 | S3.getObject({ Bucket: creds.bucketName, Key: OBJECT_KEY }), |
| 183 | ); |
| 184 | const text = |
| 185 | got.Body === undefined ? "" : yield* Stream.mkString(Stream.decodeText(got.Body)); |
| 186 | expect(text).toEqual(OBJECT_BODY); |
| 187 | |
| 188 | const listedObjects = yield* withBucketS3( |
| 189 | creds, |
| 190 | S3.listObjectsV2({ Bucket: creds.bucketName, Prefix: OBJECT_KEY }), |
| 191 | ); |
| 192 | expect((listedObjects.Contents ?? []).some((item) => item.Key === OBJECT_KEY)).toEqual(true); |
| 193 | |
| 194 | yield* withBucketS3(creds, S3.deleteObject({ Bucket: creds.bucketName, Key: OBJECT_KEY })); |
| 195 | |
| 196 | const nextName = |
| 197 | created.bucket.name.slice(0, -1) + (created.bucket.name.endsWith("z") ? "y" : "z"); |
| 198 | |
| 199 | const updated = yield* stack.deploy( |
| 200 | Effect.gen(function* () { |
| 201 | const { project, environment } = yield* suitePartition; |
| 202 | const bucket = yield* Railway.Bucket("Data", { project, environment, name: nextName }); |
| 203 | return { project, environment, bucket }; |
| 204 | }), |
| 205 | ); |
| 206 | |
| 207 | expect(updated.bucket.bucketId).toEqual(created.bucket.bucketId); |
| 208 | expect(updated.bucket.name).toEqual(nextName); |
| 209 | expect(updated.bucket.projectId).toEqual(created.project.projectId); |
| 210 | expect(updated.bucket.environmentId).toEqual(created.bucket.environmentId); |
| 211 | |
| 212 | const fetchedUpdate = yield* findBucket( |
| 213 | updated.project.projectId, |
| 214 | updated.bucket.bucketId, |
| 215 | updated.bucket.name, |
| 216 | ); |
| 217 | expect(fetchedUpdate).toBeDefined(); |
| 218 | expect(fetchedUpdate?.id).toEqual(created.bucket.bucketId); |
| 219 | expect(fetchedUpdate?.name).toEqual(nextName); |
| 220 | |
| 221 | yield* stack.destroy(); |
| 222 | |
| 223 | const gone = yield* waitUntilBucketGone( |
| 224 | created.bucket.environmentId, |
| 225 | created.project.projectId, |
| 226 | created.bucket.bucketId, |
| 227 | ); |
| 228 | expect(gone).toEqual("gone"); |
| 229 | }).pipe(logLevel), |
| 230 | { |
| 231 | tags: [ |
| 232 | "provider:aws", |
| 233 | "provider:aws:s3", |
| 234 | "provider:railway", |
| 235 | "provider:railway:bucket", |
| 236 | "provider:railway:project", |
| 237 | "provider:railway:projectenvironment", |
| 238 | "live", |
| 239 | ], |
| 240 | timeout: 120_000, |
| 241 | }, |
| 242 | ); |