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 { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as compute from "@distilled.cloud/gcp/compute_v1"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: GCP.providers() }); |
| 10 | |
| 11 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 12 | |
| 13 | const waitUntilGone = (project: string, region: string, healthCheckName: string) => |
| 14 | compute |
| 15 | .getRegionHealthChecks({ |
| 16 | project, |
| 17 | region, |
| 18 | healthCheck: healthCheckName, |
| 19 | }) |
| 20 | .pipe( |
| 21 | Effect.as("found" as const), |
| 22 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 23 | Effect.repeat({ |
| 24 | schedule: Schedule.spaced("1 second"), |
| 25 | until: (status) => status === "gone", |
| 26 | times: 10, |
| 27 | }), |
| 28 | ); |
| 29 | |
| 30 | test.provider( |
| 31 | "create, update, and delete a region health check", |
| 32 | (stack) => |
| 33 | Effect.gen(function* () { |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | const created = yield* stack.deploy( |
| 37 | Effect.gen(function* () { |
| 38 | return yield* GCP.Compute.RegionHealthCheck("Api", { |
| 39 | region: "us-central1", |
| 40 | description: "frontend probe", |
| 41 | httpHealthCheck: { port: 80, requestPath: "/health" }, |
| 42 | }); |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | expect(created.healthCheckName).toEqual(expect.any(String)); |
| 47 | expect(created.region).toEqual("us-central1"); |
| 48 | expect(created.type).toEqual("HTTP"); |
| 49 | expect(created.description).toEqual("frontend probe"); |
| 50 | expect(created.httpHealthCheck?.requestPath).toEqual("/health"); |
| 51 | expect(created.checkIntervalSec).toEqual(5); |
| 52 | |
| 53 | const fetched = yield* compute.getRegionHealthChecks({ |
| 54 | project: created.project, |
| 55 | region: created.region, |
| 56 | healthCheck: created.healthCheckName, |
| 57 | }); |
| 58 | expect(fetched.name).toEqual(created.healthCheckName); |
| 59 | expect(fetched.type).toEqual("HTTP"); |
| 60 | expect(fetched.httpHealthCheck?.requestPath).toEqual("/health"); |
| 61 | expect(fetched.description).toContain("[alchemy "); |
| 62 | expect(fetched.description).toContain("frontend probe"); |
| 63 | expect(lastSegment(fetched.region)).toEqual("us-central1"); |
| 64 | |
| 65 | const updated = yield* stack.deploy( |
| 66 | Effect.gen(function* () { |
| 67 | return yield* GCP.Compute.RegionHealthCheck("Api", { |
| 68 | healthCheckName: created.healthCheckName, |
| 69 | region: "us-central1", |
| 70 | description: "ready probe", |
| 71 | checkIntervalSec: 10, |
| 72 | timeoutSec: 5, |
| 73 | healthyThreshold: 3, |
| 74 | httpHealthCheck: { port: 80, requestPath: "/ready" }, |
| 75 | }); |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | expect(updated.healthCheckName).toEqual(created.healthCheckName); |
| 80 | expect(updated.region).toEqual("us-central1"); |
| 81 | expect(updated.type).toEqual("HTTP"); |
| 82 | expect(updated.description).toEqual("ready probe"); |
| 83 | expect(updated.checkIntervalSec).toEqual(10); |
| 84 | expect(updated.healthyThreshold).toEqual(3); |
| 85 | expect(updated.httpHealthCheck?.requestPath).toEqual("/ready"); |
| 86 | |
| 87 | const refetched = yield* compute.getRegionHealthChecks({ |
| 88 | project: updated.project, |
| 89 | region: updated.region, |
| 90 | healthCheck: updated.healthCheckName, |
| 91 | }); |
| 92 | expect(refetched.type).toEqual("HTTP"); |
| 93 | expect(refetched.httpHealthCheck?.requestPath).toEqual("/ready"); |
| 94 | expect(refetched.checkIntervalSec).toEqual(10); |
| 95 | |
| 96 | const replaced = yield* stack.deploy( |
| 97 | Effect.gen(function* () { |
| 98 | return yield* GCP.Compute.RegionHealthCheck("Api", { |
| 99 | healthCheckName: created.healthCheckName, |
| 100 | region: "us-central1", |
| 101 | description: "tcp probe", |
| 102 | type: "TCP", |
| 103 | tcpHealthCheck: { port: 8080 }, |
| 104 | }); |
| 105 | }), |
| 106 | ); |
| 107 | |
| 108 | expect(replaced.healthCheckName).toEqual(created.healthCheckName); |
| 109 | expect(replaced.type).toEqual("TCP"); |
| 110 | expect(replaced.description).toEqual("tcp probe"); |
| 111 | expect(replaced.tcpHealthCheck?.port).toEqual(8080); |
| 112 | |
| 113 | const afterReplace = yield* compute.getRegionHealthChecks({ |
| 114 | project: replaced.project, |
| 115 | region: replaced.region, |
| 116 | healthCheck: replaced.healthCheckName, |
| 117 | }); |
| 118 | expect(afterReplace.type).toEqual("TCP"); |
| 119 | expect(afterReplace.tcpHealthCheck?.port).toEqual(8080); |
| 120 | expect(afterReplace.httpHealthCheck).toBeUndefined(); |
| 121 | |
| 122 | yield* stack.destroy(); |
| 123 | |
| 124 | const gone = yield* waitUntilGone(created.project, created.region, created.healthCheckName); |
| 125 | expect(gone).toEqual("gone"); |
| 126 | }).pipe(logLevel), |
| 127 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 128 | ); |
| 129 | |
| 130 | const lastSegment = (value: string | undefined) => { |
| 131 | if (value === undefined || value.length === 0) return ""; |
| 132 | const parts = value.split("/").filter((part) => part.length > 0); |
| 133 | return parts[parts.length - 1] ?? value; |
| 134 | }; |