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 mq from "@distilled.cloud/aws/mq"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import MQBindingsFunctionLive, { MQBindingsFunction } from "./bindings-handler"; |
| 10 | |
| 11 | const testOptions = { providers: AWS.providers() }; |
| 12 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 13 | |
| 14 | // A broker bills per instance-hour and takes 5-10 min to provision, so the |
| 15 | // live Lambda E2E skipped under `--fast` (matching Broker.test.ts). |
| 16 | const RUN_LIVE = !process.env.FAST; |
| 17 | |
| 18 | // A well-formed but nonexistent broker id (the same shape Broker.test.ts |
| 19 | // proves returns the typed NotFoundException for describeBroker). |
| 20 | const BOGUS_BROKER_ID = "b-00000000-0000-0000-0000-000000000000"; |
| 21 | |
| 22 | // Ungated typed-error probes: prove the distilled error unions the bindings |
| 23 | // depend on are typed, on every account, at near-zero cost. |
| 24 | test.provider( |
| 25 | "listUsers on a nonexistent broker fails with NotFoundException", |
| 26 | () => |
| 27 | Effect.gen(function* () { |
| 28 | const error = yield* Effect.flip(mq.listUsers({ BrokerId: BOGUS_BROKER_ID })); |
| 29 | expect(error._tag).toBe("NotFoundException"); |
| 30 | }), |
| 31 | { tags: ["provider:aws", "provider:aws:mq", "live"] }, |
| 32 | ); |
| 33 | |
| 34 | test.provider( |
| 35 | "createUser on a nonexistent broker fails with NotFoundException", |
| 36 | () => |
| 37 | Effect.gen(function* () { |
| 38 | const error = yield* Effect.flip( |
| 39 | mq.createUser({ |
| 40 | BrokerId: BOGUS_BROKER_ID, |
| 41 | Username: "alchemyprobe", |
| 42 | Password: "SuperSecretPassw0rd!", |
| 43 | }), |
| 44 | ); |
| 45 | expect(error._tag).toBe("NotFoundException"); |
| 46 | }), |
| 47 | { tags: ["provider:aws", "provider:aws:mq", "live"] }, |
| 48 | ); |
| 49 | |
| 50 | test.provider( |
| 51 | "rebootBroker on a nonexistent broker fails with NotFoundException", |
| 52 | () => |
| 53 | Effect.gen(function* () { |
| 54 | const error = yield* Effect.flip(mq.rebootBroker({ BrokerId: BOGUS_BROKER_ID })); |
| 55 | expect(error._tag).toBe("NotFoundException"); |
| 56 | }), |
| 57 | { tags: ["provider:aws", "provider:aws:mq", "live"] }, |
| 58 | ); |
| 59 | |
| 60 | test.provider( |
| 61 | "promote on a nonexistent broker fails with a typed tag", |
| 62 | () => |
| 63 | Effect.gen(function* () { |
| 64 | const error = yield* Effect.flip( |
| 65 | mq.promote({ BrokerId: BOGUS_BROKER_ID, Mode: "SWITCHOVER" }), |
| 66 | ); |
| 67 | expect(["NotFoundException", "BadRequestException"]).toContain(error._tag); |
| 68 | }), |
| 69 | { tags: ["provider:aws", "provider:aws:mq", "live"] }, |
| 70 | ); |
| 71 | |
| 72 | const sharedStack = Core.scratchStack(testOptions, "MQBindings"); |
| 73 | |
| 74 | let baseUrl: string; |
| 75 | |
| 76 | const get = (path: string) => |
| 77 | HttpClient.get(`${baseUrl}${path}`).pipe(Effect.flatMap((r) => r.json)); |
| 78 | const post = (path: string) => |
| 79 | HttpClient.post(`${baseUrl}${path}`).pipe(Effect.flatMap((r) => r.json)); |
| 80 | |
| 81 | describe( |
| 82 | "MQ Bindings (E2E)", |
| 83 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:mq", "live"] }, |
| 84 | () => { |
| 85 | beforeAll( |
| 86 | Effect.gen(function* () { |
| 87 | if (!RUN_LIVE) return; |
| 88 | yield* Effect.logInfo("MQ E2E setup: destroying previous run"); |
| 89 | yield* sharedStack.destroy(); |
| 90 | |
| 91 | yield* Effect.logInfo("MQ E2E setup: deploying broker + Lambda (broker takes ~5-10 min)"); |
| 92 | const { functionUrl } = yield* sharedStack.deploy( |
| 93 | Effect.gen(function* () { |
| 94 | return yield* MQBindingsFunction; |
| 95 | }).pipe(Effect.provide(MQBindingsFunctionLive)), |
| 96 | ); |
| 97 | |
| 98 | expect(functionUrl).toBeTruthy(); |
| 99 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 100 | |
| 101 | // Readiness probe — fresh function URLs take seconds to serve 200s. |
| 102 | yield* HttpClient.get(`${baseUrl}/bindings`).pipe( |
| 103 | Effect.flatMap((response) => |
| 104 | response.status === 200 |
| 105 | ? Effect.succeed(response) |
| 106 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 107 | ), |
| 108 | Effect.retry({ |
| 109 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(60)]), |
| 110 | }), |
| 111 | ); |
| 112 | }), |
| 113 | // broker create (~5-10 min) + Lambda deploy + readiness. |
| 114 | { timeout: 1_200_000 }, |
| 115 | ); |
| 116 | afterAll( |
| 117 | Effect.gen(function* () { |
| 118 | if (!RUN_LIVE) return; |
| 119 | // The Broker provider waits for the broker to settle (e.g. after the |
| 120 | // /reboot test) and then for it to disappear. |
| 121 | yield* sharedStack.destroy(); |
| 122 | }), |
| 123 | { timeout: 1_200_000 }, |
| 124 | ); |
| 125 | |
| 126 | test.provider.skipIf(!RUN_LIVE)("all 9 capabilities initialize in the runtime", () => |
| 127 | Effect.gen(function* () { |
| 128 | const response = (yield* get("/bindings")) as any; |
| 129 | expect(response.bound).toHaveLength(9); |
| 130 | }), |
| 131 | ); |
| 132 | |
| 133 | test.provider.skipIf(!RUN_LIVE)("DescribeBroker reads the bound broker's state", () => |
| 134 | Effect.gen(function* () { |
| 135 | const response = (yield* get("/broker")) as any; |
| 136 | expect(typeof response.brokerName).toBe("string"); |
| 137 | expect(response.brokerState).toBe("RUNNING"); |
| 138 | }), |
| 139 | ); |
| 140 | |
| 141 | test.provider.skipIf(!RUN_LIVE)( |
| 142 | "ListBrokers and ListUsers see the broker and its admin user", |
| 143 | () => |
| 144 | Effect.gen(function* () { |
| 145 | const brokers = (yield* get("/brokers")) as any; |
| 146 | expect(brokers.count).toBeGreaterThanOrEqual(1); |
| 147 | const users = (yield* get("/users")) as any; |
| 148 | expect(users.count).toBeGreaterThanOrEqual(1); |
| 149 | }), |
| 150 | ); |
| 151 | |
| 152 | test.provider.skipIf(!RUN_LIVE)( |
| 153 | "CreateUser, DescribeUser, and DeleteUser round-trip a staged user", |
| 154 | () => |
| 155 | Effect.gen(function* () { |
| 156 | const response = (yield* post("/users")) as any; |
| 157 | expect(response.created).toBe(true); |
| 158 | expect(response.describedUsername).toBe("alchemytenant"); |
| 159 | expect(response.deleted).toBe(true); |
| 160 | }), |
| 161 | ); |
| 162 | |
| 163 | test.provider.skipIf(!RUN_LIVE)( |
| 164 | "UpdateUser surfaces the typed not-found for a nonexistent user", |
| 165 | () => |
| 166 | Effect.gen(function* () { |
| 167 | const response = (yield* post("/users/typed-not-found")) as any; |
| 168 | expect(response.typed).toBe(true); |
| 169 | }), |
| 170 | ); |
| 171 | |
| 172 | test.provider.skipIf(!RUN_LIVE)( |
| 173 | "Promote on a non-CRDR broker surfaces the typed BadRequest", |
| 174 | () => |
| 175 | Effect.gen(function* () { |
| 176 | const response = (yield* post("/promote")) as any; |
| 177 | expect(response.typed).toBe(true); |
| 178 | }), |
| 179 | ); |
| 180 | |
| 181 | // LAST live test — kicks off a real reboot; afterAll's destroy waits for |
| 182 | // the broker to settle before deleting it. |
| 183 | test.provider.skipIf(!RUN_LIVE)("RebootBroker reboots the broker", () => |
| 184 | Effect.gen(function* () { |
| 185 | const response = (yield* post("/reboot")) as any; |
| 186 | expect(response.rebooting).toBe(true); |
| 187 | }), |
| 188 | ); |
| 189 | }, |
| 190 | ); |