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