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 apphub from "@distilled.cloud/gcp/apphub_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 | // App Hub is not enabled in the test project; live calls fail with |
| 15 | // ServiceDisabled: |
| 16 | // "App Hub API has not been used in project <project> |
| 17 | // before or it is disabled." Workload registration also needs a |
| 18 | // discovered workload in the host project. |
| 19 | const runLifecycle = !process.env.FAST && process.env.GCP_TEST_APPHUB === "1"; |
| 20 | const location = "us-central1"; |
| 21 | |
| 22 | const waitUntilGone = (name: string) => |
| 23 | apphub.getProjectsLocationsApplicationsWorkloads({ name }).pipe( |
| 24 | Effect.as("found" as const), |
| 25 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 26 | Effect.repeat({ |
| 27 | schedule: Schedule.spaced("1 second"), |
| 28 | until: (status) => status === "gone", |
| 29 | times: 10, |
| 30 | }), |
| 31 | ); |
| 32 | |
| 33 | const firstDiscoveredWorkload = GcpEnvironment.current.pipe( |
| 34 | Effect.flatMap(({ project }) => |
| 35 | apphub |
| 36 | .listProjectsLocationsDiscoveredWorkloads({ |
| 37 | parent: `projects/${project}/locations/${location}`, |
| 38 | pageSize: 10, |
| 39 | }) |
| 40 | .pipe( |
| 41 | Effect.map((page) => page.discoveredWorkloads?.[0]?.name), |
| 42 | Effect.catchTag("NotFound", () => Effect.succeed(undefined)), |
| 43 | ), |
| 44 | ), |
| 45 | ); |
| 46 | |
| 47 | test.provider.skipIf(runLifecycle)( |
| 48 | "getProjectsLocationsApplicationsWorkloads on a missing workload fails with a typed tag", |
| 49 | (stack) => |
| 50 | Effect.gen(function* () { |
| 51 | const { project } = yield* GcpEnvironment.current; |
| 52 | yield* stack.destroy(); |
| 53 | |
| 54 | const error = yield* Effect.flip( |
| 55 | apphub.getProjectsLocationsApplicationsWorkloads({ |
| 56 | name: `projects/${project}/locations/${location}/applications/alchemy-missing-app/workloads/alchemy-missing-workload`, |
| 57 | }), |
| 58 | ); |
| 59 | expect(["ServiceDisabled", "NotFound"]).toContain(error._tag); |
| 60 | |
| 61 | yield* stack.destroy(); |
| 62 | }).pipe(logLevel), |
| 63 | { tags: ["provider:gcp", "provider:gcp:apphub", "live"], timeout: 90_000 }, |
| 64 | ); |
| 65 | |
| 66 | test.provider.skipIf(!runLifecycle)( |
| 67 | "create, update, and delete an App Hub workload", |
| 68 | (stack) => |
| 69 | Effect.gen(function* () { |
| 70 | yield* stack.destroy(); |
| 71 | |
| 72 | const discoveredWorkload = yield* firstDiscoveredWorkload; |
| 73 | expect(discoveredWorkload).toEqual(expect.any(String)); |
| 74 | |
| 75 | const created = yield* stack.deploy( |
| 76 | Effect.gen(function* () { |
| 77 | const app = yield* GCP.AppHub.Application("Store", { |
| 78 | location, |
| 79 | displayName: "store", |
| 80 | scope: { type: "REGIONAL" }, |
| 81 | }); |
| 82 | const workload = yield* GCP.AppHub.ApplicationsWorkload("Api", { |
| 83 | application: app.name, |
| 84 | location, |
| 85 | discoveredWorkload: discoveredWorkload!, |
| 86 | displayName: "api", |
| 87 | description: "checkout mig", |
| 88 | attributes: { criticality: { type: "MEDIUM" } }, |
| 89 | }); |
| 90 | return { app, workload }; |
| 91 | }), |
| 92 | ); |
| 93 | |
| 94 | expect(created.workload.name).toContain("/workloads/"); |
| 95 | expect(created.workload.application).toEqual(created.app.name); |
| 96 | expect(created.workload.displayName).toEqual("api"); |
| 97 | expect(created.workload.description).toEqual("checkout mig"); |
| 98 | expect(created.workload.discoveredWorkload).toBeDefined(); |
| 99 | |
| 100 | const fetched = yield* apphub.getProjectsLocationsApplicationsWorkloads({ |
| 101 | name: created.workload.name, |
| 102 | }); |
| 103 | expect(fetched.name).toEqual(created.workload.name); |
| 104 | expect(fetched.description).toContain("alchemy-id="); |
| 105 | expect(fetched.description).toContain("checkout mig"); |
| 106 | |
| 107 | const updated = yield* stack.deploy( |
| 108 | Effect.gen(function* () { |
| 109 | const app = yield* GCP.AppHub.Application("Store", { |
| 110 | applicationId: created.app.applicationId, |
| 111 | location, |
| 112 | displayName: "store", |
| 113 | scope: { type: "REGIONAL" }, |
| 114 | }); |
| 115 | const workload = yield* GCP.AppHub.ApplicationsWorkload("Api", { |
| 116 | workloadId: created.workload.workloadId, |
| 117 | application: app.name, |
| 118 | location, |
| 119 | discoveredWorkload: created.workload.discoveredWorkload!, |
| 120 | displayName: "api-v2", |
| 121 | description: "checkout mig v2", |
| 122 | attributes: { criticality: { type: "HIGH" } }, |
| 123 | }); |
| 124 | return { app, workload }; |
| 125 | }), |
| 126 | ); |
| 127 | |
| 128 | expect(updated.workload.name).toEqual(created.workload.name); |
| 129 | expect(updated.workload.displayName).toEqual("api-v2"); |
| 130 | expect(updated.workload.description).toEqual("checkout mig v2"); |
| 131 | expect(updated.workload.attributes?.criticality?.type).toEqual("HIGH"); |
| 132 | |
| 133 | yield* stack.destroy(); |
| 134 | const gone = yield* waitUntilGone(created.workload.name); |
| 135 | expect(gone).toEqual("gone"); |
| 136 | }).pipe(logLevel), |
| 137 | { tags: ["provider:gcp", "provider:gcp:apphub", "live"], timeout: 90_000 }, |
| 138 | ); |