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

main
ts 165 lines 6.54 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
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 Kafka from "@distilled.cloud/aws/kafka";
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 typed-error probe: proves the distilled kafka error union carries the
14 // NotFoundException tag this provider's read/delete paths depend on. A
15 // well-formed but nonexistent cluster ARN returns NotFoundException.
16 test.provider(
17 "describeClusterV2 on a nonexistent cluster fails with NotFoundException",
18 () =>
19 Effect.gen(function* () {
20 const { accountId, region } = yield* AWSEnvironment.current;
21 const error = yield* Effect.flip(
22 Kafka.describeClusterV2({
23 ClusterArn: `arn:aws:kafka:${region}:${accountId}:cluster/alchemy-nonexistent-probe/00000000-0000-0000-0000-000000000000-1`,
24 }),
25 );
26 expect(error._tag).toBe("NotFoundException");
27 }),
28 { tags: ["provider:aws", "provider:aws:kafka", "live"] },
29 );
30
31 const nonexistentClusterArn = Effect.gen(function* () {
32 const { accountId, region } = yield* AWSEnvironment.current;
33 return `arn:aws:kafka:${region}:${accountId}:cluster/alchemy-nonexistent-probe/00000000-0000-0000-0000-000000000000-1`;
34 });
35
36 // Ungated typed-error probes for the operations behind the runtime bindings
37 // (GetBootstrapBrokers + the topic control plane). Each proves the distilled
38 // error union carries the tag a caller must handle for a missing cluster.
39 test.provider(
40 "getBootstrapBrokers on a nonexistent cluster fails with a typed tag",
41 () =>
42 Effect.gen(function* () {
43 const ClusterArn = yield* nonexistentClusterArn;
44 const error = yield* Effect.flip(Kafka.getBootstrapBrokers({ ClusterArn }));
45 expect(error._tag).toBe("NotFoundException");
46 }),
47 { tags: ["provider:aws", "provider:aws:kafka", "live"] },
48 );
49
50 test.provider(
51 "listTopics on a nonexistent cluster fails with a typed tag",
52 () =>
53 Effect.gen(function* () {
54 const ClusterArn = yield* nonexistentClusterArn;
55 const error = yield* Effect.flip(Kafka.listTopics({ ClusterArn }));
56 expect(error._tag).toBe("NotFoundException");
57 }),
58 { tags: ["provider:aws", "provider:aws:kafka", "live"] },
59 );
60
61 test.provider(
62 "describeTopic on a nonexistent cluster fails with a typed tag",
63 () =>
64 Effect.gen(function* () {
65 const ClusterArn = yield* nonexistentClusterArn;
66 const error = yield* Effect.flip(
67 Kafka.describeTopic({ ClusterArn, TopicName: "alchemy-probe" }),
68 );
69 expect(error._tag).toBe("NotFoundException");
70 }),
71 { tags: ["provider:aws", "provider:aws:kafka", "live"] },
72 );
73
74 test.provider(
75 "createTopic on a nonexistent cluster fails with a typed tag",
76 () =>
77 Effect.gen(function* () {
78 const ClusterArn = yield* nonexistentClusterArn;
79 const error = yield* Effect.flip(
80 Kafka.createTopic({ ClusterArn, TopicName: "alchemy-probe", PartitionCount: 1 }),
81 );
82 // The topic control plane validates the request body before resolving
83 // the cluster, so a nonexistent cluster surfaces the typed 400.
84 expect(error._tag).toBe("BadRequestException");
85 }),
86 { tags: ["provider:aws", "provider:aws:kafka", "live"] },
87 );
88
89 // Resolve two default-for-AZ subnets from the account's default VPC — MSK
90 // requires at least two subnets in distinct Availability Zones.
91 const resolveSubnets = Effect.gen(function* () {
92 const vpc = yield* getDefaultVpc;
93 const subnets = yield* EC2.describeSubnets({
94 Filters: [
95 { Name: "vpc-id", Values: [vpc.vpcId] },
96 { Name: "default-for-az", Values: ["true"] },
97 ],
98 });
99 const byAz = new Map<string, string>();
100 for (const s of subnets.Subnets ?? []) {
101 if (s.AvailabilityZone && s.SubnetId && !byAz.has(s.AvailabilityZone)) {
102 byAz.set(s.AvailabilityZone, s.SubnetId);
103 }
104 }
105 return [...byAz.values()].slice(0, 3);
106 });
107
108 // MSK Serverless clusters take ~5-10 minutes to create and are metered while
109 // they exist. The full lifecycle is skipped under `--fast` and always
110 // destroys what it created.
111 test.provider.skipIf(!!process.env.FAST)(
112 "create serverless cluster, verify IAM-auth brokers, destroy, verify gone",
113 (stack) =>
114 Effect.gen(function* () {
115 yield* stack.destroy();
116
117 const subnetIds = yield* resolveSubnets;
118 expect(subnetIds.length).toBeGreaterThanOrEqual(2);
119
120 const { cluster } = yield* stack.deploy(
121 Effect.gen(function* () {
122 const cluster = yield* AWS.Kafka.ServerlessCluster("Events", {
123 subnetIds,
124 tags: { fixture: "kafka-serverless" },
125 });
126 return { cluster };
127 }),
128 );
129
130 expect(cluster.clusterName).toBeDefined();
131 expect(cluster.clusterArn).toContain(":cluster/");
132 expect(cluster.clusterType).toBe("SERVERLESS");
133 expect(cluster.state).toBe("ACTIVE");
134 // Serverless clusters only expose the SASL/IAM bootstrap endpoint.
135 expect(cluster.bootstrapBrokerStringSaslIam).toBeDefined();
136 expect(cluster.bootstrapBrokerStringSaslIam).toContain(":9098");
137
138 // Out-of-band verification via distilled.
139 const described = yield* Kafka.describeClusterV2({ ClusterArn: cluster.clusterArn });
140 const info = described.ClusterInfo;
141 expect(info?.State).toBe("ACTIVE");
142 expect(info?.ClusterType).toBe("SERVERLESS");
143 expect(info?.Serverless?.ClientAuthentication?.Sasl?.Iam?.Enabled).toBe(true);
144
145 yield* stack.destroy();
146 yield* assertClusterDeleted(cluster.clusterArn);
147 }),
148 // create (~5-10 min) + destroy initiation, one test.
149 { tags: ["provider:aws", "provider:aws:ec2", "provider:aws:kafka", "live"], timeout: 1_200_000 },
150 );
151
152 // Deletion is verified as INITIATED (state DELETING, irreversible) or fully
153 // gone. Full disappearance takes several more minutes server-side.
154 const assertClusterDeleted = (arn: string) =>
155 Effect.gen(function* () {
156 const state = yield* Kafka.describeClusterV2({ ClusterArn: arn }).pipe(
157 Effect.map((r) => r.ClusterInfo?.State ?? "gone"),
158 Effect.catchTag("NotFoundException", () => Effect.succeed("gone" as const)),
159 );
160 if (state !== "gone" && state !== "DELETING") {
161 return yield* Effect.fail(new Error(`MSK cluster '${arn}' still exists (state: ${state})`));
162 }
163 }).pipe(
164 Effect.retry({ schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]) }),
165 );