@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 140 lines 5.71 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Cluster, Hsm } from "@alchemy.run/aws/CloudHSMV2";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as cloudhsm from "@distilled.cloud/aws/cloudhsm-v2";
6 import * as EC2 from "@distilled.cloud/aws/ec2";
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 probes: describeClusters is a filtered list — an unknown cluster id
14 // yields an empty page rather than a typed NotFound. Proves auth + the
15 // response schema decode in every CI pass at near-zero cost.
16 test.provider(
17 "describeClusters with an unknown cluster id filter returns an empty page",
18 () =>
19 Effect.gen(function* () {
20 const response = yield* cloudhsm.describeClusters({
21 Filters: { clusterIds: ["cluster-aaaaaaaaaaa"] },
22 });
23 expect(response.Clusters ?? []).toHaveLength(0);
24 }),
25 { tags: ["provider:aws", "provider:aws:cloudhsmv2", "live"] },
26 );
27
28 // Ungated typed-error probe: prove the distilled union carries the not-found
29 // tag the provider's delete path depends on.
30 test.provider(
31 "deleteCluster on a nonexistent cluster fails with CloudHsmResourceNotFoundException",
32 () =>
33 Effect.gen(function* () {
34 const error = yield* Effect.flip(
35 cloudhsm.deleteCluster({ ClusterId: "cluster-aaaaaaaaaaa" }),
36 );
37 expect(error._tag).toBe("CloudHsmResourceNotFoundException");
38 }),
39 { tags: ["provider:aws", "provider:aws:cloudhsmv2", "live"] },
40 );
41
42 // Resolve two default-for-AZ subnets (distinct AZs) from the default VPC.
43 const defaultNetwork = Effect.gen(function* () {
44 const vpc = yield* getDefaultVpc;
45 const subnets = yield* EC2.describeSubnets({
46 Filters: [
47 { Name: "vpc-id", Values: [vpc.vpcId] },
48 { Name: "default-for-az", Values: ["true"] },
49 ],
50 });
51 // CloudHSM is not offered in every AZ — pick subnets from the region's
52 // first AZs (suffix a/b) only.
53 const picked = (subnets.Subnets ?? [])
54 .filter((s) => /[ab]$/.test(s.AvailabilityZone ?? ""))
55 .sort((l, r) => (l.AvailabilityZone ?? "").localeCompare(r.AvailabilityZone ?? ""))
56 .slice(0, 2);
57 const subnetIds = picked.map((s) => s.SubnetId).filter((id): id is string => id !== undefined);
58 const availabilityZones = picked
59 .map((s) => s.AvailabilityZone)
60 .filter((az): az is string => az !== undefined);
61 if (subnetIds.length < 2) {
62 return yield* Effect.die(new Error("default VPC is missing default-for-az subnets in AZs a/b"));
63 }
64 return { subnetIds, availabilityZones };
65 });
66
67 // Deletion is verified as INITIATED (DELETE_IN_PROGRESS, irreversible) or
68 // fully gone (absent / DELETED).
69 const assertClusterDeleting = (clusterId: string) =>
70 Effect.gen(function* () {
71 const response = yield* cloudhsm.describeClusters({ Filters: { clusterIds: [clusterId] } });
72 const state = response.Clusters?.[0]?.State ?? "gone";
73 if (state !== "gone" && state !== "DELETED" && state !== "DELETE_IN_PROGRESS") {
74 return yield* Effect.fail(new Error(`cluster '${clusterId}' still exists (state: ${state})`));
75 }
76 }).pipe(
77 Effect.retry({ schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]) }),
78 );
79
80 // A CloudHSM cluster provisions in a few minutes but its HSM takes ~10-20
81 // minutes and bills hourly while it exists (~$1.45/hr). The full lifecycle is
82 // gated behind AWS_TEST_CLOUDHSM=1 and always destroys what it created.
83 test.provider.skipIf(!process.env.AWS_TEST_CLOUDHSM)(
84 "create CloudHSM cluster with one HSM, verify, destroy",
85 (stack) =>
86 Effect.gen(function* () {
87 yield* stack.destroy();
88
89 const network = yield* defaultNetwork;
90
91 const { cluster, hsm } = yield* stack.deploy(
92 Effect.gen(function* () {
93 const cluster = yield* Cluster("HsmCluster", {
94 hsmType: "hsm2m.medium",
95 subnetIds: network.subnetIds,
96 tags: { fixture: "cloudhsm-cluster" },
97 });
98 const hsm = yield* Hsm("Primary", {
99 clusterId: cluster.clusterId,
100 availabilityZone: network.availabilityZones[0],
101 });
102 return { cluster, hsm };
103 }),
104 );
105
106 expect(cluster.clusterId).toMatch(/^cluster-/);
107 expect(["UNINITIALIZED", "INITIALIZED", "ACTIVE", "DEGRADED"]).toContain(cluster.state);
108 expect(cluster.hsmType).toBe("hsm2m.medium");
109 expect(cluster.vpcId).toBeDefined();
110 expect(cluster.securityGroup).toBeDefined();
111 expect(cluster.subnetIds.length).toBe(2);
112 expect(hsm.hsmId).toMatch(/^hsm-/);
113 expect(hsm.clusterId).toBe(cluster.clusterId);
114 expect(hsm.state).toBe("ACTIVE");
115 expect(hsm.eniIp).toBeDefined();
116
117 // Out-of-band verification via distilled.
118 const described = yield* cloudhsm.describeClusters({
119 Filters: { clusterIds: [cluster.clusterId] },
120 });
121 const observed = described.Clusters?.[0];
122 expect(observed?.ClusterId).toBe(cluster.clusterId);
123 expect(observed?.HsmType).toBe("hsm2m.medium");
124 expect(observed?.Mode).toBe("FIPS");
125 expect(
126 observed?.TagList?.some((tag) => tag.Key === "fixture" && tag.Value === "cloudhsm-cluster"),
127 ).toBe(true);
128 expect(observed?.Hsms?.some((h) => h.HsmId === hsm.hsmId)).toBe(true);
129
130 // Destroy immediately — HSMs bill hourly — and verify deletion was
131 // initiated out-of-band (HSM first, then cluster).
132 yield* stack.destroy();
133 yield* assertClusterDeleting(cluster.clusterId);
134 }),
135 // HSM create (~10-20 min) + HSM delete wait + cluster delete, one test.
136 {
137 tags: ["provider:aws", "provider:aws:cloudhsmv2", "provider:aws:ec2", "live"],
138 timeout: 2_400_000,
139 },
140 );