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

main
ts 145 lines 4.94 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 we from "@distilled.cloud/gcp/workspaceevents_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 // Subscriptions need a real Workspace resource (e.g. a Chat space) the
15 // credentials can watch; set GCP_TEST_WORKSPACE_EVENTS_TARGET to run the lifecycle.
16 const runLifecycle = !!process.env.GCP_TEST_WORKSPACE_EVENTS_TARGET;
17
18 const waitUntilGone = (name: string) =>
19 we.getSubscriptions({ name }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("1 second"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29 test.provider(
30 "getSubscriptions on a missing subscription fails with a typed tag",
31 (stack) =>
32 Effect.gen(function* () {
33 yield* stack.destroy();
34
35 const error = yield* Effect.flip(
36 we.getSubscriptions({
37 name: "subscriptions/alchemy-missing-subscription",
38 }),
39 );
40 expect(error._tag).toEqual("WorkspaceEventsScopeInsufficient");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 {
45 tags: ["provider:gcp", "provider:gcp:workspaceevents", "live"],
46 timeout: 90_000,
47 },
48 );
49
50 test.provider.skipIf(runLifecycle)(
51 "createSubscriptions without Workspace Events access fails with a typed tag",
52 (stack) =>
53 Effect.gen(function* () {
54 const { project } = yield* GcpEnvironment.current;
55 yield* stack.destroy();
56
57 const error = yield* Effect.flip(
58 we.createSubscriptions({
59 body: {
60 targetResource: "//chat.googleapis.com/spaces/alchemy-missing",
61 eventTypes: ["google.workspace.chat.message.v1.created"],
62 notificationEndpoint: {
63 pubsubTopic: `projects/${project}/topics/alchemy-missing-topic`,
64 },
65 },
66 }),
67 );
68 expect(error._tag).toEqual("WorkspaceEventsScopeInsufficient");
69
70 yield* stack.destroy();
71 }).pipe(logLevel),
72 {
73 tags: ["provider:gcp", "provider:gcp:workspaceevents", "live"],
74 timeout: 90_000,
75 },
76 );
77
78 test.provider.skipIf(!runLifecycle)(
79 "create, update, and delete a Workspace Events subscription",
80 (stack) =>
81 Effect.gen(function* () {
82 yield* stack.destroy();
83
84 const targetResource =
85 process.env.GCP_TEST_WORKSPACE_EVENTS_TARGET ?? "//chat.googleapis.com/spaces/-";
86
87 const created = yield* stack.deploy(
88 Effect.gen(function* () {
89 const topic = yield* GCP.PubSub.Topic("WorkspaceEvents", {});
90 const subscription = yield* GCP.WorkspaceEvents.Subscription("SpaceEvents", {
91 targetResource,
92 eventTypes: ["google.workspace.chat.message.v1.created"],
93 pubsubTopic: topic.name,
94 ttl: "3600s",
95 });
96 return { topic, subscription };
97 }),
98 );
99
100 expect(created.subscription.name.startsWith("subscriptions/")).toEqual(true);
101 expect(created.subscription.subscriptionId.length).toBeGreaterThan(0);
102 expect(created.subscription.targetResource).toEqual(targetResource);
103 expect(created.subscription.eventTypes).toContain("google.workspace.chat.message.v1.created");
104
105 const fetched = yield* we.getSubscriptions({
106 name: created.subscription.name,
107 });
108 expect(fetched.name).toEqual(created.subscription.name);
109 expect(fetched.notificationEndpoint?.pubsubTopic).toEqual(created.topic.name);
110
111 const updated = yield* stack.deploy(
112 Effect.gen(function* () {
113 const topic = yield* GCP.PubSub.Topic("WorkspaceEvents", {});
114 const subscription = yield* GCP.WorkspaceEvents.Subscription("SpaceEvents", {
115 subscriptionId: created.subscription.subscriptionId,
116 targetResource,
117 eventTypes: [
118 "google.workspace.chat.message.v1.created",
119 "google.workspace.chat.message.v1.updated",
120 ],
121 pubsubTopic: topic.name,
122 ttl: "7200s",
123 });
124 return { topic, subscription };
125 }),
126 );
127
128 expect(updated.subscription.name).toEqual(created.subscription.name);
129 expect(updated.subscription.eventTypes).toEqual(
130 expect.arrayContaining([
131 "google.workspace.chat.message.v1.created",
132 "google.workspace.chat.message.v1.updated",
133 ]),
134 );
135
136 yield* stack.destroy();
137
138 const gone = yield* waitUntilGone(created.subscription.name);
139 expect(gone).toEqual("gone");
140 }).pipe(logLevel),
141 {
142 tags: ["provider:gcp", "provider:gcp:workspaceevents", "live"],
143 timeout: 90_000,
144 },
145 );