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