| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as developerconnect from "@distilled.cloud/gcp/developerconnect_v1"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 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 location = "us-central1"; |
| 18 | |
| 19 | const waitUntilGone = (name: string) => |
| 20 | developerconnect.getProjectsLocationsConnections({ name }).pipe( |
| 21 | Effect.as("found" as const), |
| 22 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 23 | Effect.repeat({ |
| 24 | schedule: Schedule.spaced("1 second"), |
| 25 | until: (status) => status === "gone", |
| 26 | times: 10, |
| 27 | }), |
| 28 | ); |
| 29 | |
| 30 | test.provider( |
| 31 | "getProjectsLocationsConnections on a missing connection fails with a typed tag", |
| 32 | (stack) => |
| 33 | Effect.gen(function* () { |
| 34 | const { project } = yield* GcpEnvironment.current; |
| 35 | yield* stack.destroy(); |
| 36 | |
| 37 | const error = yield* Effect.flip( |
| 38 | developerconnect.getProjectsLocationsConnections({ |
| 39 | name: `projects/${project}/locations/${location}/connections/alchemy-missing-connection`, |
| 40 | }), |
| 41 | ); |
| 42 | expect(error._tag).toEqual("NotFound"); |
| 43 | |
| 44 | yield* stack.destroy(); |
| 45 | }).pipe(logLevel), |
| 46 | { |
| 47 | tags: ["provider:gcp", "provider:gcp:developerconnect", "live"], |
| 48 | timeout: 90_000, |
| 49 | }, |
| 50 | ); |
| 51 | |
| 52 | test.provider( |
| 53 | "create, update, and delete a developer connect connection", |
| 54 | (stack) => |
| 55 | Effect.gen(function* () { |
| 56 | yield* stack.destroy(); |
| 57 | |
| 58 | const created = yield* stack.deploy( |
| 59 | Effect.gen(function* () { |
| 60 | return yield* GCP.DeveloperConnect.Connection("Github", { |
| 61 | location, |
| 62 | githubConfig: { githubApp: "DEVELOPER_CONNECT" }, |
| 63 | labels: { env: "test" }, |
| 64 | }); |
| 65 | }), |
| 66 | ); |
| 67 | |
| 68 | expect(created.name).toContain("/connections/"); |
| 69 | expect(created.labels).toMatchObject({ env: "test" }); |
| 70 | |
| 71 | const fetched = yield* developerconnect.getProjectsLocationsConnections({ |
| 72 | name: created.name, |
| 73 | }); |
| 74 | expect(fetched.name).toEqual(created.name); |
| 75 | |
| 76 | const updated = yield* stack.deploy( |
| 77 | Effect.gen(function* () { |
| 78 | return yield* GCP.DeveloperConnect.Connection("Github", { |
| 79 | connectionId: created.connectionId, |
| 80 | location, |
| 81 | githubConfig: { githubApp: "DEVELOPER_CONNECT" }, |
| 82 | labels: { env: "prod", role: "git" }, |
| 83 | }); |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | expect(updated.name).toEqual(created.name); |
| 88 | expect(updated.labels).toMatchObject({ env: "prod", role: "git" }); |
| 89 | |
| 90 | yield* stack.destroy(); |
| 91 | const gone = yield* waitUntilGone(created.name); |
| 92 | expect(gone).toEqual("gone"); |
| 93 | }).pipe(logLevel), |
| 94 | { |
| 95 | tags: ["provider:gcp", "provider:gcp:developerconnect", "live"], |
| 96 | timeout: 180_000, |
| 97 | }, |
| 98 | ); |