1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as eventarc from "@distilled.cloud/gcp/eventarc_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 LOCATION = "us-central1";
17
18const waitUntilGone = (name: string) =>
19 eventarc.getProjectsLocationsChannels({ name }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("2 seconds"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29test.provider(
30 "create, update, and delete an Eventarc channel",
31 (stack) =>
32 Effect.gen(function* () {
33 yield* stack.destroy();
34
35 const created = yield* stack.deploy(
36 Effect.gen(function* () {
37 return yield* GCP.Eventarc.Channel("Events", {
38 location: LOCATION,
39 labels: { env: "test" },
40 });
41 }),
42 );
43
44 expect(created.name).toContain("/channels/");
45 expect(created.channelId).toEqual(expect.any(String));
46 expect(created.location).toEqual(LOCATION);
47 expect(created.labels).toMatchObject({ env: "test" });
48
49 const fetched = yield* eventarc.getProjectsLocationsChannels({
50 name: created.name,
51 });
52 expect(fetched.name).toEqual(created.name);
53 expect(fetched.labels?.env).toEqual("test");
54
55 const updated = yield* stack.deploy(
56 Effect.gen(function* () {
57 return yield* GCP.Eventarc.Channel("Events", {
58 channelId: created.channelId,
59 location: LOCATION,
60 labels: { env: "prod", role: "events" },
61 });
62 }),
63 );
64
65 expect(updated.name).toEqual(created.name);
66 expect(updated.channelId).toEqual(created.channelId);
67 expect(updated.labels).toMatchObject({ env: "prod", role: "events" });
68
69 const refetched = yield* eventarc.getProjectsLocationsChannels({
70 name: created.name,
71 });
72 expect(refetched.labels?.env).toEqual("prod");
73 expect(refetched.labels?.role).toEqual("events");
74
75 yield* stack.destroy();
76
77 const gone = yield* waitUntilGone(created.name);
78 expect(gone).toEqual("gone");
79 }).pipe(logLevel),
80 { tags: ["provider:gcp", "provider:gcp:eventarc", "live"], timeout: 120_000 },
81);