The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 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 Lambda from "@distilled.cloud/aws/lambda"; |
| 5 | import * as SQS from "@distilled.cloud/aws/sqs"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import QueueEventSourceFunctionLive, { QueueEventSourceFunction } from "./event-source-handler.ts"; |
| 11 | import InlineQueueEventSourceFunction from "./inline-event-source-handler.ts"; |
| 12 | import PartialFailureFunctionLive, { PartialFailureFunction } from "./partial-failure-handler.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: AWS.providers() }); |
| 15 | |
| 16 | describe.sequential( |
| 17 | "AWS.SQS.QueueEventSource", |
| 18 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:sqs", "live"] }, |
| 19 | () => { |
| 20 | // `fn` is deployed by the caller so both the class and the inline |
| 21 | // `Lambda.Function` forms run the same end-to-end delivery check. |
| 22 | const deliversThroughEventSource = ( |
| 23 | stack: Test.ScratchStack, |
| 24 | deployFunction: Effect.Effect<{ functionName: string; functionUrl?: string }, any, any>, |
| 25 | ) => |
| 26 | Effect.gen(function* () { |
| 27 | yield* stack.destroy(); |
| 28 | |
| 29 | const fn = yield* deployFunction; |
| 30 | |
| 31 | const functionUrl = fn.functionUrl!; |
| 32 | |
| 33 | const { sourceQueueUrl, sourceQueueArn, resultQueueUrl } = |
| 34 | yield* waitForFunctionQueues(functionUrl); |
| 35 | |
| 36 | // The event-source mapping activates asynchronously after deploy. |
| 37 | const mapping = yield* waitForEventSourceMappingEnabled(fn.functionName, sourceQueueArn); |
| 38 | expect(mapping.State).toEqual("Enabled"); |
| 39 | |
| 40 | // Send a message to the source queue out-of-band; the Lambda handler |
| 41 | // forwards its body to the result queue via QueueSink. |
| 42 | const messageBody = `event-source-${crypto.randomUUID()}`; |
| 43 | yield* SQS.sendMessage({ QueueUrl: sourceQueueUrl, MessageBody: messageBody }); |
| 44 | |
| 45 | // Poll the result queue until the forwarded body shows up. Bounded: |
| 46 | // ~30 polls, each an SQS long-poll of 2s. |
| 47 | const received = yield* Effect.gen(function* () { |
| 48 | const result = yield* SQS.receiveMessage({ |
| 49 | QueueUrl: resultQueueUrl, |
| 50 | MaxNumberOfMessages: 10, |
| 51 | WaitTimeSeconds: 2, |
| 52 | }); |
| 53 | const match = (result.Messages ?? []).find((message) => message.Body === messageBody); |
| 54 | if (!match?.ReceiptHandle) { |
| 55 | return yield* Effect.fail(new MessageNotDelivered()); |
| 56 | } |
| 57 | yield* SQS.deleteMessage({ |
| 58 | QueueUrl: resultQueueUrl, |
| 59 | ReceiptHandle: match.ReceiptHandle, |
| 60 | }); |
| 61 | return match.Body!; |
| 62 | }).pipe( |
| 63 | Effect.retry({ |
| 64 | while: (error) => error._tag === "MessageNotDelivered", |
| 65 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]), |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | expect(received).toEqual(messageBody); |
| 70 | |
| 71 | yield* stack.destroy(); |
| 72 | |
| 73 | // Out-of-band: both queues are actually gone after the destroy. |
| 74 | yield* assertQueueDeleted(sourceQueueUrl); |
| 75 | yield* assertQueueDeleted(resultQueueUrl); |
| 76 | }); |
| 77 | |
| 78 | test.provider( |
| 79 | "delivers real SQS messages through the Lambda event source", |
| 80 | (stack) => |
| 81 | deliversThroughEventSource( |
| 82 | stack, |
| 83 | stack.deploy(QueueEventSourceFunction.pipe(Effect.provide(QueueEventSourceFunctionLive))), |
| 84 | ), |
| 85 | { timeout: 240_000 }, |
| 86 | ); |
| 87 | |
| 88 | test.provider( |
| 89 | "an inline Lambda.Function can consume its event source", |
| 90 | (stack) => deliversThroughEventSource(stack, stack.deploy(InlineQueueEventSourceFunction)), |
| 91 | { timeout: 240_000 }, |
| 92 | ); |
| 93 | |
| 94 | test.provider( |
| 95 | "retries only the messages reported in batchItemFailures", |
| 96 | (stack) => |
| 97 | Effect.gen(function* () { |
| 98 | yield* stack.destroy(); |
| 99 | |
| 100 | const fn = yield* stack.deploy( |
| 101 | PartialFailureFunction.pipe(Effect.provide(PartialFailureFunctionLive)), |
| 102 | ); |
| 103 | const { sourceQueueUrl, sourceQueueArn, resultQueueUrl } = yield* waitForFunctionQueues( |
| 104 | fn.functionUrl!, |
| 105 | ); |
| 106 | yield* waitForEventSourceMappingEnabled(fn.functionName, sourceQueueArn); |
| 107 | |
| 108 | // One send so both messages land in the same batch (the mapping |
| 109 | // gathers for up to 3 seconds). |
| 110 | const ok = `ok-${crypto.randomUUID()}`; |
| 111 | const fail = `fail-${crypto.randomUUID()}`; |
| 112 | yield* SQS.sendMessageBatch({ |
| 113 | QueueUrl: sourceQueueUrl, |
| 114 | Entries: [ |
| 115 | { Id: "ok", MessageBody: ok }, |
| 116 | { Id: "fail", MessageBody: fail }, |
| 117 | ], |
| 118 | }); |
| 119 | |
| 120 | // The handler forwards `<body>#<receiveCount>`. Collect until the |
| 121 | // failed message has come back on its retry (visibility timeout 10s). |
| 122 | const forwarded = new Set<string>(); |
| 123 | yield* Effect.gen(function* () { |
| 124 | const result = yield* SQS.receiveMessage({ |
| 125 | QueueUrl: resultQueueUrl, |
| 126 | MaxNumberOfMessages: 10, |
| 127 | WaitTimeSeconds: 2, |
| 128 | }); |
| 129 | for (const message of result.Messages ?? []) { |
| 130 | forwarded.add(message.Body!); |
| 131 | yield* SQS.deleteMessage({ |
| 132 | QueueUrl: resultQueueUrl, |
| 133 | ReceiptHandle: message.ReceiptHandle!, |
| 134 | }); |
| 135 | } |
| 136 | if (!forwarded.has(`${fail}#2`)) { |
| 137 | return yield* Effect.fail(new MessageNotDelivered()); |
| 138 | } |
| 139 | }).pipe( |
| 140 | Effect.retry({ |
| 141 | while: (error) => error._tag === "MessageNotDelivered", |
| 142 | // Lambda redelivers the failed message once its 10s visibility |
| 143 | // timeout lapses and the poller picks it up again. |
| 144 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(30)]), |
| 145 | }), |
| 146 | ); |
| 147 | |
| 148 | // The successful message was deleted after its first delivery and |
| 149 | // never reprocessed; only the failed one was retried. |
| 150 | expect([...forwarded].filter((body) => body.startsWith(ok))).toEqual([`${ok}#1`]); |
| 151 | expect([...forwarded].filter((body) => body.startsWith(fail))).toEqual([`${fail}#2`]); |
| 152 | |
| 153 | yield* stack.destroy(); |
| 154 | |
| 155 | yield* assertQueueDeleted(sourceQueueUrl); |
| 156 | yield* assertQueueDeleted(resultQueueUrl); |
| 157 | }), |
| 158 | { timeout: 240_000 }, |
| 159 | ); |
| 160 | }, |
| 161 | ); |
| 162 | |
| 163 | /** Poll the fixture's Function URL until it reports its queue identifiers. */ |
| 164 | const waitForFunctionQueues = Effect.fn(function* (functionUrl: string) { |
| 165 | // First request rides out cold-start / URL propagation; keep polling until |
| 166 | // the fixture reports the queue identifiers. |
| 167 | return yield* HttpClient.get(functionUrl).pipe( |
| 168 | Effect.timeout("4 seconds"), |
| 169 | Effect.mapError(() => new FunctionNotReady("Function URL request timed out")), |
| 170 | Effect.flatMap((response) => |
| 171 | response.status === 200 |
| 172 | ? (response.json as Effect.Effect<{ |
| 173 | sourceQueueUrl?: string; |
| 174 | sourceQueueArn?: string; |
| 175 | resultQueueUrl?: string; |
| 176 | }>) |
| 177 | : Effect.fail(new FunctionNotReady(`Function not ready: ${response.status}`)), |
| 178 | ), |
| 179 | Effect.flatMap((body) => |
| 180 | body.sourceQueueUrl && body.sourceQueueArn && body.resultQueueUrl |
| 181 | ? Effect.succeed( |
| 182 | body as { |
| 183 | sourceQueueUrl: string; |
| 184 | sourceQueueArn: string; |
| 185 | resultQueueUrl: string; |
| 186 | }, |
| 187 | ) |
| 188 | : Effect.fail(new FunctionNotReady("Function returned empty queue identifiers")), |
| 189 | ), |
| 190 | Effect.retry({ |
| 191 | schedule: Schedule.max([Schedule.fixed("4 seconds"), Schedule.recurs(10)]), |
| 192 | }), |
| 193 | ); |
| 194 | }); |
| 195 | |
| 196 | const waitForEventSourceMappingEnabled = Effect.fn(function* ( |
| 197 | functionName: string, |
| 198 | eventSourceArn: string, |
| 199 | ) { |
| 200 | return yield* Lambda.listEventSourceMappings({ |
| 201 | FunctionName: functionName, |
| 202 | EventSourceArn: eventSourceArn, |
| 203 | }).pipe( |
| 204 | Effect.flatMap((result) => { |
| 205 | const mapping = result.EventSourceMappings?.[0]; |
| 206 | if (!mapping || mapping.State !== "Enabled") { |
| 207 | return Effect.fail(new EventSourceMappingNotReady()); |
| 208 | } |
| 209 | return Effect.succeed(mapping); |
| 210 | }), |
| 211 | Effect.retry({ |
| 212 | while: (error) => error._tag === "EventSourceMappingNotReady", |
| 213 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(10)]), |
| 214 | }), |
| 215 | ); |
| 216 | }); |
| 217 | |
| 218 | class QueueStillExists extends Data.TaggedError("QueueStillExists") {} |
| 219 | |
| 220 | /** Poll (bounded) until GetQueueAttributes reports the queue gone. */ |
| 221 | const assertQueueDeleted = Effect.fn(function* (queueUrl: string) { |
| 222 | yield* SQS.getQueueAttributes({ QueueUrl: queueUrl, AttributeNames: ["All"] }).pipe( |
| 223 | Effect.flatMap(() => Effect.fail(new QueueStillExists())), |
| 224 | Effect.retry({ |
| 225 | // SQS DeleteQueue propagation is documented at ~60s; poll on a fixed |
| 226 | // cadence with a bounded budget. |
| 227 | while: (e) => e._tag === "QueueStillExists", |
| 228 | schedule: Schedule.max([Schedule.spaced("5 seconds"), Schedule.recurs(10)]), |
| 229 | }), |
| 230 | Effect.catchTag("QueueDoesNotExist", () => Effect.void), |
| 231 | ); |
| 232 | }); |
| 233 | |
| 234 | class EventSourceMappingNotReady extends Data.TaggedError("EventSourceMappingNotReady") {} |
| 235 | |
| 236 | class MessageNotDelivered extends Data.TaggedError("MessageNotDelivered") {} |
| 237 | |
| 238 | class FunctionNotReady extends Data.TaggedError("FunctionNotReady")<{ message: string }> { |
| 239 | constructor(message: string) { |
| 240 | super({ message }); |
| 241 | } |
| 242 | } |