The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as apihub from "@distilled.cloud/gcp/apihub_v1"; |
| 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(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | // Needs a provisioned API Hub instance in us-central1 (one per project, |
| 15 | // behind a host project registration); without one writes fail with |
| 16 | // BadRequest ("Invalid resource state … API Hub instance …"). Set |
| 17 | // GCP_TEST_APIHUB_INSTANCE=1 when the hub exists. |
| 18 | const runLifecycle = !!process.env.GCP_TEST_APIHUB_INSTANCE; |
| 19 | const location = "us-central1"; |
| 20 | |
| 21 | const waitUntilGone = (name: string) => |
| 22 | apihub.getProjectsLocationsPlugins({ name }).pipe( |
| 23 | Effect.as("found" as const), |
| 24 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 25 | Effect.repeat({ |
| 26 | schedule: Schedule.spaced("2 seconds"), |
| 27 | until: (status) => status === "gone", |
| 28 | times: 10, |
| 29 | }), |
| 30 | ); |
| 31 | |
| 32 | test.provider.skipIf(!runLifecycle)( |
| 33 | "getProjectsLocationsPlugins on a missing plugin fails with a typed tag", |
| 34 | (stack) => |
| 35 | Effect.gen(function* () { |
| 36 | const { project } = yield* GcpEnvironment.current; |
| 37 | yield* stack.destroy(); |
| 38 | |
| 39 | const error = yield* Effect.flip( |
| 40 | apihub.getProjectsLocationsPlugins({ |
| 41 | name: `projects/${project}/locations/${location}/plugins/alchemy-missing-plugin`, |
| 42 | }), |
| 43 | ); |
| 44 | expect(error._tag).toEqual("NotFound"); |
| 45 | |
| 46 | yield* stack.destroy(); |
| 47 | }).pipe(logLevel), |
| 48 | { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 }, |
| 49 | ); |
| 50 | |
| 51 | test.provider.skipIf(runLifecycle)( |
| 52 | "getProjectsLocationsPlugins fails with ApiHubNotProvisioned without an API hub instance", |
| 53 | (stack) => |
| 54 | Effect.gen(function* () { |
| 55 | const { project } = yield* GcpEnvironment.current; |
| 56 | yield* stack.destroy(); |
| 57 | |
| 58 | const error = yield* Effect.flip( |
| 59 | apihub.getProjectsLocationsPlugins({ |
| 60 | name: `projects/${project}/locations/${location}/plugins/alchemy-missing-plugin`, |
| 61 | }), |
| 62 | ); |
| 63 | expect(error._tag).toEqual("ApiHubNotProvisioned"); |
| 64 | |
| 65 | yield* stack.destroy(); |
| 66 | }).pipe(logLevel), |
| 67 | { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 }, |
| 68 | ); |
| 69 | |
| 70 | test.provider.skipIf(!runLifecycle)( |
| 71 | "create, disable, and delete an API Hub plugin", |
| 72 | (stack) => |
| 73 | Effect.gen(function* () { |
| 74 | yield* stack.destroy(); |
| 75 | |
| 76 | const created = yield* stack.deploy( |
| 77 | Effect.gen(function* () { |
| 78 | return yield* GCP.ApiHub.Plugin("OnRamp", { |
| 79 | location, |
| 80 | displayName: "on-ramp", |
| 81 | description: "custom collector", |
| 82 | }); |
| 83 | }), |
| 84 | ); |
| 85 | |
| 86 | expect(created.name).toContain("/plugins/"); |
| 87 | expect(created.pluginId).toEqual(expect.any(String)); |
| 88 | expect(created.location).toEqual(location); |
| 89 | expect(created.displayName).toEqual("on-ramp"); |
| 90 | expect(created.description).toEqual("custom collector"); |
| 91 | expect(created.pluginCategory).toEqual("API_PRODUCER"); |
| 92 | |
| 93 | const fetched = yield* apihub.getProjectsLocationsPlugins({ |
| 94 | name: created.name, |
| 95 | }); |
| 96 | expect(fetched.name).toEqual(created.name); |
| 97 | expect(fetched.description).toContain("alchemy-id="); |
| 98 | expect(fetched.description).toContain("custom collector"); |
| 99 | |
| 100 | const updated = yield* stack.deploy( |
| 101 | Effect.gen(function* () { |
| 102 | return yield* GCP.ApiHub.Plugin("OnRamp", { |
| 103 | pluginId: created.pluginId, |
| 104 | location, |
| 105 | displayName: "on-ramp", |
| 106 | description: "custom collector", |
| 107 | enabled: false, |
| 108 | }); |
| 109 | }), |
| 110 | ); |
| 111 | |
| 112 | expect(updated.name).toEqual(created.name); |
| 113 | expect(updated.state === "DISABLED" || updated.state === undefined).toEqual(true); |
| 114 | |
| 115 | yield* stack.destroy(); |
| 116 | const gone = yield* waitUntilGone(created.name); |
| 117 | expect(gone).toEqual("gone"); |
| 118 | }).pipe(logLevel), |
| 119 | { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 }, |
| 120 | ); |