| 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 waitUntilGone = (name: string) => |
| 18 | developerconnect.getProjectsLocationsAccountConnectors({ name }).pipe( |
| 19 | Effect.as("found" as const), |
| 20 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 21 | Effect.repeat({ |
| 22 | schedule: Schedule.spaced("1 second"), |
| 23 | until: (status) => status === "gone", |
| 24 | times: 10, |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | test.provider( |
| 29 | "getProjectsLocationsAccountConnectors on a missing connector fails with a typed tag", |
| 30 | (stack) => |
| 31 | Effect.gen(function* () { |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | yield* stack.destroy(); |
| 34 | |
| 35 | const error = yield* Effect.flip( |
| 36 | developerconnect.getProjectsLocationsAccountConnectors({ |
| 37 | name: `projects/${project}/locations/us-central1/accountConnectors/alchemy-missing-connector`, |
| 38 | }), |
| 39 | ); |
| 40 | expect(error._tag).toEqual("NotFound"); |
| 41 | |
| 42 | yield* stack.destroy(); |
| 43 | }).pipe(logLevel), |
| 44 | { |
| 45 | tags: ["provider:gcp", "provider:gcp:developerconnect", "live"], |
| 46 | timeout: 90_000, |
| 47 | }, |
| 48 | ); |
| 49 | |
| 50 | test.provider( |
| 51 | "create, update, replace, and delete an account connector", |
| 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.DeveloperConnect.AccountConnector("Github", { |
| 59 | location: "us-central1", |
| 60 | providerOauthConfig: { |
| 61 | systemProviderId: "GITHUB", |
| 62 | scopes: ["repo"], |
| 63 | }, |
| 64 | labels: { env: "test" }, |
| 65 | }); |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | expect(created.accountConnectorId).toEqual(expect.any(String)); |
| 70 | expect(created.name).toContain("/accountConnectors/"); |
| 71 | expect(created.location).toEqual("us-central1"); |
| 72 | expect(created.labels).toMatchObject({ env: "test" }); |
| 73 | expect(created.providerOauthConfig?.systemProviderId).toEqual("GITHUB"); |
| 74 | |
| 75 | const fetched = |
| 76 | yield* developerconnect.getProjectsLocationsAccountConnectors({ |
| 77 | name: created.name, |
| 78 | }); |
| 79 | expect(fetched.name).toEqual(created.name); |
| 80 | expect(fetched.labels?.env).toEqual("test"); |
| 81 | expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 82 | expect(fetched.providerOauthConfig?.systemProviderId).toEqual("GITHUB"); |
| 83 | |
| 84 | const updated = yield* stack.deploy( |
| 85 | Effect.gen(function* () { |
| 86 | return yield* GCP.DeveloperConnect.AccountConnector("Github", { |
| 87 | accountConnectorId: created.accountConnectorId, |
| 88 | location: "us-central1", |
| 89 | providerOauthConfig: { |
| 90 | systemProviderId: "GITHUB", |
| 91 | scopes: ["repo"], |
| 92 | }, |
| 93 | labels: { env: "prod", role: "scm" }, |
| 94 | proxyConfig: { enabled: true }, |
| 95 | }); |
| 96 | }), |
| 97 | ); |
| 98 | |
| 99 | expect(updated.name).toEqual(created.name); |
| 100 | expect(updated.labels).toMatchObject({ env: "prod", role: "scm" }); |
| 101 | expect(updated.proxyConfig?.enabled).toEqual(true); |
| 102 | |
| 103 | const fetchedUpdate = |
| 104 | yield* developerconnect.getProjectsLocationsAccountConnectors({ |
| 105 | name: updated.name, |
| 106 | }); |
| 107 | expect(fetchedUpdate.labels?.env).toEqual("prod"); |
| 108 | expect(fetchedUpdate.labels?.role).toEqual("scm"); |
| 109 | expect(fetchedUpdate.proxyConfig?.enabled).toEqual(true); |
| 110 | |
| 111 | const replaced = yield* stack.deploy( |
| 112 | Effect.gen(function* () { |
| 113 | return yield* GCP.DeveloperConnect.AccountConnector("Github", { |
| 114 | accountConnectorId: created.accountConnectorId, |
| 115 | location: "us-east1", |
| 116 | providerOauthConfig: { |
| 117 | systemProviderId: "GITHUB", |
| 118 | scopes: ["repo"], |
| 119 | }, |
| 120 | labels: { env: "test" }, |
| 121 | }); |
| 122 | }), |
| 123 | ); |
| 124 | |
| 125 | expect(replaced.accountConnectorId).toEqual(created.accountConnectorId); |
| 126 | expect(replaced.location).toEqual("us-east1"); |
| 127 | expect(replaced.name).toContain("/locations/us-east1/"); |
| 128 | expect(replaced.name).not.toEqual(created.name); |
| 129 | |
| 130 | const oldGone = yield* waitUntilGone(created.name); |
| 131 | expect(oldGone).toEqual("gone"); |
| 132 | |
| 133 | yield* stack.destroy(); |
| 134 | |
| 135 | const gone = yield* waitUntilGone(replaced.name); |
| 136 | expect(gone).toEqual("gone"); |
| 137 | }).pipe(logLevel), |
| 138 | { |
| 139 | tags: ["provider:gcp", "provider:gcp:developerconnect", "live"], |
| 140 | timeout: 90_000, |
| 141 | }, |
| 142 | ); |