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 { connectEnvPrefix } from "@alchemy.run/aws/ElastiCache"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as ElastiCache from "@distilled.cloud/aws/elasticache"; |
| 6 | import * as Lambda from "@distilled.cloud/aws/lambda"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Redacted from "effect/Redacted"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import { FixtureCache } from "./fixture-cache.ts"; |
| 13 | import ElastiCacheTestFunctionLive, { ElastiCacheTestFunction } from "./handler.ts"; |
| 14 | import ElastiCacheSnapshotFunctionLive, { |
| 15 | ElastiCacheSnapshotFunction, |
| 16 | } from "./snapshot-handler.ts"; |
| 17 | |
| 18 | const { test } = Test.make({ providers: AWS.providers() }); |
| 19 | |
| 20 | // Ungated typed-error probe: proves the distilled error union carries the |
| 21 | // not-found tag this provider's read/delete paths depend on. |
| 22 | test.provider( |
| 23 | "describeServerlessCaches on a nonexistent cache fails with ServerlessCacheNotFoundFault", |
| 24 | () => |
| 25 | Effect.gen(function* () { |
| 26 | const error = yield* Effect.flip( |
| 27 | ElastiCache.describeServerlessCaches({ |
| 28 | ServerlessCacheName: "alchemy-nonexistent-cache-probe", |
| 29 | }), |
| 30 | ); |
| 31 | expect(error._tag).toBe("ServerlessCacheNotFoundFault"); |
| 32 | }), |
| 33 | { tags: ["provider:aws", "provider:aws:elasticache", "live"] }, |
| 34 | ); |
| 35 | |
| 36 | const SNAPSHOT_NAME = "alchemy-elasticache-fixture-snap"; |
| 37 | |
| 38 | // A snapshot still `creating` rejects deletion with |
| 39 | // InvalidServerlessCacheSnapshotStateFault — retry (bounded) while it |
| 40 | // settles. Already gone is success. An on-demand snapshot of even an empty |
| 41 | // serverless cache can stay `creating` for 5+ minutes, so the budget is |
| 42 | // ~10 min; the test destroys the (billed) cache BEFORE waiting on this. |
| 43 | const deleteSnapshot = (name: string) => |
| 44 | ElastiCache.deleteServerlessCacheSnapshot({ |
| 45 | ServerlessCacheSnapshotName: name, |
| 46 | }).pipe( |
| 47 | Effect.asVoid, |
| 48 | Effect.catchTag("ServerlessCacheSnapshotNotFoundFault", () => Effect.void), |
| 49 | Effect.retry({ |
| 50 | while: (e): boolean => e._tag === "InvalidServerlessCacheSnapshotStateFault", |
| 51 | schedule: Schedule.max([Schedule.fixed("15 seconds"), Schedule.recurs(40)]), |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | // Serverless caches take ~1-3 minutes to provision and a similar time to |
| 56 | // delete, and are metered (with a monthly floor) while they exist. The full |
| 57 | // lifecycle is therefore skipped under `--fast` and runs as a single |
| 58 | // sequential test that always destroys what it created. |
| 59 | test.provider.skipIf(!!process.env.FAST)( |
| 60 | "create serverless cache, bind Connect env, destroy, verify gone", |
| 61 | (stack) => |
| 62 | Effect.gen(function* () { |
| 63 | // Clean slate in case a previous run died mid-flight (the snapshot is |
| 64 | // created out-of-band of the stack, so it needs its own cleanup). |
| 65 | yield* stack.destroy(); |
| 66 | yield* deleteSnapshot(SNAPSHOT_NAME); |
| 67 | |
| 68 | const { cache, fn, snapshotFn } = yield* stack.deploy( |
| 69 | Effect.gen(function* () { |
| 70 | const { cache } = yield* FixtureCache; |
| 71 | const fn = yield* ElastiCacheTestFunction; |
| 72 | const snapshotFn = yield* ElastiCacheSnapshotFunction; |
| 73 | return { cache, fn, snapshotFn }; |
| 74 | }).pipe( |
| 75 | Effect.provide( |
| 76 | Layer.mergeAll(ElastiCacheTestFunctionLive, ElastiCacheSnapshotFunctionLive), |
| 77 | ), |
| 78 | ), |
| 79 | ); |
| 80 | |
| 81 | expect(cache.serverlessCacheName).toBeDefined(); |
| 82 | expect(cache.serverlessCacheArn).toContain(":serverlesscache:"); |
| 83 | expect(cache.engine).toBe("valkey"); |
| 84 | expect(cache.endpointAddress).toContain("cache.amazonaws.com"); |
| 85 | expect(cache.endpointPort).toBe(6379); |
| 86 | |
| 87 | // Out-of-band verification via distilled: cache is live with the |
| 88 | // requested cost-control limits. |
| 89 | const described = yield* ElastiCache.describeServerlessCaches({ |
| 90 | ServerlessCacheName: cache.serverlessCacheName, |
| 91 | }); |
| 92 | const observed = described.ServerlessCaches?.[0]; |
| 93 | expect(observed?.Status).toBe("available"); |
| 94 | expect(observed?.CacheUsageLimits?.DataStorage?.Maximum).toBe(1); |
| 95 | expect(observed?.CacheUsageLimits?.ECPUPerSecond?.Maximum).toBe(1000); |
| 96 | |
| 97 | // Env-only Connect binding: the deployed function's configuration |
| 98 | // must carry the endpoint env vars. |
| 99 | const config = yield* Lambda.getFunctionConfiguration({ |
| 100 | FunctionName: fn.functionName, |
| 101 | }); |
| 102 | const prefix = connectEnvPrefix("FixtureCache"); |
| 103 | const env = config.Environment?.Variables ?? {}; |
| 104 | // Distilled decodes Lambda env values as sensitive (Redacted) strings. |
| 105 | const envValue = (key: string) => { |
| 106 | const value = env[key]; |
| 107 | return value === undefined || typeof value === "string" ? value : Redacted.value(value); |
| 108 | }; |
| 109 | expect(envValue(`${prefix}_HOST`)).toBe(cache.endpointAddress); |
| 110 | expect(envValue(`${prefix}_PORT`)).toBe(String(cache.endpointPort)); |
| 111 | expect(envValue(`${prefix}_TLS`)).toBe("true"); |
| 112 | |
| 113 | // Runtime shape: the binding resolves the same connection info inside |
| 114 | // the deployed function. The first request rides URL propagation AND |
| 115 | // the VPC-attached cold start (ENI provisioning), so budget a generous |
| 116 | // bounded retry (3s x 60 ≈ 180s). |
| 117 | const baseUrl = fn.functionUrl!.replace(/\/+$/, ""); |
| 118 | const getJson = (path: string, times: number, base = baseUrl) => |
| 119 | HttpClient.get(`${base}${path}`).pipe( |
| 120 | Effect.flatMap((res) => |
| 121 | res.status === 200 |
| 122 | ? res.json |
| 123 | : res.text.pipe( |
| 124 | Effect.flatMap((body) => |
| 125 | Effect.fail(new Error(`${path} returned ${res.status}: ${body}`)), |
| 126 | ), |
| 127 | ), |
| 128 | ), |
| 129 | Effect.retry({ |
| 130 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(times)]), |
| 131 | }), |
| 132 | ); |
| 133 | const response = yield* getJson("/connection", 60); |
| 134 | expect((response as { host: string }).host).toBe(cache.endpointAddress); |
| 135 | expect((response as { port: number }).port).toBe(cache.endpointPort); |
| 136 | expect((response as { tls: boolean }).tls).toBe(true); |
| 137 | |
| 138 | // Data-plane roundtrip: the VPC-attached function SETs then GETs a key |
| 139 | // through the cache over TLS with iovalkey and returns the read-back |
| 140 | // value. |
| 141 | const roundtrip = yield* getJson("/roundtrip?value=hello-valkey", 10); |
| 142 | expect((roundtrip as { value: string }).value).toBe("hello-valkey"); |
| 143 | |
| 144 | // Cache-scoped CreateServerlessCacheSnapshot binding: take an |
| 145 | // on-demand snapshot from the (non-VPC) control-plane function, verify |
| 146 | // it out-of-band, then delete it (snapshots bill per GB-month). |
| 147 | const snapshotBaseUrl = snapshotFn.functionUrl!.replace(/\/+$/, ""); |
| 148 | const snapshot = (yield* getJson(`/snapshot?name=${SNAPSHOT_NAME}`, 10, snapshotBaseUrl)) as { |
| 149 | name: string; |
| 150 | status: string; |
| 151 | }; |
| 152 | expect(snapshot.name).toBe(SNAPSHOT_NAME); |
| 153 | expect(["creating", "available", "exists"]).toContain(snapshot.status); |
| 154 | const describedSnapshots = yield* ElastiCache.describeServerlessCacheSnapshots({ |
| 155 | ServerlessCacheSnapshotName: SNAPSHOT_NAME, |
| 156 | }); |
| 157 | expect(describedSnapshots.ServerlessCacheSnapshots?.[0]?.ServerlessCacheSnapshotName).toBe( |
| 158 | SNAPSHOT_NAME, |
| 159 | ); |
| 160 | |
| 161 | // Destroy immediately — serverless caches bill while they exist — |
| 162 | // and verify deletion out-of-band with a typed wait. The snapshot is |
| 163 | // deleted afterwards (see the `ensuring` below) since it can take |
| 164 | // minutes to leave `creating`. |
| 165 | yield* stack.destroy(); |
| 166 | yield* assertCacheDeleted(cache.serverlessCacheName); |
| 167 | }).pipe( |
| 168 | // The snapshot is created out-of-band of the stack — clean it up on |
| 169 | // every exit path so a failed run never leaves it billing. |
| 170 | Effect.ensuring(deleteSnapshot(SNAPSHOT_NAME).pipe(Effect.orDie)), |
| 171 | ), |
| 172 | // create (~2-4 min) + lambda deploy (~1-3 min, VPC ENIs) + delete |
| 173 | // initiation + snapshot settle-and-delete (up to ~10 min), one test. |
| 174 | { |
| 175 | tags: [ |
| 176 | "provider:aws", |
| 177 | "provider:aws:ec2", |
| 178 | "provider:aws:elasticache", |
| 179 | "provider:aws:lambda", |
| 180 | "live", |
| 181 | ], |
| 182 | timeout: 1_500_000, |
| 183 | }, |
| 184 | ); |
| 185 | |
| 186 | // Deletion is verified as INITIATED (status `deleting`, which is |
| 187 | // irreversible) or fully gone. Full disappearance takes another ~8 minutes |
| 188 | // server-side; waiting for it would push the test into its timeout and — |
| 189 | // with vitest's global `retry: 2` — trigger pointless create/destroy churn |
| 190 | // of a billed resource. |
| 191 | const assertCacheDeleted = (name: string) => |
| 192 | Effect.gen(function* () { |
| 193 | const status = yield* ElastiCache.describeServerlessCaches({ |
| 194 | ServerlessCacheName: name, |
| 195 | }).pipe( |
| 196 | Effect.map((r) => r.ServerlessCaches?.[0]?.Status ?? "gone"), |
| 197 | Effect.catchTag("ServerlessCacheNotFoundFault", () => Effect.succeed("gone" as const)), |
| 198 | ); |
| 199 | if (status !== "gone" && status !== "deleting") { |
| 200 | return yield* Effect.fail( |
| 201 | new Error(`Serverless cache '${name}' still exists (status: ${status})`), |
| 202 | ); |
| 203 | } |
| 204 | }).pipe( |
| 205 | Effect.retry({ |
| 206 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(24)]), |
| 207 | }), |
| 208 | ); |