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.getProjectsLocationsApisVersionsOperations({ name }).pipe( |
| 23 | Effect.as("found" as const), |
| 24 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 25 | Effect.repeat({ |
| 26 | schedule: Schedule.spaced("1 second"), |
| 27 | until: (status) => status === "gone", |
| 28 | times: 10, |
| 29 | }), |
| 30 | ); |
| 31 | |
| 32 | test.provider.skipIf(!runLifecycle)( |
| 33 | "getProjectsLocationsApisVersionsOperations on a missing operation 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.getProjectsLocationsApisVersionsOperations({ |
| 41 | name: `projects/${project}/locations/${location}/apis/alchemy-missing/versions/v1/operations/list`, |
| 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 | "getProjectsLocationsApisVersionsOperations 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.getProjectsLocationsApisVersionsOperations({ |
| 60 | name: `projects/${project}/locations/${location}/apis/alchemy-missing/versions/v1/operations/list`, |
| 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, update, and delete an API Hub operation", |
| 72 | (stack) => |
| 73 | Effect.gen(function* () { |
| 74 | yield* stack.destroy(); |
| 75 | |
| 76 | const created = yield* stack.deploy( |
| 77 | Effect.gen(function* () { |
| 78 | const api = yield* GCP.ApiHub.Api("Pets", { |
| 79 | location, |
| 80 | displayName: "pets", |
| 81 | }); |
| 82 | const version = yield* GCP.ApiHub.ApisVersion("V1", { |
| 83 | api: api.name, |
| 84 | location, |
| 85 | displayName: "v1", |
| 86 | }); |
| 87 | const operation = yield* GCP.ApiHub.ApisVersionsOperation("ListPets", { |
| 88 | version: version.name, |
| 89 | location, |
| 90 | details: { |
| 91 | httpOperation: { |
| 92 | method: "GET", |
| 93 | path: { path: "/pets" }, |
| 94 | }, |
| 95 | description: "list pets", |
| 96 | }, |
| 97 | }); |
| 98 | return { api, version, operation }; |
| 99 | }), |
| 100 | ); |
| 101 | |
| 102 | expect(created.operation.name).toContain("/operations/"); |
| 103 | expect(created.operation.version).toEqual(created.version.name); |
| 104 | expect(created.operation.details?.httpOperation?.method).toEqual("GET"); |
| 105 | expect(created.operation.details?.httpOperation?.path?.path).toEqual("/pets"); |
| 106 | expect(created.operation.details?.description).toEqual("list pets"); |
| 107 | |
| 108 | const fetched = yield* apihub.getProjectsLocationsApisVersionsOperations({ |
| 109 | name: created.operation.name, |
| 110 | }); |
| 111 | expect(fetched.name).toEqual(created.operation.name); |
| 112 | expect(fetched.details?.description).toContain("alchemy-id="); |
| 113 | |
| 114 | const updated = yield* stack.deploy( |
| 115 | Effect.gen(function* () { |
| 116 | const api = yield* GCP.ApiHub.Api("Pets", { |
| 117 | apiId: created.api.apiId, |
| 118 | location, |
| 119 | displayName: "pets", |
| 120 | }); |
| 121 | const version = yield* GCP.ApiHub.ApisVersion("V1", { |
| 122 | api: api.name, |
| 123 | versionId: created.version.versionId, |
| 124 | location, |
| 125 | displayName: "v1", |
| 126 | }); |
| 127 | const operation = yield* GCP.ApiHub.ApisVersionsOperation("ListPets", { |
| 128 | version: version.name, |
| 129 | apiOperationId: created.operation.apiOperationId, |
| 130 | location, |
| 131 | details: { |
| 132 | httpOperation: { |
| 133 | method: "GET", |
| 134 | path: { path: "/animals" }, |
| 135 | }, |
| 136 | description: "list animals", |
| 137 | deprecated: true, |
| 138 | }, |
| 139 | }); |
| 140 | return { api, version, operation }; |
| 141 | }), |
| 142 | ); |
| 143 | |
| 144 | expect(updated.operation.name).toEqual(created.operation.name); |
| 145 | expect(updated.operation.details?.httpOperation?.path?.path).toEqual("/animals"); |
| 146 | expect(updated.operation.details?.description).toEqual("list animals"); |
| 147 | expect(updated.operation.details?.deprecated).toEqual(true); |
| 148 | |
| 149 | yield* stack.destroy(); |
| 150 | |
| 151 | const gone = yield* waitUntilGone(created.operation.name); |
| 152 | expect(gone).toEqual("gone"); |
| 153 | }).pipe(logLevel), |
| 154 | { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 }, |
| 155 | ); |