The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Event-source-mapping conformance against the floci emulator, driven |
| 3 | * entirely through OUR client stack (distilled SDK + alchemy providers). |
| 4 | * |
| 5 | * Topology (all local, dev mode, no profile stub): |
| 6 | * - `AWS.SQS.Queue` + `AWS.S3.Bucket` + `AWS.IAM.Role` deploy through the |
| 7 | * dualized providers into floci; |
| 8 | * - a RAW floci Lambda is created via distilled `lambda.createFunction` |
| 9 | * against the emulator endpoint (the Lambda.Function RESOURCE is out of |
| 10 | * scope for F1) — its handler writes each consumed SQS message into the |
| 11 | * bucket; |
| 12 | * - the event source mapping is created via distilled |
| 13 | * `lambda.createEventSourceMapping` (the alchemy EventSourceMapping |
| 14 | * resource is NOT dualized: its ownership scan needs lambda ListTags on |
| 15 | * `event-source-mapping:` ARNs, which floci rejects — see the F1 |
| 16 | * fork-patch candidates); |
| 17 | * - `sqs.sendMessage` through distilled, then a bounded poll for the |
| 18 | * object the function writes — pinning floci's local event pumping |
| 19 | * (ESM poller -> containerized Lambda -> S3 write-back) end to end. |
| 20 | * |
| 21 | * Requires Docker (floci + the Lambda runtime container); skipped when the |
| 22 | * daemon is unavailable. |
| 23 | */ |
| 24 | import * as AWS from "@alchemy.run/aws"; |
| 25 | import * as Endpoint from "@alchemy.run/aws/Endpoint"; |
| 26 | import * as Region from "@alchemy.run/aws/Region"; |
| 27 | import { expect } from "@alchemy.run/test"; |
| 28 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 29 | import { Credentials } from "@distilled.cloud/aws/Credentials"; |
| 30 | import * as Lambda from "@distilled.cloud/aws/lambda"; |
| 31 | import type { RegionName } from "@distilled.cloud/aws/Region"; |
| 32 | import * as SQS from "@distilled.cloud/aws/sqs"; |
| 33 | import { zipCode } from "alchemy/Util/zip"; |
| 34 | import * as Effect from "effect/Effect"; |
| 35 | import * as Layer from "effect/Layer"; |
| 36 | import * as Redacted from "effect/Redacted"; |
| 37 | import * as Schedule from "effect/Schedule"; |
| 38 | import { FLOCI_ENDPOINT, rawS3GetObject } from "./fixtures/raw.ts"; |
| 39 | |
| 40 | const { test } = Test.make({ providers: AWS.providers(), dev: true }); |
| 41 | |
| 42 | /** |
| 43 | * Floci-scoped context for the raw distilled calls this test makes itself |
| 44 | * (create/delete the Lambda function, send the SQS message) — the same |
| 45 | * client stack the wrapped local providers use. |
| 46 | */ |
| 47 | const flociContext = Layer.mergeAll( |
| 48 | Endpoint.of(FLOCI_ENDPOINT), |
| 49 | Region.of("us-east-1"), |
| 50 | Layer.succeed( |
| 51 | Credentials, |
| 52 | Effect.succeed({ |
| 53 | accessKeyId: Redacted.make("test"), |
| 54 | secretAccessKey: Redacted.make("test"), |
| 55 | sessionToken: undefined, |
| 56 | region: "us-east-1" as RegionName, |
| 57 | }), |
| 58 | ), |
| 59 | ); |
| 60 | |
| 61 | const FUNCTION_NAME = "alchemy-local-esm-consumer"; |
| 62 | const MARKER_KEY = "consumed-marker.txt"; |
| 63 | |
| 64 | /** |
| 65 | * nodejs20.x handler: writes each consumed SQS record into the target |
| 66 | * bucket under the key carried in the message body. The Lambda runtime |
| 67 | * container gets `AWS_ENDPOINT_URL` + placeholder credentials from floci, |
| 68 | * so the bundled AWS SDK v3 targets the emulator; path-style is forced |
| 69 | * because virtual-host bucket DNS does not resolve locally. |
| 70 | */ |
| 71 | const handlerSource = /* js */ ` |
| 72 | import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3"; |
| 73 | const s3 = new S3Client({ |
| 74 | endpoint: process.env.AWS_ENDPOINT_URL, |
| 75 | forcePathStyle: true, |
| 76 | }); |
| 77 | export const handler = async (event) => { |
| 78 | for (const record of event.Records ?? []) { |
| 79 | await s3.send( |
| 80 | new PutObjectCommand({ |
| 81 | Bucket: process.env.TARGET_BUCKET, |
| 82 | Key: record.body, |
| 83 | Body: record.messageId, |
| 84 | }), |
| 85 | ); |
| 86 | } |
| 87 | return { batchItemFailures: [] }; |
| 88 | }; |
| 89 | `; |
| 90 | |
| 91 | const assumeRolePolicy = { |
| 92 | Version: "2012-10-17" as const, |
| 93 | Statement: [ |
| 94 | { |
| 95 | Effect: "Allow" as const, |
| 96 | Principal: { Service: "lambda.amazonaws.com" }, |
| 97 | Action: ["sts:AssumeRole"], |
| 98 | }, |
| 99 | ], |
| 100 | }; |
| 101 | |
| 102 | test.provider( |
| 103 | "SQS -> raw floci Lambda -> S3 event pumping through our client stack", |
| 104 | (stack) => |
| 105 | Effect.gen(function* () { |
| 106 | yield* stack.destroy(); |
| 107 | |
| 108 | // Phase 1: the data plane the function needs (all floci-local). |
| 109 | const base = yield* stack.deploy( |
| 110 | Effect.gen(function* () { |
| 111 | const queue = yield* AWS.SQS.Queue("EsmQueue"); |
| 112 | const bucket = yield* AWS.S3.Bucket("EsmTarget", { |
| 113 | forceDestroy: true, |
| 114 | }); |
| 115 | const role = yield* AWS.IAM.Role("EsmRole", { |
| 116 | assumeRolePolicyDocument: assumeRolePolicy, |
| 117 | }); |
| 118 | return { queue, bucket, role }; |
| 119 | }), |
| 120 | ); |
| 121 | expect(base.queue.queueArn).toContain(":000000000000:"); |
| 122 | expect(base.bucket.bucketName).toBeDefined(); |
| 123 | |
| 124 | // Pre-clean: the emulator container is long-lived, so a previous |
| 125 | // interrupted run may have left the function (with a stale |
| 126 | // TARGET_BUCKET env) and event source mappings whose pollers spin on |
| 127 | // deleted queues. Delete mappings first, then the function. |
| 128 | const leftover = yield* Lambda.listEventSourceMappings({ |
| 129 | FunctionName: FUNCTION_NAME, |
| 130 | }).pipe( |
| 131 | Effect.catchTag("ResourceNotFoundException", () => |
| 132 | Effect.succeed({ EventSourceMappings: [] }), |
| 133 | ), |
| 134 | Effect.provide(flociContext), |
| 135 | ); |
| 136 | yield* Effect.forEach(leftover.EventSourceMappings ?? [], (mapping) => |
| 137 | mapping.UUID |
| 138 | ? Lambda.deleteEventSourceMapping({ UUID: mapping.UUID }).pipe( |
| 139 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 140 | Effect.provide(flociContext), |
| 141 | ) |
| 142 | : Effect.void, |
| 143 | ); |
| 144 | yield* Lambda.deleteFunction({ FunctionName: FUNCTION_NAME }).pipe( |
| 145 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 146 | Effect.provide(flociContext), |
| 147 | ); |
| 148 | |
| 149 | // Raw floci Lambda via distilled against the emulator endpoint. |
| 150 | const archive = yield* zipCode(handlerSource); |
| 151 | yield* Lambda.createFunction({ |
| 152 | FunctionName: FUNCTION_NAME, |
| 153 | Runtime: "nodejs20.x", |
| 154 | Handler: "index.handler", |
| 155 | Role: base.role.roleArn, |
| 156 | Code: { ZipFile: archive }, |
| 157 | Timeout: 30, |
| 158 | Environment: { |
| 159 | Variables: { TARGET_BUCKET: base.bucket.bucketName }, |
| 160 | }, |
| 161 | }).pipe(Effect.provide(flociContext)); |
| 162 | |
| 163 | // Wait (bounded) for the function to be Active before wiring the ESM. |
| 164 | const fn = yield* Lambda.getFunction({ |
| 165 | FunctionName: FUNCTION_NAME, |
| 166 | }).pipe( |
| 167 | Effect.provide(flociContext), |
| 168 | Effect.repeat({ |
| 169 | schedule: Schedule.spaced("2 seconds"), |
| 170 | until: (res): boolean => res.Configuration?.State === "Active", |
| 171 | times: 30, |
| 172 | }), |
| 173 | ); |
| 174 | expect(fn.Configuration?.State).toBe("Active"); |
| 175 | |
| 176 | // Wire queue -> fn via distilled against the emulator (our client |
| 177 | // stack end to end). |
| 178 | const esm = yield* Lambda.createEventSourceMapping({ |
| 179 | FunctionName: FUNCTION_NAME, |
| 180 | EventSourceArn: base.queue.queueArn, |
| 181 | BatchSize: 1, |
| 182 | Enabled: true, |
| 183 | }).pipe(Effect.provide(flociContext)); |
| 184 | expect(esm.UUID).toBeDefined(); |
| 185 | |
| 186 | // Send through OUR client stack (distilled sqs against the emulator). |
| 187 | yield* SQS.sendMessage({ |
| 188 | QueueUrl: base.queue.queueUrl, |
| 189 | MessageBody: MARKER_KEY, |
| 190 | }).pipe(Effect.provide(flociContext)); |
| 191 | |
| 192 | // Bounded poll: floci's ESM poller must deliver the message to the |
| 193 | // containerized function, whose S3 write-back proves consumption. |
| 194 | const consumed = yield* rawS3GetObject(base.bucket.bucketName, MARKER_KEY).pipe( |
| 195 | Effect.repeat({ |
| 196 | schedule: Schedule.spaced("3 seconds"), |
| 197 | until: (res): boolean => res.status === 200, |
| 198 | times: 40, |
| 199 | }), |
| 200 | ); |
| 201 | expect(consumed.status).toBe(200); |
| 202 | |
| 203 | // Cleanup: remove the raw mapping + function from the emulator, then |
| 204 | // destroy the stack (queue, bucket incl. the object, role). |
| 205 | if (esm.UUID) { |
| 206 | yield* Lambda.deleteEventSourceMapping({ UUID: esm.UUID }).pipe( |
| 207 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 208 | Effect.provide(flociContext), |
| 209 | ); |
| 210 | } |
| 211 | yield* Lambda.deleteFunction({ FunctionName: FUNCTION_NAME }).pipe( |
| 212 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 213 | Effect.provide(flociContext), |
| 214 | ); |
| 215 | yield* stack.destroy(); |
| 216 | }), |
| 217 | { |
| 218 | tags: [ |
| 219 | "provider:aws", |
| 220 | "provider:aws:iam", |
| 221 | "provider:aws:lambda", |
| 222 | "provider:aws:s3", |
| 223 | "provider:aws:sqs", |
| 224 | "local", |
| 225 | ], |
| 226 | timeout: 540_000, |
| 227 | }, |
| 228 | ); |