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