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

main
ts 456 lines 17.5 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 Core from "@alchemy.run/test/Core";
5 import * as Lambda from "@distilled.cloud/aws/lambda";
6 import * as S3 from "@distilled.cloud/aws/s3";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as HttpClient from "effect/http/HttpClient";
10 import * as HttpClientRequest from "effect/http/HttpClientRequest";
11 import * as Schedule from "effect/Schedule";
12 import * as Schema from "effect/Schema";
13 import * as Stream from "effect/Stream";
14 import BucketEventSourceFunctionLive, {
15 BucketEventSourceFunction,
16 } from "./fixtures/event-source-handler.ts";
17
18 const testOptions = { providers: AWS.providers() };
19 const { test, beforeAll, afterAll } = Test.make(testOptions);
20 const sharedStack = Core.scratchStack(
21 testOptions,
22 "S3EventSource",
23 "test/AWS/S3/EventSource.test.ts",
24 );
25
26 const readinessPolicy = Schedule.spaced("2 seconds");
27
28 let baseUrl: string;
29 let fixtureFunctionName: string;
30 let fixtureBucketName: string | undefined;
31
32 describe(
33 "S3 Bucket Event Source",
34 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:s3", "live"] },
35 () => {
36 beforeAll(
37 Effect.gen(function* () {
38 yield* Effect.logInfo("S3 EventSource test: destroying previous resources");
39 yield* sharedStack.destroy();
40
41 yield* Effect.logInfo("S3 EventSource test: deploying fixture");
42 const { functionUrl, functionName } = yield* sharedStack.deploy(
43 Effect.gen(function* () {
44 return yield* BucketEventSourceFunction;
45 }).pipe(Effect.provide(BucketEventSourceFunctionLive)),
46 );
47
48 expect(functionUrl).toBeTruthy();
49 baseUrl = functionUrl!.replace(/\/+$/, "");
50 fixtureFunctionName = functionName;
51
52 yield* Effect.logInfo(
53 `S3 EventSource test: function URL ready (${functionUrl}), probing readiness`,
54 );
55
56 const named = yield* HttpClient.get(`${baseUrl}/bucket-name`).pipe(
57 Effect.flatMap((response) =>
58 response.status === 200
59 ? response.json
60 : response.text.pipe(
61 Effect.flatMap((body) =>
62 Effect.fail(new FunctionNotReady({ status: response.status, body })),
63 ),
64 ),
65 ),
66 Effect.flatMap(Schema.decodeUnknownEffect(Schema.Struct({ bucketName: Schema.String }))),
67 Effect.tapError((error) =>
68 Effect.logWarning(`S3 EventSource test: fixture not ready yet (${String(error)})`),
69 ),
70 Effect.retry({ schedule: readinessPolicy, times: 9 }),
71 );
72 fixtureBucketName = named.bucketName;
73 expect(fixtureBucketName).toBeTruthy();
74 yield* Effect.logInfo("S3 EventSource test: fixture responded successfully");
75 }),
76 { timeout: 120_000 },
77 );
78
79 afterAll(
80 Effect.gen(function* () {
81 yield* sharedStack.destroy();
82 // Prove the trailing destroy removed the fixture bucket (skip if
83 // setup never captured the name). afterAll lacks the providers layer
84 // test bodies get, so provide it for the out-of-band distilled call.
85 if (fixtureBucketName) {
86 yield* Core.withProviders(
87 assertBucketDeleted(fixtureBucketName),
88 testOptions,
89 "S3EventSource",
90 );
91 }
92 }),
93 { timeout: 120_000 },
94 );
95
96 test.provider(
97 "object created under the watched prefix triggers the subscription to write derived state",
98 (_stack) =>
99 Effect.gen(function* () {
100 const key = "e2e-object";
101
102 const content = "hello from s3 event source";
103 const response = yield* HttpClient.execute(
104 HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(`${baseUrl}/put`), {
105 key,
106 value: content,
107 }),
108 );
109 expect(response.status).toBe(200);
110 const putResponse = yield* response.json.pipe(
111 Effect.flatMap(
112 Schema.decodeUnknownEffect(
113 Schema.Struct({ ok: Schema.Boolean, versionId: Schema.String }),
114 ),
115 ),
116 );
117 expect(putResponse.ok).toBe(true);
118 expect(putResponse.versionId).toBeTruthy();
119 const processed = yield* waitForProcessed({
120 key: `incoming/${key}`,
121 eventName: "s3:ObjectCreated:Put",
122 versionId: putResponse.versionId,
123 });
124 expect(processed.size).toBe(content.length);
125 expect(processed.eTag).toBeTruthy();
126 expect(processed.content).toBe(content);
127 expect(processed.readVersionId).toBe(putResponse.versionId);
128 }),
129 { timeout: 120_000 },
130 );
131
132 test.provider(
133 "preserves both PUT versions and records delete-marker and explicit-version removal events",
134 () =>
135 Effect.gen(function* () {
136 const Bucket = fixtureBucketName!;
137 const key = "incoming/versions/a b+%?#/雪.txt";
138 const oldContent = "historical notification content";
139 const currentContent = "current notification content has different bytes";
140 const oldVersion = yield* S3.putObject({ Bucket, Key: key, Body: oldContent });
141 const currentVersion = yield* S3.putObject({ Bucket, Key: key, Body: currentContent });
142 expect(oldVersion.VersionId).toBeTruthy();
143 expect(currentVersion.VersionId).toBeTruthy();
144 expect(currentVersion.VersionId).not.toBe(oldVersion.VersionId);
145 const oldIdentity = {
146 key,
147 eventName: "s3:ObjectCreated:Put",
148 versionId: oldVersion.VersionId!,
149 };
150 const currentIdentity = {
151 key,
152 eventName: "s3:ObjectCreated:Put",
153 versionId: currentVersion.VersionId!,
154 };
155 const [oldEvent, currentEvent] = yield* Effect.all(
156 [waitForProcessed(oldIdentity), waitForProcessed(currentIdentity)],
157 { concurrency: 2 },
158 );
159 expect(oldEvent.content).toBe(oldContent);
160 expect(oldEvent.size).toBe(oldContent.length);
161 expect(oldEvent.readVersionId).toBe(oldVersion.VersionId);
162 expect(currentEvent.content).toBe(currentContent);
163 expect(currentEvent.size).toBe(currentContent.length);
164 expect(currentEvent.readVersionId).toBe(currentVersion.VersionId);
165 expect(oldEvent.sequencer).not.toBe(currentEvent.sequencer);
166
167 const marker = yield* S3.deleteObject({ Bucket, Key: key });
168 expect(marker.DeleteMarker).toBe(true);
169 expect(marker.VersionId).toBeTruthy();
170 expect(marker.VersionId).not.toBe(oldVersion.VersionId);
171 expect(marker.VersionId).not.toBe(currentVersion.VersionId);
172 const removed = yield* S3.deleteObject({
173 Bucket,
174 Key: key,
175 VersionId: oldVersion.VersionId!,
176 });
177 expect(removed.VersionId).toBe(oldVersion.VersionId);
178 const [markerEvent, removedEvent] = yield* Effect.all(
179 [
180 waitForProcessed({
181 key,
182 eventName: "s3:ObjectRemoved:DeleteMarkerCreated",
183 versionId: marker.VersionId!,
184 }),
185 waitForProcessed({
186 key,
187 eventName: "s3:ObjectRemoved:Delete",
188 versionId: removed.VersionId!,
189 }),
190 ],
191 { concurrency: 2 },
192 );
193 for (const event of [markerEvent, removedEvent]) {
194 expect(event.content).toBeUndefined();
195 expect(event.readVersionId).toBeUndefined();
196 }
197
198 // Removal records must not replace the earlier creation histories.
199 expect(yield* readProcessed(oldIdentity)).toEqual(oldEvent);
200 expect(yield* readProcessed(currentIdentity)).toEqual(currentEvent);
201 }),
202 { timeout: 120_000 },
203 );
204
205 test.provider(
206 "replays a creation event through the deployed Lambda after permanent source-version deletion",
207 () =>
208 Effect.gen(function* () {
209 const Bucket = fixtureBucketName!;
210 const key = "incoming/replay/a b+%?#/雪.txt";
211 const content = "recorded before permanent version deletion";
212 const created = yield* S3.putObject({ Bucket, Key: key, Body: content });
213 expect(created.VersionId).toBeTruthy();
214 const identity = {
215 key,
216 eventName: "s3:ObjectCreated:Put",
217 versionId: created.VersionId!,
218 };
219 const recorded = yield* waitForProcessed(identity);
220 expect(recorded.content).toBe(content);
221 expect(recorded.readVersionId).toBe(created.VersionId);
222 const evidenceKey = yield* Effect.sync(
223 () =>
224 `processed/${[key, identity.eventName, identity.versionId].map(encodeURIComponent).join("/")}.json`,
225 );
226 const before = yield* S3.headObject({ Bucket, Key: evidenceKey });
227 expect(before.VersionId).toBeTruthy();
228
229 yield* S3.deleteObject({ Bucket, Key: key, VersionId: identity.versionId });
230 const sourceExists = yield* S3.headObject({
231 Bucket,
232 Key: key,
233 VersionId: identity.versionId,
234 }).pipe(
235 Effect.as(true),
236 Effect.catchTag("NotFound", () => Effect.succeed(false)),
237 );
238 expect(sourceExists).toBe(false);
239
240 const region = yield* AWS.Region;
241 const Payload = yield* Effect.sync(() =>
242 JSON.stringify({
243 Records: [
244 {
245 eventVersion: "2.1",
246 eventSource: "aws:s3",
247 awsRegion: region,
248 eventName: "ObjectCreated:Put",
249 s3: {
250 s3SchemaVersion: "1.0",
251 bucket: { name: Bucket, arn: `arn:aws:s3:::${Bucket}` },
252 object: {
253 key: encodeURIComponent(key).replace(/%20/g, "+"),
254 versionId: identity.versionId,
255 sequencer: recorded.sequencer,
256 size: recorded.size,
257 eTag: recorded.eTag,
258 },
259 },
260 },
261 ],
262 }),
263 );
264 const replay = yield* Lambda.invoke({
265 FunctionName: fixtureFunctionName,
266 InvocationType: "RequestResponse",
267 Payload,
268 });
269 if (replay.Payload) yield* Stream.runDrain(replay.Payload);
270 expect(replay.StatusCode).toBe(200);
271 expect(replay.FunctionError).toBeUndefined();
272 expect(yield* readProcessed(identity)).toEqual(recorded);
273 const after = yield* S3.headObject({ Bucket, Key: evidenceKey });
274 expect(after.VersionId).toBe(before.VersionId);
275 }),
276 { timeout: 120_000 },
277 );
278
279 test.provider(
280 "records a copied version and reads its historical content after a later PUT",
281 () =>
282 Effect.gen(function* () {
283 const Bucket = fixtureBucketName!;
284 const sourceKey = "sources/copy.txt";
285 const key = "incoming/copied/a b+%.txt";
286 const content = "historical copy source content";
287 const source = yield* S3.putObject({ Bucket, Key: sourceKey, Body: content });
288 expect(source.VersionId).toBeTruthy();
289 yield* S3.putObject({
290 Bucket,
291 Key: sourceKey,
292 Body: "new source content must not be copied",
293 });
294 const CopySource = yield* Effect.sync(
295 () => `${Bucket}/${sourceKey}?versionId=${encodeURIComponent(source.VersionId!)}`,
296 );
297 const copied = yield* S3.copyObject({ Bucket, Key: key, CopySource });
298 expect(copied.VersionId).toBeTruthy();
299 expect(copied.CopySourceVersionId).toBe(source.VersionId);
300 const latestContent = "later destination content";
301 const latest = yield* S3.putObject({ Bucket, Key: key, Body: latestContent });
302 expect(latest.VersionId).toBeTruthy();
303 expect(latest.VersionId).not.toBe(copied.VersionId);
304 const [copyEvent, putEvent] = yield* Effect.all(
305 [
306 waitForProcessed({
307 key,
308 eventName: "s3:ObjectCreated:Copy",
309 versionId: copied.VersionId!,
310 }),
311 waitForProcessed({
312 key,
313 eventName: "s3:ObjectCreated:Put",
314 versionId: latest.VersionId!,
315 }),
316 ],
317 { concurrency: 2 },
318 );
319 expect(copyEvent.content).toBe(content);
320 expect(copyEvent.size).toBe(content.length);
321 expect(copyEvent.eTag).toBeTruthy();
322 expect(copyEvent.readVersionId).toBe(copied.VersionId);
323 expect(putEvent.content).toBe(latestContent);
324 expect(putEvent.readVersionId).toBe(latest.VersionId);
325 }),
326 { timeout: 120_000 },
327 );
328
329 test.provider(
330 "records a completed multipart version and reads its content after a later PUT",
331 () =>
332 Effect.gen(function* () {
333 const Bucket = fixtureBucketName!;
334 const key = "incoming/multipart/a b+%.txt";
335 const content = "single final multipart part";
336 const upload = yield* S3.createMultipartUpload({ Bucket, Key: key });
337 expect(upload.UploadId).toBeTruthy();
338 const part = yield* S3.uploadPart({
339 Bucket,
340 Key: key,
341 UploadId: upload.UploadId!,
342 PartNumber: 1,
343 Body: content,
344 });
345 expect(part.ETag).toBeTruthy();
346 const completed = yield* S3.completeMultipartUpload({
347 Bucket,
348 Key: key,
349 UploadId: upload.UploadId!,
350 MultipartUpload: { Parts: [{ PartNumber: 1, ETag: part.ETag! }] },
351 });
352 expect(completed.VersionId).toBeTruthy();
353 const latestContent = "later multipart destination content";
354 const latest = yield* S3.putObject({ Bucket, Key: key, Body: latestContent });
355 expect(latest.VersionId).toBeTruthy();
356 expect(latest.VersionId).not.toBe(completed.VersionId);
357 const [completedEvent, putEvent] = yield* Effect.all(
358 [
359 waitForProcessed({
360 key,
361 eventName: "s3:ObjectCreated:CompleteMultipartUpload",
362 versionId: completed.VersionId!,
363 }),
364 waitForProcessed({
365 key,
366 eventName: "s3:ObjectCreated:Put",
367 versionId: latest.VersionId!,
368 }),
369 ],
370 { concurrency: 2 },
371 );
372 expect(completedEvent.content).toBe(content);
373 expect(completedEvent.size).toBe(content.length);
374 expect(completedEvent.eTag).toBeTruthy();
375 expect(completedEvent.readVersionId).toBe(completed.VersionId);
376 expect(putEvent.content).toBe(latestContent);
377 expect(putEvent.readVersionId).toBe(latest.VersionId);
378 }),
379 { timeout: 120_000 },
380 );
381 },
382 );
383
384 interface NotificationIdentity {
385 key: string;
386 eventName: string;
387 versionId: string;
388 }
389
390 const processedRecord = Schema.Struct({
391 bucket: Schema.String,
392 key: Schema.String,
393 eventName: Schema.String,
394 versionId: Schema.String,
395 sequencer: Schema.String,
396 size: Schema.optional(Schema.Number),
397 eTag: Schema.optional(Schema.String),
398 content: Schema.optional(Schema.String),
399 readVersionId: Schema.optional(Schema.String),
400 });
401
402 const readProcessed = Effect.fn(function* (identity: NotificationIdentity) {
403 const path = yield* Effect.sync(() => `/processed?${new URLSearchParams({ ...identity })}`);
404 const response = yield* HttpClient.get(`${baseUrl}${path}`);
405 if (response.status === 404) {
406 yield* response.text;
407 return yield* Effect.fail(new ProcessedNotReady(identity));
408 }
409 if (response.status !== 200) {
410 return yield* Effect.fail(
411 new FunctionNotReady({ status: response.status, body: yield* response.text }),
412 );
413 }
414 const { processed } = yield* response.json.pipe(
415 Effect.flatMap(Schema.decodeUnknownEffect(Schema.Struct({ processed: processedRecord }))),
416 );
417 expect(processed.bucket).toBe(fixtureBucketName);
418 expect(processed.key).toBe(identity.key);
419 expect(processed.eventName).toBe(identity.eventName);
420 expect(processed.versionId).toBe(identity.versionId);
421 expect(processed.sequencer).toMatch(/^[0-9a-f]+$/i);
422 return processed;
423 });
424
425 const waitForProcessed = (identity: NotificationIdentity) =>
426 readProcessed(identity).pipe(
427 Effect.retry({
428 while: (error) => error._tag === "ProcessedNotReady",
429 schedule: Schedule.spaced("5 seconds"),
430 times: 9,
431 }),
432 );
433
434 class ProcessedNotReady extends Data.TaggedError("ProcessedNotReady")<NotificationIdentity> {}
435
436 class BucketStillExists extends Data.TaggedError("BucketStillExists") {}
437
438 // Out-of-band assert-gone after the final destroy: retry while headBucket
439 // still succeeds (S3 delete visibility is eventually consistent), settle on
440 // the typed NotFound.
441 const assertBucketDeleted = Effect.fn(function* (name: string) {
442 yield* S3.headBucket({ Bucket: name }).pipe(
443 Effect.flatMap(() => Effect.fail(new BucketStillExists())),
444 Effect.retry({
445 while: (e) => e._tag === "BucketStillExists",
446 schedule: Schedule.spaced("5 seconds"),
447 times: 9,
448 }),
449 Effect.catchTag("NotFound", () => Effect.void),
450 );
451 });
452
453 class FunctionNotReady extends Data.TaggedError("FunctionNotReady")<{
454 readonly status: number;
455 readonly body: string;
456 }> {}