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 { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as logs from "@distilled.cloud/aws/cloudwatch-logs"; |
| 5 | import * as SQS from "@distilled.cloud/aws/sqs"; |
| 6 | import * as Clock from "effect/Clock"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import LogGroupEventSourceFunctionLive, { |
| 12 | LogGroupEventSourceFunction, |
| 13 | } from "./event-source-handler.ts"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: AWS.providers() }); |
| 16 | |
| 17 | class FunctionNotReady extends Data.TaggedError("FunctionNotReady")<{ message: string }> { |
| 18 | constructor(message: string) { |
| 19 | super({ message }); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | class MessageNotDelivered extends Data.TaggedError("MessageNotDelivered") {} |
| 24 | |
| 25 | describe.sequential( |
| 26 | "AWS.Logs.LogGroupEventSource", |
| 27 | { |
| 28 | tags: ["provider:aws", "provider:aws:lambda", "provider:aws:logs", "provider:aws:sqs", "live"], |
| 29 | }, |
| 30 | () => { |
| 31 | test.provider( |
| 32 | "delivers decoded log events through the Lambda event source", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | const fn = yield* stack.deploy( |
| 38 | LogGroupEventSourceFunction.pipe(Effect.provide(LogGroupEventSourceFunctionLive)), |
| 39 | ); |
| 40 | |
| 41 | const functionUrl = fn.functionUrl!; |
| 42 | |
| 43 | // First request rides out cold-start / URL propagation; keep polling |
| 44 | // until the fixture reports its identifiers. |
| 45 | const { sourceLogGroupName, resultQueueUrl } = yield* HttpClient.get(functionUrl).pipe( |
| 46 | Effect.flatMap((response) => |
| 47 | response.status === 200 |
| 48 | ? (response.json as Effect.Effect<{ |
| 49 | sourceLogGroupName?: string; |
| 50 | resultQueueUrl?: string; |
| 51 | }>) |
| 52 | : Effect.fail(new FunctionNotReady(`Function not ready: ${response.status}`)), |
| 53 | ), |
| 54 | Effect.flatMap((body) => |
| 55 | body.sourceLogGroupName && body.resultQueueUrl |
| 56 | ? Effect.succeed(body as { sourceLogGroupName: string; resultQueueUrl: string }) |
| 57 | : Effect.fail(new FunctionNotReady("Function returned empty identifiers")), |
| 58 | ), |
| 59 | Effect.retry({ |
| 60 | schedule: Schedule.max([Schedule.fixed("1 seconds"), Schedule.recurs(60)]), |
| 61 | }), |
| 62 | ); |
| 63 | |
| 64 | // Deploy-shape assertion (ungated): the subscription filter targeting |
| 65 | // the Lambda exists on the source log group. |
| 66 | const described = yield* logs.describeSubscriptionFilters({ |
| 67 | logGroupName: sourceLogGroupName, |
| 68 | }); |
| 69 | const filter = (described.subscriptionFilters ?? []).find((f) => |
| 70 | f.destinationArn?.includes(fn.functionName), |
| 71 | ); |
| 72 | expect(filter).toBeDefined(); |
| 73 | expect(filter?.destinationArn).toContain(":function:"); |
| 74 | |
| 75 | // Write a marker log event into the source group out-of-band; the |
| 76 | // subscription delivers it to the Lambda, which decodes the gzipped |
| 77 | // payload and forwards the message to the result queue. |
| 78 | const marker = `log-event-source-${crypto.randomUUID()}`; |
| 79 | const logStreamName = "alchemy-test-event-source-stream"; |
| 80 | yield* logs |
| 81 | .createLogStream({ logGroupName: sourceLogGroupName, logStreamName }) |
| 82 | .pipe(Effect.catchTag("ResourceAlreadyExistsException", () => Effect.void)); |
| 83 | const timestamp = yield* Clock.currentTimeMillis; |
| 84 | yield* logs.putLogEvents({ |
| 85 | logGroupName: sourceLogGroupName, |
| 86 | logStreamName, |
| 87 | logEvents: [{ timestamp, message: marker }], |
| 88 | }); |
| 89 | |
| 90 | // Poll the result queue until the forwarded marker shows up. |
| 91 | // Subscription delivery latency is typically a few seconds; bounded at |
| 92 | // ~30 polls × (2s long-poll + 1s spacing) ≈ 90s. |
| 93 | const received = yield* Effect.gen(function* () { |
| 94 | const result = yield* SQS.receiveMessage({ |
| 95 | QueueUrl: resultQueueUrl, |
| 96 | MaxNumberOfMessages: 10, |
| 97 | WaitTimeSeconds: 2, |
| 98 | }); |
| 99 | const match = (result.Messages ?? []).find((message) => message.Body === marker); |
| 100 | if (!match?.ReceiptHandle) { |
| 101 | return yield* Effect.fail(new MessageNotDelivered()); |
| 102 | } |
| 103 | yield* SQS.deleteMessage({ |
| 104 | QueueUrl: resultQueueUrl, |
| 105 | ReceiptHandle: match.ReceiptHandle, |
| 106 | }); |
| 107 | return match.Body!; |
| 108 | }).pipe( |
| 109 | Effect.retry({ |
| 110 | while: (error) => error._tag === "MessageNotDelivered", |
| 111 | schedule: Schedule.max([Schedule.fixed("1 seconds"), Schedule.recurs(30)]), |
| 112 | }), |
| 113 | ); |
| 114 | |
| 115 | expect(received).toEqual(marker); |
| 116 | |
| 117 | yield* stack.destroy(); |
| 118 | |
| 119 | // The subscription filter is deleted with the log group. |
| 120 | const afterDestroy = yield* logs |
| 121 | .describeSubscriptionFilters({ logGroupName: sourceLogGroupName }) |
| 122 | .pipe( |
| 123 | Effect.catchTag("ResourceNotFoundException", () => |
| 124 | Effect.succeed({ subscriptionFilters: [] }), |
| 125 | ), |
| 126 | ); |
| 127 | expect(afterDestroy.subscriptionFilters ?? []).toHaveLength(0); |
| 128 | }), |
| 129 | { timeout: 240_000 }, |
| 130 | ); |
| 131 | }, |
| 132 | ); |