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

main
ts 187 lines 6.49 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 Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import PipesBindingsFunctionLive, { PipesBindingsFunction } from "./bindings-handler.ts";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "PipesBindings");
15
16 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17 // over 60s on a fresh deploy.
18 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
19
20 let baseUrl: string;
21
22 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
23 readonly status: number;
24 readonly body: string;
25 }> {}
26
27 class PipeNotSettled extends Data.TaggedError("PipeNotSettled")<{
28 readonly currentState: string | undefined;
29 }> {}
30
31 // The shared Lambda fixture occasionally answers a transient 5xx under load
32 // (cold re-init, IAM propagation on the freshly attached policy). Retry only
33 // 5xx; a genuine 4xx/assertion failure surfaces immediately.
34 const send = (request: HttpClientRequest.HttpClientRequest) =>
35 HttpClient.execute(request).pipe(
36 Effect.flatMap((response) =>
37 response.status >= 500
38 ? response.text.pipe(
39 Effect.flatMap((body) =>
40 Effect.fail(new TransientUpstream({ status: response.status, body })),
41 ),
42 )
43 : Effect.succeed(response),
44 ),
45 Effect.retry({
46 while: (e) => e._tag === "TransientUpstream",
47 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
48 }),
49 );
50
51 const getJson = (path: string) =>
52 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(
53 Effect.flatMap((r) => r.json),
54 // The response body has no compile-time shape; widen the distilled Json
55 // union to `unknown` so call sites can assert their expected shape.
56 Effect.map((json): unknown => json),
57 );
58
59 interface DescribeResponse {
60 name: string | undefined;
61 currentState: string | undefined;
62 desiredState: string | undefined;
63 }
64
65 // Poll the fixture's /describe route (bounded) until the pipe settles into
66 // the expected state after a stop/start.
67 const awaitState = (expected: string) =>
68 getJson("/describe").pipe(
69 Effect.flatMap((raw) => {
70 const described = raw as DescribeResponse;
71 return described.currentState === expected
72 ? Effect.succeed(described)
73 : Effect.fail(new PipeNotSettled({ currentState: described.currentState }));
74 }),
75 Effect.retry({
76 while: (e): boolean => e._tag === "PipeNotSettled",
77 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(30)]),
78 }),
79 );
80
81 describe.sequential(
82 "AWS.Pipes bindings",
83 {
84 tags: [
85 "provider:aws",
86 "provider:aws:iam",
87 "provider:aws:lambda",
88 "provider:aws:pipes",
89 "provider:aws:sqs",
90 "live",
91 ],
92 },
93 () => {
94 beforeAll(
95 Effect.gen(function* () {
96 yield* Effect.logInfo("Pipes test setup: destroying previous resources");
97 yield* sharedStack.destroy();
98
99 yield* Effect.logInfo("Pipes test setup: deploying fixture");
100 const attrs = yield* sharedStack.deploy(
101 Effect.gen(function* () {
102 return yield* PipesBindingsFunction;
103 }).pipe(Effect.provide(PipesBindingsFunctionLive)),
104 );
105
106 expect(attrs.functionUrl).toBeTruthy();
107 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
108
109 const readinessUrl = `${baseUrl}/bindings`;
110 yield* Effect.logInfo(`Pipes test setup: probing readiness at ${readinessUrl}`);
111 yield* HttpClient.get(readinessUrl).pipe(
112 Effect.flatMap((response) =>
113 response.status === 200
114 ? Effect.succeed(response)
115 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
116 ),
117 Effect.tapError((error) =>
118 Effect.logWarning(`Pipes test setup: fixture not ready yet (${String(error)})`),
119 ),
120 Effect.retry({ schedule: readinessPolicy }),
121 );
122 }),
123 { timeout: 240_000 },
124 );
125
126 afterAll(sharedStack.destroy(), { timeout: 180_000 });
127
128 describe("binding registration", () => {
129 test.provider("all four capabilities initialize in the runtime", (_stack) =>
130 Effect.gen(function* () {
131 const response = (yield* getJson("/bindings")) as { bound: string[] };
132 expect(response.bound).toHaveLength(4);
133 }),
134 );
135 });
136
137 describe("DescribePipe", () => {
138 test.provider("reads the pipe's live state", (_stack) =>
139 Effect.gen(function* () {
140 const described = (yield* getJson("/describe")) as DescribeResponse;
141 expect(described.name).toBeTruthy();
142 // The provider waits for the pipe to settle out of CREATING before
143 // returning, so the fixture pipe is already RUNNING.
144 expect(described.currentState).toBe("RUNNING");
145 expect(described.desiredState).toBe("RUNNING");
146 }),
147 );
148 });
149
150 describe("ListPipes", () => {
151 test.provider("finds the pipe by name prefix", (_stack) =>
152 Effect.gen(function* () {
153 const described = (yield* getJson("/describe")) as DescribeResponse;
154 const { names } = (yield* getJson("/list")) as { names: string[] };
155 expect(names).toContain(described.name);
156 }),
157 );
158 });
159
160 describe("StopPipe + StartPipe", () => {
161 test.provider(
162 "stops the running pipe, then starts it again",
163 (_stack) =>
164 Effect.gen(function* () {
165 const stopped = (yield* getJson("/stop")) as {
166 desiredState: string;
167 currentState: string;
168 };
169 expect(stopped.desiredState).toBe("STOPPED");
170
171 // Wait (bounded) for the pipe to settle — StartPipe during
172 // STOPPING would be a ConflictException.
173 yield* awaitState("STOPPED");
174
175 const started = (yield* getJson("/start")) as {
176 desiredState: string;
177 currentState: string;
178 };
179 expect(started.desiredState).toBe("RUNNING");
180
181 yield* awaitState("RUNNING");
182 }),
183 { timeout: 240_000 },
184 );
185 });
186 },
187 );