| 1 | import * as AWS from "@/AWS"; |
| 2 | import * as Core from "@/Test/Core"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { describe, expect } from "alchemy-test"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import Route53BindingsFunctionLive, { |
| 11 | Route53BindingsFunction, |
| 12 | } from "./bindings-handler"; |
| 13 | |
| 14 | const testOptions = { providers: AWS.providers() }; |
| 15 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 16 | const sharedStack = Core.scratchStack(testOptions, "Route53Bindings"); |
| 17 | |
| 18 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 19 | // over 60s on a fresh deploy under parallel-suite load. |
| 20 | const readinessPolicy = Schedule.max([ |
| 21 | Schedule.fixed("2 seconds"), |
| 22 | Schedule.recurs(75), |
| 23 | ]); |
| 24 | |
| 25 | let baseUrl: string; |
| 26 | |
| 27 | class 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. |
| 36 | const 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([Schedule.fixed("4 seconds"), Schedule.recurs(9)]), |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | describe.sequential( |
| 56 | "Route53 Bindings", |
| 57 | { |
| 58 | tags: [ |
| 59 | "provider:aws", |
| 60 | "provider:aws:lambda", |
| 61 | "provider:aws:route53", |
| 62 | "live", |
| 63 | ], |
| 64 | }, |
| 65 | () => { |
| 66 | beforeAll( |
| 67 | Effect.gen(function* () { |
| 68 | yield* Effect.logInfo( |
| 69 | "Route53 test setup: destroying previous resources", |
| 70 | ); |
| 71 | yield* sharedStack.destroy(); |
| 72 | |
| 73 | yield* Effect.logInfo("Route53 test setup: deploying fixture"); |
| 74 | const { functionUrl } = yield* sharedStack.deploy( |
| 75 | Effect.gen(function* () { |
| 76 | return yield* Route53BindingsFunction; |
| 77 | }).pipe(Effect.provide(Route53BindingsFunctionLive)), |
| 78 | ); |
| 79 | |
| 80 | expect(functionUrl).toBeTruthy(); |
| 81 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 82 | |
| 83 | // Probe /zone (not /bindings): it exercises the route53:GetHostedZone |
| 84 | // grant, so the readiness loop also absorbs IAM propagation on the |
| 85 | // freshly attached policy — otherwise the first real test can hit a |
| 86 | // 500 (AccessDenied via orDie) before the policy is live. |
| 87 | const readinessUrl = `${baseUrl}/zone`; |
| 88 | yield* Effect.logInfo( |
| 89 | `Route53 test setup: probing readiness at ${readinessUrl}`, |
| 90 | ); |
| 91 | yield* HttpClient.get(readinessUrl).pipe( |
| 92 | Effect.flatMap((response) => |
| 93 | response.status === 200 |
| 94 | ? Effect.succeed(response) |
| 95 | : Effect.fail( |
| 96 | new Error(`Function not ready: ${response.status}`), |
| 97 | ), |
| 98 | ), |
| 99 | Effect.tapError((error) => |
| 100 | Effect.logWarning( |
| 101 | `Route53 test setup: fixture not ready yet (${String(error)})`, |
| 102 | ), |
| 103 | ), |
| 104 | Effect.retry({ schedule: readinessPolicy }), |
| 105 | ); |
| 106 | }), |
| 107 | { timeout: 240_000 }, |
| 108 | ); |
| 109 | |
| 110 | afterAll(sharedStack.destroy(), { timeout: 240_000 }); |
| 111 | |
| 112 | describe("binding registration", () => { |
| 113 | test.provider("all 10 capabilities initialize in the runtime", (_stack) => |
| 114 | Effect.gen(function* () { |
| 115 | const response = yield* send( |
| 116 | HttpClientRequest.get(`${baseUrl}/bindings`), |
| 117 | ).pipe(Effect.flatMap((r) => r.json)); |
| 118 | expect((response as any).bound).toHaveLength(10); |
| 119 | }), |
| 120 | ); |
| 121 | }); |
| 122 | |
| 123 | describe("GetHostedZone", () => { |
| 124 | test.provider("reads the bound zone's delegation set", (_stack) => |
| 125 | Effect.gen(function* () { |
| 126 | const response = (yield* send( |
| 127 | HttpClientRequest.get(`${baseUrl}/zone`), |
| 128 | ).pipe(Effect.flatMap((r) => r.json))) as any; |
| 129 | expect(response.name).toBe("alchemy-route53-bindings.alchemy."); |
| 130 | // Public zones get exactly 4 authoritative name servers. |
| 131 | expect(response.nameServerCount).toBe(4); |
| 132 | }), |
| 133 | ); |
| 134 | }); |
| 135 | |
| 136 | describe("ListHostedZones", () => { |
| 137 | test.provider("lists the account's zones including ours", (_stack) => |
| 138 | Effect.gen(function* () { |
| 139 | const response = (yield* send( |
| 140 | HttpClientRequest.get(`${baseUrl}/zones`), |
| 141 | ).pipe(Effect.flatMap((r) => r.json))) as any; |
| 142 | expect(response.count).toBeGreaterThanOrEqual(1); |
| 143 | expect(response.found).toBe(true); |
| 144 | }), |
| 145 | ); |
| 146 | }); |
| 147 | |
| 148 | describe("ListHostedZonesByName", () => { |
| 149 | test.provider("finds the fixture zone by DNS name", (_stack) => |
| 150 | Effect.gen(function* () { |
| 151 | const response = (yield* send( |
| 152 | HttpClientRequest.get(`${baseUrl}/zones/by-name`), |
| 153 | ).pipe(Effect.flatMap((r) => r.json))) as any; |
| 154 | expect(response.firstName).toBe("alchemy-route53-bindings.alchemy."); |
| 155 | }), |
| 156 | ); |
| 157 | }); |
| 158 | |
| 159 | describe("ListHostedZonesByVPC", () => { |
| 160 | test.provider( |
| 161 | "round-trips the VPC ownership rejection for a foreign VPC id", |
| 162 | (_stack) => |
| 163 | Effect.gen(function* () { |
| 164 | // Route 53 rejects VPC ids the account doesn't own with the typed |
| 165 | // AccessDeniedException ("not owned by you") — reaching that |
| 166 | // ownership check proves the grant + query encoding end-to-end. |
| 167 | // notOwned=false would mean a genuine IAM denial: fail loudly. |
| 168 | const response = (yield* send( |
| 169 | HttpClientRequest.get(`${baseUrl}/zones/by-vpc`), |
| 170 | ).pipe(Effect.flatMap((r) => r.json))) as any; |
| 171 | expect(response.count).toBe(0); |
| 172 | if (response.rejected && !response.notOwned) { |
| 173 | // Include the upstream rejection detail in the failure output. |
| 174 | expect.fail( |
| 175 | `unexpected rejection (genuine IAM denial?): ${response.detail}`, |
| 176 | ); |
| 177 | } |
| 178 | }), |
| 179 | ); |
| 180 | }); |
| 181 | |
| 182 | describe("TestDNSAnswer", () => { |
| 183 | test.provider("answers the zone apex NS query", (_stack) => |
| 184 | Effect.gen(function* () { |
| 185 | const response = (yield* send( |
| 186 | HttpClientRequest.get(`${baseUrl}/dns-test`), |
| 187 | ).pipe(Effect.flatMap((r) => r.json))) as any; |
| 188 | expect(response.responseCode).toBe("NOERROR"); |
| 189 | expect(response.recordCount).toBe(4); |
| 190 | }), |
| 191 | ); |
| 192 | }); |
| 193 | |
| 194 | describe("ChangeResourceRecordSets + GetChange + ListResourceRecordSets", () => { |
| 195 | test.provider( |
| 196 | "upserts a TXT record, waits INSYNC, lists it, deletes it", |
| 197 | (_stack) => |
| 198 | Effect.gen(function* () { |
| 199 | const response = (yield* send( |
| 200 | HttpClientRequest.post(`${baseUrl}/record/roundtrip`), |
| 201 | ).pipe(Effect.flatMap((r) => r.json))) as any; |
| 202 | expect(response.changeStatus).toBe("INSYNC"); |
| 203 | expect(response.found).toBe(true); |
| 204 | expect(response.deleted).toBe(true); |
| 205 | }), |
| 206 | { timeout: 150_000 }, |
| 207 | ); |
| 208 | }); |
| 209 | |
| 210 | describe("GetHealthCheckStatus", () => { |
| 211 | test.provider( |
| 212 | "reads checker observations for the bound check", |
| 213 | (_stack) => |
| 214 | Effect.gen(function* () { |
| 215 | // A freshly created check may not have observations yet — the call |
| 216 | // round-tripping (HealthCheckId injection + check-scoped IAM) is the |
| 217 | // assertion. |
| 218 | const response = (yield* send( |
| 219 | HttpClientRequest.get(`${baseUrl}/health/status`), |
| 220 | ).pipe(Effect.flatMap((r) => r.json))) as any; |
| 221 | expect(response.observations).toBeGreaterThanOrEqual(0); |
| 222 | }), |
| 223 | ); |
| 224 | }); |
| 225 | |
| 226 | describe("GetHealthCheckLastFailureReason", () => { |
| 227 | test.provider( |
| 228 | "reads last failure reasons for the bound check", |
| 229 | (_stack) => |
| 230 | Effect.gen(function* () { |
| 231 | const response = (yield* send( |
| 232 | HttpClientRequest.get(`${baseUrl}/health/failure-reason`), |
| 233 | ).pipe(Effect.flatMap((r) => r.json))) as any; |
| 234 | expect(response.observations).toBeGreaterThanOrEqual(0); |
| 235 | }), |
| 236 | ); |
| 237 | }); |
| 238 | }, |
| 239 | ); |