The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as Core from "@alchemy.run/test/Core"; |
| 6 | import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 7 | import * as pubsub from "@distilled.cloud/gcp/pubsub_v1"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { callProbe, expectProbe } from "../bindingHost.ts"; |
| 11 | import PubSubBindingsHost, { |
| 12 | EventSchema, |
| 13 | PUBLISH_PAYLOAD, |
| 14 | PublishTap, |
| 15 | PublishTopic, |
| 16 | PullInbox, |
| 17 | PullTopic, |
| 18 | ReadInbox, |
| 19 | ReadTopic, |
| 20 | WRITE_BATCH_BYTES, |
| 21 | WRITE_BATCH_TEXT, |
| 22 | WRITE_PAYLOAD, |
| 23 | WriteTap, |
| 24 | WriteTopicTarget, |
| 25 | } from "./fixtures/bindings-host.ts"; |
| 26 | |
| 27 | const testOptions = { providers: GCP.providers() }; |
| 28 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 29 | const sharedStack = Core.scratchStack(testOptions, "PubSubBindings"); |
| 30 | |
| 31 | let baseUrl: string; |
| 32 | let member: string; |
| 33 | let names: { |
| 34 | publishTopic: string; |
| 35 | publishTap: string; |
| 36 | writeTopic: string; |
| 37 | writeTap: string; |
| 38 | pullTopic: string; |
| 39 | pullInbox: string; |
| 40 | readTopic: string; |
| 41 | readInbox: string; |
| 42 | schema: string; |
| 43 | }; |
| 44 | |
| 45 | const decode = (data: string | undefined) => Buffer.from(data ?? "", "base64").toString("utf8"); |
| 46 | |
| 47 | /** Roles `member` holds on a resource's IAM policy. */ |
| 48 | const rolesOf = (policy: { bindings?: { role?: string; members?: string[] }[] }, member: string) => |
| 49 | (policy.bindings ?? []) |
| 50 | .filter((binding) => (binding.members ?? []).includes(member)) |
| 51 | .map((binding) => binding.role) |
| 52 | .sort(); |
| 53 | |
| 54 | /** Pull (out of band) until `count` messages arrived, acknowledging them. */ |
| 55 | const drain = (subscription: string, count: number) => |
| 56 | Effect.gen(function* () { |
| 57 | const seen: pubsub.ReceivedMessage[] = []; |
| 58 | yield* pubsub |
| 59 | .pullProjectsSubscriptions({ |
| 60 | subscription, |
| 61 | body: { maxMessages: 10, returnImmediately: false }, |
| 62 | }) |
| 63 | .pipe( |
| 64 | Effect.tap((page) => |
| 65 | Effect.gen(function* () { |
| 66 | const received = page.receivedMessages ?? []; |
| 67 | seen.push(...received); |
| 68 | if (received.length > 0) { |
| 69 | yield* pubsub.acknowledgeProjectsSubscriptions({ |
| 70 | subscription, |
| 71 | body: { ackIds: received.map((m) => m.ackId ?? "") }, |
| 72 | }); |
| 73 | } |
| 74 | }), |
| 75 | ), |
| 76 | Effect.repeat({ |
| 77 | schedule: Schedule.spaced("1 second"), |
| 78 | until: () => seen.length >= count, |
| 79 | times: 30, |
| 80 | }), |
| 81 | ); |
| 82 | return seen; |
| 83 | }); |
| 84 | |
| 85 | /** Publish (out of band, as the deployer) one message. */ |
| 86 | const publishOutOfBand = (topic: string, text: string) => |
| 87 | pubsub.publishProjectsTopics({ |
| 88 | topic, |
| 89 | body: { messages: [{ data: Buffer.from(text).toString("base64") }] }, |
| 90 | }); |
| 91 | |
| 92 | /** Nothing is redelivered once the ack deadline (10s) has passed. */ |
| 93 | const expectNoRedelivery = (subscription: string) => |
| 94 | Effect.gen(function* () { |
| 95 | yield* Effect.sleep("15 seconds"); |
| 96 | const page = yield* pubsub.pullProjectsSubscriptions({ |
| 97 | subscription, |
| 98 | body: { maxMessages: 10, returnImmediately: true }, |
| 99 | }); |
| 100 | expect(page.receivedMessages ?? []).toEqual([]); |
| 101 | }); |
| 102 | |
| 103 | describe( |
| 104 | "PubSub Bindings", |
| 105 | { tags: ["provider:gcp", "provider:gcp:pubsub", "provider:gcp:run", "live"] }, |
| 106 | () => { |
| 107 | beforeAll( |
| 108 | Effect.gen(function* () { |
| 109 | yield* sharedStack.destroy(); |
| 110 | const out = yield* sharedStack.deploy( |
| 111 | Effect.gen(function* () { |
| 112 | const host = yield* PubSubBindingsHost; |
| 113 | return { |
| 114 | uri: host.uri, |
| 115 | serviceAccount: host.serviceAccount, |
| 116 | publishTopic: (yield* PublishTopic).name, |
| 117 | publishTap: (yield* PublishTap).name, |
| 118 | writeTopic: (yield* WriteTopicTarget).name, |
| 119 | writeTap: (yield* WriteTap).name, |
| 120 | pullTopic: (yield* PullTopic).name, |
| 121 | pullInbox: (yield* PullInbox).name, |
| 122 | readTopic: (yield* ReadTopic).name, |
| 123 | readInbox: (yield* ReadInbox).name, |
| 124 | schema: (yield* EventSchema).name, |
| 125 | }; |
| 126 | }), |
| 127 | ); |
| 128 | baseUrl = out.uri!; |
| 129 | member = `serviceAccount:${out.serviceAccount!}`; |
| 130 | names = out; |
| 131 | }), |
| 132 | { timeout: 900_000 }, |
| 133 | ); |
| 134 | |
| 135 | afterAll(sharedStack.destroy(), { timeout: 600_000 }); |
| 136 | |
| 137 | describe("Publish", () => { |
| 138 | test.provider( |
| 139 | "publishes to the topic, granted publisher on the topic only", |
| 140 | (_stack) => |
| 141 | Effect.gen(function* () { |
| 142 | const out = yield* expectProbe<{ messageIds?: string[] }>(baseUrl, "publish"); |
| 143 | expect(out.messageIds).toHaveLength(1); |
| 144 | |
| 145 | const [message] = yield* drain(names.publishTap, 1); |
| 146 | expect(message?.message?.messageId).toEqual(out.messageIds?.[0]); |
| 147 | expect(decode(message?.message?.data)).toEqual(PUBLISH_PAYLOAD); |
| 148 | expect(message?.message?.attributes).toEqual({ via: "Publish" }); |
| 149 | |
| 150 | const policy = yield* pubsub.getIamPolicyProjectsTopics({ |
| 151 | resource: names.publishTopic, |
| 152 | }); |
| 153 | expect(rolesOf(policy, member)).toEqual(["roles/pubsub.publisher"]); |
| 154 | }), |
| 155 | { |
| 156 | tags: ["provider:gcp", "provider:gcp:pubsub", "live"], |
| 157 | timeout: 600_000, |
| 158 | }, |
| 159 | ); |
| 160 | }); |
| 161 | |
| 162 | describe("Pull / Acknowledge", () => { |
| 163 | test.provider( |
| 164 | "pulls and acknowledges, granted subscriber on the subscription only", |
| 165 | (_stack) => |
| 166 | Effect.gen(function* () { |
| 167 | yield* publishOutOfBand(names.pullTopic, "pull-me"); |
| 168 | const texts = yield* expectProbe<string[]>(baseUrl, "pullAndAcknowledge"); |
| 169 | expect(texts).toEqual(["pull-me"]); |
| 170 | yield* expectNoRedelivery(names.pullInbox); |
| 171 | |
| 172 | const policy = yield* pubsub.getIamPolicyProjectsSubscriptions({ |
| 173 | resource: names.pullInbox, |
| 174 | }); |
| 175 | expect(rolesOf(policy, member)).toEqual(["roles/pubsub.subscriber"]); |
| 176 | }), |
| 177 | { |
| 178 | tags: ["provider:gcp", "provider:gcp:pubsub", "live"], |
| 179 | timeout: 600_000, |
| 180 | }, |
| 181 | ); |
| 182 | }); |
| 183 | |
| 184 | describe("WriteTopic", () => { |
| 185 | test.provider( |
| 186 | "publish and publishBatch, granted publisher on the topic only", |
| 187 | (_stack) => |
| 188 | Effect.gen(function* () { |
| 189 | const out = yield* expectProbe<{ |
| 190 | id: string; |
| 191 | batch: string[]; |
| 192 | empty: string[]; |
| 193 | }>(baseUrl, "writeTopic"); |
| 194 | expect(out.batch).toHaveLength(2); |
| 195 | // An empty batch is a no-op, not a request. |
| 196 | expect(out.empty).toEqual([]); |
| 197 | |
| 198 | // Out of band: every message landed with its payload intact. |
| 199 | const received = yield* drain(names.writeTap, 3); |
| 200 | const byId = new Map(received.map((m) => [m.message?.messageId, m.message])); |
| 201 | expect(decode(byId.get(out.id)?.data)).toEqual(WRITE_PAYLOAD); |
| 202 | expect(byId.get(out.id)?.attributes).toEqual({ via: "WriteTopic" }); |
| 203 | expect(decode(byId.get(out.batch[0])?.data)).toEqual(WRITE_BATCH_TEXT); |
| 204 | expect([...Buffer.from(byId.get(out.batch[1])?.data ?? "", "base64")]).toEqual( |
| 205 | WRITE_BATCH_BYTES, |
| 206 | ); |
| 207 | |
| 208 | const policy = yield* pubsub.getIamPolicyProjectsTopics({ |
| 209 | resource: names.writeTopic, |
| 210 | }); |
| 211 | expect(rolesOf(policy, member)).toEqual(["roles/pubsub.publisher"]); |
| 212 | }), |
| 213 | { |
| 214 | tags: ["provider:gcp", "provider:gcp:pubsub", "live"], |
| 215 | timeout: 600_000, |
| 216 | }, |
| 217 | ); |
| 218 | }); |
| 219 | |
| 220 | describe("ReadSubscription", () => { |
| 221 | test.provider( |
| 222 | "pull, modifyAckDeadline and acknowledge, granted subscriber on the subscription only", |
| 223 | (_stack) => |
| 224 | Effect.gen(function* () { |
| 225 | yield* publishOutOfBand(names.readTopic, "read-me"); |
| 226 | const messages = yield* expectProbe< |
| 227 | { text: string; attributes: Record<string, string> }[] |
| 228 | >(baseUrl, "readSubscription"); |
| 229 | expect(messages).toEqual([{ text: "read-me", attributes: {} }]); |
| 230 | yield* expectNoRedelivery(names.readInbox); |
| 231 | |
| 232 | const policy = yield* pubsub.getIamPolicyProjectsSubscriptions({ |
| 233 | resource: names.readInbox, |
| 234 | }); |
| 235 | expect(rolesOf(policy, member)).toEqual(["roles/pubsub.subscriber"]); |
| 236 | }), |
| 237 | { |
| 238 | tags: ["provider:gcp", "provider:gcp:pubsub", "live"], |
| 239 | timeout: 600_000, |
| 240 | }, |
| 241 | ); |
| 242 | }); |
| 243 | |
| 244 | describe("GetSchema", () => { |
| 245 | test.provider( |
| 246 | "reads the schema, granted viewer on the schema", |
| 247 | (_stack) => |
| 248 | Effect.gen(function* () { |
| 249 | const live = yield* expectProbe<pubsub.Pubsub_Schema>(baseUrl, "getSchema"); |
| 250 | expect(live.name).toEqual(names.schema); |
| 251 | expect(live.type).toEqual("AVRO"); |
| 252 | expect(JSON.parse(live.definition ?? "{}")).toMatchObject({ |
| 253 | name: "Event", |
| 254 | }); |
| 255 | |
| 256 | const policy = yield* pubsub.getIamPolicyProjectsSchemas({ |
| 257 | resource: names.schema, |
| 258 | }); |
| 259 | expect(rolesOf(policy, member)).toEqual(["roles/pubsub.viewer"]); |
| 260 | }), |
| 261 | { |
| 262 | tags: ["provider:gcp", "provider:gcp:pubsub", "live"], |
| 263 | timeout: 600_000, |
| 264 | }, |
| 265 | ); |
| 266 | }); |
| 267 | |
| 268 | describe("ValidateMessage", () => { |
| 269 | test.provider( |
| 270 | "accepts a conforming message and rejects another, granted viewer on the project", |
| 271 | (_stack) => |
| 272 | Effect.gen(function* () { |
| 273 | const valid = yield* expectProbe<object>(baseUrl, "validateValid"); |
| 274 | expect(valid).toEqual({}); |
| 275 | |
| 276 | const invalid = yield* callProbe(baseUrl, "validateInvalid"); |
| 277 | expect(invalid.ok ? "ok" : invalid.error._tag).toEqual("BadRequest"); |
| 278 | |
| 279 | const { project } = yield* GcpEnvironment.current; |
| 280 | const policy = yield* resourcemanager.getIamPolicyProjects({ |
| 281 | resource: `projects/${project}`, |
| 282 | body: { options: { requestedPolicyVersion: 3 } }, |
| 283 | }); |
| 284 | // schemas.validateMessage is authorized on the parent project |
| 285 | // (a viewer grant on the schema alone answers 403). |
| 286 | expect( |
| 287 | (policy.bindings ?? []) |
| 288 | .filter((binding) => (binding.members ?? []).includes(member)) |
| 289 | .map((binding) => [binding.role, binding.condition]), |
| 290 | ).toEqual([["roles/pubsub.viewer", undefined]]); |
| 291 | }), |
| 292 | { |
| 293 | tags: ["provider:gcp", "provider:gcp:pubsub", "live"], |
| 294 | timeout: 600_000, |
| 295 | }, |
| 296 | ); |
| 297 | }); |
| 298 | }, |
| 299 | ); |