The best way to control Claude Code, Codex and more from any computer or phone

main
ts 137 lines 5.31 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { ACL, Cluster, SubnetGroup, User } from "@alchemy.run/aws/MemoryDB";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as EC2 from "@distilled.cloud/aws/ec2";
6 import * as memorydb from "@distilled.cloud/aws/memorydb";
7 import * as Effect from "effect/Effect";
8 import * as Redacted from "effect/Redacted";
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: proves the distilled error union carries the
15 // not-found tag this provider's read/delete paths depend on.
16 test.provider(
17 "describeClusters on a nonexistent cluster fails with ClusterNotFoundFault",
18 () =>
19 Effect.gen(function* () {
20 const error = yield* Effect.flip(
21 memorydb.describeClusters({ ClusterName: "alchemy-nonexistent-memorydb-probe" }),
22 );
23 expect(error._tag).toBe("ClusterNotFoundFault");
24 }),
25 { tags: ["provider:aws", "provider:aws:memorydb", "live"] },
26 );
27
28 // Resolve two default-for-AZ subnets and the default security group.
29 const defaultNetwork = Effect.gen(function* () {
30 const vpc = yield* getDefaultVpc;
31 const subnets = yield* EC2.describeSubnets({
32 Filters: [
33 { Name: "vpc-id", Values: [vpc.vpcId] },
34 { Name: "default-for-az", Values: ["true"] },
35 ],
36 });
37 const subnetIds = (subnets.Subnets ?? [])
38 .map((s) => s.SubnetId)
39 .filter((id): id is string => id !== undefined)
40 .sort()
41 .slice(0, 2);
42 const groups = yield* EC2.describeSecurityGroups({
43 Filters: [
44 { Name: "vpc-id", Values: [vpc.vpcId] },
45 { Name: "group-name", Values: ["default"] },
46 ],
47 });
48 const securityGroupId = groups.SecurityGroups?.[0]?.GroupId;
49 if (subnetIds.length < 2 || securityGroupId === undefined) {
50 return yield* Effect.die(
51 new Error("default VPC is missing subnets or its default security group"),
52 );
53 }
54 return { subnetIds, securityGroupIds: [securityGroupId] };
55 });
56
57 // Deletion is verified as INITIATED (status `deleting`, irreversible) or fully
58 // gone. Full disappearance takes several more minutes server-side; waiting for
59 // it would push the test into its timeout.
60 const assertClusterDeleting = (name: string) =>
61 Effect.gen(function* () {
62 const status = yield* memorydb.describeClusters({ ClusterName: name }).pipe(
63 Effect.map((r) => r.Clusters?.[0]?.Status ?? "gone"),
64 Effect.catchTag("ClusterNotFoundFault", () => Effect.succeed("gone" as const)),
65 );
66 if (status !== "gone" && status !== "deleting") {
67 return yield* Effect.fail(new Error(`cluster '${name}' still exists (status: ${status})`));
68 }
69 }).pipe(
70 Effect.retry({ schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]) }),
71 );
72
73 // MemoryDB clusters take ~10-15 minutes to provision and are billed per node
74 // while they exist. The full lifecycle is skipped under `--fast` and
75 // always destroys what it created.
76 test.provider.skipIf(!!process.env.FAST)(
77 "create single-node MemoryDB cluster, verify, destroy",
78 (stack) =>
79 Effect.gen(function* () {
80 yield* stack.destroy();
81
82 const network = yield* defaultNetwork;
83
84 const { cluster } = yield* stack.deploy(
85 Effect.gen(function* () {
86 const user = yield* User("ClusterUser", {
87 authenticationMode: {
88 type: "password",
89 passwords: [Redacted.make("AlchemyMemoryDbTestPass01")],
90 },
91 accessString: "on ~* +@all",
92 });
93 const acl = yield* ACL("ClusterAcl", { userNames: [user.userName] });
94 const subnetGroup = yield* SubnetGroup("ClusterSubnets", {
95 description: "alchemy memorydb cluster subnets",
96 subnetIds: network.subnetIds,
97 });
98 const cluster = yield* Cluster("Cache", {
99 nodeType: "db.t4g.small",
100 engine: "valkey",
101 aclName: acl.aclName,
102 subnetGroupName: subnetGroup.subnetGroupName,
103 securityGroupIds: network.securityGroupIds,
104 numShards: 1,
105 numReplicasPerShard: 0,
106 description: "alchemy memorydb fixture",
107 tags: { fixture: "memorydb-cluster" },
108 });
109 return { cluster };
110 }),
111 );
112
113 expect(cluster.clusterName).toBeDefined();
114 expect(cluster.clusterArn).toContain(":cluster/");
115 expect(cluster.status).toBe("available");
116 expect(cluster.nodeType).toBe("db.t4g.small");
117 expect(cluster.numberOfShards).toBe(1);
118 expect(cluster.endpointAddress).toContain("memorydb");
119
120 // Out-of-band verification via distilled.
121 const described = yield* memorydb.describeClusters({ ClusterName: cluster.clusterName });
122 const observed = described.Clusters?.[0];
123 expect(observed?.Status).toBe("available");
124 expect(observed?.TLSEnabled).toBe(true);
125 expect(observed?.ACLName).toBeDefined();
126
127 // Destroy immediately — clusters bill while they exist — and verify
128 // deletion was initiated out-of-band.
129 yield* stack.destroy();
130 yield* assertClusterDeleting(cluster.clusterName);
131 }),
132 // create (~10-15 min) + delete-until-gone (~5-10 min), one test.
133 {
134 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:memorydb", "live"],
135 timeout: 2_400_000,
136 },
137 );