1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as pubsub from "@distilled.cloud/gcp/pubsub_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const logLevel = Effect.provideService(
12 MinimumLogLevel,
13 process.env.DEBUG ? "Debug" : "Info",
14);
15
16const waitUntilGone = (name: string) =>
17 pubsub.getProjectsTopics({ topic: name }).pipe(
18 Effect.as("found" as const),
19 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
20 Effect.repeat({
21 schedule: Schedule.spaced("1 second"),
22 until: (status) => status === "gone",
23 times: 10,
24 }),
25 );
26
27test.provider(
28 "create, update, and delete a topic",
29 (stack) =>
30 Effect.gen(function* () {
31 yield* stack.destroy();
32
33 const created = yield* stack.deploy(
34 Effect.gen(function* () {
35 return yield* GCP.PubSub.Topic("Events", {
36 labels: { env: "test" },
37 });
38 }),
39 );
40
41 expect(created.name).toContain("/topics/");
42 expect(created.topicId).toEqual(expect.any(String));
43 expect(created.labels).toMatchObject({ env: "test" });
44
45 const fetched = yield* pubsub.getProjectsTopics({ topic: created.name });
46 expect(fetched.name).toEqual(created.name);
47 expect(fetched.labels?.env).toEqual("test");
48
49 const updated = yield* stack.deploy(
50 Effect.gen(function* () {
51 return yield* GCP.PubSub.Topic("Events", {
52 topicId: created.topicId,
53 labels: { env: "prod", role: "events" },
54 messageRetentionDuration: "86400s",
55 });
56 }),
57 );
58
59 expect(updated.name).toEqual(created.name);
60 expect(updated.labels).toMatchObject({ env: "prod", role: "events" });
61 expect(updated.messageRetentionDuration).toEqual("86400s");
62
63 yield* stack.destroy();
64
65 const gone = yield* waitUntilGone(created.name);
66 expect(gone).toEqual("gone");
67 }).pipe(logLevel),
68 { tags: ["provider:gcp", "provider:gcp:pubsub", "live"], timeout: 120_000 },
69);