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 EC2 from "@distilled.cloud/aws/ec2"; |
| 6 | import { Region } from "@distilled.cloud/aws/Region"; |
| 7 | import * as sts from "@distilled.cloud/aws/sts"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { getDefaultVpc } from "../DefaultVpc.ts"; |
| 12 | import DocDBTestFunctionLive, { DocDBTestFunction } from "./handler"; |
| 13 | import DocDBSlowTestFunctionLive, { DocDBSlowTestFunction } from "./slow-handler"; |
| 14 | |
| 15 | const testOptions = { providers: AWS.providers() }; |
| 16 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 17 | const sharedStack = Core.scratchStack(testOptions, "DocDBBindings"); |
| 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 | |
| 25 | const getJson = (path: string) => |
| 26 | HttpClient.get(`${baseUrl}${path}`).pipe( |
| 27 | Effect.flatMap((response) => |
| 28 | response.status >= 500 |
| 29 | ? Effect.fail(new Error(`transient upstream ${response.status}`)) |
| 30 | : Effect.succeed(response), |
| 31 | ), |
| 32 | Effect.retry({ |
| 33 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 34 | }), |
| 35 | Effect.flatMap((r) => r.json), |
| 36 | ); |
| 37 | |
| 38 | describe.sequential( |
| 39 | "DocDB Bindings", |
| 40 | { tags: ["provider:aws", "provider:aws:docdb", "provider:aws:lambda", "live"] }, |
| 41 | () => { |
| 42 | beforeAll( |
| 43 | Effect.gen(function* () { |
| 44 | yield* Effect.logInfo("DocDB test setup: destroying previous resources"); |
| 45 | yield* sharedStack.destroy(); |
| 46 | |
| 47 | yield* Effect.logInfo("DocDB test setup: deploying fixture"); |
| 48 | const { functionUrl } = yield* sharedStack.deploy( |
| 49 | Effect.gen(function* () { |
| 50 | return yield* DocDBTestFunction; |
| 51 | }).pipe(Effect.provide(DocDBTestFunctionLive)), |
| 52 | ); |
| 53 | |
| 54 | expect(functionUrl).toBeTruthy(); |
| 55 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 56 | |
| 57 | const readinessUrl = `${baseUrl}/bindings`; |
| 58 | yield* HttpClient.get(readinessUrl).pipe( |
| 59 | Effect.flatMap((response) => |
| 60 | response.status === 200 |
| 61 | ? Effect.succeed(response) |
| 62 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 63 | ), |
| 64 | Effect.retry({ schedule: readinessPolicy }), |
| 65 | ); |
| 66 | }), |
| 67 | { timeout: 240_000 }, |
| 68 | ); |
| 69 | |
| 70 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 71 | |
| 72 | describe("binding registration", () => { |
| 73 | test.provider("all capabilities initialize in the runtime", (_stack) => |
| 74 | Effect.gen(function* () { |
| 75 | const response = yield* getJson("/bindings"); |
| 76 | expect((response as any).bound).toHaveLength(8); |
| 77 | }), |
| 78 | ); |
| 79 | }); |
| 80 | |
| 81 | describe("DescribeDBClusters", () => { |
| 82 | test.provider("surfaces the typed not-found tag for a nonexistent cluster", (_stack) => |
| 83 | Effect.gen(function* () { |
| 84 | const response = yield* getJson("/clusters"); |
| 85 | expect((response as any).tag).toBe("DBClusterNotFoundFault"); |
| 86 | }), |
| 87 | ); |
| 88 | }); |
| 89 | |
| 90 | describe("DescribeEvents", () => { |
| 91 | test.provider("lists the account's recent DocumentDB events", (_stack) => |
| 92 | Effect.gen(function* () { |
| 93 | const response = yield* getJson("/events"); |
| 94 | expect((response as any).count).toBeGreaterThanOrEqual(0); |
| 95 | }), |
| 96 | ); |
| 97 | }); |
| 98 | |
| 99 | describe("DescribeDBInstances", () => { |
| 100 | test.provider("surfaces the typed not-found tag for a nonexistent instance", (_stack) => |
| 101 | Effect.gen(function* () { |
| 102 | const response = yield* getJson("/instances"); |
| 103 | expect((response as any).tag).toBe("DBInstanceNotFoundFault"); |
| 104 | }), |
| 105 | ); |
| 106 | }); |
| 107 | |
| 108 | describe("DescribeDBClusterSnapshots", () => { |
| 109 | test.provider("lists the account's cluster snapshots", (_stack) => |
| 110 | Effect.gen(function* () { |
| 111 | const response = yield* getJson("/snapshots"); |
| 112 | expect((response as any).count).toBeGreaterThanOrEqual(0); |
| 113 | }), |
| 114 | ); |
| 115 | }); |
| 116 | |
| 117 | describe("DeleteDBClusterSnapshot", () => { |
| 118 | test.provider("surfaces the typed not-found tag for a nonexistent snapshot", (_stack) => |
| 119 | Effect.gen(function* () { |
| 120 | const response = yield* getJson("/delete-snapshot-probe"); |
| 121 | expect((response as any).tag).toBe("DBClusterSnapshotNotFoundFault"); |
| 122 | }), |
| 123 | ); |
| 124 | }); |
| 125 | |
| 126 | describe("CopyDBClusterSnapshot", () => { |
| 127 | test.provider( |
| 128 | "surfaces the typed not-found tag for a nonexistent source snapshot", |
| 129 | (_stack) => |
| 130 | Effect.gen(function* () { |
| 131 | const response = yield* getJson("/copy-snapshot-probe"); |
| 132 | expect((response as any).tag).toBe("DBClusterSnapshotNotFoundFault"); |
| 133 | }), |
| 134 | ); |
| 135 | }); |
| 136 | |
| 137 | describe("DescribePendingMaintenanceActions", () => { |
| 138 | test.provider("lists the account's pending maintenance actions", (_stack) => |
| 139 | Effect.gen(function* () { |
| 140 | const response = yield* getJson("/pending-maintenance"); |
| 141 | expect((response as any).count).toBeGreaterThanOrEqual(0); |
| 142 | }), |
| 143 | ); |
| 144 | }); |
| 145 | |
| 146 | describe("ApplyPendingMaintenanceAction", () => { |
| 147 | test.provider("surfaces the typed not-found tag for a nonexistent resource ARN", (_stack) => |
| 148 | Effect.gen(function* () { |
| 149 | // Build a well-formed cluster ARN in this account/region that |
| 150 | // cannot exist — the apply call must decode ResourceNotFoundFault. |
| 151 | // (`Region`'s service value is itself an Effect — resolve twice.) |
| 152 | const region = yield* yield* Region; |
| 153 | const identity = yield* sts.getCallerIdentity({}); |
| 154 | const arn = `arn:aws:rds:${region}:${identity.Account}:cluster:alchemy-nonexistent-docdb-probe`; |
| 155 | const response = yield* getJson(`/apply-probe?arn=${encodeURIComponent(arn)}`); |
| 156 | expect((response as any).tag).toBe("ResourceNotFoundFault"); |
| 157 | }), |
| 158 | ); |
| 159 | }); |
| 160 | }, |
| 161 | ); |
| 162 | |
| 163 | // --------------------------------------------------------------------------- |
| 164 | // Data-plane binding (Connect + mongo) needs a real DocumentDB cluster + |
| 165 | // instance (~15 min to provision, billed per instance-hour) and a Secrets |
| 166 | // Manager VPC interface endpoint (the VPC-attached Lambda has no internet |
| 167 | // path; deployed and destroyed with the stack) — skipped under `--fast` |
| 168 | // like the DBCluster lifecycle test. |
| 169 | // --------------------------------------------------------------------------- |
| 170 | |
| 171 | const slowStack = Core.scratchStack(testOptions, "DocDBSlowBindings"); |
| 172 | |
| 173 | // Resolve two default-for-AZ subnets and the default security group. The |
| 174 | // default security group allows all traffic from itself, so the Lambda, the |
| 175 | // cluster, and the endpoint can all share it. |
| 176 | const defaultNetwork = Effect.gen(function* () { |
| 177 | const vpc = yield* getDefaultVpc; |
| 178 | const subnets = yield* EC2.describeSubnets({ |
| 179 | Filters: [ |
| 180 | { Name: "vpc-id", Values: [vpc.vpcId] }, |
| 181 | { Name: "default-for-az", Values: ["true"] }, |
| 182 | ], |
| 183 | }); |
| 184 | const subnetIds = (subnets.Subnets ?? []) |
| 185 | .map((s) => s.SubnetId) |
| 186 | .filter((id): id is `subnet-${string}` => id !== undefined) |
| 187 | .sort() |
| 188 | .slice(0, 2); |
| 189 | const groups = yield* EC2.describeSecurityGroups({ |
| 190 | Filters: [ |
| 191 | { Name: "vpc-id", Values: [vpc.vpcId] }, |
| 192 | { Name: "group-name", Values: ["default"] }, |
| 193 | ], |
| 194 | }); |
| 195 | const securityGroupId = groups.SecurityGroups?.[0]?.GroupId; |
| 196 | if (subnetIds.length < 2 || securityGroupId === undefined) { |
| 197 | return yield* Effect.die( |
| 198 | new Error("default VPC is missing subnets or its default security group"), |
| 199 | ); |
| 200 | } |
| 201 | return { vpcId: vpc.vpcId, subnetIds, securityGroupIds: [securityGroupId] }; |
| 202 | }); |
| 203 | |
| 204 | test.provider.skipIf(!!process.env.FAST)( |
| 205 | "connect + mongo data plane against a live cluster", |
| 206 | () => |
| 207 | Effect.gen(function* () { |
| 208 | yield* slowStack.destroy(); |
| 209 | const network = yield* defaultNetwork; |
| 210 | // `Region`'s service value is itself an Effect — resolve twice. |
| 211 | const region = yield* yield* Region; |
| 212 | |
| 213 | // The fixture's init Effect runs in this process — pass the resolved |
| 214 | // network through env (the module cannot look it up itself). |
| 215 | yield* Effect.sync(() => { |
| 216 | process.env.DOCDB_TEST_SUBNET_IDS = network.subnetIds.join(","); |
| 217 | process.env.DOCDB_TEST_SG_IDS = network.securityGroupIds.join(","); |
| 218 | }); |
| 219 | |
| 220 | yield* Effect.gen(function* () { |
| 221 | const { functionUrl } = yield* slowStack.deploy( |
| 222 | Effect.gen(function* () { |
| 223 | // The VPC-attached Lambda resolves the master secret through a |
| 224 | // Secrets Manager interface endpoint. It is part of the stack |
| 225 | // (created and destroyed with the test) — an interface endpoint |
| 226 | // bills per AZ-hour, so it must never outlive the run. |
| 227 | yield* AWS.EC2.VpcEndpoint("SecretsManagerEndpoint", { |
| 228 | vpcId: network.vpcId as `vpc-${string}`, |
| 229 | serviceName: `com.amazonaws.${region}.secretsmanager`, |
| 230 | vpcEndpointType: "Interface", |
| 231 | subnetIds: network.subnetIds as `subnet-${string}`[], |
| 232 | securityGroupIds: network.securityGroupIds as `sg-${string}`[], |
| 233 | privateDnsEnabled: true, |
| 234 | }); |
| 235 | return yield* DocDBSlowTestFunction; |
| 236 | }).pipe(Effect.provide(DocDBSlowTestFunctionLive)), |
| 237 | ); |
| 238 | expect(functionUrl).toBeTruthy(); |
| 239 | const slowBaseUrl = functionUrl!.replace(/\/+$/, ""); |
| 240 | |
| 241 | // A non-200 is a test failure carrying the status — the fixture |
| 242 | // answers an empty 500 on a defect, which `r.json` decodes to null. |
| 243 | const get = (path: string) => |
| 244 | HttpClient.get(`${slowBaseUrl}${path}`).pipe( |
| 245 | Effect.retry({ |
| 246 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(10)]), |
| 247 | }), |
| 248 | Effect.flatMap((r) => |
| 249 | r.status === 200 |
| 250 | ? r.json |
| 251 | : r.text.pipe( |
| 252 | Effect.flatMap((body) => |
| 253 | Effect.fail(new Error(`${path} returned ${r.status}: ${body}`)), |
| 254 | ), |
| 255 | ), |
| 256 | ), |
| 257 | ); |
| 258 | |
| 259 | // Connection info resolves from the runtime attributes, the secret, |
| 260 | // and the published environment variables. |
| 261 | const info = (yield* get("/info")) as { |
| 262 | host: string; |
| 263 | port: number; |
| 264 | database?: string; |
| 265 | username?: string; |
| 266 | tls: boolean; |
| 267 | hasPassword: boolean; |
| 268 | env: { host?: string; port?: string }; |
| 269 | }; |
| 270 | expect(info.host).toContain("docdb"); |
| 271 | expect(info.port).toBe(27017); |
| 272 | expect(info.database).toBe("alchemy_test"); |
| 273 | expect(info.username).toBe("alchemy"); |
| 274 | expect(info.tls).toBe(true); |
| 275 | expect(info.hasPassword).toBe(true); |
| 276 | expect(info.env.host).toBe(info.host); |
| 277 | expect(info.env.port).toBe(String(info.port)); |
| 278 | |
| 279 | // The mongo Effect client opens a TLS socket, authenticates via the |
| 280 | // managed master secret, and round-trips a document. |
| 281 | const ping = (yield* get("/ping")) as { ok: number }; |
| 282 | expect(ping.ok).toBe(1); |
| 283 | |
| 284 | const crud = (yield* get("/crud?value=alchemy-docdb")) as { marker?: string }; |
| 285 | expect(crud.marker).toBe("alchemy-docdb"); |
| 286 | }).pipe(Effect.ensuring(slowStack.destroy().pipe(Effect.orDie))); |
| 287 | }), |
| 288 | // cluster + instance create (~15 min) + probes + delete initiation. |
| 289 | { |
| 290 | tags: ["provider:aws", "provider:aws:docdb", "provider:aws:ec2", "provider:aws:lambda", "live"], |
| 291 | timeout: 2_400_000, |
| 292 | }, |
| 293 | ); |