1import * as AWS from "@/AWS";
2import * as Core from "@/Test/Core";
3import * as Test from "@/Test/Alchemy";
4import { describe, expect } from "alchemy-test";
5import * as Data from "effect/Data";
6import * as Effect from "effect/Effect";
7import * as Schedule from "effect/Schedule";
8import * as HttpClient from "effect/http/HttpClient";
9import * as HttpClientRequest from "effect/http/HttpClientRequest";
10import NotificationsTestFunctionLive, {
11 NotificationsTestFunction,
12} from "./handler";
13
14const testOptions = { providers: AWS.providers() };
15const { test, beforeAll, afterAll } = Test.make(testOptions);
16const sharedStack = Core.scratchStack(testOptions, "NotificationsBindings");
17
18// Lambda function URL cold-start (DNS, IAM propagation, init) can take well
19// over 60s on a fresh deploy.
20const readinessPolicy = Schedule.max([
21 Schedule.fixed("2 seconds"),
22 Schedule.recurs(75),
23]);
24
25let baseUrl: string;
26
27class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
28 readonly status: number;
29 readonly body: string;
30}> {}
31
32// The shared Lambda fixture occasionally answers a transient 5xx under load
33// (cold re-init, IAM propagation on the freshly attached policy that the
34// handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine
35// 4xx/assertion failure surfaces immediately.
36const send = (request: HttpClientRequest.HttpClientRequest) =>
37 HttpClient.execute(request).pipe(
38 Effect.flatMap((response) =>
39 response.status >= 500
40 ? response.text.pipe(
41 Effect.flatMap((body) =>
42 Effect.fail(
43 new TransientUpstream({ status: response.status, body }),
44 ),
45 ),
46 )
47 : Effect.succeed(response),
48 ),
49 Effect.retry({
50 while: (e) => e._tag === "TransientUpstream",
51 schedule: Schedule.max([
52 Schedule.exponential("500 millis"),
53 Schedule.recurs(6),
54 ]),
55 }),
56 );
57
58const getJson = (path: string) =>
59 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(
60 Effect.flatMap((r) => r.json),
61 );
62
63describe.sequential(
64 "Notifications Bindings",
65 {
66 tags: [
67 "provider:aws",
68 "provider:aws:lambda",
69 "provider:aws:notifications",
70 "live",
71 ],
72 },
73 () => {
74 beforeAll(
75 Effect.gen(function* () {
76 yield* Effect.logInfo(
77 "Notifications test setup: destroying previous resources",
78 );
79 yield* sharedStack.destroy();
80
81 yield* Effect.logInfo("Notifications test setup: deploying fixture");
82 const { functionUrl } = yield* sharedStack.deploy(
83 Effect.gen(function* () {
84 return yield* NotificationsTestFunction;
85 }).pipe(Effect.provide(NotificationsTestFunctionLive)),
86 );
87
88 expect(functionUrl).toBeTruthy();
89 baseUrl = functionUrl!.replace(/\/+$/, "");
90
91 const readinessUrl = `${baseUrl}/bindings`;
92 yield* Effect.logInfo(
93 `Notifications test setup: probing readiness at ${readinessUrl}`,
94 );
95 yield* HttpClient.get(readinessUrl).pipe(
96 Effect.flatMap((response) =>
97 response.status === 200
98 ? Effect.succeed(response)
99 : Effect.fail(
100 new Error(`Function not ready: ${response.status}`),
101 ),
102 ),
103 Effect.tapError((error) =>
104 Effect.logWarning(
105 `Notifications test setup: fixture not ready yet (${String(error)})`,
106 ),
107 ),
108 Effect.retry({ schedule: readinessPolicy }),
109 );
110 }),
111 { timeout: 240_000 },
112 );
113
114 afterAll(sharedStack.destroy(), { timeout: 120_000 });
115
116 describe("binding registration", () => {
117 test.provider("all 10 capabilities initialize in the runtime", (_stack) =>
118 Effect.gen(function* () {
119 const response = yield* getJson("/bindings");
120 expect((response as any).bound).toHaveLength(10);
121 }),
122 );
123 });
124
125 describe("ListNotificationEvents", () => {
126 test.provider(
127 "lists the last week's notification events",
128 (_stack) =>
129 Effect.gen(function* () {
130 const response = (yield* getJson("/events")) as any;
131 expect(response.count).toBeGreaterThanOrEqual(0);
132 }),
133 { timeout: 60_000 },
134 );
135 });
136
137 describe("GetNotificationEvent", () => {
138 test.provider(
139 "surfaces the typed not-found for a nonexistent event (proving the grant)",
140 (_stack) =>
141 Effect.gen(function* () {
142 const response = (yield* getJson("/event-nonexistent")) as any;
143 // Either typed rejection proves the grant reached the API — an
144 // IAM gap would surface AccessDeniedException and 500 the route.
145 expect([
146 "ResourceNotFoundException",
147 "ValidationException",
148 ]).toContain(response.tag);
149 }),
150 { timeout: 60_000 },
151 );
152 });
153
154 describe("ListManagedNotificationConfigurations", () => {
155 test.provider(
156 "lists the AWS Health managed configurations",
157 (_stack) =>
158 Effect.gen(function* () {
159 const response = (yield* getJson("/managed-configs")) as any;
160 // Every account has the AWS Health categories.
161 expect(response.count).toBeGreaterThan(0);
162 }),
163 { timeout: 60_000 },
164 );
165 });
166
167 describe("GetManagedNotificationConfiguration", () => {
168 test.provider(
169 "fetches the first managed configuration",
170 (_stack) =>
171 Effect.gen(function* () {
172 const response = (yield* getJson("/managed-config")) as any;
173 expect(response.tag).toBe("Ok");
174 expect(typeof response.name).toBe("string");
175 }),
176 { timeout: 60_000 },
177 );
178 });
179
180 describe("ListManagedNotificationEvents", () => {
181 test.provider(
182 "lists the last week's managed notification events",
183 (_stack) =>
184 Effect.gen(function* () {
185 const response = (yield* getJson("/managed-events")) as any;
186 expect(response.count).toBeGreaterThanOrEqual(0);
187 }),
188 { timeout: 60_000 },
189 );
190 });
191
192 describe("GetManagedNotificationEvent", () => {
193 test.provider(
194 "fetches the first managed event (or reports none, both proving the grant)",
195 (_stack) =>
196 Effect.gen(function* () {
197 const response = (yield* getJson("/managed-event")) as any;
198 expect(["Ok", "NoEvents"]).toContain(response.tag);
199 if (response.tag === "Ok") {
200 expect(response.configurationArn).toContain(
201 ":managed-notification-configuration/",
202 );
203 }
204 }),
205 { timeout: 60_000 },
206 );
207 });
208
209 describe("ListManagedNotificationChildEvents", () => {
210 test.provider(
211 "lists an aggregate's children (or the typed rejection for a non-aggregate)",
212 (_stack) =>
213 Effect.gen(function* () {
214 const response = (yield* getJson("/managed-child-events")) as any;
215 expect([
216 "Ok",
217 "ValidationException",
218 "ResourceNotFoundException",
219 "NoEvents",
220 ]).toContain(response.tag);
221 }),
222 { timeout: 60_000 },
223 );
224 });
225
226 describe("GetManagedNotificationChildEvent", () => {
227 test.provider(
228 "surfaces the typed not-found for a nonexistent child event (proving the grant)",
229 (_stack) =>
230 Effect.gen(function* () {
231 const response = (yield* getJson(
232 "/managed-child-event-nonexistent",
233 )) as any;
234 expect([
235 "ResourceNotFoundException",
236 "ValidationException",
237 ]).toContain(response.tag);
238 }),
239 { timeout: 60_000 },
240 );
241 });
242
243 describe("ListManagedNotificationChannelAssociations", () => {
244 test.provider(
245 "lists the first managed configuration's channel associations",
246 (_stack) =>
247 Effect.gen(function* () {
248 const response = (yield* getJson(
249 "/managed-channel-associations",
250 )) as any;
251 expect(response.tag).toBe("Ok");
252 expect(response.count).toBeGreaterThanOrEqual(0);
253 }),
254 { timeout: 60_000 },
255 );
256 });
257
258 describe("ListChannels", () => {
259 test.provider(
260 "lists the fixture configuration's channels (proving ARN injection + the scoped grant)",
261 (_stack) =>
262 Effect.gen(function* () {
263 const response = (yield* getJson("/channels")) as any;
264 expect(response.count).toBeGreaterThanOrEqual(0);
265 }),
266 { timeout: 60_000 },
267 );
268 });
269 },
270);