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, 06:00 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 Kinesis from "@distilled.cloud/aws/kinesis"; |
| 5 | import * as Lambda from "@distilled.cloud/aws/lambda"; |
| 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 KinesisStreamFunctionLive, { KinesisStreamFunction } from "./stream-handler.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: AWS.providers() }); |
| 13 | |
| 14 | describe.sequential( |
| 15 | "AWS.Kinesis.StreamEventSource", |
| 16 | { |
| 17 | tags: [ |
| 18 | "provider:aws", |
| 19 | "provider:aws:kinesis", |
| 20 | "provider:aws:lambda", |
| 21 | "provider:aws:sqs", |
| 22 | "live", |
| 23 | ], |
| 24 | }, |
| 25 | () => { |
| 26 | test.provider( |
| 27 | "processes real Kinesis records through Lambda", |
| 28 | (stack) => |
| 29 | Effect.gen(function* () { |
| 30 | yield* stack.destroy(); |
| 31 | |
| 32 | const streamFunction = yield* stack.deploy( |
| 33 | KinesisStreamFunction.pipe(Effect.provide(KinesisStreamFunctionLive)), |
| 34 | ); |
| 35 | |
| 36 | const functionUrl = streamFunction.functionUrl!; |
| 37 | |
| 38 | const { streamName, streamArn } = yield* HttpClient.get(functionUrl).pipe( |
| 39 | Effect.flatMap((response) => |
| 40 | response.status === 200 |
| 41 | ? (response.json as Effect.Effect<{ streamName: string; streamArn: string }>) |
| 42 | : Effect.fail(new FunctionNotReady(`Function not ready: ${response.status}`)), |
| 43 | ), |
| 44 | Effect.flatMap((body) => |
| 45 | body.streamName && body.streamArn |
| 46 | ? Effect.succeed(body) |
| 47 | : Effect.fail(new FunctionNotReady("Function returned empty stream identifiers")), |
| 48 | ), |
| 49 | Effect.retry({ |
| 50 | schedule: Schedule.max([Schedule.fixed("1 seconds"), Schedule.recurs(60)]), |
| 51 | }), |
| 52 | ); |
| 53 | |
| 54 | yield* waitForEventSourceMappingEnabled(streamFunction.functionName, streamArn); |
| 55 | yield* Effect.sleep("10 seconds"); |
| 56 | |
| 57 | yield* Kinesis.putRecord({ |
| 58 | StreamName: streamName, |
| 59 | PartitionKey: "stream-event-source", |
| 60 | Data: new TextEncoder().encode("payload"), |
| 61 | }); |
| 62 | yield* Effect.sleep("5 seconds"); |
| 63 | |
| 64 | const mapping = yield* waitForEventSourceMappingEnabled( |
| 65 | streamFunction.functionName, |
| 66 | streamArn, |
| 67 | ); |
| 68 | expect(mapping.State).toEqual("Enabled"); |
| 69 | |
| 70 | yield* stack.destroy(); |
| 71 | }), |
| 72 | { timeout: 240_000 }, |
| 73 | ); |
| 74 | }, |
| 75 | ); |
| 76 | |
| 77 | const waitForEventSourceMappingEnabled = Effect.fn(function* ( |
| 78 | functionName: string, |
| 79 | eventSourceArn: string, |
| 80 | ) { |
| 81 | return yield* Lambda.listEventSourceMappings({ |
| 82 | FunctionName: functionName, |
| 83 | EventSourceArn: eventSourceArn, |
| 84 | }).pipe( |
| 85 | Effect.flatMap((result) => { |
| 86 | const mapping = result.EventSourceMappings?.[0]; |
| 87 | if (!mapping || mapping.State !== "Enabled") { |
| 88 | return Effect.fail(new EventSourceMappingNotReady()); |
| 89 | } |
| 90 | return Effect.succeed(mapping); |
| 91 | }), |
| 92 | Effect.retry({ |
| 93 | while: (error) => error._tag === "EventSourceMappingNotReady", |
| 94 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(20)]), |
| 95 | }), |
| 96 | ); |
| 97 | }); |
| 98 | |
| 99 | class EventSourceMappingNotReady extends Data.TaggedError("EventSourceMappingNotReady") {} |
| 100 | |
| 101 | class FunctionNotReady extends Data.TaggedError("FunctionNotReady")<{ message: string }> { |
| 102 | constructor(message: string) { |
| 103 | super({ message }); |
| 104 | } |
| 105 | } |