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 | // API Hub needs a host project registration first; without it instance |
| 15 | // create fails with BadRequest ("Unable to find host project registration |
| 16 | // for this project."). Set GCP_TEST_APIHUB_HOST_PROJECT=1 on a registered |
| 17 | // project that has no hub yet (one instance per project). |
| 18 | const runLifecycle = !!process.env.GCP_TEST_APIHUB_HOST_PROJECT; |
| 19 | const location = "us-central1"; |
| 20 | |
| 21 | const waitUntilGone = (name: string) => |
| 22 | apihub.getProjectsLocationsApiHubInstances({ 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( |
| 33 | "getProjectsLocationsApiHubInstances on a missing instance 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.getProjectsLocationsApiHubInstances({ |
| 41 | name: `projects/${project}/locations/${location}/apiHubInstances/alchemy-missing-hub`, |
| 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 | "createProjectsLocationsApiHubInstances without a host project registration fails with BadRequest", |
| 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.createProjectsLocationsApiHubInstances({ |
| 60 | parent: `projects/${project}/locations/${location}`, |
| 61 | apiHubInstanceId: "alchemy-apihub-probe", |
| 62 | body: { config: { vertexLocation: "us" } }, |
| 63 | }), |
| 64 | ); |
| 65 | expect(error._tag).toEqual("BadRequest"); |
| 66 | expect(error.message).toContain("host project registration"); |
| 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 instance", |
| 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.ApiHubInstance("Hub", { |
| 82 | location, |
| 83 | description: "alchemy test hub", |
| 84 | labels: { env: "test" }, |
| 85 | config: { disableSearch: false, vertexLocation: "us" }, |
| 86 | }); |
| 87 | }), |
| 88 | ); |
| 89 | |
| 90 | expect(created.name).toContain("/apiHubInstances/"); |
| 91 | expect(created.apiHubInstanceId).toEqual(expect.any(String)); |
| 92 | expect(created.location).toEqual(location); |
| 93 | expect(created.description).toEqual("alchemy test hub"); |
| 94 | expect(created.labels).toMatchObject({ env: "test" }); |
| 95 | |
| 96 | const fetched = yield* apihub.getProjectsLocationsApiHubInstances({ |
| 97 | name: created.name, |
| 98 | }); |
| 99 | expect(fetched.name).toEqual(created.name); |
| 100 | expect(fetched.labels?.env).toEqual("test"); |
| 101 | expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual( |
| 102 | true, |
| 103 | ); |
| 104 | |
| 105 | const updated = yield* stack.deploy( |
| 106 | Effect.gen(function* () { |
| 107 | return yield* GCP.ApiHub.ApiHubInstance("Hub", { |
| 108 | apiHubInstanceId: created.apiHubInstanceId, |
| 109 | location, |
| 110 | description: "alchemy test hub v2", |
| 111 | labels: { env: "prod" }, |
| 112 | config: { |
| 113 | disableSearch: true, |
| 114 | vertexLocation: "us", |
| 115 | }, |
| 116 | }); |
| 117 | }), |
| 118 | ); |
| 119 | |
| 120 | expect(updated.name).toEqual(created.name); |
| 121 | expect(updated.disableSearch).toEqual(true); |
| 122 | expect(updated.vertexLocation).toEqual("us"); |
| 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: 120_000 }, |
| 130 | ); |