The best way to control Claude Code, Codex and more from any computer or phone

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