@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 201 lines 7.83 KB
Raw
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 DynamoDB from "@distilled.cloud/aws/dynamodb";
5 import * as Lambda from "@distilled.cloud/aws/lambda";
6 import * as SQS from "@distilled.cloud/aws/sqs";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Layer from "effect/Layer";
10 import * as Schedule from "effect/Schedule";
11 import DynamoDBStreamFunctionLive, {
12 DynamoDBStreamFunction,
13 TableAndQueue,
14 TableAndQueueLive,
15 } from "./stream-handler.ts";
16
17 const { test } = Test.make({ providers: AWS.providers() });
18
19 describe.skipIf(!!process.env.FAST).sequential(
20 "AWS.DynamoDB.Stream",
21 {
22 tags: [
23 "provider:aws",
24 "provider:aws:dynamodb",
25 "provider:aws:lambda",
26 "provider:aws:sqs",
27 "live",
28 ],
29 },
30 () => {
31 test.provider(
32 "processes real DynamoDB stream records through Lambda",
33 (stack) =>
34 Effect.gen(function* () {
35 yield* Effect.logInfo("DynamoDB Stream test: destroying previous resources");
36 yield* stack.destroy();
37
38 yield* Effect.logInfo("DynamoDB Stream test: deploying stream fixture");
39 const { table, queue, streamFunction } = yield* stack.deploy(
40 Effect.gen(function* () {
41 const { table, queue } = yield* TableAndQueue;
42
43 const func = yield* DynamoDBStreamFunction;
44
45 return { table, queue, streamFunction: func };
46 }).pipe(Effect.provide(Layer.mergeAll(DynamoDBStreamFunctionLive, TableAndQueueLive))),
47 );
48
49 const streamState = yield* waitForTableStreamSpecification(table.tableName, {
50 StreamEnabled: true,
51 StreamViewType: "NEW_AND_OLD_IMAGES",
52 });
53 expect(streamState.Table?.StreamSpecification).toEqual({
54 StreamEnabled: true,
55 StreamViewType: "NEW_AND_OLD_IMAGES",
56 });
57 expect(streamState.Table?.LatestStreamArn).toBeDefined();
58
59 yield* waitForEventSourceMappingEnabled(
60 streamFunction.functionName,
61 streamState.Table?.LatestStreamArn!,
62 );
63
64 yield* Effect.logInfo(`DynamoDB Stream test: writing item into ${table.tableName}`);
65 yield* DynamoDB.putItem({
66 TableName: table.tableName,
67 Item: { pk: { S: "stream#1" }, sk: { S: "item#1" }, data: { S: "payload" } },
68 });
69
70 const message = yield* waitForQueueMessage(queue.queueUrl);
71 const body = JSON.parse(message.Body!);
72
73 expect(body.eventName).toEqual("INSERT");
74 expect(body.keys.pk.S).toEqual("stream#1");
75 expect(body.keys.sk.S).toEqual("item#1");
76 expect(body.newImage.data.S).toEqual("payload");
77 expect(body.oldImage).toBeUndefined();
78
79 yield* Effect.logInfo("DynamoDB Stream test: destroying fixture");
80 yield* stack.destroy();
81 yield* assertTableIsDeleted(table.tableName);
82 }),
83 // Must stay UNDER the factory's 300s hard-kill wall: a vitest timeout
84 // interrupts the fiber and the `test.provider` ensuring-destroy reclaims
85 // every deployed resource, whereas an external SIGKILL leaks the whole
86 // fixture stack (Table + Queue + Function + Permission + ESM) with no
87 // way to reclaim it (scratch state is in-memory). Typical green run is
88 // ~105s, so 230s leaves interruption + destroy comfortably inside 300s.
89 { timeout: 230_000 },
90 );
91 },
92 );
93
94 // Out-of-band proof that the trailing destroy deleted the fixture table.
95 const assertTableIsDeleted = Effect.fn(function* (tableName: string) {
96 yield* Effect.logInfo(`DynamoDB Stream test: waiting for deletion of ${tableName}`);
97 yield* DynamoDB.describeTable({ TableName: tableName }).pipe(
98 Effect.flatMap(() => Effect.fail(new TableStillExists())),
99 Effect.retry({
100 while: (e) => e._tag === "TableStillExists",
101 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(30)]),
102 }),
103 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
104 );
105 });
106
107 const waitForEventSourceMappingEnabled = Effect.fn(function* (
108 functionName: string,
109 eventSourceArn: string,
110 ) {
111 yield* Effect.logInfo(
112 `DynamoDB Stream test: waiting for Lambda event source mapping on ${functionName}`,
113 );
114
115 return yield* Lambda.listEventSourceMappings({
116 FunctionName: functionName,
117 EventSourceArn: eventSourceArn,
118 }).pipe(
119 Effect.flatMap((result) => {
120 const mapping = result.EventSourceMappings?.[0];
121 if (!mapping || mapping.State !== "Enabled") {
122 return Effect.logInfo(
123 `DynamoDB Stream test: event source mapping not ready yet. state=${mapping?.State ?? "missing"}`,
124 ).pipe(Effect.andThen(Effect.fail(new EventSourceMappingNotReady())));
125 }
126 return Effect.logInfo(
127 `DynamoDB Stream test: event source mapping ready (${mapping.UUID})`,
128 ).pipe(Effect.andThen(Effect.succeed(mapping)));
129 }),
130 Effect.retry({
131 while: (error) => error._tag === "EventSourceMappingNotReady",
132 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(20)]),
133 }),
134 );
135 });
136
137 const waitForTableStreamSpecification = Effect.fn(function* (
138 tableName: string,
139 expected: DynamoDB.StreamSpecification,
140 ) {
141 yield* Effect.logInfo(`DynamoDB Stream test: waiting for stream configuration on ${tableName}`);
142
143 return yield* DynamoDB.describeTable({ TableName: tableName }).pipe(
144 Effect.flatMap((result) => {
145 const actual = result.Table?.StreamSpecification;
146 if (JSON.stringify(actual) !== JSON.stringify(expected)) {
147 return Effect.logInfo(
148 `DynamoDB Stream test: stream configuration not ready yet. actual=${JSON.stringify(actual)} expected=${JSON.stringify(expected)}`,
149 ).pipe(Effect.andThen(Effect.fail(new TableStreamConfigurationNotReady())));
150 }
151 return Effect.logInfo(
152 `DynamoDB Stream test: stream configuration ready on ${tableName}`,
153 ).pipe(Effect.andThen(Effect.succeed(result)));
154 }),
155 Effect.retry({
156 while: (error) => error._tag === "TableStreamConfigurationNotReady",
157 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(20)]),
158 }),
159 );
160 });
161
162 const waitForQueueMessage = Effect.fn(function* (queueUrl: string) {
163 yield* Effect.logInfo(`DynamoDB Stream test: waiting for stream output message on ${queueUrl}`);
164
165 // Even after the EventSourceMapping reports `Enabled`, AWS needs a cold-
166 // start window (typically 30–90s for the first record) before the Lambda
167 // is reliably invoked from a freshly-provisioned DynamoDB Stream shard.
168 // Use SQS long-polling (WaitTimeSeconds=20) and budget ~3 minutes so this
169 // is robust on first-deploy runs without slowing down the happy path.
170 return yield* SQS.receiveMessage({
171 QueueUrl: queueUrl,
172 MaxNumberOfMessages: 1,
173 WaitTimeSeconds: 20,
174 }).pipe(
175 Effect.flatMap((result) => {
176 const message = result.Messages?.[0];
177 if (!message?.Body) {
178 return Effect.logInfo("DynamoDB Stream test: stream output queue is still empty").pipe(
179 Effect.andThen(Effect.fail(new StreamMessageNotReady())),
180 );
181 }
182 return Effect.logInfo(
183 `DynamoDB Stream test: received stream output message ${message.MessageId}`,
184 ).pipe(Effect.andThen(Effect.succeed(message)));
185 }),
186 Effect.retry({
187 while: (error) => error._tag === "StreamMessageNotReady",
188 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(72)]),
189 }),
190 );
191 });
192
193 class TableStillExists extends Data.TaggedError("TableStillExists") {}
194
195 class EventSourceMappingNotReady extends Data.TaggedError("EventSourceMappingNotReady") {}
196
197 class TableStreamConfigurationNotReady extends Data.TaggedError(
198 "TableStreamConfigurationNotReady",
199 ) {}
200
201 class StreamMessageNotReady extends Data.TaggedError("StreamMessageNotReady") {}