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 * as Cloudflare from "@alchemy.run/cloudflare"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as Alchemy from "alchemy"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import CloudflareStack from "./fixtures/microvm/cloudflare-stack.ts"; |
| 13 | import ExternalStack from "./fixtures/microvm/external/stack.ts"; |
| 14 | import EffectfulStack from "./fixtures/microvm/stack.ts"; |
| 15 | |
| 16 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 17 | // The Cloudflare stack deploys a Worker driving a MicroVM image, so the |
| 18 | // harness needs both provider sets. Cloudflare credentials resolve lazily, |
| 19 | // so the AWS-only blocks run without them. |
| 20 | providers: Layer.mergeAll(AWS.providers(), Cloudflare.providers()), |
| 21 | state: Alchemy.localState(), |
| 22 | }); |
| 23 | |
| 24 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 25 | |
| 26 | // MicroVM image builds run server-side (Firecracker snapshot) and can take |
| 27 | // several minutes, so give deploy/destroy plenty of room. |
| 28 | const HOOK_TIMEOUT = 1_500_000; |
| 29 | const TEST_TIMEOUT = 300_000; |
| 30 | |
| 31 | // Lambda MicroVM is a preview feature: builds are asynchronous (minutes) and the |
| 32 | // account must be onboarded to the preview, with a bootstrapped Assets bucket. |
| 33 | const skip = !process.env.LAMBDA_TEST_MICROVM; |
| 34 | |
| 35 | // The cross-cloud Worker leg additionally needs real Cloudflare credentials, |
| 36 | // so it is opt-in on top of LAMBDA_TEST_MICROVM. |
| 37 | const skipCloudflare = skip || !process.env.LAMBDA_TEST_MICROVM_CLOUDFLARE; |
| 38 | |
| 39 | const readinessSchedule = Schedule.min([ |
| 40 | Schedule.exponential("500 millis"), |
| 41 | Schedule.spaced("3 seconds"), |
| 42 | ]); |
| 43 | const readinessRetries = 30; |
| 44 | |
| 45 | // Retry a freshly-deployed Lambda URL until it answers 200 (cold-start, IAM |
| 46 | // propagation). |
| 47 | const send = (req: HttpClientRequest.HttpClientRequest) => |
| 48 | Effect.gen(function* () { |
| 49 | const client = yield* HttpClient.HttpClient; |
| 50 | return yield* client.execute(req).pipe( |
| 51 | Effect.flatMap((r) => |
| 52 | r.status === 200 ? Effect.succeed(r) : Effect.fail(new Error(`not ready: ${r.status}`)), |
| 53 | ), |
| 54 | Effect.timeout("30 seconds"), |
| 55 | Effect.retry({ schedule: readinessSchedule, times: readinessRetries }), |
| 56 | ); |
| 57 | }); |
| 58 | |
| 59 | /** |
| 60 | * Effectful MicroVM (`main`): the in-VM HTTP server is written in TypeScript and |
| 61 | * bundled into a generated image. A Lambda orchestrator binds the instance |
| 62 | * operations and drives the full lifecycle (run -> auth-token -> list -> |
| 63 | * terminate) over its function URL. |
| 64 | */ |
| 65 | describe.skipIf(skip)( |
| 66 | "effectful microvm (main)", |
| 67 | { |
| 68 | tags: ["provider:aws", "provider:aws:iam", "provider:aws:lambda", "live"], |
| 69 | }, |
| 70 | () => { |
| 71 | const stack = beforeAll(deploy(EffectfulStack), { timeout: HOOK_TIMEOUT }); |
| 72 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(EffectfulStack), { |
| 73 | timeout: HOOK_TIMEOUT, |
| 74 | }); |
| 75 | |
| 76 | test( |
| 77 | "runs a MicroVM, mints a token, and terminates it", |
| 78 | Effect.gen(function* () { |
| 79 | const { url } = yield* stack; |
| 80 | const baseUrl = url.replace(/\/+$/, ""); |
| 81 | |
| 82 | const runRes = yield* send(HttpClientRequest.post(`${baseUrl}/run`)); |
| 83 | const vm = (yield* runRes.json) as { |
| 84 | microvmId: string; |
| 85 | endpoint: string; |
| 86 | state: string; |
| 87 | }; |
| 88 | expect(vm.microvmId).toBeTruthy(); |
| 89 | // RunMicrovm returns the MicroVM's endpoint as a bare hostname |
| 90 | // (`<id>.lambda-microvm.<region>.on.aws`), not a full URL. |
| 91 | expect(vm.endpoint).toContain("lambda-microvm"); |
| 92 | |
| 93 | yield* send(HttpClientRequest.get(`${baseUrl}/get?id=${vm.microvmId}`)); |
| 94 | |
| 95 | const tokenRes = yield* send( |
| 96 | HttpClientRequest.post(`${baseUrl}/auth-token?id=${vm.microvmId}`), |
| 97 | ); |
| 98 | const token = (yield* tokenRes.json) as { hasToken: boolean }; |
| 99 | expect(token.hasToken).toBe(true); |
| 100 | |
| 101 | const listRes = yield* send(HttpClientRequest.get(`${baseUrl}/list`)); |
| 102 | const list = (yield* listRes.json) as { count: number }; |
| 103 | expect(list.count).toBeGreaterThanOrEqual(1); |
| 104 | |
| 105 | const termRes = yield* send( |
| 106 | HttpClientRequest.post(`${baseUrl}/terminate?id=${vm.microvmId}`), |
| 107 | ); |
| 108 | expect(termRes.status).toBe(200); |
| 109 | }).pipe(logLevel), |
| 110 | { timeout: TEST_TIMEOUT }, |
| 111 | ); |
| 112 | |
| 113 | test( |
| 114 | "drives the in-VM tagged RPC method and the fetch route", |
| 115 | Effect.gen(function* () { |
| 116 | const { url } = yield* stack; |
| 117 | const baseUrl = url.replace(/\/+$/, ""); |
| 118 | const client = yield* HttpClient.HttpClient; |
| 119 | |
| 120 | // The orchestrator runs ONE MicroVM, calls `hello` over RPC, and GETs the |
| 121 | // `/echo` fetch route — both against the in-VM server's endpoint — then |
| 122 | // terminates it. We issue a single request (the handler waits for RUNNING |
| 123 | // internally) and retry only transport errors, NOT 500s: a 500 is a real |
| 124 | // failure to surface, and retrying it would spawn more MicroVMs. |
| 125 | const res = yield* client.post(`${baseUrl}/rpc?message=world`).pipe( |
| 126 | Effect.timeout("150 seconds"), |
| 127 | Effect.retry({ |
| 128 | schedule: Schedule.exponential("1 second"), |
| 129 | times: 3, |
| 130 | }), |
| 131 | Effect.orDie, |
| 132 | ); |
| 133 | if (res.status !== 200) { |
| 134 | const text = yield* res.text.pipe(Effect.orDie); |
| 135 | return yield* Effect.die(new Error(`/rpc ${res.status}: ${text}`)); |
| 136 | } |
| 137 | const body = (yield* res.json.pipe(Effect.orDie)) as { |
| 138 | reply: string; |
| 139 | echo: string; |
| 140 | }; |
| 141 | expect(body.reply).toBe("hello, world!"); |
| 142 | expect(body.echo).toBe("world"); |
| 143 | }).pipe(logLevel), |
| 144 | { timeout: TEST_TIMEOUT }, |
| 145 | ); |
| 146 | }, |
| 147 | ); |
| 148 | |
| 149 | /** |
| 150 | * Cross-cloud MicroVM: the same bundled image driven from a Cloudflare Worker |
| 151 | * that reaches AWS via assume-role credentials minted by the binding. Gated |
| 152 | * separately because it needs real Cloudflare credentials. |
| 153 | */ |
| 154 | describe.skipIf(skipCloudflare)( |
| 155 | "effectful microvm (Cloudflare Worker host)", |
| 156 | { |
| 157 | tags: [ |
| 158 | "provider:aws", |
| 159 | "provider:aws:iam", |
| 160 | "provider:aws:lambda", |
| 161 | "provider:cloudflare", |
| 162 | "provider:cloudflare:worker", |
| 163 | "live", |
| 164 | ], |
| 165 | }, |
| 166 | () => { |
| 167 | const stack = beforeAll(deploy(CloudflareStack), { timeout: HOOK_TIMEOUT }); |
| 168 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(CloudflareStack), { |
| 169 | timeout: HOOK_TIMEOUT, |
| 170 | }); |
| 171 | |
| 172 | test( |
| 173 | "drives the MicroVM from a Cloudflare Worker (cross-cloud assume-role)", |
| 174 | Effect.gen(function* () { |
| 175 | const { workerUrl } = yield* stack; |
| 176 | const baseUrl = workerUrl.replace(/\/+$/, ""); |
| 177 | const client = yield* HttpClient.HttpClient; |
| 178 | |
| 179 | // Same `/rpc` flow as the Lambda host, but the orchestrator is a |
| 180 | // Cloudflare Worker reaching AWS via the IAM User → assume-role |
| 181 | // credentials minted by the binding. A single request (waits for RUNNING |
| 182 | // internally); retry only transport errors, not 500s. |
| 183 | const res = yield* client.post(`${baseUrl}/rpc?message=cloudflare`).pipe( |
| 184 | Effect.timeout("150 seconds"), |
| 185 | Effect.retry({ |
| 186 | schedule: Schedule.exponential("1 second"), |
| 187 | times: 3, |
| 188 | }), |
| 189 | Effect.orDie, |
| 190 | ); |
| 191 | if (res.status !== 200) { |
| 192 | const text = yield* res.text.pipe(Effect.orDie); |
| 193 | return yield* Effect.die(new Error(`/rpc ${res.status}: ${text}`)); |
| 194 | } |
| 195 | const body = (yield* res.json.pipe(Effect.orDie)) as { |
| 196 | reply: string; |
| 197 | echo: string; |
| 198 | }; |
| 199 | expect(body.reply).toBe("hello, cloudflare!"); |
| 200 | expect(body.echo).toBe("cloudflare"); |
| 201 | }).pipe(logLevel), |
| 202 | { timeout: TEST_TIMEOUT }, |
| 203 | ); |
| 204 | }, |
| 205 | ); |
| 206 | |
| 207 | /** |
| 208 | * External MicroVM (`context`/`dockerfile`): Alchemy zips the build context and |
| 209 | * AWS runs the user's Dockerfile server-side. |
| 210 | */ |
| 211 | describe.skipIf(skip)( |
| 212 | "external microvm (context/dockerfile)", |
| 213 | { |
| 214 | tags: ["provider:aws", "provider:aws:iam", "provider:aws:lambda", "live"], |
| 215 | }, |
| 216 | () => { |
| 217 | const stack = beforeAll(deploy(ExternalStack), { timeout: HOOK_TIMEOUT }); |
| 218 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(ExternalStack), { |
| 219 | timeout: HOOK_TIMEOUT, |
| 220 | }); |
| 221 | |
| 222 | test( |
| 223 | "builds the image from a Dockerfile + context", |
| 224 | Effect.gen(function* () { |
| 225 | // The stack outputs reflect the resolved image — a `CREATED` state proves |
| 226 | // the server-side build of the user Dockerfile succeeded. |
| 227 | const { imageArn, state } = yield* stack; |
| 228 | expect(state).toBe("CREATED"); |
| 229 | expect(imageArn).toContain("microvm-image"); |
| 230 | }).pipe(logLevel), |
| 231 | { timeout: TEST_TIMEOUT }, |
| 232 | ); |
| 233 | }, |
| 234 | ); |