The best way to control Claude Code, Codex and more from any computer or phone
| 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 logs from "@distilled.cloud/aws/cloudwatch-logs"; |
| 6 | import * as eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 7 | import * as s3 from "@distilled.cloud/aws/s3"; |
| 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 Schedule from "effect/Schedule"; |
| 13 | import MwaaServerlessTestFunctionLive, { |
| 14 | BINDINGS_BUCKET_NAME, |
| 15 | BINDINGS_WORKFLOW_NAME, |
| 16 | DEFINITION_KEY, |
| 17 | MwaaServerlessTestFunction, |
| 18 | WORKFLOW_DEFINITION, |
| 19 | } from "./handler"; |
| 20 | |
| 21 | const testOptions = { providers: AWS.providers() }; |
| 22 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 23 | const sharedStack = Core.scratchStack(testOptions, "MWAAServerlessBindings"); |
| 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 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 38 | // (cold re-init, IAM propagation on the freshly attached policy). Retry only |
| 39 | // 5xx; a genuine 4xx/assertion failure surfaces immediately. |
| 40 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 41 | HttpClient.execute(request).pipe( |
| 42 | Effect.flatMap((response) => |
| 43 | response.status >= 500 |
| 44 | ? response.text.pipe( |
| 45 | Effect.flatMap((body) => |
| 46 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 47 | ), |
| 48 | ) |
| 49 | : Effect.succeed(response), |
| 50 | ), |
| 51 | Effect.retry({ |
| 52 | while: (e) => e._tag === "TransientUpstream", |
| 53 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 54 | }), |
| 55 | ); |
| 56 | |
| 57 | const getJson = (path: string) => |
| 58 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 59 | |
| 60 | const postJson = (path: string) => |
| 61 | send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 62 | |
| 63 | // Direct distilled calls in beforeAll/afterAll run OUTSIDE test.provider, so |
| 64 | // the AWS client context (credentials, region) must be provided explicitly. |
| 65 | const withAws = <A, E>(effect: Effect.Effect<A, E, any>) => |
| 66 | Core.withProviders(effect, testOptions, sharedStack.name); |
| 67 | |
| 68 | // The definitions bucket + workflow YAML must exist in S3 BEFORE the fixture |
| 69 | // deploys — MWAA Serverless validates the definition at createWorkflow time. |
| 70 | const ensureDefinitionUploaded = Effect.gen(function* () { |
| 71 | const region = process.env.AWS_REGION ?? "us-west-2"; |
| 72 | yield* s3 |
| 73 | .createBucket({ |
| 74 | Bucket: BINDINGS_BUCKET_NAME, |
| 75 | ...(region === "us-east-1" |
| 76 | ? {} |
| 77 | : { CreateBucketConfiguration: { LocationConstraint: region } }), |
| 78 | }) |
| 79 | .pipe(Effect.catchTag("BucketAlreadyOwnedByYou", () => Effect.void)); |
| 80 | yield* s3.putObject({ |
| 81 | Bucket: BINDINGS_BUCKET_NAME, |
| 82 | Key: DEFINITION_KEY, |
| 83 | Body: new TextEncoder().encode(WORKFLOW_DEFINITION), |
| 84 | ContentType: "application/yaml", |
| 85 | }); |
| 86 | }); |
| 87 | |
| 88 | // Best-effort cleanup: empty and delete the out-of-band definitions bucket, |
| 89 | // and reap any orphaned auto-created log group left by an interrupted run |
| 90 | // (its name embeds a service-assigned random suffix we cannot predict). |
| 91 | const cleanupOutOfBand = Effect.gen(function* () { |
| 92 | yield* s3 |
| 93 | .deleteObject({ Bucket: BINDINGS_BUCKET_NAME, Key: DEFINITION_KEY }) |
| 94 | .pipe(Effect.catchTag("NoSuchBucket", () => Effect.void)); |
| 95 | yield* s3 |
| 96 | .deleteBucket({ Bucket: BINDINGS_BUCKET_NAME }) |
| 97 | .pipe(Effect.catchTag("NoSuchBucket", () => Effect.void)); |
| 98 | const found = yield* logs.describeLogGroups({ |
| 99 | logGroupNamePrefix: `/aws/mwaa-serverless/${BINDINGS_WORKFLOW_NAME}-`, |
| 100 | }); |
| 101 | for (const group of found.logGroups ?? []) { |
| 102 | if (group.logGroupName !== undefined) { |
| 103 | yield* logs |
| 104 | .deleteLogGroup({ logGroupName: group.logGroupName }) |
| 105 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 106 | } |
| 107 | } |
| 108 | }); |
| 109 | |
| 110 | describe.sequential( |
| 111 | "MWAAServerless Bindings", |
| 112 | { |
| 113 | tags: [ |
| 114 | "provider:aws", |
| 115 | "provider:aws:iam", |
| 116 | "provider:aws:lambda", |
| 117 | "provider:aws:mwaaserverless", |
| 118 | "provider:aws:s3", |
| 119 | "live", |
| 120 | ], |
| 121 | }, |
| 122 | () => { |
| 123 | beforeAll( |
| 124 | Effect.gen(function* () { |
| 125 | yield* Effect.logInfo("MWAAServerless test setup: destroying previous resources"); |
| 126 | yield* sharedStack.destroy(); |
| 127 | |
| 128 | yield* Effect.logInfo("MWAAServerless test setup: uploading workflow definition"); |
| 129 | yield* withAws(ensureDefinitionUploaded); |
| 130 | |
| 131 | yield* Effect.logInfo("MWAAServerless test setup: deploying fixture"); |
| 132 | const attrs = yield* sharedStack.deploy( |
| 133 | Effect.gen(function* () { |
| 134 | return yield* MwaaServerlessTestFunction; |
| 135 | }).pipe(Effect.provide(MwaaServerlessTestFunctionLive)), |
| 136 | ); |
| 137 | |
| 138 | expect(attrs.functionUrl).toBeTruthy(); |
| 139 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 140 | functionArn = attrs.functionArn; |
| 141 | |
| 142 | const readinessUrl = `${baseUrl}/bindings`; |
| 143 | yield* Effect.logInfo(`MWAAServerless test setup: probing readiness at ${readinessUrl}`); |
| 144 | yield* HttpClient.get(readinessUrl).pipe( |
| 145 | Effect.flatMap((response) => |
| 146 | response.status === 200 |
| 147 | ? Effect.succeed(response) |
| 148 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 149 | ), |
| 150 | Effect.tapError((error) => |
| 151 | Effect.logWarning( |
| 152 | `MWAAServerless test setup: fixture not ready yet (${String(error)})`, |
| 153 | ), |
| 154 | ), |
| 155 | Effect.retry({ schedule: readinessPolicy }), |
| 156 | ); |
| 157 | }), |
| 158 | { timeout: 300_000 }, |
| 159 | ); |
| 160 | |
| 161 | afterAll(sharedStack.destroy().pipe(Effect.andThen(Effect.orDie(withAws(cleanupOutOfBand)))), { |
| 162 | timeout: 240_000, |
| 163 | }); |
| 164 | |
| 165 | describe("binding registration", () => { |
| 166 | test.provider("all 7 capabilities initialize in the runtime", (_stack) => |
| 167 | Effect.gen(function* () { |
| 168 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 169 | expect(response.bound).toHaveLength(7); |
| 170 | }), |
| 171 | ); |
| 172 | }); |
| 173 | |
| 174 | describe("ListWorkflowRuns", () => { |
| 175 | test.provider("reads runs through the injected workflow ARN", (_stack) => |
| 176 | Effect.gen(function* () { |
| 177 | const response = (yield* getJson("/runs")) as { ids: string[] }; |
| 178 | expect(Array.isArray(response.ids)).toBe(true); |
| 179 | }), |
| 180 | ); |
| 181 | }); |
| 182 | |
| 183 | describe("ListWorkflowVersions", () => { |
| 184 | test.provider("lists the workflow's versions", (_stack) => |
| 185 | Effect.gen(function* () { |
| 186 | const response = (yield* getJson("/versions")) as { versions: string[] }; |
| 187 | expect(response.versions.length).toBeGreaterThanOrEqual(1); |
| 188 | }), |
| 189 | ); |
| 190 | }); |
| 191 | |
| 192 | // Typed probes on a nonexistent run: a typed service error (not |
| 193 | // AccessDeniedException) proves the IAM grant covers the workflow ARN and |
| 194 | // the ARN was injected from the binding. ListTaskInstances is the |
| 195 | // exception — the service answers a nonexistent run with an empty list |
| 196 | // (`ok`), which proves the same thing. |
| 197 | describe("typed not-found probes", () => { |
| 198 | const probes: ReadonlyArray< |
| 199 | readonly [name: string, method: "GET" | "POST", path: string, tags: readonly string[]] |
| 200 | > = [ |
| 201 | [ |
| 202 | "GetWorkflowRun", |
| 203 | "GET", |
| 204 | "/run-fake", |
| 205 | ["ResourceNotFoundException", "ValidationException"], |
| 206 | ], |
| 207 | [ |
| 208 | "ListTaskInstances", |
| 209 | "GET", |
| 210 | "/tasks-fake", |
| 211 | ["ok", "ResourceNotFoundException", "ValidationException"], |
| 212 | ], |
| 213 | [ |
| 214 | "GetTaskInstance", |
| 215 | "GET", |
| 216 | "/task-fake", |
| 217 | ["ResourceNotFoundException", "ValidationException"], |
| 218 | ], |
| 219 | [ |
| 220 | "StopWorkflowRun", |
| 221 | "POST", |
| 222 | "/run-stop-fake", |
| 223 | ["ResourceNotFoundException", "ValidationException"], |
| 224 | ], |
| 225 | ] as const; |
| 226 | |
| 227 | for (const [name, method, path, tags] of probes) { |
| 228 | test.provider(`${name} answers with a typed outcome (not AccessDenied)`, (_stack) => |
| 229 | Effect.gen(function* () { |
| 230 | const response = (yield* method === "GET" ? getJson(path) : postJson(path)) as { |
| 231 | tag: string; |
| 232 | detail: string; |
| 233 | }; |
| 234 | expect(response.detail).not.toContain("not authorized"); |
| 235 | expect(tags).toContain(response.tag); |
| 236 | }), |
| 237 | ); |
| 238 | } |
| 239 | }); |
| 240 | |
| 241 | describe("workflow run round trip", () => { |
| 242 | test.provider( |
| 243 | "start a run, observe it, stop it", |
| 244 | (_stack) => |
| 245 | Effect.gen(function* () { |
| 246 | // 1. StartWorkflowRun — an on-demand run of the fixture workflow. |
| 247 | const started = (yield* postJson("/run-start")) as { runId: string; status: string }; |
| 248 | expect(started.runId).toBeTruthy(); |
| 249 | |
| 250 | // 2. GetWorkflowRun observes the run through the binding. |
| 251 | const detail = (yield* getJson(`/run-detail?id=${started.runId}`)) as { |
| 252 | runId: string; |
| 253 | status?: string; |
| 254 | }; |
| 255 | expect(detail.runId).toBe(started.runId); |
| 256 | |
| 257 | // 3. StopWorkflowRun — a typed outcome either way: `ok` when the |
| 258 | // run was still stoppable, or a typed conflict/validation error |
| 259 | // if it already reached a terminal state. Reaching the service |
| 260 | // (not AccessDenied) is what the binding must prove. |
| 261 | const stopped = (yield* postJson(`/run-stop?id=${started.runId}`)) as { tag: string }; |
| 262 | expect(stopped.tag).not.toBe("AccessDeniedException"); |
| 263 | |
| 264 | // 4. The run shows up in ListWorkflowRuns. |
| 265 | const listed = (yield* getJson("/runs")) as { ids: string[] }; |
| 266 | expect(listed.ids).toContain(started.runId); |
| 267 | }), |
| 268 | { timeout: 120_000 }, |
| 269 | ); |
| 270 | }); |
| 271 | |
| 272 | describe("consumeWorkflowRunEvents", { tags: ["provider:aws:eventbridge"] }, () => { |
| 273 | test.provider("the deploy created an EventBridge rule targeting the function", (_stack) => |
| 274 | Effect.gen(function* () { |
| 275 | // Out-of-band via distilled: the fixture's consumeWorkflowRunEvents |
| 276 | // must have materialized as a rule on the default bus with the |
| 277 | // Lambda as target. |
| 278 | const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({ |
| 279 | TargetArn: functionArn, |
| 280 | }); |
| 281 | expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1); |
| 282 | }), |
| 283 | ); |
| 284 | }); |
| 285 | }, |
| 286 | ); |