The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Floci local-emulator smoke test. |
| 3 | * |
| 4 | * Deploys real AWS resources (S3 Bucket, SQS Queue, DynamoDB Table) through |
| 5 | * the regular live providers, but pointed at a locally-running floci emulator |
| 6 | * via the floci-scoped environment the local AWS providers carry. |
| 7 | * |
| 8 | * ## How local mode is activated |
| 9 | * |
| 10 | * The AWS auth method is normally chosen by the profile entry in |
| 11 | * `~/.alchemy/profiles.json` (written by `alchemy profile edit`). Tests |
| 12 | * must not depend on the developer's on-disk profiles. `Test.make({ dev: |
| 13 | * true })` runs the stack the way `alchemy dev` does, so the local AWS |
| 14 | * providers are selected; they carry their own floci-scoped environment, |
| 15 | * which resolves dummy credentials (`test`/`test`), accountId |
| 16 | * `000000000000`, and endpoint `http://localhost:4566`, and `ensureFloci()` |
| 17 | * guarantees the emulator is serving (starting the `alchemy-floci` |
| 18 | * container if needed). Without `dev: true` the live providers run against |
| 19 | * whatever account the profile resolves — the real cloud. |
| 20 | * |
| 21 | * ## Why no real-AWS calls can happen |
| 22 | * |
| 23 | * The floci environment's credentials are hardcoded dummies and its accountId |
| 24 | * is fixed — it never calls STS. Every SDK call carries the emulator endpoint |
| 25 | * (via `Endpoint.fromEnvironment`); if any call ever escaped to real AWS it |
| 26 | * would fail auth immediately (the dummy keys exist in no real account). |
| 27 | * |
| 28 | * Requires Docker (floci runs as a container); skipped when the Docker |
| 29 | * daemon is unavailable. |
| 30 | */ |
| 31 | import * as AWS from "@alchemy.run/aws"; |
| 32 | import { Table } from "@alchemy.run/aws/DynamoDB"; |
| 33 | import { Bucket } from "@alchemy.run/aws/S3"; |
| 34 | import { Queue } from "@alchemy.run/aws/SQS"; |
| 35 | import { expect } from "@alchemy.run/test"; |
| 36 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 37 | import * as Effect from "effect/Effect"; |
| 38 | import * as HttpBody from "effect/http/HttpBody"; |
| 39 | import * as HttpClient from "effect/http/HttpClient"; |
| 40 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 41 | |
| 42 | const FLOCI_ENDPOINT = "http://localhost:4566"; |
| 43 | |
| 44 | const providers = AWS.providers(); |
| 45 | |
| 46 | // `dev: true` runs the same topology as the real `alchemy dev` command |
| 47 | // (including the RPC sidecar default for RPC-backed providers). |
| 48 | const { test } = Test.make({ providers, dev: true }); |
| 49 | |
| 50 | /** |
| 51 | * Raw (non-distilled) call against the emulator gateway — out-of-band proof |
| 52 | * that a resource exists IN THE EMULATOR, not the real cloud. The dummy |
| 53 | * SigV4 Authorization header is never verified by the emulator; it only |
| 54 | * carries the service/region/account scoping the gateway routes by. |
| 55 | */ |
| 56 | const rawAwsJson = Effect.fn(function* (options: { |
| 57 | service: string; |
| 58 | region: string; |
| 59 | target: string; |
| 60 | contentType: string; |
| 61 | body: Record<string, unknown>; |
| 62 | }) { |
| 63 | const client = yield* HttpClient.HttpClient; |
| 64 | return yield* client.execute( |
| 65 | HttpClientRequest.post(`${FLOCI_ENDPOINT}/`).pipe( |
| 66 | HttpClientRequest.setHeaders({ |
| 67 | "content-type": options.contentType, |
| 68 | "x-amz-target": options.target, |
| 69 | "x-amz-date": "20260101T000000Z", |
| 70 | authorization: `AWS4-HMAC-SHA256 Credential=test/20260101/${options.region}/${options.service}/aws4_request, SignedHeaders=host;x-amz-date, Signature=dummy`, |
| 71 | }), |
| 72 | HttpClientRequest.setBody(HttpBody.text(JSON.stringify(options.body), options.contentType)), |
| 73 | ), |
| 74 | ); |
| 75 | }); |
| 76 | |
| 77 | /** Path-style S3 GET against the gateway (list the bucket's objects). */ |
| 78 | const rawS3GetBucket = Effect.fn(function* (bucketName: string) { |
| 79 | const client = yield* HttpClient.HttpClient; |
| 80 | return yield* client.execute( |
| 81 | HttpClientRequest.get(`${FLOCI_ENDPOINT}/${bucketName}`).pipe( |
| 82 | HttpClientRequest.setHeaders({ |
| 83 | "x-amz-date": "20260101T000000Z", |
| 84 | authorization: |
| 85 | "AWS4-HMAC-SHA256 Credential=test/20260101/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-date, Signature=dummy", |
| 86 | }), |
| 87 | ), |
| 88 | ); |
| 89 | }); |
| 90 | |
| 91 | /** Region segment of an ARN (`arn:aws:sqs:REGION:ACCOUNT:name`). */ |
| 92 | const regionOfArn = (arn: string) => arn.split(":")[3]!; |
| 93 | |
| 94 | test.provider( |
| 95 | "deploys S3 + SQS + DynamoDB into floci and destroys them", |
| 96 | (stack) => |
| 97 | Effect.gen(function* () { |
| 98 | yield* stack.destroy(); |
| 99 | |
| 100 | const outputs = yield* stack.deploy( |
| 101 | Effect.gen(function* () { |
| 102 | const bucket = yield* Bucket("FlociBucket"); |
| 103 | const queue = yield* Queue("FlociQueue"); |
| 104 | const table = yield* Table("FlociTable", { |
| 105 | partitionKey: "id", |
| 106 | attributes: { id: "S" }, |
| 107 | }); |
| 108 | return { bucket, queue, table }; |
| 109 | }), |
| 110 | ); |
| 111 | |
| 112 | // Attributes prove the providers ran their full reconcile loops. The |
| 113 | // dummy-account ARNs double as proof no real AWS account was involved. |
| 114 | expect(outputs.bucket.bucketName).toBeDefined(); |
| 115 | expect(outputs.bucket.bucketArn).toContain(outputs.bucket.bucketName); |
| 116 | expect(outputs.queue.queueUrl).toContain(outputs.queue.queueName); |
| 117 | expect(outputs.queue.queueArn).toContain(":000000000000:"); |
| 118 | expect(outputs.table.tableArn).toContain(":000000000000:"); |
| 119 | |
| 120 | const region = regionOfArn(outputs.queue.queueArn); |
| 121 | |
| 122 | // --- Out-of-band verification: raw HTTP against the emulator gateway, |
| 123 | // no distilled SDK, no alchemy layers. If the resources had gone to |
| 124 | // real AWS these calls would not find them here. |
| 125 | const listQueues = yield* rawAwsJson({ |
| 126 | service: "sqs", |
| 127 | region, |
| 128 | target: "AmazonSQS.ListQueues", |
| 129 | contentType: "application/x-amz-json-1.0", |
| 130 | body: {}, |
| 131 | }); |
| 132 | expect(listQueues.status).toBe(200); |
| 133 | const queues = (yield* listQueues.json) as { QueueUrls?: string[] }; |
| 134 | expect(queues.QueueUrls?.some((url) => url.endsWith(`/${outputs.queue.queueName}`))).toBe( |
| 135 | true, |
| 136 | ); |
| 137 | |
| 138 | const describeTable = yield* rawAwsJson({ |
| 139 | service: "dynamodb", |
| 140 | region: regionOfArn(outputs.table.tableArn), |
| 141 | target: "DynamoDB_20120810.DescribeTable", |
| 142 | contentType: "application/x-amz-json-1.0", |
| 143 | body: { TableName: outputs.table.tableName }, |
| 144 | }); |
| 145 | expect(describeTable.status).toBe(200); |
| 146 | const described = (yield* describeTable.json) as { |
| 147 | Table?: { TableArn?: string }; |
| 148 | }; |
| 149 | expect(described.Table?.TableArn).toBe(outputs.table.tableArn); |
| 150 | |
| 151 | const getBucket = yield* rawS3GetBucket(outputs.bucket.bucketName); |
| 152 | expect(getBucket.status).toBe(200); |
| 153 | |
| 154 | // --- Destroy and verify gone from the emulator. |
| 155 | yield* stack.destroy(); |
| 156 | |
| 157 | const listQueuesAfter = yield* rawAwsJson({ |
| 158 | service: "sqs", |
| 159 | region, |
| 160 | target: "AmazonSQS.ListQueues", |
| 161 | contentType: "application/x-amz-json-1.0", |
| 162 | body: {}, |
| 163 | }); |
| 164 | const queuesAfter = (yield* listQueuesAfter.json) as { |
| 165 | QueueUrls?: string[]; |
| 166 | }; |
| 167 | expect( |
| 168 | queuesAfter.QueueUrls?.some((url) => url.endsWith(`/${outputs.queue.queueName}`)) ?? false, |
| 169 | ).toBe(false); |
| 170 | |
| 171 | const describeTableAfter = yield* rawAwsJson({ |
| 172 | service: "dynamodb", |
| 173 | region: regionOfArn(outputs.table.tableArn), |
| 174 | target: "DynamoDB_20120810.DescribeTable", |
| 175 | contentType: "application/x-amz-json-1.0", |
| 176 | body: { TableName: outputs.table.tableName }, |
| 177 | }); |
| 178 | expect(describeTableAfter.status).toBe(400); // ResourceNotFoundException |
| 179 | |
| 180 | const getBucketAfter = yield* rawS3GetBucket(outputs.bucket.bucketName); |
| 181 | expect(getBucketAfter.status).toBe(404); // NoSuchBucket |
| 182 | }), |
| 183 | { |
| 184 | tags: ["provider:aws", "provider:aws:dynamodb", "provider:aws:s3", "provider:aws:sqs", "local"], |
| 185 | timeout: 240_000, |
| 186 | }, |
| 187 | ); |