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

main
ts 464 lines 17.6 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 SFNTestFunctionLive, { SFNTestFunction } from "./handler";
12
13 const testOptions = { providers: AWS.providers() };
14 const { test, beforeAll, afterAll } = Test.make(testOptions);
15 const sharedStack = Core.scratchStack(testOptions, "SFNBindings");
16
17 // Lambda function URL cold-start (DNS, IAM propagation, init) can take well
18 // over 60s on a fresh deploy under parallel-suite load.
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 // Retry transient 5xx only; genuine 4xx/assertion failures surface
30 // immediately.
31 const send = (request: HttpClientRequest.HttpClientRequest) =>
32 HttpClient.execute(request).pipe(
33 Effect.flatMap((response) =>
34 response.status >= 500
35 ? response.text.pipe(
36 Effect.flatMap((body) =>
37 Effect.fail(new TransientUpstream({ status: response.status, body })),
38 ),
39 )
40 : Effect.succeed(response),
41 ),
42 Effect.retry({
43 while: (e) => e._tag === "TransientUpstream",
44 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
45 }),
46 );
47
48 const postJson = (url: string, body: unknown) =>
49 send(HttpClientRequest.post(url).pipe(HttpClientRequest.bodyJsonUnsafe(body))).pipe(
50 Effect.flatMap((r) => r.json),
51 );
52
53 const getJson = (url: string) =>
54 send(HttpClientRequest.get(url)).pipe(Effect.flatMap((r) => r.json));
55
56 /** Poll an execution's status via the fixture until it leaves RUNNING. */
57 const pollExecution = (route: string, executionArn: string) =>
58 getJson(`${baseUrl}/${route}?executionArn=${encodeURIComponent(executionArn)}`).pipe(
59 Effect.repeat({
60 schedule: Schedule.spaced("2 seconds"),
61 until: (r) => (r as { status: string }).status !== "RUNNING",
62 times: 20,
63 }),
64 ) as Effect.Effect<{ status: string; output?: string; error?: string }, unknown>;
65
66 /**
67 * Drain the callback queue until we hold the task token for the given
68 * execution. Tokens for other executions (stale from earlier tests in the
69 * sequential block) are consumed and discarded.
70 */
71 const receiveTokenFor = (executionArn: string) =>
72 getJson(`${baseUrl}/receive-token`).pipe(
73 Effect.repeat({
74 schedule: Schedule.spaced("1 second"),
75 until: (r) => (r as { executionArn?: string }).executionArn === executionArn,
76 times: 15,
77 }),
78 Effect.map((r) => (r as { token: string }).token),
79 );
80
81 describe(
82 "StepFunctions Bindings",
83 {
84 tags: [
85 "provider:aws",
86 "provider:aws:lambda",
87 "provider:aws:sqs",
88 "provider:aws:stepfunctions",
89 "live",
90 ],
91 },
92 () => {
93 beforeAll(
94 Effect.gen(function* () {
95 yield* Effect.logInfo("SFN test setup: destroying previous resources");
96 yield* sharedStack.destroy();
97
98 yield* Effect.logInfo("SFN test setup: deploying fixture");
99 const deployed = yield* sharedStack.deploy(
100 Effect.gen(function* () {
101 return yield* SFNTestFunction;
102 }).pipe(Effect.provide(SFNTestFunctionLive)),
103 );
104
105 expect(deployed.functionUrl).toBeTruthy();
106 baseUrl = deployed.functionUrl!.replace(/\/+$/, "");
107 functionArn = deployed.functionArn;
108 const readinessUrl = `${baseUrl}/health`;
109
110 yield* Effect.logInfo(`SFN 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(`SFN 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: 120_000 });
127
128 describe("StartSyncExecution", () => {
129 test.provider(
130 "runs an EXPRESS workflow synchronously and returns its output",
131 (_stack) =>
132 Effect.gen(function* () {
133 const response = (yield* postJson(`${baseUrl}/start-sync`, {
134 input: JSON.stringify({ value: 42 }),
135 })) as {
136 executionArn: string;
137 status: string;
138 output?: string;
139 error?: string;
140 cause?: string;
141 };
142
143 expect(response.status).toBe("SUCCEEDED");
144 expect(response.executionArn).toContain(":express:");
145 const output = JSON.parse(response.output!);
146 expect(output.greeting).toBe("hello");
147 expect(output.echo.value).toBe(42);
148 }),
149 { timeout: 60_000 },
150 );
151 });
152
153 describe("StartExecution", () => {
154 test.provider(
155 "starts a STANDARD execution asynchronously",
156 (_stack) =>
157 Effect.gen(function* () {
158 const response = (yield* postJson(`${baseUrl}/start`, {
159 input: JSON.stringify({ order: 1 }),
160 })) as { executionArn: string };
161
162 expect(response.executionArn).toContain(":execution:");
163 }),
164 { timeout: 60_000 },
165 );
166 });
167
168 describe("DescribeExecution", () => {
169 test.provider(
170 "observes a STANDARD execution completing",
171 (_stack) =>
172 Effect.gen(function* () {
173 const started = (yield* postJson(`${baseUrl}/start`, {
174 input: JSON.stringify({ order: 2 }),
175 })) as { executionArn: string };
176
177 const final = yield* pollExecution("describe-standard", started.executionArn);
178 expect(final.status).toBe("SUCCEEDED");
179 expect(JSON.parse(final.output!)).toEqual({ done: true });
180 }),
181 { timeout: 90_000 },
182 );
183 });
184
185 describe("ValidateStateMachineDefinition", () => {
186 test.provider(
187 "returns OK for a valid definition and FAIL with diagnostics for an invalid one",
188 (_stack) =>
189 Effect.gen(function* () {
190 const ok = (yield* postJson(`${baseUrl}/validate-definition`, {
191 definition: JSON.stringify({
192 StartAt: "Done",
193 States: { Done: { Type: "Pass", End: true } },
194 }),
195 })) as { result: string; diagnostics: unknown[] };
196 expect(ok.result).toBe("OK");
197 expect(ok.diagnostics).toEqual([]);
198
199 const fail = (yield* postJson(`${baseUrl}/validate-definition`, {
200 definition: JSON.stringify({
201 StartAt: "Missing",
202 States: { Done: { Type: "Pass", End: true } },
203 }),
204 })) as { result: string; diagnostics: { code: string }[] };
205 expect(fail.result).toBe("FAIL");
206 expect(fail.diagnostics.length).toBeGreaterThan(0);
207 }),
208 { timeout: 60_000 },
209 );
210 });
211
212 describe("TestState", () => {
213 test.provider(
214 "executes a single JSONata Pass state against an input",
215 (_stack) =>
216 Effect.gen(function* () {
217 const response = (yield* postJson(`${baseUrl}/test-state`, {
218 definition: JSON.stringify({
219 Type: "Pass",
220 QueryLanguage: "JSONata",
221 Output: "{% $states.input.value * 2 %}",
222 End: true,
223 }),
224 input: JSON.stringify({ value: 21 }),
225 })) as { status: string; output?: string };
226 expect(response.status).toBe("SUCCEEDED");
227 expect(response.output).toBe("42");
228 }),
229 { timeout: 60_000 },
230 );
231 });
232
233 // The callback flows share ONE SQS queue carrying task tokens — receives
234 // steal each other's messages under concurrency, so run them sequentially
235 // and drain only the matching token per test.
236 describe.sequential("callback pattern", () => {
237 describe("SendTaskSuccess", () => {
238 test.provider(
239 "heartbeats then completes a waiting execution",
240 (_stack) =>
241 Effect.gen(function* () {
242 const started = (yield* postJson(`${baseUrl}/start-callback`, {})) as {
243 executionArn: string;
244 };
245
246 const token = yield* receiveTokenFor(started.executionArn);
247 expect(token).toBeTruthy();
248
249 // SendTaskHeartbeat keeps the task alive without completing it
250 const heartbeat = (yield* postJson(`${baseUrl}/task-heartbeat`, { token })) as {
251 sent: boolean;
252 };
253 expect(heartbeat.sent).toBe(true);
254
255 const success = (yield* postJson(`${baseUrl}/task-success`, {
256 token,
257 output: JSON.stringify({ approved: true }),
258 })) as { sent: boolean };
259 expect(success.sent).toBe(true);
260
261 const final = yield* pollExecution("describe-callback", started.executionArn);
262 expect(final.status).toBe("SUCCEEDED");
263 expect(JSON.parse(final.output!)).toEqual({ approved: true });
264 }),
265 { timeout: 120_000 },
266 );
267 });
268
269 describe("SendTaskFailure", () => {
270 test.provider(
271 "fails a waiting execution with a typed error",
272 (_stack) =>
273 Effect.gen(function* () {
274 const started = (yield* postJson(`${baseUrl}/start-callback`, {})) as {
275 executionArn: string;
276 };
277
278 const token = yield* receiveTokenFor(started.executionArn);
279
280 const failed = (yield* postJson(`${baseUrl}/task-failure`, {
281 token,
282 error: "ApprovalRejected",
283 cause: "rejected by integration test",
284 })) as { sent: boolean };
285 expect(failed.sent).toBe(true);
286
287 const final = yield* pollExecution("describe-callback", started.executionArn);
288 expect(final.status).toBe("FAILED");
289 expect(final.error).toBe("ApprovalRejected");
290 }),
291 { timeout: 120_000 },
292 );
293 });
294
295 describe("StopExecution", () => {
296 test.provider(
297 "aborts a running execution",
298 (_stack) =>
299 Effect.gen(function* () {
300 const started = (yield* postJson(`${baseUrl}/start-callback`, {})) as {
301 executionArn: string;
302 };
303
304 // consume this execution's token first so it never pollutes the
305 // queue for other flows
306 yield* receiveTokenFor(started.executionArn);
307
308 const stopped = (yield* postJson(`${baseUrl}/stop`, {
309 executionArn: started.executionArn,
310 })) as { stopDate: string };
311 expect(stopped.stopDate).toBeTruthy();
312
313 const final = yield* pollExecution("describe-callback", started.executionArn);
314 expect(final.status).toBe("ABORTED");
315 }),
316 { timeout: 120_000 },
317 );
318 });
319
320 describe("SendTaskHeartbeat", () => {
321 test.provider(
322 "rejects an invalid task token with a typed error surfaced as 500",
323 (_stack) =>
324 Effect.gen(function* () {
325 // an obviously-invalid token dies in the fixture (orDie) — the
326 // route answers 500; we only assert the request round-trips and
327 // does not hang (IAM wiring for states:SendTaskHeartbeat works,
328 // otherwise this would be AccessDenied at a different layer).
329 const response = yield* HttpClient.execute(
330 HttpClientRequest.post(`${baseUrl}/task-heartbeat`).pipe(
331 HttpClientRequest.bodyJsonUnsafe({ token: "not-a-token" }),
332 ),
333 );
334 expect(response.status).toBeGreaterThanOrEqual(400);
335 }),
336 { timeout: 60_000 },
337 );
338 });
339
340 describe("GetActivityTask", () => {
341 test.provider(
342 "polls a scheduled activity task and completes it",
343 (_stack) =>
344 Effect.gen(function* () {
345 const started = (yield* postJson(`${baseUrl}/start-activity`, {})) as {
346 executionArn: string;
347 };
348
349 // GetActivityTask long-polls; repeat until the scheduled task's
350 // token arrives.
351 const task = yield* getJson(`${baseUrl}/activity-task`).pipe(
352 Effect.repeat({
353 schedule: Schedule.spaced("2 seconds"),
354 until: (r): boolean => (r as { taskToken: string | null }).taskToken !== null,
355 times: 8,
356 }),
357 );
358 const token = (task as { taskToken: string }).taskToken;
359 expect(token).toBeTruthy();
360
361 const success = (yield* postJson(`${baseUrl}/task-success`, {
362 token,
363 output: JSON.stringify({ workedBy: "activity-worker" }),
364 })) as { sent: boolean };
365 expect(success.sent).toBe(true);
366
367 const final = yield* pollExecution("describe-activity", started.executionArn);
368 expect(final.status).toBe("SUCCEEDED");
369 expect(JSON.parse(final.output!)).toEqual({ workedBy: "activity-worker" });
370 }),
371 { timeout: 120_000 },
372 );
373 });
374 });
375
376 describe("ListExecutions + GetExecutionHistory + RedriveExecution", () => {
377 test.provider(
378 "lists executions, reads history, and rejects redrive of a succeeded execution",
379 (_stack) =>
380 Effect.gen(function* () {
381 const started = (yield* postJson(`${baseUrl}/start`, {
382 input: JSON.stringify({ order: 4 }),
383 })) as { executionArn: string };
384
385 const final = yield* pollExecution("describe-standard", started.executionArn);
386 expect(final.status).toBe("SUCCEEDED");
387
388 const list = (yield* getJson(`${baseUrl}/list-executions`)) as {
389 count: number;
390 executionArns: string[];
391 };
392 expect(list.count).toBeGreaterThanOrEqual(1);
393 expect(list.executionArns).toContain(started.executionArn);
394
395 const history = (yield* getJson(
396 `${baseUrl}/history?executionArn=${encodeURIComponent(started.executionArn)}`,
397 )) as { count: number; lastType: string | null };
398 expect(history.count).toBeGreaterThan(0);
399 expect(history.lastType).toBe("ExecutionSucceeded");
400
401 // a SUCCEEDED execution is not redrivable — the typed
402 // ExecutionNotRedrivable tag proves the IAM grant and error union
403 const redrive = (yield* postJson(`${baseUrl}/redrive`, {
404 executionArn: started.executionArn,
405 })) as { redriven: boolean; error?: string };
406 expect(redrive.redriven).toBe(false);
407 expect(redrive.error).toBe("ExecutionNotRedrivable");
408 }),
409 { timeout: 90_000 },
410 );
411 });
412
413 describe("Distributed Map Runs", () => {
414 test.provider(
415 "ListMapRuns finds the run; DescribeMapRun and UpdateMapRun round-trip",
416 (_stack) =>
417 Effect.gen(function* () {
418 const started = (yield* postJson(`${baseUrl}/start-map`, {})) as {
419 executionArn: string;
420 };
421
422 const final = yield* pollExecution("describe-map", started.executionArn);
423 expect(final.status).toBe("SUCCEEDED");
424
425 const runs = (yield* getJson(
426 `${baseUrl}/map-runs?executionArn=${encodeURIComponent(started.executionArn)}`,
427 )) as { mapRunArns: string[] };
428 expect(runs.mapRunArns.length).toBe(1);
429 const mapRunArn = runs.mapRunArns[0]!;
430
431 const mapRun = (yield* getJson(
432 `${baseUrl}/describe-map-run?mapRunArn=${encodeURIComponent(mapRunArn)}`,
433 )) as { status: string; succeeded?: number };
434 expect(mapRun.status).toBe("SUCCEEDED");
435 expect(mapRun.succeeded).toBe(2);
436
437 // UpdateMapRun targets in-progress runs; on a completed run the
438 // API answers with the typed ValidationException — either result
439 // proves the grant + typed union.
440 const updated = (yield* postJson(`${baseUrl}/update-map-run`, {
441 mapRunArn,
442 maxConcurrency: 2,
443 })) as { updated: boolean; error?: string };
444 expect(updated.updated || updated.error === "ValidationException").toBe(true);
445 }),
446 { timeout: 120_000 },
447 );
448 });
449
450 describe("consumeExecutionEvents", { tags: ["provider:aws:eventbridge"] }, () => {
451 test.provider("the deploy created an EventBridge rule targeting the function", (_stack) =>
452 Effect.gen(function* () {
453 // Out-of-band via distilled: the fixture's consumeExecutionEvents
454 // must have materialized as a rule on the default bus with the
455 // Lambda as target.
456 const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({
457 TargetArn: functionArn,
458 });
459 expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1);
460 }),
461 );
462 });
463 },
464 );