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

main
ts 99 lines 3.88 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 SQS from "@distilled.cloud/aws/sqs";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as Schedule from "effect/Schedule";
9 import PipeTargetFunctionLive, { PipeTargetFunction } from "./pipe-handler.ts";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 describe.sequential(
14 "AWS.Pipes delivery",
15 {
16 tags: ["provider:aws", "provider:aws:lambda", "provider:aws:pipes", "provider:aws:sqs", "live"],
17 },
18 () => {
19 test.provider(
20 "delivers SQS messages through a pipe to the Lambda target",
21 (stack) =>
22 Effect.gen(function* () {
23 yield* stack.destroy();
24
25 const fn = yield* stack.deploy(
26 PipeTargetFunction.pipe(Effect.provide(PipeTargetFunctionLive)),
27 );
28
29 const functionUrl = fn.functionUrl!;
30
31 // First request rides out cold-start / URL propagation; keep polling
32 // until the fixture reports both queue identifiers.
33 const { sourceQueueUrl, sinkQueueUrl } = yield* HttpClient.get(functionUrl).pipe(
34 Effect.flatMap((response) =>
35 response.status === 200
36 ? (response.json as Effect.Effect<{
37 sourceQueueUrl?: string;
38 sinkQueueUrl?: string;
39 }>)
40 : Effect.fail(new FunctionNotReady(`Function not ready: ${response.status}`)),
41 ),
42 Effect.flatMap((body) =>
43 body.sourceQueueUrl && body.sinkQueueUrl
44 ? Effect.succeed(body as { sourceQueueUrl: string; sinkQueueUrl: string })
45 : Effect.fail(new FunctionNotReady("Function returned empty queue identifiers")),
46 ),
47 Effect.retry({
48 schedule: Schedule.max([Schedule.fixed("1 seconds"), Schedule.recurs(45)]),
49 }),
50 );
51
52 // The Pipe provider waits for RUNNING inside reconcile, so the pipe
53 // is already active. Send a message to the source queue out-of-band;
54 // the pipe delivers it to the Lambda target, whose listener forwards
55 // the body into the sink queue.
56 const messageBody = `pipe-delivery-${crypto.randomUUID()}`;
57 yield* SQS.sendMessage({ QueueUrl: sourceQueueUrl, MessageBody: messageBody });
58
59 // Poll the sink queue until the forwarded body shows up. Nine 8s
60 // long polls with 500ms spacing give delivery a bounded ~76s budget
61 // without spending half the time asleep between polls.
62 const received = yield* Effect.gen(function* () {
63 const result = yield* SQS.receiveMessage({
64 QueueUrl: sinkQueueUrl,
65 MaxNumberOfMessages: 10,
66 WaitTimeSeconds: 8,
67 });
68 const match = (result.Messages ?? []).find((message) => message.Body === messageBody);
69 if (!match?.ReceiptHandle) {
70 return yield* Effect.fail(new MessageNotDelivered());
71 }
72 yield* SQS.deleteMessage({
73 QueueUrl: sinkQueueUrl,
74 ReceiptHandle: match.ReceiptHandle,
75 });
76 return match.Body!;
77 }).pipe(
78 Effect.retry({
79 while: (error) => error._tag === "MessageNotDelivered",
80 schedule: Schedule.max([Schedule.spaced("500 millis"), Schedule.recurs(8)]),
81 }),
82 );
83
84 expect(received).toEqual(messageBody);
85
86 yield* stack.destroy();
87 }),
88 { timeout: 240_000 },
89 );
90 },
91 );
92
93 class MessageNotDelivered extends Data.TaggedError("MessageNotDelivered") {}
94
95 class FunctionNotReady extends Data.TaggedError("FunctionNotReady")<{ message: string }> {
96 constructor(message: string) {
97 super({ message });
98 }
99 }