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 { DBCluster, DBInstance, DBSubnetGroup } from "@alchemy.run/aws/DocDB"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as docdb from "@distilled.cloud/aws/docdb"; |
| 6 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { getDefaultVpc } from "../DefaultVpc.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: AWS.providers() }); |
| 12 | |
| 13 | // Ungated typed-error probes: prove the distilled error union carries the |
| 14 | // not-found tags this provider's read/delete paths depend on. These run in |
| 15 | // every CI pass at near-zero cost, unlike the gated lifecycle below. |
| 16 | test.provider( |
| 17 | "describeDBClusters on a nonexistent cluster fails with DBClusterNotFoundFault", |
| 18 | () => |
| 19 | Effect.gen(function* () { |
| 20 | const error = yield* Effect.flip( |
| 21 | docdb.describeDBClusters({ |
| 22 | DBClusterIdentifier: "alchemy-nonexistent-docdb-cluster-probe", |
| 23 | }), |
| 24 | ); |
| 25 | expect(error._tag).toBe("DBClusterNotFoundFault"); |
| 26 | }), |
| 27 | { tags: ["provider:aws", "provider:aws:docdb", "live"] }, |
| 28 | ); |
| 29 | |
| 30 | test.provider( |
| 31 | "describeDBInstances on a nonexistent instance fails with DBInstanceNotFoundFault", |
| 32 | () => |
| 33 | Effect.gen(function* () { |
| 34 | const error = yield* Effect.flip( |
| 35 | docdb.describeDBInstances({ |
| 36 | DBInstanceIdentifier: "alchemy-nonexistent-docdb-instance-probe", |
| 37 | }), |
| 38 | ); |
| 39 | expect(error._tag).toBe("DBInstanceNotFoundFault"); |
| 40 | }), |
| 41 | { tags: ["provider:aws", "provider:aws:docdb", "live"] }, |
| 42 | ); |
| 43 | |
| 44 | test.provider( |
| 45 | "describeDBSubnetGroups on a nonexistent group fails with DBSubnetGroupNotFoundFault", |
| 46 | () => |
| 47 | Effect.gen(function* () { |
| 48 | const error = yield* Effect.flip( |
| 49 | docdb.describeDBSubnetGroups({ |
| 50 | DBSubnetGroupName: "alchemy-nonexistent-docdb-subnet-probe", |
| 51 | }), |
| 52 | ); |
| 53 | expect(error._tag).toBe("DBSubnetGroupNotFoundFault"); |
| 54 | }), |
| 55 | { tags: ["provider:aws", "provider:aws:docdb", "live"] }, |
| 56 | ); |
| 57 | |
| 58 | // Resolve two default-for-AZ subnets and the default security group. |
| 59 | const defaultNetwork = Effect.gen(function* () { |
| 60 | const vpc = yield* getDefaultVpc; |
| 61 | const subnets = yield* EC2.describeSubnets({ |
| 62 | Filters: [ |
| 63 | { Name: "vpc-id", Values: [vpc.vpcId] }, |
| 64 | { Name: "default-for-az", Values: ["true"] }, |
| 65 | ], |
| 66 | }); |
| 67 | const subnetIds = (subnets.Subnets ?? []) |
| 68 | .map((s) => s.SubnetId) |
| 69 | .filter((id): id is `subnet-${string}` => id !== undefined) |
| 70 | .sort() |
| 71 | .slice(0, 2); |
| 72 | const groups = yield* EC2.describeSecurityGroups({ |
| 73 | Filters: [ |
| 74 | { Name: "vpc-id", Values: [vpc.vpcId] }, |
| 75 | { Name: "group-name", Values: ["default"] }, |
| 76 | ], |
| 77 | }); |
| 78 | const securityGroupId = groups.SecurityGroups?.[0]?.GroupId; |
| 79 | if (subnetIds.length < 2 || securityGroupId === undefined) { |
| 80 | return yield* Effect.die( |
| 81 | new Error("default VPC is missing subnets or its default security group"), |
| 82 | ); |
| 83 | } |
| 84 | return { subnetIds, securityGroupIds: [securityGroupId] }; |
| 85 | }); |
| 86 | |
| 87 | // Deletion is verified as INITIATED (status `deleting`, irreversible) or fully |
| 88 | // gone. Full disappearance takes several more minutes server-side; waiting for |
| 89 | // it would push the test into its timeout. |
| 90 | const assertClusterDeleting = (identifier: string) => |
| 91 | Effect.gen(function* () { |
| 92 | const status = yield* docdb.describeDBClusters({ DBClusterIdentifier: identifier }).pipe( |
| 93 | Effect.map((r) => r.DBClusters?.[0]?.Status ?? "gone"), |
| 94 | Effect.catchTag("DBClusterNotFoundFault", () => Effect.succeed("gone" as const)), |
| 95 | ); |
| 96 | if (status !== "gone" && status !== "deleting") { |
| 97 | return yield* Effect.fail( |
| 98 | new Error(`cluster '${identifier}' still exists (status: ${status})`), |
| 99 | ); |
| 100 | } |
| 101 | }).pipe( |
| 102 | Effect.retry({ schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]) }), |
| 103 | ); |
| 104 | |
| 105 | // A DocumentDB cluster + instance take ~5-10 minutes to provision and bill per |
| 106 | // instance-hour while they exist. The full lifecycle is skipped under |
| 107 | // `--fast` and always destroys what it created. |
| 108 | test.provider.skipIf(!!process.env.FAST)( |
| 109 | "create DocumentDB cluster + instance, verify, destroy", |
| 110 | (stack) => |
| 111 | Effect.gen(function* () { |
| 112 | yield* stack.destroy(); |
| 113 | |
| 114 | const network = yield* defaultNetwork; |
| 115 | |
| 116 | const { cluster, instance } = yield* stack.deploy( |
| 117 | Effect.gen(function* () { |
| 118 | const subnetGroup = yield* DBSubnetGroup("Subnets", { |
| 119 | description: "alchemy docdb cluster subnets", |
| 120 | subnetIds: network.subnetIds, |
| 121 | }); |
| 122 | const cluster = yield* DBCluster("Cluster", { |
| 123 | dbSubnetGroupName: subnetGroup.dbSubnetGroupName, |
| 124 | vpcSecurityGroupIds: network.securityGroupIds, |
| 125 | masterUsername: "alchemy", |
| 126 | manageMasterUserPassword: true, |
| 127 | backupRetentionPeriod: "1 day", |
| 128 | deletionProtection: false, |
| 129 | tags: { fixture: "docdb-cluster" }, |
| 130 | }); |
| 131 | const instance = yield* DBInstance("Writer", { |
| 132 | dbClusterIdentifier: cluster.dbClusterIdentifier, |
| 133 | dbInstanceClass: "db.t3.medium", |
| 134 | tags: { fixture: "docdb-instance" }, |
| 135 | }); |
| 136 | return { cluster, instance }; |
| 137 | }), |
| 138 | ); |
| 139 | |
| 140 | expect(cluster.dbClusterIdentifier).toBeDefined(); |
| 141 | expect(cluster.dbClusterArn).toContain(":cluster:"); |
| 142 | expect(cluster.engine).toBe("docdb"); |
| 143 | expect(cluster.status).toBe("available"); |
| 144 | expect(cluster.endpoint).toContain("docdb"); |
| 145 | expect(cluster.masterUserSecretArn).toBeDefined(); |
| 146 | |
| 147 | expect(instance.dbInstanceIdentifier).toBeDefined(); |
| 148 | expect(instance.dbClusterIdentifier).toBe(cluster.dbClusterIdentifier); |
| 149 | expect(instance.dbInstanceClass).toBe("db.t3.medium"); |
| 150 | expect(instance.status).toBe("available"); |
| 151 | |
| 152 | // Out-of-band verification via distilled. |
| 153 | const describedCluster = yield* docdb.describeDBClusters({ |
| 154 | DBClusterIdentifier: cluster.dbClusterIdentifier, |
| 155 | }); |
| 156 | const observedCluster = describedCluster.DBClusters?.[0]; |
| 157 | expect(observedCluster?.Status).toBe("available"); |
| 158 | expect(observedCluster?.Engine).toBe("docdb"); |
| 159 | expect(observedCluster?.DBClusterMembers?.length).toBe(1); |
| 160 | |
| 161 | const describedInstance = yield* docdb.describeDBInstances({ |
| 162 | DBInstanceIdentifier: instance.dbInstanceIdentifier, |
| 163 | }); |
| 164 | expect(describedInstance.DBInstances?.[0]?.DBInstanceStatus).toBe("available"); |
| 165 | |
| 166 | // Destroy immediately — instances bill while they exist — and verify |
| 167 | // cluster deletion was initiated out-of-band. |
| 168 | yield* stack.destroy(); |
| 169 | yield* assertClusterDeleting(cluster.dbClusterIdentifier); |
| 170 | }), |
| 171 | // cluster + instance create (~5-10 min) + delete initiation, one test. |
| 172 | { tags: ["provider:aws", "provider:aws:docdb", "provider:aws:ec2", "live"], timeout: 1_500_000 }, |
| 173 | ); |