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

main
ts 231 lines 8.97 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 eventbridge from "@distilled.cloud/aws/eventbridge";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as HttpClientRequest from "effect/http/HttpClientRequest";
10 import * as Schedule from "effect/Schedule";
11 import EmrTestFunctionLive, { EmrTestFunction, PROBE_RELEASE_LABEL } from "./handler";
12 import EmrSlowTestFunctionLive, { EmrSlowTestFunction } from "./slow-handler";
13
14 const testOptions = { providers: AWS.providers() };
15 const { test, beforeAll, afterAll } = Test.make(testOptions);
16 const sharedStack = Core.scratchStack(testOptions, "EMRBindings");
17
18 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
19 // over 60s on a fresh deploy.
20 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
21
22 let baseUrl: string;
23 let functionArn: string;
24
25 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
26 readonly status: number;
27 readonly body: string;
28 }> {}
29
30 // The shared Lambda fixture occasionally answers a transient 5xx (cold
31 // re-init, IAM propagation on the freshly attached policy that the handler's
32 // `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine 4xx surfaces
33 // 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(Effect.flatMap((r) => r.json));
53
54 describe.sequential(
55 "EMR Bindings",
56 { tags: ["provider:aws", "provider:aws:emr", "provider:aws:lambda", "live"] },
57 () => {
58 beforeAll(
59 Effect.gen(function* () {
60 yield* Effect.logInfo("EMR test setup: destroying previous resources");
61 yield* sharedStack.destroy();
62
63 yield* Effect.logInfo("EMR test setup: deploying fixture");
64 const attrs = yield* sharedStack.deploy(
65 Effect.gen(function* () {
66 return yield* EmrTestFunction;
67 }).pipe(Effect.provide(EmrTestFunctionLive)),
68 );
69
70 expect(attrs.functionUrl).toBeTruthy();
71 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
72 functionArn = attrs.functionArn;
73
74 const readinessUrl = `${baseUrl}/bindings`;
75 yield* HttpClient.get(readinessUrl).pipe(
76 Effect.flatMap((response) =>
77 response.status === 200
78 ? Effect.succeed(response)
79 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
80 ),
81 Effect.retry({ schedule: readinessPolicy }),
82 );
83 }),
84 { timeout: 240_000 },
85 );
86
87 afterAll(sharedStack.destroy(), { timeout: 120_000 });
88
89 describe("binding registration", () => {
90 test.provider("all four capabilities initialize in the runtime", () =>
91 Effect.gen(function* () {
92 const response = (yield* getJson("/bindings")) as { bound: string[] };
93 expect(response.bound).toHaveLength(4);
94 }),
95 );
96 });
97
98 describe("ListClusters", () => {
99 test.provider("lists the account's active clusters", () =>
100 Effect.gen(function* () {
101 const response = (yield* getJson("/clusters")) as { count: number };
102 expect(response.count).toBeGreaterThanOrEqual(0);
103 }),
104 );
105 });
106
107 describe("ListReleaseLabels", () => {
108 test.provider("lists the region's release catalog, newest first", () =>
109 Effect.gen(function* () {
110 const response = (yield* getJson("/releases")) as { count: number; latest?: string };
111 expect(response.count).toBeGreaterThan(0);
112 expect(response.latest).toMatch(/^emr-/);
113 }),
114 );
115 });
116
117 describe("DescribeReleaseLabel", () => {
118 test.provider("reads the applications a release ships", () =>
119 Effect.gen(function* () {
120 const response = (yield* getJson("/release")) as { applications: string[] };
121 expect(response.applications).toContain("Spark");
122 }),
123 );
124 });
125
126 describe("ListSupportedInstanceTypes", () => {
127 test.provider(`lists instance types for ${PROBE_RELEASE_LABEL}`, () =>
128 Effect.gen(function* () {
129 const response = (yield* getJson("/instance-types")) as { types: string[] };
130 expect(response.types.length).toBeGreaterThan(0);
131 }),
132 );
133 });
134
135 describe("consumeClusterEvents", { tags: ["provider:aws:eventbridge"] }, () => {
136 test.provider("the deploy created an EventBridge rule targeting the function", () =>
137 Effect.gen(function* () {
138 // Out-of-band via distilled: the fixture's consumeClusterEvents
139 // must have materialized as a rule on the default bus with the
140 // Lambda as target.
141 const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({
142 TargetArn: functionArn,
143 });
144 expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1);
145 }),
146 );
147 });
148 },
149 );
150
151 // ---------------------------------------------------------------------------
152 // Cluster-scoped bindings (steps data plane, inspection reads, managed
153 // scaling) need a real EMR cluster (~10-15 min to WAITING, billed per
154 // instance-hour) — skipped under `--fast` like the Cluster lifecycle
155 // test.
156 // ---------------------------------------------------------------------------
157
158 const slowStack = Core.scratchStack(testOptions, "EMRSlowBindings");
159
160 test.provider.skipIf(!!process.env.FAST)(
161 "steps + inspection + managed-scaling bindings against a live cluster",
162 () =>
163 Effect.gen(function* () {
164 yield* slowStack.destroy();
165
166 yield* Effect.gen(function* () {
167 const { functionUrl } = yield* slowStack.deploy(
168 Effect.gen(function* () {
169 return yield* EmrSlowTestFunction;
170 }).pipe(Effect.provide(EmrSlowTestFunctionLive)),
171 );
172 expect(functionUrl).toBeTruthy();
173 const slowBaseUrl = functionUrl!.replace(/\/+$/, "");
174
175 const get = (path: string) =>
176 HttpClient.get(`${slowBaseUrl}${path}`).pipe(
177 Effect.retry({
178 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
179 }),
180 Effect.flatMap((r) => r.json),
181 );
182
183 // Steps data plane: submit, describe (poll until terminal), list,
184 // and cancel — proves the JobFlowId/ClusterId injections and grants.
185 const added = (yield* get("/steps/add")) as { stepId?: string };
186 expect(added.stepId).toMatch(/^s-/);
187
188 const step = (yield* get(`/step?id=${added.stepId}`).pipe(
189 Effect.repeat({
190 schedule: Schedule.spaced("10 seconds"),
191 until: (s): boolean =>
192 (s as { state?: string }).state !== "PENDING" &&
193 (s as { state?: string }).state !== "RUNNING",
194 times: 30,
195 }),
196 )) as { state?: string; name?: string };
197 expect(step.name).toBe("alchemy-echo");
198 expect(["COMPLETED", "FAILED", "CANCELLED"]).toContain(step.state);
199
200 const steps = (yield* get("/steps")) as { count: number };
201 expect(steps.count).toBeGreaterThanOrEqual(1);
202
203 // Cancelling an already-terminal step reports a per-step failure
204 // status — either status proves the grant.
205 const cancel = (yield* get(`/steps/cancel?id=${added.stepId}`)) as { status?: string };
206 expect(cancel.status).toBeDefined();
207
208 // Inspection reads.
209 const instances = (yield* get("/instances")) as { count: number };
210 expect(instances.count).toBeGreaterThanOrEqual(1);
211 const groups = (yield* get("/groups")) as { types: string[] };
212 expect(groups.types).toContain("MASTER");
213 const bootstrap = (yield* get("/bootstrap")) as { count: number };
214 expect(bootstrap.count).toBeGreaterThanOrEqual(0);
215
216 // Managed scaling put -> get -> remove (or a typed validation tag on
217 // a master-only cluster).
218 const scaling = (yield* get("/scaling")) as { ok: boolean; max?: number; tag?: string };
219 if (scaling.ok) {
220 expect(scaling.max).toBe(4);
221 } else {
222 expect(scaling.tag).toBe("ValidationException");
223 }
224 }).pipe(Effect.ensuring(slowStack.destroy().pipe(Effect.orDie)));
225 }),
226 // cluster create (~10-15 min) + step run + probes + termination initiation.
227 {
228 tags: ["provider:aws", "provider:aws:emr", "provider:aws:iam", "provider:aws:lambda", "live"],
229 timeout: 2_700_000,
230 },
231 );