@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 133 lines 5.39 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Cluster } from "@alchemy.run/aws/DocDBElastic";
3 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as docdbelastic from "@distilled.cloud/aws/docdb-elastic";
7 import * as EC2 from "@distilled.cloud/aws/ec2";
8 import * as Effect from "effect/Effect";
9 import * as Redacted from "effect/Redacted";
10 import * as Schedule from "effect/Schedule";
11 import { getDefaultVpc } from "../DefaultVpc.ts";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 // Ungated typed-error probe: prove the distilled error union carries the
16 // not-found tag this provider's read/delete paths depend on. Runs in every
17 // CI pass at near-zero cost, unlike the gated lifecycle below.
18 test.provider(
19 "getCluster on a nonexistent cluster ARN fails with ResourceNotFoundException",
20 () =>
21 Effect.gen(function* () {
22 const { accountId, region } = yield* AWSEnvironment.current;
23 const error = yield* Effect.flip(
24 docdbelastic.getCluster({
25 clusterArn: `arn:aws:docdb-elastic:${region}:${accountId}:cluster/00000000-0000-0000-0000-000000000000`,
26 }),
27 );
28 expect(error._tag).toBe("ResourceNotFoundException");
29 }),
30 { tags: ["provider:aws", "provider:aws:docdbelastic", "live"] },
31 );
32
33 // Resolve two default-for-AZ subnets and the default security group.
34 const defaultNetwork = Effect.gen(function* () {
35 const vpc = yield* getDefaultVpc;
36 const subnets = yield* EC2.describeSubnets({
37 Filters: [
38 { Name: "vpc-id", Values: [vpc.vpcId] },
39 { Name: "default-for-az", Values: ["true"] },
40 ],
41 });
42 // DocumentDB elastic clusters are not offered in every AZ (e.g.
43 // us-west-2d) — pick subnets from the region's first three AZs (suffix
44 // a/b/c) only.
45 const subnetIds = (subnets.Subnets ?? [])
46 .filter((s) => /[abc]$/.test(s.AvailabilityZone ?? ""))
47 .sort((l, r) => (l.AvailabilityZone ?? "").localeCompare(r.AvailabilityZone ?? ""))
48 .map((s) => s.SubnetId)
49 .filter((id): id is `subnet-${string}` => id !== undefined)
50 .slice(0, 2);
51 const groups = yield* EC2.describeSecurityGroups({
52 Filters: [
53 { Name: "vpc-id", Values: [vpc.vpcId] },
54 { Name: "group-name", Values: ["default"] },
55 ],
56 });
57 const securityGroupId = groups.SecurityGroups?.[0]?.GroupId;
58 if (subnetIds.length < 2 || securityGroupId === undefined) {
59 return yield* Effect.die(
60 new Error("default VPC is missing subnets or its default security group"),
61 );
62 }
63 return { subnetIds, securityGroupIds: [securityGroupId] };
64 });
65
66 // Deletion is verified as INITIATED (status DELETING, irreversible) or fully
67 // gone. Full disappearance takes several more minutes server-side.
68 const assertClusterDeleting = (arn: string) =>
69 Effect.gen(function* () {
70 const status = yield* docdbelastic.getCluster({ clusterArn: arn }).pipe(
71 Effect.map((r) => r.cluster.status),
72 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("GONE" as const)),
73 );
74 if (status !== "GONE" && status !== "DELETING") {
75 return yield* Effect.fail(new Error(`cluster '${arn}' still exists (status: ${status})`));
76 }
77 }).pipe(
78 Effect.retry({ schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]) }),
79 );
80
81 // A DocumentDB elastic cluster takes ~8-10 minutes to provision and bills
82 // per shard-vCPU-hour while it exists. The full lifecycle is skipped under
83 // `--fast` and always destroys what it created.
84 test.provider.skipIf(!!process.env.FAST)(
85 "create elastic cluster (1 shard, capacity 2), verify, destroy",
86 (stack) =>
87 Effect.gen(function* () {
88 yield* stack.destroy();
89
90 const network = yield* defaultNetwork;
91
92 const { cluster } = yield* stack.deploy(
93 Effect.gen(function* () {
94 const cluster = yield* Cluster("Documents", {
95 adminUserName: "alchemyadmin",
96 adminUserPassword: Redacted.make("AlchemyTestPassw0rd"),
97 shardCapacity: 2,
98 shardCount: 1,
99 subnetIds: network.subnetIds,
100 vpcSecurityGroupIds: network.securityGroupIds,
101 backupRetentionPeriod: "1 day",
102 tags: { fixture: "docdb-elastic-cluster" },
103 });
104 return { cluster };
105 }),
106 );
107
108 expect(cluster.clusterName).toBeDefined();
109 expect(cluster.clusterArn).toContain(":cluster/");
110 expect(cluster.status).toBe("ACTIVE");
111 expect(cluster.clusterEndpoint).toContain("docdb-elastic");
112 expect(cluster.adminUserName).toBe("alchemyadmin");
113 expect(cluster.authType).toBe("PLAIN_TEXT");
114 expect(cluster.shardCapacity).toBe(2);
115 expect(cluster.shardCount).toBe(1);
116 expect(new Set(cluster.subnetIds)).toEqual(new Set(network.subnetIds));
117
118 // Out-of-band verification via distilled.
119 const described = yield* docdbelastic.getCluster({ clusterArn: cluster.clusterArn });
120 expect(described.cluster.status).toBe("ACTIVE");
121 expect(described.cluster.shardCount).toBe(1);
122
123 // Destroy immediately — clusters bill while they exist — and verify
124 // deletion was initiated out-of-band.
125 yield* stack.destroy();
126 yield* assertClusterDeleting(cluster.clusterArn);
127 }),
128 // cluster create (~10 min) + delete initiation, one test.
129 {
130 tags: ["provider:aws", "provider:aws:docdbelastic", "provider:aws:ec2", "live"],
131 timeout: 1_500_000,
132 },
133 );