@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 347 lines 13.9 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 emr from "@distilled.cloud/aws/emr-serverless";
6 import * as eventbridge from "@distilled.cloud/aws/eventbridge";
7 import * as sts from "@distilled.cloud/aws/sts";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as HttpClient from "effect/http/HttpClient";
11 import * as HttpClientRequest from "effect/http/HttpClientRequest";
12 import * as Option from "effect/Option";
13 import * as Schedule from "effect/Schedule";
14 import * as Stream from "effect/Stream";
15 import EmrServerlessTestFunctionLive, {
16 BINDINGS_APP_NAME,
17 BINDINGS_ROLE_NAME,
18 EmrServerlessTestFunction,
19 } from "./handler";
20
21 const testOptions = { providers: AWS.providers() };
22 const { test, beforeAll, afterAll } = Test.make(testOptions);
23 const sharedStack = Core.scratchStack(testOptions, "EMRServerlessBindings");
24
25 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
26 // over 60s on a fresh deploy.
27 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
28
29 let baseUrl: string;
30 let functionArn: string;
31
32 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
33 readonly status: number;
34 readonly body: string;
35 }> {}
36
37 class JobRunNotTerminal extends Data.TaggedError("JobRunNotTerminal")<{
38 readonly jobRunId: string;
39 readonly state: string;
40 readonly stateDetails?: string;
41 }> {
42 override get message() {
43 return `EMR Serverless job ${this.jobRunId} is still ${this.state}: ${this.stateDetails ?? "no state details"}`;
44 }
45 }
46
47 class ApplicationNotStarted extends Data.TaggedError("ApplicationNotStarted")<{
48 readonly applicationId: string;
49 readonly state: string;
50 }> {}
51
52 const terminalJobRunStates = ["SUCCESS", "CANCELLED", "FAILED"] as const;
53
54 // The shared Lambda fixture occasionally answers a transient 5xx under load
55 // (cold re-init, IAM propagation on the freshly attached policy). Retry only
56 // 5xx; a genuine 4xx/assertion failure surfaces immediately.
57 const send = (request: HttpClientRequest.HttpClientRequest) =>
58 HttpClient.execute(request).pipe(
59 Effect.flatMap((response) =>
60 response.status >= 500
61 ? response.text.pipe(
62 Effect.flatMap((body) =>
63 Effect.fail(new TransientUpstream({ status: response.status, body })),
64 ),
65 )
66 : Effect.succeed(response),
67 ),
68 Effect.retry({
69 while: (e) => e._tag === "TransientUpstream",
70 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
71 }),
72 );
73
74 const getJson = (path: string) =>
75 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
76
77 const postJson = (path: string) =>
78 send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json));
79
80 const waitForTerminalJobRun = (jobRunId: string) =>
81 getJson(`/jobrun-detail?id=${jobRunId}`).pipe(
82 Effect.flatMap((value) => {
83 const detail = value as { jobRunId: string; state: string; stateDetails?: string };
84 return terminalJobRunStates.includes(detail.state as (typeof terminalJobRunStates)[number])
85 ? Effect.succeed(detail)
86 : Effect.fail(
87 new JobRunNotTerminal({
88 jobRunId: detail.jobRunId,
89 state: detail.state,
90 stateDetails: detail.stateDetails,
91 }),
92 );
93 }),
94 Effect.tapError((error) =>
95 error._tag === "JobRunNotTerminal" ? Effect.logInfo(error.message) : Effect.void,
96 ),
97 Effect.retry({
98 while: (error) => error._tag === "JobRunNotTerminal",
99 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(40)]),
100 }),
101 );
102
103 const waitForApplicationStarted = (applicationId: string) =>
104 emr.getApplication({ applicationId }).pipe(
105 Effect.flatMap(({ application }) =>
106 application.state === "STARTED"
107 ? Effect.succeed(application)
108 : Effect.fail(new ApplicationNotStarted({ applicationId, state: application.state })),
109 ),
110 Effect.retry({
111 while: (error) => error._tag === "ApplicationNotStarted",
112 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(15)]),
113 }),
114 );
115
116 /** Find the fixture application out-of-band via its deterministic name. */
117 const findApplication = Effect.gen(function* () {
118 const summary = yield* emr.listApplications.items({}).pipe(
119 Stream.filter((s) => s.name === BINDINGS_APP_NAME && s.state !== "TERMINATED"),
120 Stream.runHead,
121 Effect.map(Option.getOrUndefined),
122 );
123 expect(summary).toBeDefined();
124 return summary!;
125 });
126
127 describe.sequential(
128 "EMRServerless Bindings",
129 {
130 tags: [
131 "provider:aws",
132 "provider:aws:emrserverless",
133 "provider:aws:iam",
134 "provider:aws:lambda",
135 "live",
136 ],
137 },
138 () => {
139 beforeAll(
140 Effect.gen(function* () {
141 yield* Effect.logInfo("EMRServerless test setup: destroying previous resources");
142 yield* sharedStack.destroy();
143
144 yield* Effect.logInfo("EMRServerless test setup: deploying fixture");
145 const attrs = yield* sharedStack.deploy(
146 Effect.gen(function* () {
147 return yield* EmrServerlessTestFunction;
148 }).pipe(Effect.provide(EmrServerlessTestFunctionLive)),
149 );
150
151 expect(attrs.functionUrl).toBeTruthy();
152 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
153 functionArn = attrs.functionArn;
154
155 const readinessUrl = `${baseUrl}/bindings`;
156 yield* Effect.logInfo(`EMRServerless test setup: probing readiness at ${readinessUrl}`);
157 yield* HttpClient.get(readinessUrl).pipe(
158 Effect.flatMap((response) =>
159 response.status === 200
160 ? Effect.succeed(response)
161 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
162 ),
163 Effect.tapError((error) =>
164 Effect.logWarning(`EMRServerless test setup: fixture not ready yet (${String(error)})`),
165 ),
166 Effect.retry({ schedule: readinessPolicy }),
167 );
168 }),
169 { timeout: 300_000 },
170 );
171
172 afterAll(sharedStack.destroy(), { timeout: 240_000 });
173
174 describe("binding registration", () => {
175 test.provider("all 14 capabilities initialize in the runtime", (_stack) =>
176 Effect.gen(function* () {
177 const response = (yield* getJson("/bindings")) as { bound: string[] };
178 expect(response.bound).toHaveLength(14);
179 }),
180 );
181 });
182
183 describe("ListJobRuns", () => {
184 test.provider("reads job runs through the injected application id", (_stack) =>
185 Effect.gen(function* () {
186 const response = (yield* getJson("/jobruns")) as { ids: string[] };
187 expect(Array.isArray(response.ids)).toBe(true);
188 }),
189 );
190 });
191
192 describe("ListSessions", () => {
193 test.provider("lists sessions on the bound application", (_stack) =>
194 Effect.gen(function* () {
195 const response = (yield* getJson("/sessions")) as { tag: string };
196 expect(response.tag).toBe("ok");
197 }),
198 );
199 });
200
201 // Typed not-found probes: a ResourceNotFoundException (not
202 // AccessDeniedException) proves the IAM grant covers the sub-resource ARN
203 // and the application id was injected from the binding.
204 describe("typed not-found probes", () => {
205 // The session APIs validate the session id format before lookup, so a
206 // well-formed-but-fake job-run-shaped id answers with the typed
207 // ValidationException instead — either tag proves the IAM grant passed
208 // authorization and the application id was injected.
209 const probes: ReadonlyArray<
210 readonly [name: string, method: "GET" | "POST", path: string, tags: readonly string[]]
211 > = [
212 ["GetJobRun", "GET", "/jobrun", ["ResourceNotFoundException"]],
213 ["GetDashboardForJobRun", "GET", "/jobrun-dashboard", ["ResourceNotFoundException"]],
214 ["ListJobRunAttempts", "GET", "/jobrun-attempts", ["ResourceNotFoundException"]],
215 ["CancelJobRun", "POST", "/jobrun-cancel-fake", ["ResourceNotFoundException"]],
216 ["GetSession", "GET", "/session", ["ResourceNotFoundException", "ValidationException"]],
217 [
218 "GetSessionEndpoint",
219 "GET",
220 "/session-endpoint",
221 ["ResourceNotFoundException", "ValidationException"],
222 ],
223 [
224 "TerminateSession",
225 "POST",
226 "/session-terminate",
227 ["ResourceNotFoundException", "ValidationException"],
228 ],
229 ] as const;
230
231 for (const [name, method, path, tags] of probes) {
232 test.provider(`${name} answers with a typed error (not AccessDenied)`, (_stack) =>
233 Effect.gen(function* () {
234 const response = (yield* method === "GET" ? getJson(path) : postJson(path)) as {
235 tag: string;
236 detail: string;
237 };
238 expect(response.detail).not.toContain("not authorized");
239 expect(tags).toContain(response.tag);
240 }),
241 );
242 }
243 });
244
245 describe("GetResourceDashboard", () => {
246 test.provider("answers with the typed service-gate tag (see probe.test.ts)", (_stack) =>
247 Effect.gen(function* () {
248 // The service currently denies emr-serverless:GetResourceDashboard
249 // for every caller (even Action:"*" admins) — the operation backs
250 // the console dashboards and has not launched for API callers.
251 // probe.test.ts pins that platform gate; here we assert the
252 // binding surfaces the same typed tag end-to-end from the Lambda.
253 const response = (yield* getJson("/resource-dashboard")) as { tag: string };
254 expect(response.tag).toBe("AccessDeniedException");
255 }),
256 );
257 });
258
259 describe("StartSession", () => {
260 test.provider("grant + PassRole reach the service (typed validation error)", (_stack) =>
261 Effect.gen(function* () {
262 // The fixture application has no interactive configuration, so the
263 // service must answer with a typed validation error — reaching it
264 // proves both the StartSession grant and the PassRole statement.
265 const { Account } = yield* sts.getCallerIdentity({});
266 const roleArn = `arn:aws:iam::${Account}:role/${BINDINGS_ROLE_NAME}`;
267 const response = (yield* postJson(
268 `/session-start?roleArn=${encodeURIComponent(roleArn)}`,
269 )) as { tag: string; detail: string };
270 expect(response.detail).not.toContain("not authorized");
271 expect(response.tag).toBe("ValidationException");
272 }),
273 );
274 });
275
276 describe("application control + job run round trip", () => {
277 test.provider(
278 "start application, submit + cancel a job run, stop application",
279 (_stack) =>
280 Effect.gen(function* () {
281 // 1. StartApplication (free — the fixture has no pre-init capacity).
282 const started = (yield* postJson("/app-start")) as { tag: string };
283 expect(started.tag).toBe("ok");
284
285 // 2. Submit a job run (SparkPi from the EMR image).
286 const { Account } = yield* sts.getCallerIdentity({});
287 const roleArn = `arn:aws:iam::${Account}:role/${BINDINGS_ROLE_NAME}`;
288 const run = (yield* postJson(`/jobrun-run?roleArn=${encodeURIComponent(roleArn)}`)) as {
289 jobRunId: string;
290 arn: string;
291 };
292 expect(run.jobRunId).toBeTruthy();
293 expect(run.arn).toContain("/jobruns/");
294
295 // 3. Cancel it immediately (no workers ever run).
296 const cancelled = (yield* postJson(`/jobrun-cancel?id=${run.jobRunId}`)) as {
297 jobRunId: string;
298 };
299 expect(cancelled.jobRunId).toBe(run.jobRunId);
300
301 // 4. GetJobRun observes the cancellation through a terminal state.
302 // StopApplication rejects while any run is still CANCELLING, so the
303 // cancellation response alone is not a sufficient readiness signal.
304 const detail = yield* waitForTerminalJobRun(run.jobRunId);
305 expect(detail.jobRunId).toBe(run.jobRunId);
306 expect(terminalJobRunStates).toContain(detail.state);
307
308 // 5. The submitted run shows up in ListJobRuns.
309 const listed = (yield* getJson("/jobruns")) as { ids: string[] };
310 expect(listed.ids).toContain(run.jobRunId);
311
312 // 6. Converge the application back to STARTED. The fixture's
313 // one-minute auto-stop can legitimately win while the cancelled
314 // job settles under load, but StopApplication is only valid from
315 // STARTED.
316 const app = yield* findApplication;
317 const { application } = yield* emr.getApplication({ applicationId: app.id });
318 if (application.state === "CREATED" || application.state === "STOPPED") {
319 const restarted = (yield* postJson("/app-start")) as { tag: string };
320 expect(restarted.tag).toBe("ok");
321 }
322 const startedApplication = yield* waitForApplicationStarted(app.id);
323 expect(startedApplication.state).toBe("STARTED");
324
325 // 7. StopApplication.
326 const stopped = (yield* postJson("/app-stop")) as { tag: string };
327 expect(stopped.tag).toBe("ok");
328 }),
329 { timeout: 240_000 },
330 );
331 });
332
333 describe("consumeJobRunEvents", { tags: ["provider:aws:eventbridge"] }, () => {
334 test.provider("the deploy created an EventBridge rule targeting the function", (_stack) =>
335 Effect.gen(function* () {
336 // Out-of-band via distilled: the fixture's consumeJobRunEvents must
337 // have materialized as a rule on the default bus with the Lambda as
338 // target.
339 const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({
340 TargetArn: functionArn,
341 });
342 expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1);
343 }),
344 );
345 });
346 },
347 );