| 1 | import * as AWS from "@/AWS"; |
| 2 | import { DBCluster, DBInstance, DBSubnetGroup } from "@/AWS/Neptune"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 5 | import * as neptune from "@distilled.cloud/aws/neptune"; |
| 6 | import { expect } from "alchemy-test"; |
| 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 | neptune.describeDBClusters({ |
| 22 | DBClusterIdentifier: "alchemy-nonexistent-neptune-cluster-probe", |
| 23 | }), |
| 24 | ); |
| 25 | expect(error._tag).toBe("DBClusterNotFoundFault"); |
| 26 | }), |
| 27 | { tags: ["provider:aws", "provider:aws:neptune", "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 | neptune.describeDBInstances({ |
| 36 | DBInstanceIdentifier: "alchemy-nonexistent-neptune-instance-probe", |
| 37 | }), |
| 38 | ); |
| 39 | expect(error._tag).toBe("DBInstanceNotFoundFault"); |
| 40 | }), |
| 41 | { tags: ["provider:aws", "provider:aws:neptune", "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 | neptune.describeDBSubnetGroups({ |
| 50 | DBSubnetGroupName: "alchemy-nonexistent-neptune-subnet-probe", |
| 51 | }), |
| 52 | ); |
| 53 | expect(error._tag).toBe("DBSubnetGroupNotFoundFault"); |
| 54 | }), |
| 55 | { tags: ["provider:aws", "provider:aws:neptune", "live"] }, |
| 56 | ); |
| 57 | |
| 58 | test.provider( |
| 59 | "describeDBClusterParameterGroups on a nonexistent group fails with DBParameterGroupNotFoundFault", |
| 60 | () => |
| 61 | Effect.gen(function* () { |
| 62 | const error = yield* Effect.flip( |
| 63 | neptune.describeDBClusterParameterGroups({ |
| 64 | DBClusterParameterGroupName: |
| 65 | "alchemy-nonexistent-neptune-params-probe", |
| 66 | }), |
| 67 | ); |
| 68 | expect(error._tag).toBe("DBParameterGroupNotFoundFault"); |
| 69 | }), |
| 70 | { tags: ["provider:aws", "provider:aws:neptune", "live"] }, |
| 71 | ); |
| 72 | |
| 73 | // Resolve two default-for-AZ subnets and the default security group. |
| 74 | const defaultNetwork = Effect.gen(function* () { |
| 75 | const vpc = yield* getDefaultVpc; |
| 76 | const subnets = yield* EC2.describeSubnets({ |
| 77 | Filters: [ |
| 78 | { Name: "vpc-id", Values: [vpc.vpcId] }, |
| 79 | { Name: "default-for-az", Values: ["true"] }, |
| 80 | ], |
| 81 | }); |
| 82 | const subnetIds = (subnets.Subnets ?? []) |
| 83 | .map((s) => s.SubnetId) |
| 84 | .filter((id): id is `subnet-${string}` => id !== undefined) |
| 85 | .sort() |
| 86 | .slice(0, 2); |
| 87 | const groups = yield* EC2.describeSecurityGroups({ |
| 88 | Filters: [ |
| 89 | { Name: "vpc-id", Values: [vpc.vpcId] }, |
| 90 | { Name: "group-name", Values: ["default"] }, |
| 91 | ], |
| 92 | }); |
| 93 | const securityGroupId = groups.SecurityGroups?.[0]?.GroupId; |
| 94 | if (subnetIds.length < 2 || securityGroupId === undefined) { |
| 95 | return yield* Effect.die( |
| 96 | new Error("default VPC is missing subnets or its default security group"), |
| 97 | ); |
| 98 | } |
| 99 | return { subnetIds, securityGroupIds: [securityGroupId] }; |
| 100 | }); |
| 101 | |
| 102 | // Deletion is verified as INITIATED (status `deleting`, irreversible) or fully |
| 103 | // gone. Full disappearance takes several more minutes server-side; waiting for |
| 104 | // it would push the test into its timeout. |
| 105 | const assertClusterDeleting = (identifier: string) => |
| 106 | Effect.gen(function* () { |
| 107 | const status = yield* neptune |
| 108 | .describeDBClusters({ DBClusterIdentifier: identifier }) |
| 109 | .pipe( |
| 110 | Effect.map((r) => r.DBClusters?.[0]?.Status ?? "gone"), |
| 111 | Effect.catchTag("DBClusterNotFoundFault", () => |
| 112 | Effect.succeed("gone" as const), |
| 113 | ), |
| 114 | ); |
| 115 | if (status !== "gone" && status !== "deleting") { |
| 116 | return yield* Effect.fail( |
| 117 | new Error(`cluster '${identifier}' still exists (status: ${status})`), |
| 118 | ); |
| 119 | } |
| 120 | }).pipe( |
| 121 | Effect.retry({ |
| 122 | schedule: Schedule.max([ |
| 123 | Schedule.fixed("10 seconds"), |
| 124 | Schedule.recurs(18), |
| 125 | ]), |
| 126 | }), |
| 127 | ); |
| 128 | |
| 129 | // A Neptune cluster + instance take ~10 minutes to provision and bill per |
| 130 | // instance-hour while they exist. The full lifecycle is gated behind |
| 131 | // AWS_TEST_SLOW=1 and always destroys what it created. |
| 132 | test.provider.skipIf(!process.env.AWS_TEST_SLOW)( |
| 133 | "create Neptune cluster + serverless instance, verify, destroy", |
| 134 | (stack) => |
| 135 | Effect.gen(function* () { |
| 136 | yield* stack.destroy(); |
| 137 | |
| 138 | const network = yield* defaultNetwork; |
| 139 | |
| 140 | const { cluster, instance } = yield* stack.deploy( |
| 141 | Effect.gen(function* () { |
| 142 | const subnetGroup = yield* DBSubnetGroup("Subnets", { |
| 143 | description: "alchemy neptune cluster subnets", |
| 144 | subnetIds: network.subnetIds, |
| 145 | }); |
| 146 | const cluster = yield* DBCluster("Cluster", { |
| 147 | dbSubnetGroupName: subnetGroup.dbSubnetGroupName, |
| 148 | vpcSecurityGroupIds: network.securityGroupIds, |
| 149 | backupRetentionPeriod: "1 day", |
| 150 | deletionProtection: false, |
| 151 | enableIAMDatabaseAuthentication: true, |
| 152 | serverlessV2ScalingConfiguration: { |
| 153 | minCapacity: 1, |
| 154 | maxCapacity: 2.5, |
| 155 | }, |
| 156 | tags: { fixture: "neptune-cluster" }, |
| 157 | }); |
| 158 | const instance = yield* DBInstance("Writer", { |
| 159 | dbClusterIdentifier: cluster.dbClusterIdentifier, |
| 160 | dbInstanceClass: "db.serverless", |
| 161 | tags: { fixture: "neptune-instance" }, |
| 162 | }); |
| 163 | return { cluster, instance }; |
| 164 | }), |
| 165 | ); |
| 166 | |
| 167 | expect(cluster.dbClusterIdentifier).toBeDefined(); |
| 168 | expect(cluster.dbClusterArn).toContain(":cluster:"); |
| 169 | expect(cluster.engine).toBe("neptune"); |
| 170 | expect(cluster.status).toBe("available"); |
| 171 | expect(cluster.endpoint).toContain("neptune"); |
| 172 | expect(cluster.port).toBe(8182); |
| 173 | expect(cluster.iamDatabaseAuthenticationEnabled).toBe(true); |
| 174 | |
| 175 | expect(instance.dbInstanceIdentifier).toBeDefined(); |
| 176 | expect(instance.dbClusterIdentifier).toBe(cluster.dbClusterIdentifier); |
| 177 | expect(instance.dbInstanceClass).toBe("db.serverless"); |
| 178 | expect(instance.status).toBe("available"); |
| 179 | |
| 180 | // Out-of-band verification via distilled. |
| 181 | const describedCluster = yield* neptune.describeDBClusters({ |
| 182 | DBClusterIdentifier: cluster.dbClusterIdentifier, |
| 183 | }); |
| 184 | const observedCluster = describedCluster.DBClusters?.[0]; |
| 185 | expect(observedCluster?.Status).toBe("available"); |
| 186 | expect(observedCluster?.Engine).toBe("neptune"); |
| 187 | expect(observedCluster?.DBClusterMembers?.length).toBe(1); |
| 188 | |
| 189 | const describedInstance = yield* neptune.describeDBInstances({ |
| 190 | DBInstanceIdentifier: instance.dbInstanceIdentifier, |
| 191 | }); |
| 192 | expect(describedInstance.DBInstances?.[0]?.DBInstanceStatus).toBe( |
| 193 | "available", |
| 194 | ); |
| 195 | |
| 196 | // Destroy immediately — instances bill while they exist — and verify |
| 197 | // cluster deletion was initiated out-of-band. |
| 198 | yield* stack.destroy(); |
| 199 | yield* assertClusterDeleting(cluster.dbClusterIdentifier); |
| 200 | }), |
| 201 | // cluster + instance create (~10 min) + delete initiation, one test. |
| 202 | { |
| 203 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:neptune", "live"], |
| 204 | timeout: 1_500_000, |
| 205 | }, |
| 206 | ); |