@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 144 lines 5.69 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Cluster, SubnetGroup } from "@alchemy.run/aws/DAX";
3 import { Role } from "@alchemy.run/aws/IAM/Role";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as dax from "@distilled.cloud/aws/dax";
7 import * as EC2 from "@distilled.cloud/aws/ec2";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { getDefaultVpc } from "../DefaultVpc.ts";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 // Ungated typed-error probe: prove the distilled error union carries the
15 // not-found tag this provider's read/delete paths depend on. Runs in every
16 // CI pass at near-zero cost, unlike the gated lifecycle below.
17 test.provider(
18 "describeClusters on a nonexistent cluster fails with ClusterNotFoundFault",
19 () =>
20 Effect.gen(function* () {
21 const error = yield* Effect.flip(
22 dax.describeClusters({ ClusterNames: ["alchemy-nonexistent-dax-cluster-probe"] }),
23 );
24 expect(error._tag).toBe("ClusterNotFoundFault");
25 }),
26 { tags: ["provider:aws", "provider:aws:dax", "live"] },
27 );
28
29 // Resolve two default-for-AZ subnets and the default security group.
30 const defaultNetwork = Effect.gen(function* () {
31 const vpc = yield* getDefaultVpc;
32 const subnets = yield* EC2.describeSubnets({
33 Filters: [
34 { Name: "vpc-id", Values: [vpc.vpcId] },
35 { Name: "default-for-az", Values: ["true"] },
36 ],
37 });
38 // DAX is not offered in every AZ (e.g. us-west-2d) — pick subnets from the
39 // region's first three AZs (suffix a/b/c) only.
40 const subnetIds = (subnets.Subnets ?? [])
41 .filter((s) => /[abc]$/.test(s.AvailabilityZone ?? ""))
42 .sort((l, r) => (l.AvailabilityZone ?? "").localeCompare(r.AvailabilityZone ?? ""))
43 .map((s) => s.SubnetId)
44 .filter((id): id is `subnet-${string}` => id !== undefined)
45 .slice(0, 2);
46 const groups = yield* EC2.describeSecurityGroups({
47 Filters: [
48 { Name: "vpc-id", Values: [vpc.vpcId] },
49 { Name: "group-name", Values: ["default"] },
50 ],
51 });
52 const securityGroupId = groups.SecurityGroups?.[0]?.GroupId;
53 if (subnetIds.length < 2 || securityGroupId === undefined) {
54 return yield* Effect.die(
55 new Error("default VPC is missing subnets or its default security group"),
56 );
57 }
58 return { subnetIds, securityGroupIds: [securityGroupId] };
59 });
60
61 // Deletion is verified as INITIATED (status `deleting`, irreversible) or
62 // fully gone. Full disappearance takes several more minutes server-side.
63 const assertClusterDeleting = (name: string) =>
64 Effect.gen(function* () {
65 const status = yield* dax.describeClusters({ ClusterNames: [name] }).pipe(
66 Effect.map((r) => r.Clusters?.[0]?.Status ?? "gone"),
67 Effect.catchTag("ClusterNotFoundFault", () => Effect.succeed("gone" as const)),
68 );
69 if (status !== "gone" && status !== "deleting") {
70 return yield* Effect.fail(new Error(`cluster '${name}' still exists (status: ${status})`));
71 }
72 }).pipe(
73 Effect.retry({ schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]) }),
74 );
75
76 // A DAX cluster takes ~5-10 minutes to provision and bills per node-hour
77 // while it exists. The full lifecycle is skipped under `--fast` and
78 // always destroys what it created.
79 test.provider.skipIf(!!process.env.FAST)(
80 "create DAX cluster (dax.t3.small), verify, destroy",
81 (stack) =>
82 Effect.gen(function* () {
83 yield* stack.destroy();
84
85 const network = yield* defaultNetwork;
86
87 const { cluster } = yield* stack.deploy(
88 Effect.gen(function* () {
89 const role = yield* Role("DaxRole", {
90 assumeRolePolicyDocument: {
91 Version: "2012-10-17",
92 Statement: [
93 {
94 Effect: "Allow",
95 Principal: { Service: "dax.amazonaws.com" },
96 Action: ["sts:AssumeRole"],
97 },
98 ],
99 },
100 managedPolicyArns: ["arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess"],
101 });
102 const subnetGroup = yield* SubnetGroup("Subnets", {
103 description: "alchemy dax cluster subnets",
104 subnetIds: network.subnetIds,
105 });
106 const cluster = yield* Cluster("Cache", {
107 nodeType: "dax.t3.small",
108 replicationFactor: 1,
109 iamRoleArn: role.roleArn,
110 subnetGroupName: subnetGroup.subnetGroupName,
111 securityGroupIds: network.securityGroupIds,
112 description: "alchemy dax test cluster",
113 tags: { fixture: "dax-cluster" },
114 });
115 return { cluster };
116 }),
117 );
118
119 expect(cluster.clusterName).toBeDefined();
120 expect(cluster.clusterArn).toContain(":cache/");
121 expect(cluster.status).toBe("available");
122 expect(cluster.nodeType).toBe("dax.t3.small");
123 expect(cluster.totalNodes).toBe(1);
124 expect(cluster.discoveryEndpointAddress).toBeDefined();
125 expect(cluster.discoveryEndpointPort).toBeDefined();
126
127 // Out-of-band verification via distilled.
128 const described = yield* dax.describeClusters({ ClusterNames: [cluster.clusterName] });
129 const observed = described.Clusters?.[0];
130 expect(observed?.Status).toBe("available");
131 expect(observed?.NodeType).toBe("dax.t3.small");
132 expect(observed?.TotalNodes).toBe(1);
133
134 // Destroy immediately — clusters bill while they exist — and verify
135 // deletion was initiated out-of-band.
136 yield* stack.destroy();
137 yield* assertClusterDeleting(cluster.clusterName);
138 }),
139 // cluster create (~10 min) + delete initiation, one test.
140 {
141 tags: ["provider:aws", "provider:aws:dax", "provider:aws:ec2", "provider:aws:iam", "live"],
142 timeout: 1_500_000,
143 },
144 );