The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { |
| 10 | Markers, |
| 11 | markerFor, |
| 12 | PullConsumer, |
| 13 | PullOrders, |
| 14 | PushConsumer, |
| 15 | PushOrders, |
| 16 | } from "./fixtures/consumers.ts"; |
| 17 | |
| 18 | const { test } = Test.make({ providers: GCP.providers() }); |
| 19 | |
| 20 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 21 | |
| 22 | // Pub/Sub reads are eventually consistent; ride out a lagging replica. |
| 23 | const readSubscription = (subscription: string) => |
| 24 | pubsub.getProjectsSubscriptions({ subscription }).pipe( |
| 25 | Effect.retry({ |
| 26 | while: (error) => error._tag === "NotFound", |
| 27 | schedule: Schedule.spaced("2 seconds"), |
| 28 | times: 15, |
| 29 | }), |
| 30 | ); |
| 31 | |
| 32 | const subscriptionStatus = (subscription: string) => |
| 33 | pubsub.getProjectsSubscriptions({ subscription }).pipe( |
| 34 | Effect.as("found" as const), |
| 35 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 36 | ); |
| 37 | |
| 38 | const publishAndAwaitMarker = (topic: string, bucket: string, data: string) => |
| 39 | Effect.gen(function* () { |
| 40 | const { messageIds = [] } = yield* pubsub.publishProjectsTopics({ |
| 41 | topic, |
| 42 | body: { messages: [{ data: btoa(data), attributes: { kind: "test" } }] }, |
| 43 | }); |
| 44 | expect(messageIds.length).toEqual(1); |
| 45 | const media = yield* makeObjectMedia; |
| 46 | const marker = yield* media.download({ bucket, object: markerFor(messageIds[0]!) }).pipe( |
| 47 | Effect.retry({ |
| 48 | while: (error) => error._tag === "GCP.Storage.ObjectNotFound", |
| 49 | schedule: Schedule.spaced("5 seconds"), |
| 50 | times: 36, |
| 51 | }), |
| 52 | ); |
| 53 | return JSON.parse(new TextDecoder().decode(marker.body)) as { |
| 54 | data: string; |
| 55 | attributes: Record<string, string>; |
| 56 | subscription: string; |
| 57 | }; |
| 58 | }); |
| 59 | |
| 60 | test.provider( |
| 61 | "topic messages are pushed to an effect-native Function", |
| 62 | (stack) => |
| 63 | Effect.gen(function* () { |
| 64 | yield* stack.destroy(); |
| 65 | |
| 66 | const out = yield* stack.deploy( |
| 67 | Effect.gen(function* () { |
| 68 | const service = yield* PushConsumer; |
| 69 | const topic = yield* PushOrders; |
| 70 | const bucket = yield* Markers; |
| 71 | return { |
| 72 | uri: service.uri, |
| 73 | topic: topic.name, |
| 74 | bucket: bucket.bucketName, |
| 75 | }; |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | const { subscriptions = [] } = yield* pubsub.listProjectsTopicsSubscriptions({ |
| 80 | topic: out.topic, |
| 81 | }); |
| 82 | expect(subscriptions.length).toEqual(1); |
| 83 | const subscription = yield* readSubscription(subscriptions[0]!); |
| 84 | const pushEndpoint = `${out.uri}/__alchemy/pubsub/pushorders`; |
| 85 | expect(subscription.pushConfig?.pushEndpoint).toEqual(pushEndpoint); |
| 86 | expect(subscription.pushConfig?.oidcToken?.audience).toEqual(pushEndpoint); |
| 87 | |
| 88 | const event = yield* publishAndAwaitMarker(out.topic, out.bucket, "push"); |
| 89 | expect(event).toEqual({ |
| 90 | data: "push", |
| 91 | attributes: { kind: "test" }, |
| 92 | subscription: subscriptions[0]!, |
| 93 | }); |
| 94 | |
| 95 | yield* stack.destroy(); |
| 96 | |
| 97 | expect(yield* subscriptionStatus(subscriptions[0]!)).toEqual("gone"); |
| 98 | }).pipe(logLevel), |
| 99 | { tags: ["provider:gcp", "provider:gcp:pubsub", "live"], timeout: 600_000 }, |
| 100 | ); |
| 101 | |
| 102 | test.provider( |
| 103 | "topic messages are pulled by an effect-native WorkerPool", |
| 104 | (stack) => |
| 105 | Effect.gen(function* () { |
| 106 | yield* stack.destroy(); |
| 107 | |
| 108 | const out = yield* stack.deploy( |
| 109 | Effect.gen(function* () { |
| 110 | yield* PullConsumer; |
| 111 | const topic = yield* PullOrders; |
| 112 | const bucket = yield* Markers; |
| 113 | return { topic: topic.name, bucket: bucket.bucketName }; |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | const { subscriptions = [] } = yield* pubsub.listProjectsTopicsSubscriptions({ |
| 118 | topic: out.topic, |
| 119 | }); |
| 120 | expect(subscriptions.length).toEqual(1); |
| 121 | const subscription = yield* readSubscription(subscriptions[0]!); |
| 122 | expect(subscription.pushConfig?.pushEndpoint).toBeUndefined(); |
| 123 | |
| 124 | const event = yield* publishAndAwaitMarker(out.topic, out.bucket, "pull"); |
| 125 | expect(event).toEqual({ |
| 126 | data: "pull", |
| 127 | attributes: { kind: "test" }, |
| 128 | subscription: subscriptions[0]!, |
| 129 | }); |
| 130 | |
| 131 | yield* stack.destroy(); |
| 132 | |
| 133 | expect(yield* subscriptionStatus(subscriptions[0]!)).toEqual("gone"); |
| 134 | }).pipe(logLevel), |
| 135 | { tags: ["provider:gcp", "provider:gcp:pubsub", "live"], timeout: 600_000 }, |
| 136 | ); |