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

main
ts 159 lines 5.86 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 EmrcTestFunctionLive, { EmrcTestFunction } from "./handler";
12
13 const testOptions = { providers: AWS.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "EMRContainersBindings");
16
17 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
18 // over 60s on a fresh deploy.
19 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
20
21 let baseUrl: string;
22 let functionArn: string;
23
24 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
25 readonly status: number;
26 readonly body: string;
27 }> {}
28
29 // The shared Lambda fixture occasionally answers a transient 5xx under load
30 // (cold re-init, IAM propagation on the freshly attached policy that the
31 // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
32 // 4xx/assertion failure surfaces immediately.
33 const send = (request: HttpClientRequest.HttpClientRequest) =>
34 HttpClient.execute(request).pipe(
35 Effect.flatMap((response) =>
36 response.status >= 500
37 ? response.text.pipe(
38 Effect.flatMap((body) =>
39 Effect.fail(new TransientUpstream({ status: response.status, body })),
40 ),
41 )
42 : Effect.succeed(response),
43 ),
44 Effect.retry({
45 while: (e) => e._tag === "TransientUpstream",
46 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
47 }),
48 );
49
50 const getJson = (path: string) =>
51 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
52
53 describe.sequential(
54 "EMRContainers Bindings",
55 {
56 tags: [
57 "provider:aws",
58 "provider:aws:emrcontainers",
59 "provider:aws:iam",
60 "provider:aws:lambda",
61 "live",
62 ],
63 },
64 () => {
65 beforeAll(
66 Effect.gen(function* () {
67 yield* Effect.logInfo("EMRContainers test setup: destroying previous resources");
68 yield* sharedStack.destroy();
69
70 yield* Effect.logInfo("EMRContainers test setup: deploying fixture");
71 const attrs = yield* sharedStack.deploy(
72 Effect.gen(function* () {
73 return yield* EmrcTestFunction;
74 }).pipe(Effect.provide(EmrcTestFunctionLive)),
75 );
76
77 expect(attrs.functionUrl).toBeTruthy();
78 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
79 functionArn = attrs.functionArn;
80
81 const readinessUrl = `${baseUrl}/bindings`;
82 yield* Effect.logInfo(`EMRContainers test setup: probing readiness at ${readinessUrl}`);
83 yield* HttpClient.get(readinessUrl).pipe(
84 Effect.flatMap((response) =>
85 response.status === 200
86 ? Effect.succeed(response)
87 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
88 ),
89 Effect.tapError((error) =>
90 Effect.logWarning(`EMRContainers test setup: fixture not ready yet (${String(error)})`),
91 ),
92 Effect.retry({ schedule: readinessPolicy }),
93 );
94 }),
95 { timeout: 240_000 },
96 );
97
98 afterAll(sharedStack.destroy(), { timeout: 120_000 });
99
100 describe("binding registration", () => {
101 test.provider("all capabilities initialize in the runtime", (_stack) =>
102 Effect.gen(function* () {
103 const response = (yield* getJson("/bindings")) as { bound: string[] };
104 expect(response.bound).toHaveLength(3);
105 }),
106 );
107 });
108
109 describe("DescribeJobTemplate", () => {
110 test.provider("reads the bound template's data (injected template id)", (_stack) =>
111 Effect.gen(function* () {
112 const response = (yield* getJson("/template")) as {
113 id: string;
114 name: string;
115 releaseLabel: string;
116 };
117 expect(response.id).toBeTruthy();
118 expect(response.releaseLabel).toBe("emr-7.5.0-latest");
119 }),
120 );
121 });
122
123 describe("ListJobTemplates", () => {
124 test.provider("enumerates the account's templates including the fixture's", (_stack) =>
125 Effect.gen(function* () {
126 const template = (yield* getJson("/template")) as { id: string };
127 const response = (yield* getJson("/templates")) as { ids: string[] };
128 expect(response.ids).toContain(template.id);
129 }),
130 );
131 });
132
133 describe("ListVirtualClusters", () => {
134 test.provider("enumerates the account's virtual clusters (IAM grant + call path)", (_stack) =>
135 Effect.gen(function* () {
136 // The account may have zero RUNNING virtual clusters (they require
137 // an EKS-backed cluster); the assertion is the authorized round
138 // trip, not the count.
139 const response = (yield* getJson("/virtual-clusters")) as { ids: string[] };
140 expect(Array.isArray(response.ids)).toBe(true);
141 }),
142 );
143 });
144
145 describe("consumeJobRunEvents", { tags: ["provider:aws:eventbridge"] }, () => {
146 test.provider("the deploy created an EventBridge rule targeting the function", (_stack) =>
147 Effect.gen(function* () {
148 // Out-of-band via distilled: the fixture's consumeJobRunEvents
149 // must have materialized as a rule on the default bus with the
150 // Lambda as target.
151 const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({
152 TargetArn: functionArn,
153 });
154 expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1);
155 }),
156 );
157 });
158 },
159 );