1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as cloudasset from "@distilled.cloud/gcp/cloudasset_v1";
4import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9import { GcpEnvironment } from "@/GCP/Environment";
10
11const { test } = Test.make({ providers: GCP.providers() });
12
13const logLevel = Effect.provideService(
14 MinimumLogLevel,
15 process.env.DEBUG ? "Debug" : "Info",
16);
17
18const projectNumber = Effect.gen(function* () {
19 const { project } = yield* GcpEnvironment.current;
20 const resource = yield* resourcemanager.getProjects({
21 name: `projects/${project}`,
22 });
23 const parts = (resource.name ?? "").split("/");
24 return parts[parts.length - 1] || project;
25});
26
27const waitUntilGone = (name: string) =>
28 cloudasset.getFeeds({ name }).pipe(
29 Effect.as("found" as const),
30 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
31 Effect.repeat({
32 schedule: Schedule.spaced("1 second"),
33 until: (status) => status === "gone",
34 times: 10,
35 }),
36 );
37
38test.provider(
39 "getFeeds on a missing feed fails with a typed tag",
40 (stack) =>
41 Effect.gen(function* () {
42 yield* stack.destroy();
43
44 const number = yield* projectNumber;
45 const error = yield* Effect.flip(
46 cloudasset.getFeeds({
47 name: `projects/${number}/feeds/alchemy-missing-feed`,
48 }),
49 );
50 expect(error._tag).toEqual("NotFound");
51
52 yield* stack.destroy();
53 }).pipe(logLevel),
54 {
55 tags: ["provider:gcp", "provider:gcp:cloudasset", "live"],
56 timeout: 90_000,
57 },
58);
59
60test.provider(
61 "create, update, and delete an asset feed",
62 (stack) =>
63 Effect.gen(function* () {
64 const { project } = yield* GcpEnvironment.current;
65 yield* stack.destroy();
66
67 const created = yield* stack.deploy(
68 Effect.gen(function* () {
69 const topic = yield* GCP.PubSub.Topic("AssetEvents", {
70 labels: { env: "test" },
71 });
72 const feed = yield* GCP.CloudAsset.Feed("Buckets", {
73 pubsubTopic: topic.name,
74 assetTypes: ["storage.googleapis.com/Bucket"],
75 contentType: "RESOURCE",
76 condition: { description: "watch buckets" },
77 });
78 return { topic, feed };
79 }),
80 );
81
82 expect(created.feed.name).toContain("/feeds/");
83 expect(created.feed.feedId).toEqual(expect.any(String));
84 expect(created.feed.pubsubTopic).toEqual(created.topic.name);
85 expect(created.feed.assetTypes).toEqual(
86 expect.arrayContaining(["storage.googleapis.com/Bucket"]),
87 );
88 expect(created.feed.contentType).toEqual("RESOURCE");
89 expect(created.feed.condition?.description).toEqual("watch buckets");
90
91 const fetched = yield* cloudasset.getFeeds({ name: created.feed.name });
92 expect(fetched.name).toEqual(created.feed.name);
93 expect(fetched.feedOutputConfig?.pubsubDestination?.topic).toEqual(
94 created.topic.name,
95 );
96 expect(fetched.condition?.description).toContain("alchemy-id=");
97 expect(fetched.condition?.description).toContain("watch buckets");
98
99 const updated = yield* stack.deploy(
100 Effect.gen(function* () {
101 const topic = yield* GCP.PubSub.Topic("AssetEvents", {
102 topicId: created.topic.topicId,
103 labels: { env: "test" },
104 });
105 const feed = yield* GCP.CloudAsset.Feed("Buckets", {
106 feedId: created.feed.feedId,
107 pubsubTopic: topic.name,
108 assetTypes: [
109 "storage.googleapis.com/Bucket",
110 "pubsub.googleapis.com/Topic",
111 ],
112 contentType: "RESOURCE",
113 condition: { description: "watch buckets and topics" },
114 });
115 return { topic, feed };
116 }),
117 );
118
119 expect(updated.feed.name).toEqual(created.feed.name);
120 expect(updated.feed.feedId).toEqual(created.feed.feedId);
121 expect(updated.feed.assetTypes).toEqual(
122 expect.arrayContaining([
123 "storage.googleapis.com/Bucket",
124 "pubsub.googleapis.com/Topic",
125 ]),
126 );
127 expect(updated.feed.condition?.description).toEqual(
128 "watch buckets and topics",
129 );
130
131 const refetched = yield* cloudasset.getFeeds({
132 name: created.feed.name,
133 });
134 expect(refetched.assetTypes).toEqual(
135 expect.arrayContaining(["pubsub.googleapis.com/Topic"]),
136 );
137 expect(refetched.condition?.description).toContain(
138 "watch buckets and topics",
139 );
140
141 yield* stack.destroy();
142
143 const gone = yield* waitUntilGone(created.feed.name);
144 expect(gone).toEqual("gone");
145 }).pipe(logLevel),
146 {
147 tags: ["provider:gcp", "provider:gcp:cloudasset", "live"],
148 timeout: 90_000,
149 },
150);