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

main
ts 131 lines 4.65 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as kafka from "@distilled.cloud/gcp/managedkafka_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { withKafkaClusterSlot } from "./quota.ts";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // Kafka and Connect clusters take ~30 minutes to provision.
16 const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST;
17
18 const waitUntilGone = (name: string) =>
19 kafka.getProjectsLocationsConnectClustersConnectors({ name }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("1 second"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29 test.provider(
30 "getProjectsLocationsConnectClustersConnectors on a missing connector fails with a typed tag",
31 (stack) =>
32 Effect.gen(function* () {
33 const { project } = yield* GcpEnvironment.current;
34
35 yield* stack.destroy();
36
37 const error = yield* Effect.flip(
38 kafka.getProjectsLocationsConnectClustersConnectors({
39 name: `projects/${project}/locations/us-central1/connectClusters/alchemy-missing/connectors/alchemy-missing`,
40 }),
41 );
42 expect(error._tag).toEqual("NotFound");
43
44 yield* stack.destroy();
45 }).pipe(logLevel),
46 {
47 tags: ["provider:gcp", "provider:gcp:managedkafka", "live"],
48 timeout: 90_000,
49 },
50 );
51
52 test.provider.skipIf(!runLifecycle)(
53 "create, update, and delete a kafka connector",
54 (stack) =>
55 Effect.gen(function* () {
56 const { project } = yield* GcpEnvironment.current;
57
58 yield* stack.destroy();
59
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 const cluster = yield* GCP.ManagedKafka.Cluster("Brokers", {
63 location: "us-central1",
64 labels: { env: "test" },
65 });
66 const connect = yield* GCP.ManagedKafka.ConnectCluster("Connect", {
67 kafkaCluster: cluster.name,
68 labels: { env: "test" },
69 });
70 const connector = yield* GCP.ManagedKafka.ConnectClustersConnector("Sink", {
71 connectCluster: connect.name,
72 configs: {
73 "connector.class": "com.google.pubsub.kafka.sink.CloudPubSubSinkConnector",
74 "tasks.max": "1",
75 topics: "orders",
76 "cps.project": project,
77 "cps.topic": "alchemy-managedkafka-sink",
78 },
79 });
80 return { cluster, connect, connector };
81 }),
82 );
83
84 expect(created.connector.name).toContain("/connectors/");
85 expect(created.connector.configs["connector.class"]).toContain("PubSub");
86
87 const fetched = yield* kafka.getProjectsLocationsConnectClustersConnectors({
88 name: created.connector.name,
89 });
90 expect(fetched.name).toEqual(created.connector.name);
91
92 const updated = yield* stack.deploy(
93 Effect.gen(function* () {
94 const cluster = yield* GCP.ManagedKafka.Cluster("Brokers", {
95 clusterId: created.cluster.clusterId,
96 location: "us-central1",
97 labels: { env: "test" },
98 });
99 const connect = yield* GCP.ManagedKafka.ConnectCluster("Connect", {
100 connectClusterId: created.connect.connectClusterId,
101 kafkaCluster: cluster.name,
102 labels: { env: "test" },
103 });
104 const connector = yield* GCP.ManagedKafka.ConnectClustersConnector("Sink", {
105 connectCluster: connect.name,
106 connectorId: created.connector.connectorId,
107 configs: {
108 "connector.class": "com.google.pubsub.kafka.sink.CloudPubSubSinkConnector",
109 "tasks.max": "2",
110 topics: "orders",
111 "cps.project": project,
112 "cps.topic": "alchemy-managedkafka-sink",
113 },
114 });
115 return { cluster, connect, connector };
116 }),
117 );
118 expect(updated.connector.connectorId).toEqual(created.connector.connectorId);
119 expect(updated.connector.configs["tasks.max"]).toEqual("2");
120
121 yield* stack.destroy();
122
123 const gone = yield* waitUntilGone(created.connector.name);
124 expect(gone).toEqual("gone");
125 }).pipe(logLevel, withKafkaClusterSlot),
126 {
127 tags: ["provider:gcp", "provider:gcp:managedkafka", "live"],
128 timeout: 10_800_000,
129 retry: 0,
130 },
131 );