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 mwaa from "@distilled.cloud/aws/mwaa"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import MWAATestFunctionLive, { MWAATestFunction } from "./bindings-handler.ts"; |
| 10 | |
| 11 | const testOptions = { providers: AWS.providers() }; |
| 12 | const { test } = Test.make(testOptions); |
| 13 | |
| 14 | // A well-formed-but-nonexistent environment name the ungated probes are |
| 15 | // driven against. |
| 16 | const NONEXISTENT_ENVIRONMENT = "alchemy-mwaa-nonexistent-probe"; |
| 17 | |
| 18 | // --------------------------------------------------------------------------- |
| 19 | // Ungated typed-error probes: every data-plane operation the four bindings |
| 20 | // wrap is exercised directly through distilled against a nonexistent |
| 21 | // environment, and must answer with the typed not-found tag. These prove the |
| 22 | // distilled error unions (and request serialization) at near-zero cost on |
| 23 | // every CI pass, while the full runtime fixture below is gated behind the |
| 24 | // 20-30 minute environment provisioning. |
| 25 | // --------------------------------------------------------------------------- |
| 26 | |
| 27 | describe( |
| 28 | "MWAA data-plane operations (typed-error probes)", |
| 29 | { tags: ["provider:aws", "provider:aws:mwaa", "live"] }, |
| 30 | () => { |
| 31 | test.provider( |
| 32 | "createCliToken on a nonexistent environment fails with ResourceNotFoundException", |
| 33 | () => |
| 34 | Effect.gen(function* () { |
| 35 | const error = yield* Effect.flip(mwaa.createCliToken({ Name: NONEXISTENT_ENVIRONMENT })); |
| 36 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 37 | }), |
| 38 | ); |
| 39 | |
| 40 | test.provider( |
| 41 | "createWebLoginToken on a nonexistent environment fails with ResourceNotFoundException", |
| 42 | () => |
| 43 | Effect.gen(function* () { |
| 44 | const error = yield* Effect.flip( |
| 45 | mwaa.createWebLoginToken({ Name: NONEXISTENT_ENVIRONMENT }), |
| 46 | ); |
| 47 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 48 | }), |
| 49 | ); |
| 50 | |
| 51 | test.provider( |
| 52 | "invokeRestApi on a nonexistent environment fails with ResourceNotFoundException", |
| 53 | () => |
| 54 | Effect.gen(function* () { |
| 55 | const error = yield* Effect.flip( |
| 56 | mwaa.invokeRestApi({ Name: NONEXISTENT_ENVIRONMENT, Method: "GET", Path: "/dags" }), |
| 57 | ); |
| 58 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 59 | }), |
| 60 | ); |
| 61 | }, |
| 62 | ); |
| 63 | |
| 64 | // --------------------------------------------------------------------------- |
| 65 | // Full runtime fixture: a Lambda bound to all four MWAA bindings against a |
| 66 | // live Airflow environment. The fixture builds its own private networking |
| 67 | // (VPC + 2 private subnets in 2 AZs + IGW + one NAT gateway). An environment |
| 68 | // takes ~20-30 minutes to provision and ~20-30 minutes to delete and is billed |
| 69 | // hourly while it exists (~$0.49/h + ~$0.045/h NAT), so this is skipped under |
| 70 | // `--fast` (same gate as the Environment lifecycle test) and always |
| 71 | // destroys what it created. |
| 72 | // --------------------------------------------------------------------------- |
| 73 | |
| 74 | const sharedStack = Core.scratchStack(testOptions, "MWAABindings"); |
| 75 | |
| 76 | test.provider.skipIf(!!process.env.FAST)( |
| 77 | "token, REST API, and describe bindings against a live environment", |
| 78 | () => |
| 79 | Effect.gen(function* () { |
| 80 | yield* sharedStack.destroy(); |
| 81 | |
| 82 | yield* Effect.gen(function* () { |
| 83 | const { functionUrl } = yield* sharedStack.deploy( |
| 84 | Effect.gen(function* () { |
| 85 | return yield* MWAATestFunction; |
| 86 | }).pipe(Effect.provide(MWAATestFunctionLive)), |
| 87 | ); |
| 88 | expect(functionUrl).toBeTruthy(); |
| 89 | const baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 90 | |
| 91 | const getJson = (path: string) => |
| 92 | HttpClient.get(`${baseUrl}${path}`).pipe( |
| 93 | Effect.flatMap((response) => |
| 94 | response.status >= 500 |
| 95 | ? Effect.fail(new Error(`transient upstream ${response.status}`)) |
| 96 | : Effect.succeed(response), |
| 97 | ), |
| 98 | Effect.retry({ |
| 99 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 100 | }), |
| 101 | Effect.flatMap((r) => r.json), |
| 102 | ); |
| 103 | |
| 104 | // All four capabilities initialized in the runtime. |
| 105 | const bindings = (yield* getJson("/bindings")) as { bound: string[] }; |
| 106 | expect(bindings.bound).toHaveLength(4); |
| 107 | |
| 108 | // GetEnvironment — proves the environment-ARN grant + Name injection. |
| 109 | const env = (yield* getJson("/environment")) as { status?: string; errorTag?: string }; |
| 110 | expect(env.errorTag).toBeUndefined(); |
| 111 | expect(env.status).toBe("AVAILABLE"); |
| 112 | |
| 113 | // CreateCliToken — 60-second Airflow CLI token. |
| 114 | const cli = (yield* getJson("/cli-token")) as { |
| 115 | hasToken?: boolean; |
| 116 | hostname?: string; |
| 117 | errorTag?: string; |
| 118 | }; |
| 119 | expect(cli.errorTag).toBeUndefined(); |
| 120 | expect(cli.hasToken).toBe(true); |
| 121 | expect(cli.hostname).toBeTruthy(); |
| 122 | |
| 123 | // CreateWebLoginToken — proves the Airflow-role-ARN grant (Admin). |
| 124 | const web = (yield* getJson("/web-login-token")) as { |
| 125 | hasToken?: boolean; |
| 126 | hostname?: string; |
| 127 | errorTag?: string; |
| 128 | }; |
| 129 | expect(web.errorTag).toBeUndefined(); |
| 130 | expect(web.hasToken).toBe(true); |
| 131 | expect(web.hostname).toBeTruthy(); |
| 132 | |
| 133 | // InvokeRestApi — GET /dags through the Airflow REST API. |
| 134 | const dags = (yield* getJson("/dags")) as { statusCode?: number; errorTag?: string }; |
| 135 | expect(dags.errorTag).toBeUndefined(); |
| 136 | expect(dags.statusCode).toBe(200); |
| 137 | }).pipe(Effect.ensuring(sharedStack.destroy().pipe(Effect.orDie))); |
| 138 | }), |
| 139 | // network + environment create (~20-30 min) + binding checks + |
| 140 | // delete-until-gone (~20-30 min) + network teardown. |
| 141 | { |
| 142 | tags: [ |
| 143 | "provider:aws", |
| 144 | "provider:aws:ec2", |
| 145 | "provider:aws:iam", |
| 146 | "provider:aws:lambda", |
| 147 | "provider:aws:mwaa", |
| 148 | "provider:aws:s3", |
| 149 | "live", |
| 150 | ], |
| 151 | timeout: 6_600_000, |
| 152 | }, |
| 153 | ); |