| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as apigee from "@distilled.cloud/gcp/apigee_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 siteId = process.env.GCP_TEST_APIGEE_SITE ?? ""; |
| 18 | |
| 19 | // Needs a provisioned Apigee organization on the testing project (paid, or |
| 20 | // ~1h eval provisioning); without one calls fail with ApigeeResourceNotFound (403 "Permission |
| 21 | // denied on resource \"organizations/{project}\" (or it may not exist)"). |
| 22 | // Set GCP_TEST_APIGEE_ORG=1 when the org exists. |
| 23 | const runLifecycle = !!process.env.GCP_TEST_APIGEE_ORG && !!siteId; |
| 24 | |
| 25 | const waitUntilGone = (name: string) => |
| 26 | apigee.getOrganizationsSitesApicategories({ name }).pipe( |
| 27 | Effect.as("found" as const), |
| 28 | Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => |
| 29 | Effect.succeed("gone" as const), |
| 30 | ), |
| 31 | Effect.repeat({ |
| 32 | schedule: Schedule.spaced("1 second"), |
| 33 | until: (status) => status === "gone", |
| 34 | times: 10, |
| 35 | }), |
| 36 | ); |
| 37 | |
| 38 | test.provider( |
| 39 | "getOrganizationsSitesApicategories on a missing category fails with ApigeeResourceNotFound", |
| 40 | (stack) => |
| 41 | Effect.gen(function* () { |
| 42 | const { project } = yield* GcpEnvironment.current; |
| 43 | |
| 44 | yield* stack.destroy(); |
| 45 | |
| 46 | const error = yield* Effect.flip( |
| 47 | apigee.getOrganizationsSitesApicategories({ |
| 48 | name: `organizations/${project}/sites/alchemy-missing-site/apicategories/alchemy-missing-category`, |
| 49 | }), |
| 50 | ); |
| 51 | expect(error._tag).toEqual("ApigeeResourceNotFound"); |
| 52 | |
| 53 | yield* stack.destroy(); |
| 54 | }).pipe(logLevel), |
| 55 | { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 }, |
| 56 | ); |
| 57 | |
| 58 | test.provider.skipIf(!runLifecycle)( |
| 59 | "create, update, and delete an Apigee API category", |
| 60 | (stack) => |
| 61 | Effect.gen(function* () { |
| 62 | const { project } = yield* GcpEnvironment.current; |
| 63 | |
| 64 | yield* stack.destroy(); |
| 65 | |
| 66 | const created = yield* stack.deploy( |
| 67 | Effect.gen(function* () { |
| 68 | return yield* GCP.Apigee.SitesApicategory("Payments", { |
| 69 | siteId, |
| 70 | name: "Payments", |
| 71 | }); |
| 72 | }), |
| 73 | ); |
| 74 | |
| 75 | expect(created.categoryId).toEqual(expect.any(String)); |
| 76 | expect(created.organization).toEqual(project); |
| 77 | expect(created.siteId).toEqual(siteId); |
| 78 | expect(created.categoryName).toEqual("Payments"); |
| 79 | expect(created.name).toEqual( |
| 80 | `organizations/${project}/sites/${siteId}/apicategories/${created.categoryId}`, |
| 81 | ); |
| 82 | |
| 83 | const fetched = yield* apigee.getOrganizationsSitesApicategories({ |
| 84 | name: created.name, |
| 85 | }); |
| 86 | expect(fetched.data?.id).toEqual(created.categoryId); |
| 87 | expect(fetched.data?.name).toEqual("Payments"); |
| 88 | |
| 89 | const updated = yield* stack.deploy( |
| 90 | Effect.gen(function* () { |
| 91 | return yield* GCP.Apigee.SitesApicategory("Payments", { |
| 92 | siteId, |
| 93 | name: "Billing", |
| 94 | }); |
| 95 | }), |
| 96 | ); |
| 97 | |
| 98 | expect(updated.categoryId).toEqual(created.categoryId); |
| 99 | expect(updated.categoryName).toEqual("Billing"); |
| 100 | |
| 101 | const fetchedUpdate = yield* apigee.getOrganizationsSitesApicategories({ |
| 102 | name: updated.name, |
| 103 | }); |
| 104 | expect(fetchedUpdate.data?.name).toEqual("Billing"); |
| 105 | |
| 106 | yield* stack.destroy(); |
| 107 | |
| 108 | const gone = yield* waitUntilGone(created.name); |
| 109 | expect(gone).toEqual("gone"); |
| 110 | }).pipe(logLevel), |
| 111 | { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 }, |
| 112 | ); |