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 compute from "@distilled.cloud/gcp/compute_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 | const region = "us-central1"; |
| 15 | const poolName = "alchemy-fr-test-pool"; |
| 16 | |
| 17 | const waitUntilGone = (forwardingRuleName: string) => |
| 18 | GcpEnvironment.current.pipe( |
| 19 | Effect.flatMap(({ project }) => |
| 20 | compute |
| 21 | .getForwardingRules({ |
| 22 | project, |
| 23 | region, |
| 24 | forwardingRule: forwardingRuleName, |
| 25 | }) |
| 26 | .pipe( |
| 27 | Effect.as("found" as const), |
| 28 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 29 | Effect.repeat({ |
| 30 | schedule: Schedule.spaced("2 seconds"), |
| 31 | until: (status) => status === "gone", |
| 32 | times: 10, |
| 33 | }), |
| 34 | ), |
| 35 | ), |
| 36 | ); |
| 37 | |
| 38 | const waitOp = (operation: compute.Operation) => { |
| 39 | if (operation.status === "DONE") return Effect.succeed(operation); |
| 40 | const name = (operation.name ?? "").split("/").pop() ?? ""; |
| 41 | return GcpEnvironment.current.pipe( |
| 42 | Effect.flatMap(({ project }) => |
| 43 | compute.getRegionOperations({ project, region, operation: name }).pipe( |
| 44 | Effect.repeat({ |
| 45 | schedule: Schedule.spaced("2 seconds"), |
| 46 | until: (op) => op.status === "DONE", |
| 47 | times: 12, |
| 48 | }), |
| 49 | ), |
| 50 | ), |
| 51 | ); |
| 52 | }; |
| 53 | |
| 54 | const ensureTargetPool = () => |
| 55 | Effect.gen(function* () { |
| 56 | const { project } = yield* GcpEnvironment.current; |
| 57 | const existing = yield* compute |
| 58 | .getTargetPools({ project, region, targetPool: poolName }) |
| 59 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 60 | if (existing !== undefined) return existing; |
| 61 | yield* compute |
| 62 | .insertTargetPools({ |
| 63 | project, |
| 64 | region, |
| 65 | body: { name: poolName, description: "alchemy forwarding-rule test" }, |
| 66 | }) |
| 67 | .pipe( |
| 68 | Effect.flatMap(waitOp), |
| 69 | Effect.catchTag("Conflict", () => Effect.void), |
| 70 | ); |
| 71 | return yield* compute.getTargetPools({ |
| 72 | project, |
| 73 | region, |
| 74 | targetPool: poolName, |
| 75 | }); |
| 76 | }); |
| 77 | |
| 78 | const deleteTargetPool = () => |
| 79 | GcpEnvironment.current.pipe( |
| 80 | Effect.flatMap(({ project }) => |
| 81 | compute.deleteTargetPools({ project, region, targetPool: poolName }).pipe( |
| 82 | Effect.flatMap(waitOp), |
| 83 | Effect.catchTag("NotFound", () => Effect.void), |
| 84 | Effect.catchTag("Conflict", () => Effect.void), |
| 85 | ), |
| 86 | ), |
| 87 | ); |
| 88 | |
| 89 | test.provider( |
| 90 | "create, update, and delete a regional forwarding rule", |
| 91 | (stack) => |
| 92 | Effect.gen(function* () { |
| 93 | yield* stack.destroy(); |
| 94 | const pool = yield* ensureTargetPool(); |
| 95 | expect(pool.selfLink).toEqual(expect.any(String)); |
| 96 | |
| 97 | const created = yield* stack.deploy( |
| 98 | Effect.gen(function* () { |
| 99 | return yield* GCP.Compute.ForwardingRule("Frontend", { |
| 100 | region, |
| 101 | target: pool.selfLink, |
| 102 | ipProtocol: "TCP", |
| 103 | portRange: "80", |
| 104 | labels: { env: "test" }, |
| 105 | }); |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | expect(created.forwardingRuleName).toEqual(expect.any(String)); |
| 110 | expect(created.region).toEqual(region); |
| 111 | expect(created.ipAddress).toEqual(expect.any(String)); |
| 112 | expect(created.ipProtocol).toEqual("TCP"); |
| 113 | expect(created.loadBalancingScheme).toEqual("EXTERNAL"); |
| 114 | expect(created.labels).toMatchObject({ env: "test" }); |
| 115 | expect(created.target).toEqual(expect.stringContaining(poolName)); |
| 116 | |
| 117 | const fetched = yield* compute.getForwardingRules({ |
| 118 | project: created.project, |
| 119 | region: created.region, |
| 120 | forwardingRule: created.forwardingRuleName, |
| 121 | }); |
| 122 | expect(fetched.name).toEqual(created.forwardingRuleName); |
| 123 | expect(fetched.IPAddress).toEqual(created.ipAddress); |
| 124 | expect(fetched.IPProtocol).toEqual("TCP"); |
| 125 | expect(fetched.labels?.env).toEqual("test"); |
| 126 | expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 127 | expect(fetched.target).toEqual(expect.stringContaining(poolName)); |
| 128 | |
| 129 | const updated = yield* stack.deploy( |
| 130 | Effect.gen(function* () { |
| 131 | return yield* GCP.Compute.ForwardingRule("Frontend", { |
| 132 | forwardingRuleName: created.forwardingRuleName, |
| 133 | region, |
| 134 | target: pool.selfLink, |
| 135 | ipProtocol: "TCP", |
| 136 | portRange: "80", |
| 137 | labels: { env: "prod", role: "frontend" }, |
| 138 | }); |
| 139 | }), |
| 140 | ); |
| 141 | |
| 142 | expect(updated.forwardingRuleName).toEqual(created.forwardingRuleName); |
| 143 | expect(updated.ipAddress).toEqual(created.ipAddress); |
| 144 | expect(updated.labels).toMatchObject({ env: "prod", role: "frontend" }); |
| 145 | |
| 146 | const fetchedUpdated = yield* compute.getForwardingRules({ |
| 147 | project: updated.project, |
| 148 | region: updated.region, |
| 149 | forwardingRule: updated.forwardingRuleName, |
| 150 | }); |
| 151 | expect(fetchedUpdated.labels?.env).toEqual("prod"); |
| 152 | expect(fetchedUpdated.labels?.role).toEqual("frontend"); |
| 153 | |
| 154 | yield* stack.destroy(); |
| 155 | |
| 156 | const gone = yield* waitUntilGone(created.forwardingRuleName); |
| 157 | expect(gone).toEqual("gone"); |
| 158 | }).pipe(logLevel, Effect.ensuring(deleteTargetPool().pipe(Effect.ignore))), |
| 159 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 }, |
| 160 | ); |