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 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 HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import IoTEventSourceFunctionLive, { IoTEventSourceFunction } from "./iot-event-source-handler.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | |
| 15 | describe.sequential( |
| 16 | "AWS.IoT.TopicRuleEventSource", |
| 17 | { tags: ["provider:aws", "provider:aws:iot", "provider:aws:lambda", "provider:aws:sqs", "live"] }, |
| 18 | () => { |
| 19 | test.provider( |
| 20 | "publishes via the Publish binding, routes through the topic rule, and observes delivery", |
| 21 | (stack) => |
| 22 | Effect.gen(function* () { |
| 23 | yield* stack.destroy(); |
| 24 | |
| 25 | const fn = yield* stack.deploy( |
| 26 | IoTEventSourceFunction.pipe(Effect.provide(IoTEventSourceFunctionLive)), |
| 27 | ); |
| 28 | const functionUrl = fn.functionUrl!.replace(/\/+$/, ""); |
| 29 | |
| 30 | // Ride out cold-start / URL propagation until /ready reports the queue. |
| 31 | const { resultQueueUrl } = yield* HttpClient.get(`${functionUrl}/ready`).pipe( |
| 32 | // Bound each fetch attempt so a transient Function URL DNS/socket |
| 33 | // stall reaches the retry schedule instead of consuming the whole |
| 34 | // test timeout. |
| 35 | Effect.timeout("4 seconds"), |
| 36 | Effect.mapError(() => new FunctionNotReady("ready request")), |
| 37 | Effect.flatMap((response) => |
| 38 | response.status === 200 |
| 39 | ? (response.json as Effect.Effect<{ resultQueueUrl?: string }>) |
| 40 | : Effect.fail(new FunctionNotReady(`status ${response.status}`)), |
| 41 | ), |
| 42 | Effect.flatMap((body) => |
| 43 | body.resultQueueUrl |
| 44 | ? Effect.succeed(body as { resultQueueUrl: string }) |
| 45 | : Effect.fail(new FunctionNotReady("no result queue")), |
| 46 | ), |
| 47 | Effect.retry({ |
| 48 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(10)]), |
| 49 | }), |
| 50 | ); |
| 51 | |
| 52 | // Publish a uniquely-marked message from inside the Lambda via the IoT |
| 53 | // Publish binding. The topic rule matches it and re-invokes the Lambda, |
| 54 | // whose event-source handler forwards it into the result queue. |
| 55 | const marker = `iot-${crypto.randomUUID()}`; |
| 56 | yield* HttpClient.execute( |
| 57 | HttpClientRequest.post(`${functionUrl}/publish`).pipe( |
| 58 | HttpClientRequest.bodyJsonUnsafe({ marker }), |
| 59 | ), |
| 60 | ).pipe( |
| 61 | Effect.timeout("4 seconds"), |
| 62 | Effect.mapError(() => new FunctionNotReady("publish request")), |
| 63 | Effect.filterOrFail( |
| 64 | (response) => response.status === 200, |
| 65 | (response) => new FunctionNotReady(`publish ${response.status}`), |
| 66 | ), |
| 67 | Effect.retry({ |
| 68 | while: (e) => e._tag === "FunctionNotReady", |
| 69 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(10)]), |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | // Poll the result queue until the forwarded message (carrying the |
| 74 | // marker) shows up. IoT rule and permission propagation are eventually |
| 75 | // consistent, so republish while waiting, bounded to about 50 seconds. |
| 76 | const received = yield* Effect.gen(function* () { |
| 77 | const result = yield* SQS.receiveMessage({ |
| 78 | QueueUrl: resultQueueUrl, |
| 79 | MaxNumberOfMessages: 10, |
| 80 | WaitTimeSeconds: 2, |
| 81 | }); |
| 82 | const match = (result.Messages ?? []).find((message) => message.Body?.includes(marker)); |
| 83 | if (!match?.ReceiptHandle) { |
| 84 | // Republish in case the earlier publish predated rule/permission |
| 85 | // readiness. |
| 86 | yield* HttpClient.execute( |
| 87 | HttpClientRequest.post(`${functionUrl}/publish`).pipe( |
| 88 | HttpClientRequest.bodyJsonUnsafe({ marker }), |
| 89 | ), |
| 90 | ).pipe(Effect.timeout("4 seconds"), Effect.ignore); |
| 91 | return yield* Effect.fail(new MessageNotDelivered()); |
| 92 | } |
| 93 | yield* SQS.deleteMessage({ |
| 94 | QueueUrl: resultQueueUrl, |
| 95 | ReceiptHandle: match.ReceiptHandle, |
| 96 | }); |
| 97 | return match.Body!; |
| 98 | }).pipe( |
| 99 | Effect.retry({ |
| 100 | while: (error) => error._tag === "MessageNotDelivered", |
| 101 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]), |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | expect(received).toContain(marker); |
| 106 | |
| 107 | yield* stack.destroy(); |
| 108 | |
| 109 | // Assert the stack's observable resources are gone: the Lambda |
| 110 | // function (which hosted the event source) and the result queue. |
| 111 | yield* Lambda.getFunction({ FunctionName: fn.functionName }).pipe( |
| 112 | Effect.flatMap(() => Effect.fail(new ResourceStillExists("lambda"))), |
| 113 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 114 | Effect.retry({ |
| 115 | while: (e) => e._tag === "ResourceStillExists", |
| 116 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]), |
| 117 | }), |
| 118 | ); |
| 119 | // SQS DeleteQueue propagation is documented at up to ~60s. |
| 120 | yield* SQS.getQueueAttributes({ QueueUrl: resultQueueUrl, AttributeNames: ["All"] }).pipe( |
| 121 | Effect.flatMap(() => Effect.fail(new ResourceStillExists("queue"))), |
| 122 | Effect.catchTag("QueueDoesNotExist", () => Effect.void), |
| 123 | Effect.retry({ |
| 124 | while: (e) => e._tag === "ResourceStillExists", |
| 125 | schedule: Schedule.max([Schedule.spaced("5 seconds"), Schedule.recurs(10)]), |
| 126 | }), |
| 127 | ); |
| 128 | }), |
| 129 | { timeout: 180_000 }, |
| 130 | ); |
| 131 | }, |
| 132 | ); |
| 133 | |
| 134 | class MessageNotDelivered extends Data.TaggedError("MessageNotDelivered") {} |
| 135 | |
| 136 | class ResourceStillExists extends Data.TaggedError("ResourceStillExists")<{ what: string }> { |
| 137 | constructor(what: string) { |
| 138 | super({ what }); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | class FunctionNotReady extends Data.TaggedError("FunctionNotReady")<{ message: string }> { |
| 143 | constructor(message: string) { |
| 144 | super({ message }); |
| 145 | } |
| 146 | } |