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 openApiV1 = `openapi: 3.0.0 |
| 22 | info: |
| 23 | title: pets |
| 24 | version: 1.0.0 |
| 25 | paths: |
| 26 | /pets: |
| 27 | get: |
| 28 | operationId: listPets |
| 29 | responses: |
| 30 | "200": |
| 31 | description: ok |
| 32 | `; |
| 33 | |
| 34 | const openApiV2 = `openapi: 3.0.0 |
| 35 | info: |
| 36 | title: pets |
| 37 | version: 2.0.0 |
| 38 | paths: |
| 39 | /pets: |
| 40 | get: |
| 41 | operationId: listPets |
| 42 | responses: |
| 43 | "200": |
| 44 | description: ok |
| 45 | /pets/{id}: |
| 46 | get: |
| 47 | operationId: getPet |
| 48 | parameters: |
| 49 | - name: id |
| 50 | in: path |
| 51 | required: true |
| 52 | schema: |
| 53 | type: string |
| 54 | responses: |
| 55 | "200": |
| 56 | description: ok |
| 57 | `; |
| 58 | |
| 59 | const waitUntilGone = (name: string) => |
| 60 | apihub.getProjectsLocationsApisVersionsSpecs({ name }).pipe( |
| 61 | Effect.as("found" as const), |
| 62 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 63 | Effect.repeat({ |
| 64 | schedule: Schedule.spaced("1 second"), |
| 65 | until: (status) => status === "gone", |
| 66 | times: 10, |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | test.provider( |
| 71 | "getProjectsLocationsApisVersionsSpecs on a missing spec fails with a typed tag", |
| 72 | (stack) => |
| 73 | Effect.gen(function* () { |
| 74 | const { project } = yield* GcpEnvironment.current; |
| 75 | yield* stack.destroy(); |
| 76 | |
| 77 | const error = yield* Effect.flip( |
| 78 | apihub.getProjectsLocationsApisVersionsSpecs({ |
| 79 | name: `projects/${project}/locations/${location}/apis/alchemy-missing/versions/v1/specs/openapi`, |
| 80 | }), |
| 81 | ); |
| 82 | expect(error._tag).toEqual("NotFound"); |
| 83 | |
| 84 | yield* stack.destroy(); |
| 85 | }).pipe(logLevel), |
| 86 | { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 }, |
| 87 | ); |
| 88 | |
| 89 | test.provider.skipIf(!runLifecycle)( |
| 90 | "create, update, and delete an API Hub spec", |
| 91 | (stack) => |
| 92 | Effect.gen(function* () { |
| 93 | yield* stack.destroy(); |
| 94 | |
| 95 | const created = yield* stack.deploy( |
| 96 | Effect.gen(function* () { |
| 97 | const api = yield* GCP.ApiHub.Api("Pets", { |
| 98 | location, |
| 99 | displayName: "pets", |
| 100 | }); |
| 101 | const version = yield* GCP.ApiHub.ApisVersion("V1", { |
| 102 | api: api.name, |
| 103 | location, |
| 104 | displayName: "v1", |
| 105 | }); |
| 106 | const spec = yield* GCP.ApiHub.ApisVersionsSpec("OpenApi", { |
| 107 | version: version.name, |
| 108 | location, |
| 109 | displayName: "openapi.yaml", |
| 110 | contents: { |
| 111 | contents: openApiV1, |
| 112 | mimeType: "application/yaml", |
| 113 | }, |
| 114 | }); |
| 115 | return { api, version, spec }; |
| 116 | }), |
| 117 | ); |
| 118 | |
| 119 | expect(created.spec.name).toContain("/specs/"); |
| 120 | expect(created.spec.version).toEqual(created.version.name); |
| 121 | expect(created.spec.displayName).toEqual("openapi.yaml"); |
| 122 | |
| 123 | const fetched = yield* apihub.getProjectsLocationsApisVersionsSpecs({ |
| 124 | name: created.spec.name, |
| 125 | }); |
| 126 | expect(fetched.name).toEqual(created.spec.name); |
| 127 | expect(fetched.displayName).toContain("alchemy-"); |
| 128 | |
| 129 | const updated = yield* stack.deploy( |
| 130 | Effect.gen(function* () { |
| 131 | const api = yield* GCP.ApiHub.Api("Pets", { |
| 132 | apiId: created.api.apiId, |
| 133 | location, |
| 134 | displayName: "pets", |
| 135 | }); |
| 136 | const version = yield* GCP.ApiHub.ApisVersion("V1", { |
| 137 | api: api.name, |
| 138 | versionId: created.version.versionId, |
| 139 | location, |
| 140 | displayName: "v1", |
| 141 | }); |
| 142 | const spec = yield* GCP.ApiHub.ApisVersionsSpec("OpenApi", { |
| 143 | version: version.name, |
| 144 | specId: created.spec.specId, |
| 145 | location, |
| 146 | displayName: "openapi-v2.yaml", |
| 147 | contents: { |
| 148 | contents: openApiV2, |
| 149 | mimeType: "application/yaml", |
| 150 | }, |
| 151 | }); |
| 152 | return { api, version, spec }; |
| 153 | }), |
| 154 | ); |
| 155 | |
| 156 | expect(updated.spec.name).toEqual(created.spec.name); |
| 157 | expect(updated.spec.displayName).toEqual("openapi-v2.yaml"); |
| 158 | |
| 159 | yield* stack.destroy(); |
| 160 | |
| 161 | const gone = yield* waitUntilGone(created.spec.name); |
| 162 | expect(gone).toEqual("gone"); |
| 163 | }).pipe(logLevel), |
| 164 | { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 }, |
| 165 | ); |