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";
17const PUBSUB_EVENT_TYPE = "google.cloud.pubsub.topic.v1.messagePublished";
18const WORKFLOW_SOURCE = `main:
19 params: [event]
20 steps:
21 - return_event:
22 return: \${event}
23`;
24
25const waitUntilGone = (name: string) =>
26 eventarc.getProjectsLocationsTriggers({ name }).pipe(
27 Effect.as("found" as const),
28 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
29 Effect.repeat({
30 schedule: Schedule.spaced("2 seconds"),
31 until: (status) => status === "gone",
32 times: 10,
33 }),
34 );
35
36const eventFilters: GCP.Eventarc.EventFilter[] = [
37 { attribute: "type", value: PUBSUB_EVENT_TYPE },
38];
39
40test.provider(
41 "create, update, and delete an Eventarc trigger",
42 (stack) =>
43 Effect.gen(function* () {
44 yield* stack.destroy();
45
46 const created = yield* stack.deploy(
47 Effect.gen(function* () {
48 const topic = yield* GCP.PubSub.Topic("Events", {
49 labels: { env: "test" },
50 });
51 const workflow = yield* GCP.Workflows.Workflow("Sink", {
52 location: LOCATION,
53 sourceContents: WORKFLOW_SOURCE,
54 labels: { env: "test" },
55 });
56 const trigger = yield* GCP.Eventarc.Trigger("PubSubEvents", {
57 location: LOCATION,
58 eventFilters,
59 destination: { workflow: workflow.name },
60 transport: { pubsub: { topic: topic.name } },
61 labels: { env: "test" },
62 });
63 return { topic, workflow, trigger };
64 }),
65 );
66
67 expect(created.trigger.name).toContain("/triggers/");
68 expect(created.trigger.triggerId).toEqual(expect.any(String));
69 expect(created.trigger.location).toEqual(LOCATION);
70 expect(created.trigger.labels).toMatchObject({ env: "test" });
71 expect(created.trigger.eventFilters).toEqual(
72 expect.arrayContaining([
73 expect.objectContaining({
74 attribute: "type",
75 value: PUBSUB_EVENT_TYPE,
76 }),
77 ]),
78 );
79 expect(created.trigger.destination?.workflow).toEqual(
80 created.workflow.name,
81 );
82 expect(created.trigger.transport?.pubsub?.topic).toEqual(
83 created.topic.name,
84 );
85
86 const fetched = yield* eventarc.getProjectsLocationsTriggers({
87 name: created.trigger.name,
88 });
89 expect(fetched.name).toEqual(created.trigger.name);
90 expect(fetched.labels?.env).toEqual("test");
91 expect(fetched.destination?.workflow).toEqual(created.workflow.name);
92 expect(fetched.transport?.pubsub?.topic).toEqual(created.topic.name);
93
94 const updated = yield* stack.deploy(
95 Effect.gen(function* () {
96 const topic = yield* GCP.PubSub.Topic("Events", {
97 topicId: created.topic.topicId,
98 labels: { env: "test" },
99 });
100 const workflow = yield* GCP.Workflows.Workflow("Sink", {
101 workflowId: created.workflow.workflowId,
102 location: LOCATION,
103 sourceContents: WORKFLOW_SOURCE,
104 labels: { env: "test" },
105 });
106 return yield* GCP.Eventarc.Trigger("PubSubEvents", {
107 triggerId: created.trigger.triggerId,
108 location: LOCATION,
109 eventFilters,
110 destination: { workflow: workflow.name },
111 transport: { pubsub: { topic: topic.name } },
112 eventDataContentType: "application/json",
113 labels: { env: "prod", role: "events" },
114 });
115 }),
116 );
117
118 expect(updated.name).toEqual(created.trigger.name);
119 expect(updated.triggerId).toEqual(created.trigger.triggerId);
120 expect(updated.labels).toMatchObject({ env: "prod", role: "events" });
121 expect(updated.eventDataContentType).toEqual("application/json");
122
123 const refetched = yield* eventarc.getProjectsLocationsTriggers({
124 name: created.trigger.name,
125 });
126 expect(refetched.labels?.env).toEqual("prod");
127 expect(refetched.labels?.role).toEqual("events");
128 expect(
129 refetched.eventDataContentType === undefined ||
130 refetched.eventDataContentType === "application/json",
131 ).toEqual(true);
132
133 yield* stack.destroy();
134
135 const gone = yield* waitUntilGone(created.trigger.name);
136 expect(gone).toEqual("gone");
137 }).pipe(logLevel),
138 { tags: ["provider:gcp", "provider:gcp:eventarc", "live"], timeout: 120_000 },
139);