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