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 { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as cloudfunctions from "@distilled.cloud/gcp/cloudfunctions_v2"; |
| 5 | import * as pubsub from "@distilled.cloud/gcp/pubsub_v1"; |
| 6 | import * as storage from "@distilled.cloud/gcp/storage_v1"; |
| 7 | import * as iam from "@distilled.cloud/gcp/unstable/iam_v1"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as HttpClient from "effect/http/HttpClient"; |
| 11 | import { MinimumLogLevel } from "effect/References"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import EffectFunction, { Marker, Pings } from "./fixtures/effect-function.ts"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: GCP.providers() }); |
| 16 | |
| 17 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 18 | |
| 19 | class NotReady extends Data.TaggedError("NotReady")<{ status: number }> {} |
| 20 | |
| 21 | test.provider.skipIf(!!process.env.FAST)( |
| 22 | "effect-native function serves fetch with bindings and consumes a topic", |
| 23 | (stack) => |
| 24 | Effect.gen(function* () { |
| 25 | yield* stack.destroy(); |
| 26 | |
| 27 | const out = yield* stack.deploy( |
| 28 | Effect.gen(function* () { |
| 29 | const fn = yield* EffectFunction; |
| 30 | const bucket = yield* Marker; |
| 31 | const topic = yield* Pings; |
| 32 | // Let the test call the function without an identity token. |
| 33 | yield* GCP.IAM.Member("PublicInvoker", { |
| 34 | kind: "run.service", |
| 35 | name: fn.service.as<string>(), |
| 36 | role: "roles/run.invoker", |
| 37 | member: "allUsers", |
| 38 | }); |
| 39 | return { |
| 40 | name: fn.name, |
| 41 | url: fn.url, |
| 42 | project: fn.project, |
| 43 | runtime: fn.runtime, |
| 44 | entryPoint: fn.entryPoint, |
| 45 | serviceAccount: fn.serviceAccountEmail, |
| 46 | managed: fn.managedServiceAccount, |
| 47 | bucket: bucket.bucketName, |
| 48 | topic: topic.name, |
| 49 | }; |
| 50 | }), |
| 51 | ); |
| 52 | |
| 53 | expect(out.runtime).toEqual("nodejs22"); |
| 54 | expect(out.entryPoint).toEqual("handler"); |
| 55 | expect(out.managed).toEqual(true); |
| 56 | |
| 57 | // fetch runs the Effect program with its Storage bindings. |
| 58 | const client = yield* HttpClient.HttpClient; |
| 59 | const response = yield* client.get(out.url!).pipe( |
| 60 | Effect.flatMap((response) => |
| 61 | response.status === 200 |
| 62 | ? Effect.succeed(response) |
| 63 | : Effect.fail(new NotReady({ status: response.status })), |
| 64 | ), |
| 65 | Effect.retry({ |
| 66 | while: (error) => error._tag === "NotReady", |
| 67 | schedule: Schedule.spaced("5 seconds"), |
| 68 | times: 18, |
| 69 | }), |
| 70 | ); |
| 71 | const body = (yield* response.json) as { read: string; node: string }; |
| 72 | expect(body.read).toEqual("from-function"); |
| 73 | expect(body.node).toMatch(/^22\./); |
| 74 | |
| 75 | // A published message is pushed to the function and recorded. |
| 76 | const published = yield* pubsub.publishProjectsTopics({ |
| 77 | topic: out.topic, |
| 78 | body: { messages: [{ data: btoa("ping!") }] }, |
| 79 | }); |
| 80 | const messageId = published.messageIds?.[0]; |
| 81 | expect(messageId).toEqual(expect.any(String)); |
| 82 | const marker = yield* storage |
| 83 | .getObjects({ bucket: out.bucket, object: `ping-${messageId}.txt` }) |
| 84 | .pipe( |
| 85 | Effect.map((object) => object.size), |
| 86 | Effect.catchTag("NotFound", () => Effect.succeed(undefined)), |
| 87 | Effect.repeat({ |
| 88 | schedule: Schedule.spaced("5 seconds"), |
| 89 | until: (size) => size !== undefined, |
| 90 | times: 24, |
| 91 | }), |
| 92 | ); |
| 93 | expect(marker).toEqual("5"); |
| 94 | |
| 95 | yield* stack.destroy(); |
| 96 | |
| 97 | const gone = yield* cloudfunctions.getProjectsLocationsFunctions({ name: out.name }).pipe( |
| 98 | Effect.as("found" as const), |
| 99 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 100 | ); |
| 101 | expect(gone).toEqual("gone"); |
| 102 | const saGone = yield* iam |
| 103 | .getProjectsServiceAccounts({ |
| 104 | name: `projects/${out.project}/serviceAccounts/${out.serviceAccount}`, |
| 105 | }) |
| 106 | .pipe( |
| 107 | Effect.as("found" as const), |
| 108 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 109 | ); |
| 110 | expect(saGone).toEqual("gone"); |
| 111 | }).pipe(logLevel), |
| 112 | { |
| 113 | tags: ["provider:gcp", "provider:gcp:cloudfunctions", "live"], |
| 114 | timeout: 900_000, |
| 115 | }, |
| 116 | ); |