| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as apihub from "@distilled.cloud/gcp/apihub_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 | // Needs a provisioned API Hub instance in us-central1 (one per project, |
| 18 | // behind a host project registration); without one writes fail with |
| 19 | // BadRequest ("Invalid resource state … API Hub instance …"). Set |
| 20 | // GCP_TEST_APIHUB_INSTANCE=1 when the hub exists. |
| 21 | const runLifecycle = !!process.env.GCP_TEST_APIHUB_INSTANCE; |
| 22 | const location = "us-central1"; |
| 23 | |
| 24 | const waitUntilGone = (name: string) => |
| 25 | apihub.getProjectsLocationsAttributes({ name }).pipe( |
| 26 | Effect.as("found" as const), |
| 27 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 28 | Effect.repeat({ |
| 29 | schedule: Schedule.spaced("1 second"), |
| 30 | until: (status) => status === "gone", |
| 31 | times: 10, |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | test.provider.skipIf(!runLifecycle)( |
| 36 | "getProjectsLocationsAttributes on a missing attribute fails with a typed tag", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | const { project } = yield* GcpEnvironment.current; |
| 40 | yield* stack.destroy(); |
| 41 | |
| 42 | const error = yield* Effect.flip( |
| 43 | apihub.getProjectsLocationsAttributes({ |
| 44 | name: `projects/${project}/locations/${location}/attributes/alchemy-missing-attr`, |
| 45 | }), |
| 46 | ); |
| 47 | expect(error._tag).toEqual("NotFound"); |
| 48 | |
| 49 | yield* stack.destroy(); |
| 50 | }).pipe(logLevel), |
| 51 | { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 }, |
| 52 | ); |
| 53 | |
| 54 | test.provider.skipIf(runLifecycle)( |
| 55 | "getProjectsLocationsAttributes fails with ApiHubNotProvisioned without an API hub instance", |
| 56 | (stack) => |
| 57 | Effect.gen(function* () { |
| 58 | const { project } = yield* GcpEnvironment.current; |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const error = yield* Effect.flip( |
| 62 | apihub.getProjectsLocationsAttributes({ |
| 63 | name: `projects/${project}/locations/${location}/attributes/alchemy-missing-attr`, |
| 64 | }), |
| 65 | ); |
| 66 | expect(error._tag).toEqual("ApiHubNotProvisioned"); |
| 67 | |
| 68 | yield* stack.destroy(); |
| 69 | }).pipe(logLevel), |
| 70 | { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 }, |
| 71 | ); |
| 72 | |
| 73 | test.provider.skipIf(!runLifecycle)( |
| 74 | "create, update, and delete an API Hub attribute", |
| 75 | (stack) => |
| 76 | Effect.gen(function* () { |
| 77 | yield* stack.destroy(); |
| 78 | |
| 79 | const created = yield* stack.deploy( |
| 80 | Effect.gen(function* () { |
| 81 | return yield* GCP.ApiHub.Attribute("OwnerTeam", { |
| 82 | location, |
| 83 | displayName: "owner-team", |
| 84 | description: "owning team", |
| 85 | dataType: "STRING", |
| 86 | scope: "API", |
| 87 | }); |
| 88 | }), |
| 89 | ); |
| 90 | |
| 91 | expect(created.name).toContain("/attributes/"); |
| 92 | expect(created.attributeId).toEqual(expect.any(String)); |
| 93 | expect(created.displayName).toEqual("owner-team"); |
| 94 | expect(created.description).toEqual("owning team"); |
| 95 | expect(created.dataType).toEqual("STRING"); |
| 96 | expect(created.scope).toEqual("API"); |
| 97 | |
| 98 | const fetched = yield* apihub.getProjectsLocationsAttributes({ |
| 99 | name: created.name, |
| 100 | }); |
| 101 | expect(fetched.name).toEqual(created.name); |
| 102 | expect(fetched.description).toContain("alchemy-id="); |
| 103 | expect(fetched.description).toContain("owning team"); |
| 104 | |
| 105 | const updated = yield* stack.deploy( |
| 106 | Effect.gen(function* () { |
| 107 | return yield* GCP.ApiHub.Attribute("OwnerTeam", { |
| 108 | attributeId: created.attributeId, |
| 109 | location, |
| 110 | displayName: "owner-team-v2", |
| 111 | description: "owning team v2", |
| 112 | dataType: "STRING", |
| 113 | scope: "API", |
| 114 | cardinality: 2, |
| 115 | }); |
| 116 | }), |
| 117 | ); |
| 118 | |
| 119 | expect(updated.name).toEqual(created.name); |
| 120 | expect(updated.displayName).toEqual("owner-team-v2"); |
| 121 | expect(updated.description).toEqual("owning team v2"); |
| 122 | expect(updated.cardinality).toEqual(2); |
| 123 | |
| 124 | yield* stack.destroy(); |
| 125 | |
| 126 | const gone = yield* waitUntilGone(created.name); |
| 127 | expect(gone).toEqual("gone"); |
| 128 | }).pipe(logLevel), |
| 129 | { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 }, |
| 130 | ); |