| 1 | import * as AWS from "@/AWS"; |
| 2 | import * as Core from "@/Test/Core"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 5 | import { describe, expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import ElastiCacheBindingsTestFunctionLive, { |
| 10 | ElastiCacheBindingsTestFunction, |
| 11 | } from "./bindings-handler.ts"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "ElastiCacheBindings"); |
| 16 | |
| 17 | const NONEXISTENT_NAME = "alchemy-elasticache-nonexistent-probe"; |
| 18 | |
| 19 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 20 | // over 60s on a fresh deploy. |
| 21 | const readinessPolicy = Schedule.max([ |
| 22 | Schedule.fixed("2 seconds"), |
| 23 | Schedule.recurs(75), |
| 24 | ]); |
| 25 | |
| 26 | let baseUrl: string; |
| 27 | let functionArn: string; |
| 28 | |
| 29 | const getJson = (path: string) => |
| 30 | HttpClient.get(`${baseUrl}${path}`).pipe( |
| 31 | Effect.flatMap((response) => |
| 32 | response.status >= 500 |
| 33 | ? Effect.fail(new Error(`transient upstream ${response.status}`)) |
| 34 | : Effect.succeed(response), |
| 35 | ), |
| 36 | Effect.retry({ |
| 37 | schedule: Schedule.max([ |
| 38 | Schedule.exponential("500 millis"), |
| 39 | Schedule.recurs(6), |
| 40 | ]), |
| 41 | }), |
| 42 | Effect.flatMap((r) => r.json), |
| 43 | ); |
| 44 | |
| 45 | describe.sequential( |
| 46 | "ElastiCache Bindings", |
| 47 | { |
| 48 | tags: [ |
| 49 | "provider:aws", |
| 50 | "provider:aws:elasticache", |
| 51 | "provider:aws:lambda", |
| 52 | "live", |
| 53 | ], |
| 54 | }, |
| 55 | () => { |
| 56 | beforeAll( |
| 57 | Effect.gen(function* () { |
| 58 | yield* Effect.logInfo( |
| 59 | "ElastiCache bindings setup: destroying previous resources", |
| 60 | ); |
| 61 | yield* sharedStack.destroy(); |
| 62 | |
| 63 | yield* Effect.logInfo("ElastiCache bindings setup: deploying fixture"); |
| 64 | const attrs = yield* sharedStack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | return yield* ElastiCacheBindingsTestFunction; |
| 67 | }).pipe(Effect.provide(ElastiCacheBindingsTestFunctionLive)), |
| 68 | ); |
| 69 | |
| 70 | expect(attrs.functionUrl).toBeTruthy(); |
| 71 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 72 | functionArn = attrs.functionArn; |
| 73 | |
| 74 | const readinessUrl = `${baseUrl}/bindings`; |
| 75 | yield* HttpClient.get(readinessUrl).pipe( |
| 76 | Effect.flatMap((response) => |
| 77 | response.status === 200 |
| 78 | ? Effect.succeed(response) |
| 79 | : Effect.fail( |
| 80 | new Error(`Function not ready: ${response.status}`), |
| 81 | ), |
| 82 | ), |
| 83 | Effect.retry({ schedule: readinessPolicy }), |
| 84 | ); |
| 85 | }), |
| 86 | { timeout: 240_000 }, |
| 87 | ); |
| 88 | |
| 89 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 90 | |
| 91 | describe("binding registration", () => { |
| 92 | test.provider("all six capabilities initialize in the runtime", () => |
| 93 | Effect.gen(function* () { |
| 94 | const response = yield* getJson("/bindings"); |
| 95 | expect((response as any).bound).toHaveLength(6); |
| 96 | }), |
| 97 | ); |
| 98 | }); |
| 99 | |
| 100 | describe("DescribeServerlessCaches", () => { |
| 101 | test.provider("lists the account's serverless caches", () => |
| 102 | Effect.gen(function* () { |
| 103 | const response = yield* getJson("/caches"); |
| 104 | expect((response as any).count).toBeGreaterThanOrEqual(0); |
| 105 | }), |
| 106 | ); |
| 107 | |
| 108 | test.provider("surfaces the typed not-found tag", () => |
| 109 | Effect.gen(function* () { |
| 110 | const response = yield* getJson( |
| 111 | `/cache-probe?name=${NONEXISTENT_NAME}`, |
| 112 | ); |
| 113 | expect((response as any).tag).toBe("ServerlessCacheNotFoundFault"); |
| 114 | }), |
| 115 | ); |
| 116 | }); |
| 117 | |
| 118 | describe("DescribeServerlessCacheSnapshots", () => { |
| 119 | test.provider("lists the account's snapshots", () => |
| 120 | Effect.gen(function* () { |
| 121 | const response = yield* getJson("/snapshots"); |
| 122 | expect((response as any).count).toBeGreaterThanOrEqual(0); |
| 123 | }), |
| 124 | ); |
| 125 | }); |
| 126 | |
| 127 | describe("DescribeEvents", () => { |
| 128 | test.provider("lists recent serverless-cache events", () => |
| 129 | Effect.gen(function* () { |
| 130 | const response = yield* getJson("/events"); |
| 131 | expect((response as any).count).toBeGreaterThanOrEqual(0); |
| 132 | }), |
| 133 | ); |
| 134 | }); |
| 135 | |
| 136 | describe("DeleteServerlessCacheSnapshot", () => { |
| 137 | test.provider("surfaces the typed not-found tag", () => |
| 138 | Effect.gen(function* () { |
| 139 | const response = yield* getJson( |
| 140 | `/delete-probe?name=${NONEXISTENT_NAME}`, |
| 141 | ); |
| 142 | // ServiceLinkedRoleNotFoundFault: ElastiCache validates the SLR |
| 143 | // before snapshot existence in accounts that never kept a cache. |
| 144 | expect([ |
| 145 | "ServerlessCacheSnapshotNotFoundFault", |
| 146 | "ServiceLinkedRoleNotFoundFault", |
| 147 | ]).toContain((response as any).tag); |
| 148 | }), |
| 149 | ); |
| 150 | }); |
| 151 | |
| 152 | describe("CopyServerlessCacheSnapshot", () => { |
| 153 | test.provider( |
| 154 | "rejects a nonexistent source snapshot with a typed tag", |
| 155 | () => |
| 156 | Effect.gen(function* () { |
| 157 | const response = yield* getJson( |
| 158 | `/copy-probe?name=${NONEXISTENT_NAME}`, |
| 159 | ); |
| 160 | expect([ |
| 161 | "ServerlessCacheSnapshotNotFoundFault", |
| 162 | "InvalidParameterValueException", |
| 163 | "ServiceLinkedRoleNotFoundFault", |
| 164 | ]).toContain((response as any).tag); |
| 165 | }), |
| 166 | ); |
| 167 | }); |
| 168 | |
| 169 | describe( |
| 170 | "consumeCacheEvents", |
| 171 | { tags: ["provider:aws:eventbridge"] }, |
| 172 | () => { |
| 173 | test.provider( |
| 174 | "the deploy created an EventBridge rule targeting the function", |
| 175 | () => |
| 176 | Effect.gen(function* () { |
| 177 | // Out-of-band via distilled: the fixture's consumeCacheEvents must |
| 178 | // have materialized as a rule on the default bus with the Lambda |
| 179 | // as target. |
| 180 | const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({ |
| 181 | TargetArn: functionArn, |
| 182 | }); |
| 183 | expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1); |
| 184 | }), |
| 185 | ); |
| 186 | }, |
| 187 | ); |
| 188 | |
| 189 | describe("ExportServerlessCacheSnapshot", () => { |
| 190 | test.provider("rejects a nonexistent snapshot with a typed tag", () => |
| 191 | Effect.gen(function* () { |
| 192 | const response = yield* getJson( |
| 193 | `/export-probe?name=${NONEXISTENT_NAME}`, |
| 194 | ); |
| 195 | expect([ |
| 196 | "ServerlessCacheSnapshotNotFoundFault", |
| 197 | "InvalidParameterValueException", |
| 198 | "ServiceLinkedRoleNotFoundFault", |
| 199 | ]).toContain((response as any).tag); |
| 200 | }), |
| 201 | ); |
| 202 | }); |
| 203 | }, |
| 204 | ); |