| 1 | import * as GCP from "@/GCP"; |
| 2 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as eventarc from "@distilled.cloud/gcp/eventarc_v1"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService( |
| 13 | MinimumLogLevel, |
| 14 | process.env.DEBUG ? "Debug" : "Info", |
| 15 | ); |
| 16 | |
| 17 | const runLifecycle = !process.env.FAST; |
| 18 | const LOCATION = "us-central1"; |
| 19 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | eventarc.getProjectsLocationsMessageBuses({ name }).pipe( |
| 22 | Effect.as("found" as const), |
| 23 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 24 | Effect.repeat({ |
| 25 | schedule: Schedule.spaced("2 seconds"), |
| 26 | until: (status) => status === "gone", |
| 27 | times: 10, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | test.provider( |
| 32 | "getProjectsLocationsMessageBuses on a missing bus fails with a typed tag", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | const { project } = yield* GcpEnvironment.current; |
| 36 | yield* stack.destroy(); |
| 37 | |
| 38 | const error = yield* Effect.flip( |
| 39 | eventarc.getProjectsLocationsMessageBuses({ |
| 40 | name: `projects/${project}/locations/${LOCATION}/messageBuses/alchemy-missing-bus`, |
| 41 | }), |
| 42 | ); |
| 43 | expect(error._tag).toEqual("NotFound"); |
| 44 | |
| 45 | yield* stack.destroy(); |
| 46 | }).pipe(logLevel), |
| 47 | { tags: ["provider:gcp", "provider:gcp:eventarc", "live"], timeout: 90_000 }, |
| 48 | ); |
| 49 | |
| 50 | test.provider.skipIf(!runLifecycle)( |
| 51 | "create, update, and delete an Eventarc message bus", |
| 52 | (stack) => |
| 53 | Effect.gen(function* () { |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const created = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | return yield* GCP.Eventarc.MessageBus("Events", { |
| 59 | location: LOCATION, |
| 60 | displayName: "events", |
| 61 | loggingConfig: { logSeverity: "INFO" }, |
| 62 | labels: { env: "test" }, |
| 63 | }); |
| 64 | }), |
| 65 | ); |
| 66 | |
| 67 | expect(created.name).toContain("/messageBuses/"); |
| 68 | expect(created.messageBusId).toEqual(expect.any(String)); |
| 69 | expect(created.location).toEqual(LOCATION); |
| 70 | expect(created.displayName).toEqual("events"); |
| 71 | expect(created.labels).toMatchObject({ env: "test" }); |
| 72 | |
| 73 | const fetched = yield* eventarc.getProjectsLocationsMessageBuses({ |
| 74 | name: created.name, |
| 75 | }); |
| 76 | expect(fetched.name).toEqual(created.name); |
| 77 | expect(fetched.labels?.env).toEqual("test"); |
| 78 | expect(fetched.displayName).toEqual("events"); |
| 79 | |
| 80 | const updated = yield* stack.deploy( |
| 81 | Effect.gen(function* () { |
| 82 | return yield* GCP.Eventarc.MessageBus("Events", { |
| 83 | messageBusId: created.messageBusId, |
| 84 | location: LOCATION, |
| 85 | displayName: "events-v2", |
| 86 | loggingConfig: { logSeverity: "WARNING" }, |
| 87 | labels: { env: "prod", role: "bus" }, |
| 88 | }); |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | expect(updated.name).toEqual(created.name); |
| 93 | expect(updated.messageBusId).toEqual(created.messageBusId); |
| 94 | expect(updated.displayName).toEqual("events-v2"); |
| 95 | expect(updated.labels).toMatchObject({ env: "prod", role: "bus" }); |
| 96 | |
| 97 | const refetched = yield* eventarc.getProjectsLocationsMessageBuses({ |
| 98 | name: created.name, |
| 99 | }); |
| 100 | expect(refetched.labels?.env).toEqual("prod"); |
| 101 | expect(refetched.labels?.role).toEqual("bus"); |
| 102 | expect(refetched.displayName).toEqual("events-v2"); |
| 103 | |
| 104 | yield* stack.destroy(); |
| 105 | |
| 106 | const gone = yield* waitUntilGone(created.name); |
| 107 | expect(gone).toEqual("gone"); |
| 108 | }).pipe(logLevel), |
| 109 | { tags: ["provider:gcp", "provider:gcp:eventarc", "live"], timeout: 120_000 }, |
| 110 | ); |