The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * True `alchemy dev` end-to-end for AWS: spawns the REAL CLI and drives |
| 3 | * every local binding over HTTP against the floci emulator. |
| 4 | * |
| 5 | * This is a different code path from the `Test.make({ dev: true })` harness |
| 6 | * suites under packages/integration-tests/test/AWS — the harness mirrors the RPC |
| 7 | * sidecar topology, but only this test covers the CLI itself: arg parsing, |
| 8 | * the `bin/exec` child under `--watch`, the `ALCHEMY_RPC_SPAWNER_URL` |
| 9 | * handshake, and provider sidecars whose lifetime is tied to the CLI |
| 10 | * process (the #1007 bug class, mirrored from |
| 11 | * examples/cloudflare-dev/test/dev.test.ts). |
| 12 | * |
| 13 | * Coverage, all against floci (no cloud credentials, no cloud resources): |
| 14 | * - Function URL → `/` serves through the emulator's URL proxy |
| 15 | * - env vars → `/` echoes MY_VARIABLE via effect/Config |
| 16 | * - S3 binding → `/s3` PutObject/GetObject roundtrip |
| 17 | * - DynamoDB binding → `/dynamo` PutItem/GetItem roundtrip |
| 18 | * - SQS + consumer → `/queue/send` produces; floci's ESM poller |
| 19 | * delivers to `consumeQueueMessages`, which records |
| 20 | * into the table read back by `/queue/messages` |
| 21 | * - SNS + consumer → `/topic/send` publishes; the lambda-protocol |
| 22 | * Subscription + invoke Permission (glue that must |
| 23 | * be created ON the emulator, not real AWS) deliver |
| 24 | * to `consumeTopicNotifications`, read back by |
| 25 | * `/topic/messages` |
| 26 | * - DynamoDB Streams → `/items` writes; the table stream + its |
| 27 | * EventSourceMapping deliver the change record to |
| 28 | * `consumeTableChanges`, read back by `/changes` |
| 29 | * - Action data plane → the deploy-time SeedObject Action put/gets an |
| 30 | * object through the S3 bindings inside the CLI |
| 31 | * process; the stack outputs carry the dummy |
| 32 | * account id (000000000000) proving the calls hit |
| 33 | * the emulator, not real AWS |
| 34 | * - HOT RELOAD → rewriting src/marker.ts (no redeploy) must serve |
| 35 | * the new marker; restoring it must swap back |
| 36 | */ |
| 37 | import { afterAll, expect, test } from "bun:test"; |
| 38 | import * as fs from "node:fs"; |
| 39 | import * as path from "node:path"; |
| 40 | import { DevCli, fetchOk } from "@alchemy.run/test/DevCli"; |
| 41 | |
| 42 | const root = path.resolve(import.meta.dirname, ".."); |
| 43 | const STAGE = "dev-cli-test"; |
| 44 | // Credential-free dev, deterministically: an isolated (unconfigured) |
| 45 | // alchemy profile plus stripped AWS env credentials keep this suite |
| 46 | // hermetic — it must pass on a machine with zero AWS configuration, and |
| 47 | // must never touch a developer's real profile. (Action data-plane calls |
| 48 | // are routed to the emulator per bound resource by the engine regardless |
| 49 | // of ambient credentials — see Binding.Service's data-plane routing.) |
| 50 | const cli = new DevCli({ |
| 51 | root, |
| 52 | stage: STAGE, |
| 53 | env: { |
| 54 | ALCHEMY_PROFILE: "aws-dev-cli-test", |
| 55 | AWS_ACCESS_KEY_ID: undefined, |
| 56 | AWS_SECRET_ACCESS_KEY: undefined, |
| 57 | AWS_SESSION_TOKEN: undefined, |
| 58 | AWS_PROFILE: undefined, |
| 59 | }, |
| 60 | }); |
| 61 | |
| 62 | const markerPath = path.join(root, "src", "marker.ts"); |
| 63 | const markerSource = fs.readFileSync(markerPath, "utf8"); |
| 64 | |
| 65 | afterAll(async () => { |
| 66 | // Always leave the repo tree clean, even on a mid-reload failure. |
| 67 | fs.writeFileSync(markerPath, markerSource); |
| 68 | |
| 69 | await cli.stop(); |
| 70 | if (!process.env.NO_DESTROY) { |
| 71 | cli.destroy(); |
| 72 | } |
| 73 | }, 180_000); |
| 74 | |
| 75 | test( |
| 76 | "alchemy dev serves every local AWS binding end-to-end with hot reload", |
| 77 | async () => { |
| 78 | cli.start(); |
| 79 | |
| 80 | // The first dev deploy may pull the floci image, provision the local |
| 81 | // data plane, and package the function before printing stack outputs. |
| 82 | const api = await cli.pollUntil("api url in stack outputs", () => cli.outputUrl("api"), { |
| 83 | tries: 300, |
| 84 | delayMs: 1000, |
| 85 | }); |
| 86 | |
| 87 | // Dev identity: the function URL is served locally, not by AWS |
| 88 | // (e.g. http://<id>.lambda-url.us-east-1.localhost:<port>/). The port |
| 89 | // is whatever the emulator bound — never hard-code it, only the URL |
| 90 | // captured from the CLI's stdout is authoritative. |
| 91 | expect(new URL(api).hostname).toEndWith("localhost"); |
| 92 | expect(api).not.toContain("amazonaws.com"); |
| 93 | |
| 94 | // Action data plane: the deploy-time SeedObject Action ran inside the |
| 95 | // CLI's exec process and put/get an object through the S3 bindings. |
| 96 | // The dummy account id proves the calls hit the emulator, not real |
| 97 | // AWS; the roundtripped body proves the put actually landed. |
| 98 | const seedAccount = await cli.pollUntil("seedAccount in stack outputs", () => |
| 99 | cli.outputValue("seedAccount"), |
| 100 | ); |
| 101 | expect(seedAccount).toBe("000000000000"); |
| 102 | const seedText = await cli.pollUntil("seedText in stack outputs", () => |
| 103 | cli.outputValue("seedText"), |
| 104 | ); |
| 105 | expect(seedText).toBe("seed-object-body-v1"); |
| 106 | |
| 107 | // Marker + env: the function reads MY_VARIABLE through effect/Config. |
| 108 | const home = (await (await fetchOk(api)).json()) as { |
| 109 | marker: string; |
| 110 | variable: string; |
| 111 | }; |
| 112 | expect(home.marker).toBe("aws-dev-marker-v1"); |
| 113 | expect(home.variable).toBe("my-variable-abc123"); |
| 114 | |
| 115 | // S3 binding: put/get roundtrip against the emulator. |
| 116 | const s3 = (await (await fetchOk(new URL("/s3", api))).json()) as { |
| 117 | text: string; |
| 118 | }; |
| 119 | expect(s3.text).toBe("hello from s3"); |
| 120 | |
| 121 | // DynamoDB binding: put/get roundtrip against the emulator. |
| 122 | const dynamo = (await (await fetchOk(new URL("/dynamo", api))).json()) as { |
| 123 | text: string | null; |
| 124 | }; |
| 125 | expect(dynamo.text).toBe("hello from dynamo"); |
| 126 | |
| 127 | // SQS: produce over the binding; floci's event-source-mapping poller |
| 128 | // delivers to the consumer, which records into the table. |
| 129 | const message = { id: crypto.randomUUID(), text: "hello from alchemy dev" }; |
| 130 | await fetchOk(new URL("/queue/send", api), { |
| 131 | method: "POST", |
| 132 | headers: { "content-type": "application/json" }, |
| 133 | body: JSON.stringify(message), |
| 134 | }); |
| 135 | const received = await cli.pollUntil( |
| 136 | "queue message to be consumed", |
| 137 | async () => { |
| 138 | const res = await fetch(new URL(`/queue/messages?id=${message.id}`, api)); |
| 139 | if (!res.ok) return undefined; |
| 140 | const { body } = (await res.json()) as { body: string | null }; |
| 141 | return body ?? undefined; |
| 142 | }, |
| 143 | { tries: 60, delayMs: 500 }, |
| 144 | ); |
| 145 | expect(JSON.parse(received)).toEqual(message); |
| 146 | |
| 147 | // SNS: publish over the binding. Delivery runs through the |
| 148 | // lambda-protocol Subscription and the invoke Permission — the glue |
| 149 | // resources that regressed by being created against real AWS with |
| 150 | // emulator ARNs (InvalidParameterException: TopicArn / Function not |
| 151 | // found). Consumption proves both were created on the emulator. |
| 152 | const notification = { id: crypto.randomUUID(), text: "hello from sns" }; |
| 153 | await fetchOk(new URL("/topic/send", api), { |
| 154 | method: "POST", |
| 155 | headers: { "content-type": "application/json" }, |
| 156 | body: JSON.stringify(notification), |
| 157 | }); |
| 158 | const delivered = await cli.pollUntil( |
| 159 | "topic notification to be consumed", |
| 160 | async () => { |
| 161 | const res = await fetch(new URL(`/topic/messages?id=${notification.id}`, api)); |
| 162 | if (!res.ok) return undefined; |
| 163 | const { body } = (await res.json()) as { body: string | null }; |
| 164 | return body ?? undefined; |
| 165 | }, |
| 166 | { tries: 60, delayMs: 500 }, |
| 167 | ); |
| 168 | expect(JSON.parse(delivered)).toEqual(notification); |
| 169 | |
| 170 | // DynamoDB Streams: write a plain item; the table stream's |
| 171 | // EventSourceMapping delivers the change record to |
| 172 | // `consumeTableChanges`, which records it under `change:<id>`. |
| 173 | const itemId = crypto.randomUUID(); |
| 174 | await fetchOk(new URL("/items", api), { |
| 175 | method: "POST", |
| 176 | headers: { "content-type": "application/json" }, |
| 177 | body: JSON.stringify({ id: itemId }), |
| 178 | }); |
| 179 | const change = await cli.pollUntil( |
| 180 | "table change record to be consumed", |
| 181 | async () => { |
| 182 | const res = await fetch(new URL(`/changes?id=${itemId}`, api)); |
| 183 | if (!res.ok) return undefined; |
| 184 | const { body } = (await res.json()) as { body: string | null }; |
| 185 | return body ?? undefined; |
| 186 | }, |
| 187 | { tries: 60, delayMs: 500 }, |
| 188 | ); |
| 189 | expect(change).toBe("INSERT"); |
| 190 | |
| 191 | // ── HOT RELOAD: rewrite src/marker.ts with the CLI still running. The |
| 192 | // `--watch` exec child re-runs, the dev provider hot-swaps the function |
| 193 | // code in floci, and the SAME url serves the new marker — no deploy ── |
| 194 | fs.writeFileSync(markerPath, markerSource.replace("aws-dev-marker-v1", "aws-dev-marker-v2")); |
| 195 | await cli.pollUntil( |
| 196 | "hot-swapped marker v2", |
| 197 | async () => { |
| 198 | try { |
| 199 | const res = await fetch(api); |
| 200 | if (!res.ok) return undefined; |
| 201 | const { marker } = (await res.json()) as { marker: string }; |
| 202 | return marker === "aws-dev-marker-v2" ? marker : undefined; |
| 203 | } catch { |
| 204 | return undefined; // mid-swap |
| 205 | } |
| 206 | }, |
| 207 | { tries: 240, delayMs: 500 }, |
| 208 | ); |
| 209 | |
| 210 | // Bindings survived the reload: the table still holds the consumed |
| 211 | // message and the S3 roundtrip still works. |
| 212 | const afterReload = (await ( |
| 213 | await fetchOk(new URL(`/queue/messages?id=${message.id}`, api)) |
| 214 | ).json()) as { body: string | null }; |
| 215 | expect(afterReload.body).toBe(JSON.stringify(message)); |
| 216 | const s3After = (await (await fetchOk(new URL("/s3", api))).json()) as { |
| 217 | text: string; |
| 218 | }; |
| 219 | expect(s3After.text).toBe("hello from s3"); |
| 220 | |
| 221 | // Restore the marker — the swap back is itself a second hot reload, |
| 222 | // and leaves the checked-in tree clean. |
| 223 | fs.writeFileSync(markerPath, markerSource); |
| 224 | await cli.pollUntil( |
| 225 | "restored marker v1", |
| 226 | async () => { |
| 227 | try { |
| 228 | const res = await fetch(api); |
| 229 | if (!res.ok) return undefined; |
| 230 | const { marker } = (await res.json()) as { marker: string }; |
| 231 | return marker === "aws-dev-marker-v1" ? marker : undefined; |
| 232 | } catch { |
| 233 | return undefined; // mid-swap |
| 234 | } |
| 235 | }, |
| 236 | { tries: 240, delayMs: 500 }, |
| 237 | ); |
| 238 | }, |
| 239 | { timeout: 600_000 }, |
| 240 | ); |