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