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 eventarc from "@distilled.cloud/gcp/eventarc_v1"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: GCP.providers() }); |
| 10 | |
| 11 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 12 | |
| 13 | const LOCATION = "us-central1"; |
| 14 | const PUBSUB_EVENT_TYPE = "google.cloud.pubsub.topic.v1.messagePublished"; |
| 15 | const WORKFLOW_SOURCE = `main: |
| 16 | params: [event] |
| 17 | steps: |
| 18 | - return_event: |
| 19 | return: \${event} |
| 20 | `; |
| 21 | |
| 22 | const waitUntilGone = (name: string) => |
| 23 | eventarc.getProjectsLocationsTriggers({ name }).pipe( |
| 24 | Effect.as("found" as const), |
| 25 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 26 | Effect.repeat({ |
| 27 | schedule: Schedule.spaced("2 seconds"), |
| 28 | until: (status) => status === "gone", |
| 29 | times: 10, |
| 30 | }), |
| 31 | ); |
| 32 | |
| 33 | const eventFilters: GCP.Eventarc.EventFilter[] = [{ attribute: "type", value: PUBSUB_EVENT_TYPE }]; |
| 34 | |
| 35 | test.provider( |
| 36 | "create, update, and delete an Eventarc trigger", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | yield* stack.destroy(); |
| 40 | |
| 41 | const created = yield* stack.deploy( |
| 42 | Effect.gen(function* () { |
| 43 | const topic = yield* GCP.PubSub.Topic("Events", { |
| 44 | labels: { env: "test" }, |
| 45 | }); |
| 46 | const workflow = yield* GCP.Workflows.Workflow("Sink", { |
| 47 | location: LOCATION, |
| 48 | sourceContents: WORKFLOW_SOURCE, |
| 49 | labels: { env: "test" }, |
| 50 | }); |
| 51 | const trigger = yield* GCP.Eventarc.Trigger("PubSubEvents", { |
| 52 | location: LOCATION, |
| 53 | eventFilters, |
| 54 | destination: { workflow: workflow.name }, |
| 55 | transport: { pubsub: { topic: topic.name } }, |
| 56 | labels: { env: "test" }, |
| 57 | }); |
| 58 | return { topic, workflow, trigger }; |
| 59 | }), |
| 60 | ); |
| 61 | |
| 62 | expect(created.trigger.name).toContain("/triggers/"); |
| 63 | expect(created.trigger.triggerId).toEqual(expect.any(String)); |
| 64 | expect(created.trigger.location).toEqual(LOCATION); |
| 65 | expect(created.trigger.labels).toMatchObject({ env: "test" }); |
| 66 | expect(created.trigger.eventFilters).toEqual( |
| 67 | expect.arrayContaining([ |
| 68 | expect.objectContaining({ |
| 69 | attribute: "type", |
| 70 | value: PUBSUB_EVENT_TYPE, |
| 71 | }), |
| 72 | ]), |
| 73 | ); |
| 74 | expect(created.trigger.destination?.workflow).toEqual(created.workflow.name); |
| 75 | expect(created.trigger.transport?.pubsub?.topic).toEqual(created.topic.name); |
| 76 | |
| 77 | const fetched = yield* eventarc.getProjectsLocationsTriggers({ |
| 78 | name: created.trigger.name, |
| 79 | }); |
| 80 | expect(fetched.name).toEqual(created.trigger.name); |
| 81 | expect(fetched.labels?.env).toEqual("test"); |
| 82 | expect(fetched.destination?.workflow).toEqual(created.workflow.name); |
| 83 | expect(fetched.transport?.pubsub?.topic).toEqual(created.topic.name); |
| 84 | |
| 85 | const updated = yield* stack.deploy( |
| 86 | Effect.gen(function* () { |
| 87 | const topic = yield* GCP.PubSub.Topic("Events", { |
| 88 | topicId: created.topic.topicId, |
| 89 | labels: { env: "test" }, |
| 90 | }); |
| 91 | const workflow = yield* GCP.Workflows.Workflow("Sink", { |
| 92 | workflowId: created.workflow.workflowId, |
| 93 | location: LOCATION, |
| 94 | sourceContents: WORKFLOW_SOURCE, |
| 95 | labels: { env: "test" }, |
| 96 | }); |
| 97 | return yield* GCP.Eventarc.Trigger("PubSubEvents", { |
| 98 | triggerId: created.trigger.triggerId, |
| 99 | location: LOCATION, |
| 100 | eventFilters, |
| 101 | destination: { workflow: workflow.name }, |
| 102 | transport: { pubsub: { topic: topic.name } }, |
| 103 | eventDataContentType: "application/json", |
| 104 | labels: { env: "prod", role: "events" }, |
| 105 | }); |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | expect(updated.name).toEqual(created.trigger.name); |
| 110 | expect(updated.triggerId).toEqual(created.trigger.triggerId); |
| 111 | expect(updated.labels).toMatchObject({ env: "prod", role: "events" }); |
| 112 | expect(updated.eventDataContentType).toEqual("application/json"); |
| 113 | |
| 114 | const refetched = yield* eventarc.getProjectsLocationsTriggers({ |
| 115 | name: created.trigger.name, |
| 116 | }); |
| 117 | expect(refetched.labels?.env).toEqual("prod"); |
| 118 | expect(refetched.labels?.role).toEqual("events"); |
| 119 | expect( |
| 120 | refetched.eventDataContentType === undefined || |
| 121 | refetched.eventDataContentType === "application/json", |
| 122 | ).toEqual(true); |
| 123 | |
| 124 | yield* stack.destroy(); |
| 125 | |
| 126 | const gone = yield* waitUntilGone(created.trigger.name); |
| 127 | expect(gone).toEqual("gone"); |
| 128 | }).pipe(logLevel), |
| 129 | { tags: ["provider:gcp", "provider:gcp:eventarc", "live"], timeout: 120_000 }, |
| 130 | ); |