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

main
ts 133 lines 4.85 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import type { SubnetId } from "@alchemy.run/aws/EC2/Subnet";
3 import * as Core from "@alchemy.run/test/Core";
4 import * as EC2 from "@distilled.cloud/aws/ec2";
5 import * as ElastiCache from "@distilled.cloud/aws/elasticache";
6 import * as Deferred from "effect/Deferred";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import { getDefaultVpc } from "../DefaultVpc.ts";
10 import { networkState, type ProvisionedNetwork } from "./ProvisionedNetwork.ts";
11
12 export { getProvisionedNetwork, type ProvisionedNetwork } from "./ProvisionedNetwork.ts";
13
14 const testOptions = { providers: AWS.providers() };
15 const networkStack = Core.scratchStack(
16 testOptions,
17 "Network",
18 "test/AWS/ElastiCache/ProvisionedFixture.ts",
19 );
20
21 let holders = 0;
22 let deployed = false;
23
24 // The account's default VPC costs no VPC quota, so cache suites never queue
25 // behind EC2 suites for VPC capacity. Lambdas reach the caches over private
26 // addresses, so public default subnets need no NAT.
27 const findDefaultSubnets = Effect.gen(function* () {
28 const vpc = yield* getDefaultVpc;
29 const subnets = yield* EC2.describeSubnets({
30 Filters: [
31 { Name: "vpc-id", Values: [vpc.vpcId] },
32 { Name: "default-for-az", Values: ["true"] },
33 { Name: "state", Values: ["available"] },
34 ],
35 });
36 // Not every AZ offers every cache node type; stay within the first three.
37 const subnetIds = (subnets.Subnets ?? [])
38 .filter((subnet) => /[abc]$/.test(subnet.AvailabilityZone ?? ""))
39 .sort((l, r) => (l.AvailabilityZone ?? "").localeCompare(r.AvailabilityZone ?? ""))
40 .flatMap((subnet) => (subnet.SubnetId ? [subnet.SubnetId] : []))
41 .slice(0, 2) as SubnetId[];
42 if (subnetIds.length < 2) {
43 return yield* Effect.fail(
44 new Error("ElastiCache tests require two default subnets in AZs a-c"),
45 );
46 }
47 return { vpcId: vpc.vpcId, subnetIds };
48 });
49
50 const deployNetwork = Effect.gen(function* () {
51 const { vpcId, subnetIds } = yield* Core.withProviders(
52 findDefaultSubnets,
53 testOptions,
54 "Network",
55 );
56 yield* networkStack.destroy();
57 return yield* networkStack.deploy(
58 Effect.gen(function* () {
59 const securityGroup = yield* AWS.EC2.SecurityGroup("CacheSecurityGroup", {
60 vpcId,
61 description: "ElastiCache shared cache access",
62 tags: { fixture: "elasticache-provisioned" },
63 });
64 const subnetGroup = yield* AWS.ElastiCache.SubnetGroup("Subnets", {
65 description: "alchemy provisioned cache subnets",
66 subnetIds,
67 tags: { fixture: "elasticache-provisioned" },
68 });
69 return {
70 vpcId,
71 subnetIds,
72 securityGroupId: securityGroup.groupId,
73 subnetGroupName: subnetGroup.subnetGroupName,
74 } as unknown as ProvisionedNetwork;
75 }),
76 );
77 });
78
79 /** First caller deploys the shared VPC; everyone else waits for it. */
80 export const acquireProvisionedNetwork = Effect.gen(function* () {
81 holders += 1;
82 if (networkState.started) {
83 return yield* Deferred.await(networkState.ready);
84 }
85 networkState.started = true;
86 const attrs = yield* deployNetwork.pipe(
87 Effect.tapError((error) =>
88 Effect.gen(function* () {
89 networkState.started = false;
90 yield* Deferred.fail(networkState.ready, error);
91 networkState.ready = Deferred.makeUnsafe();
92 }),
93 ),
94 );
95 deployed = true;
96 yield* Deferred.succeed(networkState.ready, attrs);
97 return attrs;
98 });
99
100 export const releaseProvisionedNetwork = Effect.suspend(() => {
101 holders = Math.max(0, holders - 1);
102 if (holders > 0 || !deployed) return Effect.void;
103 deployed = false;
104 networkState.started = false;
105 networkState.ready = Deferred.makeUnsafe();
106 return networkStack.destroy();
107 });
108
109 export const shareProvisionedNetwork = (hooks: {
110 beforeAll: (eff: Effect.Effect<unknown, any, any>, options?: { timeout?: number }) => unknown;
111 afterAll: (eff: Effect.Effect<unknown, any, any>, options?: { timeout?: number }) => void;
112 }) => {
113 hooks.beforeAll(acquireProvisionedNetwork, { timeout: 180_000 });
114 hooks.afterAll(releaseProvisionedNetwork, { timeout: 180_000 });
115 };
116
117 export const assertReplicationGroupGone = (name: string) =>
118 ElastiCache.describeReplicationGroups({ ReplicationGroupId: name }).pipe(
119 Effect.flatMap(() => Effect.fail(new Error(`replication group '${name}' still exists`))),
120 Effect.catchTag("ReplicationGroupNotFoundFault", () => Effect.void),
121 Effect.retry({
122 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]),
123 }),
124 );
125
126 export const assertCacheClusterGone = (name: string) =>
127 ElastiCache.describeCacheClusters({ CacheClusterId: name }).pipe(
128 Effect.flatMap(() => Effect.fail(new Error(`cache cluster '${name}' still exists`))),
129 Effect.catchTag("CacheClusterNotFoundFault", () => Effect.void),
130 Effect.retry({
131 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]),
132 }),
133 );