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 { makeObjectMedia } from "@alchemy.run/gcp/Storage/ObjectMedia"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as pubsub from "@distilled.cloud/gcp/pubsub_v1"; |
| 6 | import * as storage from "@distilled.cloud/gcp/storage_v1"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import BucketEventsService, { |
| 11 | INCOMING_PREFIX, |
| 12 | markerFor, |
| 13 | Uploads, |
| 14 | } from "./fixtures/bucket-events.ts"; |
| 15 | |
| 16 | const { test } = Test.make({ providers: GCP.providers() }); |
| 17 | |
| 18 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 19 | |
| 20 | test.provider( |
| 21 | "bucket finalize events are pushed to an effect-native Function", |
| 22 | (stack) => |
| 23 | Effect.gen(function* () { |
| 24 | yield* stack.destroy(); |
| 25 | |
| 26 | const out = yield* stack.deploy( |
| 27 | Effect.gen(function* () { |
| 28 | const service = yield* BucketEventsService; |
| 29 | const bucket = yield* Uploads; |
| 30 | return { uri: service.uri, bucket: bucket.bucketName }; |
| 31 | }), |
| 32 | ); |
| 33 | const bucket = out.bucket; |
| 34 | |
| 35 | // The notification publishes only `incoming/` finalizes to a topic |
| 36 | // with one push subscription to the service. |
| 37 | const notifications = (yield* storage.listNotifications({ bucket })).items; |
| 38 | expect(notifications?.length).toEqual(1); |
| 39 | const notification = notifications![0]!; |
| 40 | expect(notification.event_types).toEqual(["OBJECT_FINALIZE"]); |
| 41 | expect(notification.object_name_prefix).toEqual(INCOMING_PREFIX); |
| 42 | expect(notification.payload_format).toEqual("JSON_API_V1"); |
| 43 | const topic = notification.topic!.replace("//pubsub.googleapis.com/", ""); |
| 44 | const { subscriptions = [] } = yield* pubsub.listProjectsTopicsSubscriptions({ topic }); |
| 45 | expect(subscriptions.length).toEqual(1); |
| 46 | const subscription = yield* pubsub.getProjectsSubscriptions({ |
| 47 | subscription: subscriptions[0]!, |
| 48 | }); |
| 49 | const pushEndpoint = `${out.uri}/__alchemy/pubsub/uploads-bucketevents`; |
| 50 | expect(subscription.pushConfig?.pushEndpoint).toEqual(pushEndpoint); |
| 51 | expect(subscription.pushConfig?.oidcToken?.audience).toEqual(pushEndpoint); |
| 52 | |
| 53 | const media = yield* makeObjectMedia; |
| 54 | // Outside the prefix: must not be delivered. |
| 55 | yield* media.upload(bucket, { name: "other/skip.txt", body: "skip" }); |
| 56 | const uploaded = yield* media.upload(bucket, { |
| 57 | name: `${INCOMING_PREFIX}hello.txt`, |
| 58 | body: "Hello, events!", |
| 59 | }); |
| 60 | |
| 61 | const marker = yield* media |
| 62 | .download({ bucket, object: markerFor(`${INCOMING_PREFIX}hello.txt`) }) |
| 63 | .pipe( |
| 64 | Effect.retry({ |
| 65 | while: (error) => error._tag === "GCP.Storage.ObjectNotFound", |
| 66 | schedule: Schedule.spaced("5 seconds"), |
| 67 | times: 36, |
| 68 | }), |
| 69 | ); |
| 70 | const event = JSON.parse(new TextDecoder().decode(marker.body)); |
| 71 | expect(event).toMatchObject({ |
| 72 | eventType: "OBJECT_FINALIZE", |
| 73 | bucket, |
| 74 | object: `${INCOMING_PREFIX}hello.txt`, |
| 75 | generation: uploaded.generation, |
| 76 | size: "14", |
| 77 | }); |
| 78 | expect(event.contentType).toContain("text/plain"); |
| 79 | expect(event.eventTime).toEqual(expect.any(String)); |
| 80 | |
| 81 | const skipped = yield* media.download({ bucket, object: markerFor("other/skip.txt") }).pipe( |
| 82 | Effect.as("delivered" as const), |
| 83 | Effect.catchTag("GCP.Storage.ObjectNotFound", () => Effect.succeed("filtered" as const)), |
| 84 | ); |
| 85 | expect(skipped).toEqual("filtered"); |
| 86 | |
| 87 | yield* stack.destroy(); |
| 88 | |
| 89 | const topicGone = yield* pubsub.getProjectsTopics({ topic }).pipe( |
| 90 | Effect.as("found" as const), |
| 91 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 92 | ); |
| 93 | expect(topicGone).toEqual("gone"); |
| 94 | const subscriptionGone = yield* pubsub |
| 95 | .getProjectsSubscriptions({ subscription: subscriptions[0]! }) |
| 96 | .pipe( |
| 97 | Effect.as("found" as const), |
| 98 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 99 | ); |
| 100 | expect(subscriptionGone).toEqual("gone"); |
| 101 | // The bucket (and with it the notification) is gone. |
| 102 | const notificationsGone = yield* storage |
| 103 | .getNotifications({ bucket, notification: notification.id! }) |
| 104 | .pipe( |
| 105 | Effect.as("found" as const), |
| 106 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 107 | ); |
| 108 | expect(notificationsGone).toEqual("gone"); |
| 109 | }).pipe(logLevel), |
| 110 | { tags: ["provider:gcp", "provider:gcp:storage", "live"], timeout: 600_000 }, |
| 111 | ); |