The best way to control Claude Code, Codex and more from any computer or phone

main
ts 260 lines 10.2 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 Firehose from "@distilled.cloud/aws/firehose";
5 import * as S3 from "@distilled.cloud/aws/s3";
6 import * as Alchemy from "alchemy";
7 import * as State from "alchemy/State";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as HttpClient from "effect/http/HttpClient";
11 import * as HttpClientRequest from "effect/http/HttpClientRequest";
12 import * as Schedule from "effect/Schedule";
13 import * as Stream from "effect/Stream";
14 import FirehoseApiFunctionLive, {
15 BucketAndDeliveryStream,
16 FirehoseApiFunction,
17 } from "./handler.ts";
18
19 const providers = AWS.providers();
20 const state = State.localState();
21 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ providers, state });
22
23 const Stack = Alchemy.Stack(
24 "firehose-bindings",
25 { providers, state },
26 Effect.gen(function* () {
27 // Share the bucket/delivery-stream between the deployed function and the
28 // stack outputs so the test can verify ingest out-of-band via distilled.
29 const { bucket, deliveryStream } = yield* BucketAndDeliveryStream;
30 const fn = yield* FirehoseApiFunction;
31 return {
32 url: fn.functionUrl.as<string>(),
33 deliveryStreamName: deliveryStream.deliveryStreamName.as<string>(),
34 bucketName: bucket.bucketName.as<string>(),
35 };
36 }).pipe(Effect.provide(FirehoseApiFunctionLive)),
37 );
38
39 const stack = beforeAll(
40 Effect.gen(function* () {
41 // A prior interrupted run may leave local state pointing at a partially
42 // deleted Lambda/IAM fixture. Start from a clean stack; provider deletes
43 // are idempotent, and deploy then proves stale-state recovery as well.
44 yield* destroy(Stack);
45 return yield* deploy(Stack);
46 }),
47 { timeout: 240_000 },
48 );
49 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { timeout: 120_000 });
50
51 // Lambda Function URLs cold-start (DNS, init) and a fresh role's IAM grants
52 // (eventual consistency) can both take a while on the first hit. Retrying on
53 // any non-200 lets the first request wait through that window; warm calls
54 // return on the first try and never retry.
55 const readinessSchedule = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
56
57 const urlOf = (baseUrl: string, path: string) => `${baseUrl.replace(/\/+$/, "")}${path}`;
58
59 const getJson = (baseUrl: string, path: string) =>
60 HttpClient.execute(HttpClientRequest.get(urlOf(baseUrl, path))).pipe(
61 Effect.flatMap((response) =>
62 response.status === 200
63 ? response.json
64 : Effect.fail(new Error(`Request failed: ${response.status}`)),
65 ),
66 Effect.retry({ schedule: readinessSchedule }),
67 );
68
69 const postJson = (baseUrl: string, path: string, body: unknown) =>
70 HttpClient.execute(
71 HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(urlOf(baseUrl, path)), body),
72 ).pipe(
73 Effect.flatMap((response) =>
74 response.status === 200
75 ? response.json
76 : Effect.fail(new Error(`Request failed: ${response.status}`)),
77 ),
78 Effect.retry({ schedule: readinessSchedule }),
79 );
80
81 describe(
82 "Firehose Bindings",
83 {
84 tags: [
85 "provider:aws",
86 "provider:aws:firehose",
87 "provider:aws:lambda",
88 "provider:aws:s3",
89 "live",
90 ],
91 },
92 () => {
93 describe("PutRecord", () => {
94 // `test.provider` supplies the AWS environment for the out-of-band
95 // describeDeliveryStream verification via distilled.
96 test.provider("writes a single record and the stream stays ACTIVE", () =>
97 Effect.gen(function* () {
98 const { url, deliveryStreamName } = yield* stack;
99 const response = yield* postJson(url, "/put-record", {
100 data: `put-record-${crypto.randomUUID()}`,
101 });
102 expect((response as any).RecordId).toBeTruthy();
103
104 // Ingest success is the assertion — S3 delivery is asynchronous
105 // (buffering ≥ 60s), so verify the stream is ACTIVE out-of-band
106 // instead of waiting for objects to land.
107 const described = yield* Firehose.describeDeliveryStream({
108 DeliveryStreamName: deliveryStreamName,
109 });
110 expect(described.DeliveryStreamDescription.DeliveryStreamStatus).toEqual("ACTIVE");
111 }),
112 );
113 });
114
115 describe("PutRecordBatch", () => {
116 test(
117 "writes a batch of records with zero failures",
118 Effect.gen(function* () {
119 const { url } = yield* stack;
120 const response = yield* postJson(url, "/put-record-batch", {
121 records: [
122 `batch-1-${crypto.randomUUID()}`,
123 `batch-2-${crypto.randomUUID()}`,
124 `batch-3-${crypto.randomUUID()}`,
125 ],
126 });
127 expect((response as any).FailedPutCount).toBe(0);
128 const entries = (response as any).RequestResponses ?? [];
129 expect(entries.length).toBe(3);
130 for (const entry of entries) {
131 expect(entry.RecordId).toBeTruthy();
132 expect(entry.ErrorCode).toBeUndefined();
133 }
134 }),
135 );
136 });
137
138 describe("ListDeliveryStreams", () => {
139 test(
140 "lists delivery streams and includes the fixture stream",
141 Effect.gen(function* () {
142 const { url, deliveryStreamName } = yield* stack;
143 const response = (yield* getJson(url, "/list-streams")) as {
144 DeliveryStreamNames: string[];
145 HasMoreDeliveryStreams: boolean;
146 };
147 expect(Array.isArray(response.DeliveryStreamNames)).toBe(true);
148 expect(response.DeliveryStreamNames).toContain(deliveryStreamName);
149 }),
150 );
151 });
152
153 describe("DeliveryStreamSink", () => {
154 // `test.provider` supplies the AWS environment for the out-of-band
155 // describeDeliveryStream verification via distilled.
156 test.provider("streams records through the sink helper", () =>
157 Effect.gen(function* () {
158 const { url, deliveryStreamName } = yield* stack;
159 const response = yield* postJson(url, "/sink", {
160 records: [
161 `sink-1-${crypto.randomUUID()}`,
162 `sink-2-${crypto.randomUUID()}`,
163 `sink-3-${crypto.randomUUID()}`,
164 ],
165 });
166 expect((response as any).ok).toBe(true);
167 expect((response as any).count).toBe(3);
168
169 // Ingest success is the assertion — S3 delivery is asynchronous
170 // (buffering ≥ 60s; see the gated slow test below for arrival proof).
171 const described = yield* Firehose.describeDeliveryStream({
172 DeliveryStreamName: deliveryStreamName,
173 });
174 expect(described.DeliveryStreamDescription.DeliveryStreamStatus).toEqual("ACTIVE");
175 }),
176 );
177
178 test(
179 "splits more than 500 records into multiple PutRecordBatch calls",
180 Effect.gen(function* () {
181 const { url } = yield* stack;
182 // 501 records > the PutRecordBatch limit of 500, so the batched sink
183 // must split the chunk into 2 sequential API calls (500 + 1). Any
184 // per-record ServiceUnavailable failures are retried by the sink
185 // engine before the handler responds.
186 const marker = crypto.randomUUID();
187 const response = yield* postJson(url, "/sink", {
188 records: Array.from({ length: 501 }, (_, i) => `sink-${marker}-${i}`),
189 });
190 expect((response as any).ok).toBe(true);
191 expect((response as any).count).toBe(501);
192 }),
193 { timeout: 120_000 },
194 );
195 });
196
197 // Even with zero buffering, Firehose treats the interval and size as hints
198 // (~50s observed), so the end-to-end proof is skipped under --fast; its
199 // independent checks run concurrently so they share one bounded delivery
200 // window.
201 describe.concurrent.skipIf(!!process.env.FAST)("S3 delivery (slow)", () => {
202 class MarkerNotDeliveredYet extends Data.TaggedError("MarkerNotDeliveredYet") {}
203
204 const waitForMarker = (bucketName: string, marker: string) => {
205 const findMarker = Effect.gen(function* () {
206 const listing = yield* S3.listObjectsV2({ Bucket: bucketName, Prefix: "records/" });
207 for (const object of listing.Contents ?? []) {
208 if (object.Key === undefined) {
209 continue;
210 }
211 const got = yield* S3.getObject({ Bucket: bucketName, Key: object.Key });
212 // The body read surfaces plain `Error` (streaming transport) — a
213 // mid-delivery read hiccup is just "not delivered yet".
214 const text = yield* Stream.mkString(Stream.decodeText(got.Body!)).pipe(
215 Effect.catch(() => Effect.fail(new MarkerNotDeliveredYet())),
216 );
217 if (text.includes(marker)) {
218 return object.Key;
219 }
220 }
221 return yield* new MarkerNotDeliveredYet();
222 });
223
224 return findMarker.pipe(
225 Effect.retry({
226 while: (e) => e._tag === "MarkerNotDeliveredYet",
227 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(12)]),
228 }),
229 );
230 };
231
232 test.provider(
233 "delivers buffered records to the destination bucket",
234 () =>
235 Effect.gen(function* () {
236 const { url, bucketName } = yield* stack;
237 // Randomness is only a payload correlation marker, never a
238 // physical resource name. It prevents a prior run's object from
239 // satisfying this delivery proof.
240 const marker = `put-record-delivery-${crypto.randomUUID()}`;
241 yield* postJson(url, "/put-record", { data: marker });
242 expect(yield* waitForMarker(bucketName, marker)).toBeTruthy();
243 }),
244 { timeout: 180_000 },
245 );
246
247 test.provider(
248 "sink records land in the destination bucket (marker-anchored)",
249 () =>
250 Effect.gen(function* () {
251 const { url, bucketName } = yield* stack;
252 const marker = `sink-delivery-${crypto.randomUUID()}`;
253 yield* postJson(url, "/sink", { records: [`${marker}-1`, `${marker}-2`] });
254 expect(yield* waitForMarker(bucketName, marker)).toBeTruthy();
255 }),
256 { timeout: 180_000 },
257 );
258 });
259 },
260 );