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.getProjectsLocationsDependencies({ 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( |
| 33 | "getProjectsLocationsDependencies on a missing dependency 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.getProjectsLocationsDependencies({ |
| 41 | name: `projects/${project}/locations/${location}/dependencies/alchemy-missing-dep`, |
| 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 | "create, update, and delete an API Hub dependency", |
| 53 | (stack) => |
| 54 | Effect.gen(function* () { |
| 55 | yield* stack.destroy(); |
| 56 | |
| 57 | const created = yield* stack.deploy( |
| 58 | Effect.gen(function* () { |
| 59 | const consumerApi = yield* GCP.ApiHub.Api("Consumer", { |
| 60 | location, |
| 61 | displayName: "consumer", |
| 62 | }); |
| 63 | const supplierApi = yield* GCP.ApiHub.Api("Supplier", { |
| 64 | location, |
| 65 | displayName: "supplier", |
| 66 | }); |
| 67 | const consumerVersion = yield* GCP.ApiHub.ApisVersion("ConsumerV1", { |
| 68 | api: consumerApi.name, |
| 69 | location, |
| 70 | displayName: "v1", |
| 71 | }); |
| 72 | const supplierVersion = yield* GCP.ApiHub.ApisVersion("SupplierV1", { |
| 73 | api: supplierApi.name, |
| 74 | location, |
| 75 | displayName: "v1", |
| 76 | }); |
| 77 | const consumerOp = yield* GCP.ApiHub.ApisVersionsOperation("ListPets", { |
| 78 | version: consumerVersion.name, |
| 79 | location, |
| 80 | details: { |
| 81 | httpOperation: { |
| 82 | method: "GET", |
| 83 | path: { path: "/pets" }, |
| 84 | }, |
| 85 | description: "list pets", |
| 86 | }, |
| 87 | }); |
| 88 | const supplierOp = yield* GCP.ApiHub.ApisVersionsOperation("GetPet", { |
| 89 | version: supplierVersion.name, |
| 90 | location, |
| 91 | details: { |
| 92 | httpOperation: { |
| 93 | method: "GET", |
| 94 | path: { path: "/pets/{id}" }, |
| 95 | }, |
| 96 | description: "get pet", |
| 97 | }, |
| 98 | }); |
| 99 | const dependency = yield* GCP.ApiHub.Dependency("Calls", { |
| 100 | location, |
| 101 | description: "listPets calls getPet", |
| 102 | consumer: { operationResourceName: consumerOp.name }, |
| 103 | supplier: { operationResourceName: supplierOp.name }, |
| 104 | }); |
| 105 | return { consumerOp, supplierOp, dependency }; |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | expect(created.dependency.name).toContain("/dependencies/"); |
| 110 | expect(created.dependency.consumer?.operationResourceName).toEqual(created.consumerOp.name); |
| 111 | expect(created.dependency.supplier?.operationResourceName).toEqual(created.supplierOp.name); |
| 112 | expect(created.dependency.description).toEqual("listPets calls getPet"); |
| 113 | |
| 114 | const fetched = yield* apihub.getProjectsLocationsDependencies({ |
| 115 | name: created.dependency.name, |
| 116 | }); |
| 117 | expect(fetched.name).toEqual(created.dependency.name); |
| 118 | expect(fetched.description).toContain("alchemy-id="); |
| 119 | |
| 120 | const updated = yield* stack.deploy( |
| 121 | Effect.gen(function* () { |
| 122 | const consumerApi = yield* GCP.ApiHub.Api("Consumer", { |
| 123 | location, |
| 124 | displayName: "consumer", |
| 125 | }); |
| 126 | const supplierApi = yield* GCP.ApiHub.Api("Supplier", { |
| 127 | location, |
| 128 | displayName: "supplier", |
| 129 | }); |
| 130 | const consumerVersion = yield* GCP.ApiHub.ApisVersion("ConsumerV1", { |
| 131 | api: consumerApi.name, |
| 132 | location, |
| 133 | displayName: "v1", |
| 134 | }); |
| 135 | const supplierVersion = yield* GCP.ApiHub.ApisVersion("SupplierV1", { |
| 136 | api: supplierApi.name, |
| 137 | location, |
| 138 | displayName: "v1", |
| 139 | }); |
| 140 | const consumerOp = yield* GCP.ApiHub.ApisVersionsOperation("ListPets", { |
| 141 | version: consumerVersion.name, |
| 142 | location, |
| 143 | details: { |
| 144 | httpOperation: { |
| 145 | method: "GET", |
| 146 | path: { path: "/pets" }, |
| 147 | }, |
| 148 | description: "list pets", |
| 149 | }, |
| 150 | }); |
| 151 | const supplierOp = yield* GCP.ApiHub.ApisVersionsOperation("GetPet", { |
| 152 | version: supplierVersion.name, |
| 153 | location, |
| 154 | details: { |
| 155 | httpOperation: { |
| 156 | method: "GET", |
| 157 | path: { path: "/pets/{id}" }, |
| 158 | }, |
| 159 | description: "get pet", |
| 160 | }, |
| 161 | }); |
| 162 | const dependency = yield* GCP.ApiHub.Dependency("Calls", { |
| 163 | dependencyId: created.dependency.dependencyId, |
| 164 | location, |
| 165 | description: "listPets still calls getPet", |
| 166 | consumer: { operationResourceName: consumerOp.name }, |
| 167 | supplier: { operationResourceName: supplierOp.name }, |
| 168 | }); |
| 169 | return { consumerOp, supplierOp, dependency }; |
| 170 | }), |
| 171 | ); |
| 172 | |
| 173 | expect(updated.dependency.name).toEqual(created.dependency.name); |
| 174 | expect(updated.dependency.description).toEqual("listPets still calls getPet"); |
| 175 | |
| 176 | yield* stack.destroy(); |
| 177 | |
| 178 | const gone = yield* waitUntilGone(created.dependency.name); |
| 179 | expect(gone).toEqual("gone"); |
| 180 | }).pipe(logLevel), |
| 181 | { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 }, |
| 182 | ); |