The best way to control Claude Code, Codex and more from any computer or phone
| 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 Wafv2BindingsFunctionLive, { Wafv2BindingsFunction } from "./handler"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "Wafv2Bindings"); |
| 15 | |
| 16 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 17 | // over 60s on a fresh deploy. |
| 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). Retry only |
| 29 | // 5xx; a genuine 4xx/assertion failure surfaces immediately. |
| 30 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 31 | HttpClient.execute(request).pipe( |
| 32 | Effect.flatMap((response) => |
| 33 | response.status >= 500 |
| 34 | ? response.text.pipe( |
| 35 | Effect.flatMap((body) => |
| 36 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 37 | ), |
| 38 | ) |
| 39 | : Effect.succeed(response), |
| 40 | ), |
| 41 | Effect.retry({ |
| 42 | while: (e) => e._tag === "TransientUpstream", |
| 43 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 44 | }), |
| 45 | ); |
| 46 | |
| 47 | const getJson = (path: string) => |
| 48 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 49 | |
| 50 | const postJson = (path: string, body: object) => |
| 51 | send( |
| 52 | HttpClientRequest.post(`${baseUrl}${path}`).pipe(HttpClientRequest.bodyJsonUnsafe(body)), |
| 53 | ).pipe(Effect.flatMap((r) => r.json)); |
| 54 | |
| 55 | describe.sequential( |
| 56 | "WAFv2 Bindings", |
| 57 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:wafv2", "live"] }, |
| 58 | () => { |
| 59 | beforeAll( |
| 60 | Effect.gen(function* () { |
| 61 | yield* Effect.logInfo("WAFv2 test setup: destroying previous resources"); |
| 62 | yield* sharedStack.destroy(); |
| 63 | |
| 64 | yield* Effect.logInfo("WAFv2 test setup: deploying fixture"); |
| 65 | const attrs = yield* sharedStack.deploy( |
| 66 | Effect.gen(function* () { |
| 67 | return yield* Wafv2BindingsFunction; |
| 68 | }).pipe(Effect.provide(Wafv2BindingsFunctionLive)), |
| 69 | ); |
| 70 | |
| 71 | expect(attrs.functionUrl).toBeTruthy(); |
| 72 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 73 | |
| 74 | const readinessUrl = `${baseUrl}/bindings`; |
| 75 | yield* Effect.logInfo(`WAFv2 test setup: probing readiness at ${readinessUrl}`); |
| 76 | yield* HttpClient.get(readinessUrl).pipe( |
| 77 | Effect.flatMap((response) => |
| 78 | response.status === 200 |
| 79 | ? Effect.succeed(response) |
| 80 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 81 | ), |
| 82 | Effect.tapError((error) => |
| 83 | Effect.logWarning(`WAFv2 test setup: fixture not ready yet (${String(error)})`), |
| 84 | ), |
| 85 | Effect.retry({ schedule: readinessPolicy }), |
| 86 | ); |
| 87 | }), |
| 88 | { timeout: 600_000 }, |
| 89 | ); |
| 90 | |
| 91 | afterAll(process.env.NO_DESTROY ? Effect.void : sharedStack.destroy(), { timeout: 600_000 }); |
| 92 | |
| 93 | describe("binding registration", () => { |
| 94 | test.provider("all 20 capabilities initialize in the runtime", (_stack) => |
| 95 | Effect.gen(function* () { |
| 96 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 97 | expect(response.bound).toHaveLength(20); |
| 98 | expect(response.bound).toContain("getIPSet"); |
| 99 | expect(response.bound).toContain("updateIPSet"); |
| 100 | expect(response.bound).toContain("checkCapacity"); |
| 101 | expect(response.bound).toContain("getWebACLForResource"); |
| 102 | }), |
| 103 | ); |
| 104 | }); |
| 105 | |
| 106 | describe("GetIPSet / UpdateIPSet", () => { |
| 107 | test.provider( |
| 108 | "reads and replaces the dynamic block list (LockToken handled)", |
| 109 | (_stack) => |
| 110 | Effect.gen(function* () { |
| 111 | const initial = (yield* getJson("/ip-set")) as { |
| 112 | addresses: string[]; |
| 113 | description: string; |
| 114 | }; |
| 115 | expect(initial.addresses).toEqual(["192.0.2.44/32"]); |
| 116 | expect(initial.description).toBe("bindings fixture"); |
| 117 | |
| 118 | const updated = (yield* postJson("/ip-set", { |
| 119 | addresses: ["192.0.2.44/32", "198.51.100.7/32"], |
| 120 | })) as { addresses: string[] }; |
| 121 | expect(updated.addresses).toEqual(["192.0.2.44/32", "198.51.100.7/32"]); |
| 122 | |
| 123 | // restore for idempotent re-runs |
| 124 | const restored = (yield* postJson("/ip-set", { addresses: ["192.0.2.44/32"] })) as { |
| 125 | addresses: string[]; |
| 126 | }; |
| 127 | expect(restored.addresses).toEqual(["192.0.2.44/32"]); |
| 128 | }), |
| 129 | { timeout: 120_000 }, |
| 130 | ); |
| 131 | }); |
| 132 | |
| 133 | describe("GetSampledRequests", () => { |
| 134 | test.provider("reads the (empty) sample for the rate rule", (_stack) => |
| 135 | Effect.gen(function* () { |
| 136 | const response = (yield* getJson("/sampled")) as { |
| 137 | sampled: number; |
| 138 | populationSize: number; |
| 139 | }; |
| 140 | expect(response.sampled).toBe(0); |
| 141 | }), |
| 142 | ); |
| 143 | }); |
| 144 | |
| 145 | describe("GetRateBasedStatementManagedKeys", () => { |
| 146 | test.provider("reads the currently rate-limited addresses", (_stack) => |
| 147 | Effect.gen(function* () { |
| 148 | const response = (yield* getJson("/rate-keys")) as { |
| 149 | v4: number; |
| 150 | v4Version: string | null; |
| 151 | }; |
| 152 | expect(response.v4).toBe(0); |
| 153 | expect(response.v4Version).toBe("IPV4"); |
| 154 | }), |
| 155 | ); |
| 156 | }); |
| 157 | |
| 158 | describe("ListResourcesForWebACL", () => { |
| 159 | test.provider("lists no associated resources", (_stack) => |
| 160 | Effect.gen(function* () { |
| 161 | const response = (yield* getJson("/resources")) as { resources: string[] }; |
| 162 | expect(response.resources).toEqual([]); |
| 163 | }), |
| 164 | ); |
| 165 | }); |
| 166 | |
| 167 | describe("GetTopPathStatisticsByTraffic", () => { |
| 168 | test.provider("returns data or the typed pricing-plan gate", (_stack) => |
| 169 | Effect.gen(function* () { |
| 170 | const response = (yield* getJson("/top-paths")) as { ok: boolean; tag: string | null }; |
| 171 | if (!response.ok) { |
| 172 | // account without the bot-statistics pricing plan — the typed |
| 173 | // tag proves the grant + wiring reached WAF |
| 174 | expect([ |
| 175 | "WAFFeatureNotIncludedInPricingPlanException", |
| 176 | "WAFInvalidOperationException", |
| 177 | "WAFNonexistentItemException", |
| 178 | ]).toContain(response.tag); |
| 179 | } |
| 180 | }), |
| 181 | ); |
| 182 | }); |
| 183 | |
| 184 | describe("PutPermissionPolicy / GetPermissionPolicy / DeletePermissionPolicy", () => { |
| 185 | test.provider("shares and unshares the rule group", (_stack) => |
| 186 | Effect.gen(function* () { |
| 187 | const response = (yield* getJson("/permission-policy")) as { |
| 188 | beforePolicy: string | null; |
| 189 | hasPolicy: boolean; |
| 190 | deleted: boolean; |
| 191 | }; |
| 192 | expect(response.beforePolicy).toBeNull(); |
| 193 | expect(response.hasPolicy).toBe(true); |
| 194 | expect(response.deleted).toBe(true); |
| 195 | }), |
| 196 | ); |
| 197 | }); |
| 198 | |
| 199 | describe("CheckCapacity", () => { |
| 200 | test.provider("computes the WCU cost of a geo-match rule", (_stack) => |
| 201 | Effect.gen(function* () { |
| 202 | const response = (yield* getJson("/capacity")) as { capacity: number }; |
| 203 | expect(response.capacity).toBeGreaterThan(0); |
| 204 | }), |
| 205 | ); |
| 206 | }); |
| 207 | |
| 208 | describe("CAPTCHA API keys", () => { |
| 209 | test.provider("mints, lists, decrypts, and deletes an API key", (_stack) => |
| 210 | Effect.gen(function* () { |
| 211 | const response = (yield* getJson("/api-keys")) as { |
| 212 | created: boolean; |
| 213 | listed: number; |
| 214 | domains: string[]; |
| 215 | deleted: boolean; |
| 216 | }; |
| 217 | expect(response.created).toBe(true); |
| 218 | expect(response.listed).toBeGreaterThan(0); |
| 219 | expect(response.domains).toContain("example.com"); |
| 220 | expect(response.deleted).toBe(true); |
| 221 | }), |
| 222 | ); |
| 223 | }); |
| 224 | |
| 225 | describe("managed rule group catalog", () => { |
| 226 | test.provider("lists groups and describes the common rule set", (_stack) => |
| 227 | Effect.gen(function* () { |
| 228 | const response = (yield* getJson("/managed")) as { |
| 229 | groups: number; |
| 230 | capacity: number; |
| 231 | currentDefaultVersion: string | null; |
| 232 | }; |
| 233 | expect(response.groups).toBeGreaterThan(0); |
| 234 | expect(response.capacity).toBeGreaterThan(0); |
| 235 | expect(response.currentDefaultVersion).toBeTruthy(); |
| 236 | }), |
| 237 | ); |
| 238 | |
| 239 | test.provider("reads the managed products catalog", (_stack) => |
| 240 | Effect.gen(function* () { |
| 241 | const response = (yield* getJson("/products")) as { all: number; aws: number }; |
| 242 | expect(response.all).toBeGreaterThan(0); |
| 243 | expect(response.aws).toBeGreaterThan(0); |
| 244 | }), |
| 245 | ); |
| 246 | }); |
| 247 | |
| 248 | describe("GetWebACLForResource", () => { |
| 249 | test.provider("returns not-found for an unassociated resource (typed)", (_stack) => |
| 250 | Effect.gen(function* () { |
| 251 | const response = (yield* getJson("/waf-for-resource")) as { found: boolean }; |
| 252 | expect(response.found).toBe(false); |
| 253 | }), |
| 254 | ); |
| 255 | }); |
| 256 | }, |
| 257 | ); |