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 ProfilesTestFunctionLive, { ProfilesTestFunction } from "./handler";
11
12const testOptions = { providers: AWS.providers() };
13const { test, beforeAll, afterAll } = Test.make(testOptions);
14const sharedStack = Core.scratchStack(testOptions, "Route53ProfilesBindings");
15
16// Lambda function URL cold-start (DNS, IAM propagation, init) can take well
17// over 60s on a fresh deploy.
18const readinessPolicy = Schedule.max([
19 Schedule.fixed("2 seconds"),
20 Schedule.recurs(75),
21]);
22
23let baseUrl: string;
24
25class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
26 readonly status: number;
27 readonly body: string;
28}> {}
29
30// The Lambda fixture occasionally answers a transient 5xx under load (cold
31// re-init, IAM propagation on the freshly attached policy that the handler's
32// `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine 4xx/assertion
33// failure surfaces immediately.
34const send = (request: HttpClientRequest.HttpClientRequest) =>
35 HttpClient.execute(request).pipe(
36 Effect.flatMap((response) =>
37 response.status >= 500
38 ? response.text.pipe(
39 Effect.flatMap((body) =>
40 Effect.fail(
41 new TransientUpstream({ status: response.status, body }),
42 ),
43 ),
44 )
45 : Effect.succeed(response),
46 ),
47 Effect.retry({
48 while: (e) => e._tag === "TransientUpstream",
49 schedule: Schedule.max([
50 Schedule.exponential("500 millis"),
51 Schedule.recurs(6),
52 ]),
53 }),
54 );
55
56const getJson = (path: string) =>
57 send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(
58 Effect.flatMap((r) => r.json),
59 );
60
61describe.sequential(
62 "Route53Profiles Bindings",
63 {
64 tags: [
65 "provider:aws",
66 "provider:aws:lambda",
67 "provider:aws:route53profiles",
68 "live",
69 ],
70 },
71 () => {
72 beforeAll(
73 Effect.gen(function* () {
74 yield* Effect.logInfo(
75 "Route53Profiles bindings setup: destroying previous resources",
76 );
77 yield* sharedStack.destroy();
78
79 yield* Effect.logInfo(
80 "Route53Profiles bindings setup: deploying fixture",
81 );
82 const attrs = yield* sharedStack.deploy(
83 Effect.gen(function* () {
84 return yield* ProfilesTestFunction;
85 }).pipe(Effect.provide(ProfilesTestFunctionLive)),
86 );
87
88 expect(attrs.functionUrl).toBeTruthy();
89 baseUrl = attrs.functionUrl!.replace(/\/+$/, "");
90
91 const readinessUrl = `${baseUrl}/bindings`;
92 yield* Effect.logInfo(
93 `Route53Profiles bindings 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 `Route53Profiles bindings 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: 240_000 });
115
116 describe("binding registration", () => {
117 test.provider("the capabilities initialize in the runtime", (_stack) =>
118 Effect.gen(function* () {
119 const response = (yield* getJson("/bindings")) as { bound: string[] };
120 expect(response.bound).toEqual([
121 "listProfileAssociations",
122 "listProfileResourceAssociations",
123 ]);
124 }),
125 );
126 });
127
128 describe("ListProfileAssociations", () => {
129 test.provider(
130 "lists the bound profile's VPC associations (injected profile id)",
131 (_stack) =>
132 Effect.gen(function* () {
133 // The fixture's profile is never associated with a VPC, so an
134 // empty list proves both the
135 // route53profiles:ListProfileAssociations grant and the ProfileId
136 // filter injection.
137 const response = (yield* getJson("/associations")) as {
138 count: number;
139 resourceIds: string[];
140 };
141 expect(response.count).toBe(0);
142 expect(response.resourceIds).toEqual([]);
143 }),
144 );
145 });
146
147 describe("ListProfileResourceAssociations", () => {
148 test.provider(
149 "lists the bound profile's attached DNS resources (injected profile id)",
150 (_stack) =>
151 Effect.gen(function* () {
152 // No DNS resources are ever attached to the fixture's profile, so
153 // an empty list proves both the
154 // route53profiles:ListProfileResourceAssociations grant and the
155 // ProfileId injection.
156 const response = (yield* getJson("/resource-associations")) as {
157 count: number;
158 resourceArns: string[];
159 };
160 expect(response.count).toBe(0);
161 expect(response.resourceArns).toEqual([]);
162 }),
163 );
164 });
165 },
166);