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, routerName: string) => |
| 14 | compute.getRouters({ project, region, router: routerName }).pipe( |
| 15 | Effect.as("found" as const), |
| 16 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 17 | Effect.repeat({ |
| 18 | schedule: Schedule.spaced("1 second"), |
| 19 | until: (status) => status === "gone", |
| 20 | times: 20, |
| 21 | }), |
| 22 | ); |
| 23 | |
| 24 | test.provider( |
| 25 | "create, update, replace, and delete a router", |
| 26 | (stack) => |
| 27 | Effect.gen(function* () { |
| 28 | yield* stack.destroy(); |
| 29 | |
| 30 | const created = yield* stack.deploy( |
| 31 | Effect.gen(function* () { |
| 32 | return yield* GCP.Compute.Router("Edge", { |
| 33 | network: "default", |
| 34 | region: "us-central1", |
| 35 | description: "test router", |
| 36 | bgp: { asn: 65001, advertiseMode: "DEFAULT" }, |
| 37 | }); |
| 38 | }), |
| 39 | ); |
| 40 | |
| 41 | expect(created.routerName).toEqual(expect.any(String)); |
| 42 | expect(created.region).toEqual("us-central1"); |
| 43 | expect(created.description).toEqual("test router"); |
| 44 | expect(created.encryptedInterconnectRouter).toEqual(false); |
| 45 | expect(created.bgp?.asn).toEqual(65001); |
| 46 | expect(created.bgp?.advertiseMode).toEqual("DEFAULT"); |
| 47 | expect(created.network).toEqual(expect.stringContaining("networks/default")); |
| 48 | |
| 49 | const fetched = yield* compute.getRouters({ |
| 50 | project: created.project, |
| 51 | region: created.region, |
| 52 | router: created.routerName, |
| 53 | }); |
| 54 | expect(fetched.name).toEqual(created.routerName); |
| 55 | expect(fetched.description).toContain("alchemy-id="); |
| 56 | expect(fetched.description).toContain("test router"); |
| 57 | expect(fetched.bgp?.asn).toEqual(65001); |
| 58 | expect(fetched.bgp?.advertiseMode).toEqual("DEFAULT"); |
| 59 | |
| 60 | const updated = yield* stack.deploy( |
| 61 | Effect.gen(function* () { |
| 62 | return yield* GCP.Compute.Router("Edge", { |
| 63 | routerName: created.routerName, |
| 64 | network: "default", |
| 65 | region: "us-central1", |
| 66 | description: "updated router", |
| 67 | bgp: { |
| 68 | asn: 65001, |
| 69 | advertiseMode: "CUSTOM", |
| 70 | advertisedGroups: ["ALL_SUBNETS"], |
| 71 | advertisedIpRanges: [{ range: "10.0.0.0/8", description: "rfc1918" }], |
| 72 | keepaliveInterval: 30, |
| 73 | }, |
| 74 | }); |
| 75 | }), |
| 76 | ); |
| 77 | |
| 78 | expect(updated.routerName).toEqual(created.routerName); |
| 79 | expect(updated.routerId).toEqual(created.routerId); |
| 80 | expect(updated.description).toEqual("updated router"); |
| 81 | expect(updated.bgp?.advertiseMode).toEqual("CUSTOM"); |
| 82 | expect(updated.bgp?.advertisedGroups).toEqual(["ALL_SUBNETS"]); |
| 83 | expect(updated.bgp?.keepaliveInterval).toEqual(30); |
| 84 | expect(updated.bgp?.advertisedIpRanges).toEqual([ |
| 85 | { range: "10.0.0.0/8", description: "rfc1918" }, |
| 86 | ]); |
| 87 | |
| 88 | const fetchedUpdate = yield* compute.getRouters({ |
| 89 | project: updated.project, |
| 90 | region: updated.region, |
| 91 | router: updated.routerName, |
| 92 | }); |
| 93 | expect(fetchedUpdate.description).toContain("updated router"); |
| 94 | expect(fetchedUpdate.bgp?.advertiseMode).toEqual("CUSTOM"); |
| 95 | expect(fetchedUpdate.bgp?.advertisedGroups).toEqual(["ALL_SUBNETS"]); |
| 96 | expect(fetchedUpdate.bgp?.keepaliveInterval).toEqual(30); |
| 97 | |
| 98 | const replaced = yield* stack.deploy( |
| 99 | Effect.gen(function* () { |
| 100 | return yield* GCP.Compute.Router("Edge", { |
| 101 | routerName: created.routerName, |
| 102 | network: "default", |
| 103 | region: "us-central1", |
| 104 | description: "updated router", |
| 105 | encryptedInterconnectRouter: true, |
| 106 | bgp: { |
| 107 | asn: 65001, |
| 108 | advertiseMode: "CUSTOM", |
| 109 | advertisedGroups: ["ALL_SUBNETS"], |
| 110 | advertisedIpRanges: [{ range: "10.0.0.0/8", description: "rfc1918" }], |
| 111 | keepaliveInterval: 30, |
| 112 | }, |
| 113 | }); |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | expect(replaced.routerName).toEqual(created.routerName); |
| 118 | expect(replaced.encryptedInterconnectRouter).toEqual(true); |
| 119 | expect(replaced.routerId).not.toEqual(created.routerId); |
| 120 | |
| 121 | const fetchedReplace = yield* compute.getRouters({ |
| 122 | project: replaced.project, |
| 123 | region: replaced.region, |
| 124 | router: replaced.routerName, |
| 125 | }); |
| 126 | expect(fetchedReplace.encryptedInterconnectRouter).toEqual(true); |
| 127 | |
| 128 | yield* stack.destroy(); |
| 129 | |
| 130 | const gone = yield* waitUntilGone(created.project, created.region, created.routerName); |
| 131 | expect(gone).toEqual("gone"); |
| 132 | }).pipe(logLevel), |
| 133 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 180_000 }, |
| 134 | ); |
| 135 | |
| 136 | test.provider( |
| 137 | "manages Cloud NAT timeouts, address tier, and endpoint types in place", |
| 138 | (stack) => |
| 139 | Effect.gen(function* () { |
| 140 | yield* stack.destroy(); |
| 141 | |
| 142 | const natRouter = (tcpTimeWaitTimeoutSec: number, autoNetworkTier: "PREMIUM" | "STANDARD") => |
| 143 | GCP.Compute.Router("NatEdge", { |
| 144 | network: "default", |
| 145 | region: "us-central1", |
| 146 | nats: [ |
| 147 | { |
| 148 | name: "egress", |
| 149 | sourceSubnetworkIpRangesToNat: "ALL_SUBNETWORKS_ALL_IP_RANGES", |
| 150 | natIpAllocateOption: "AUTO_ONLY", |
| 151 | tcpTimeWaitTimeoutSec, |
| 152 | autoNetworkTier, |
| 153 | endpointTypes: ["ENDPOINT_TYPE_VM"], |
| 154 | }, |
| 155 | ], |
| 156 | }); |
| 157 | |
| 158 | const created = yield* stack.deploy(natRouter(90, "PREMIUM")); |
| 159 | expect(created.nats).toHaveLength(1); |
| 160 | expect(created.nats[0]).toMatchObject({ |
| 161 | name: "egress", |
| 162 | tcpTimeWaitTimeoutSec: 90, |
| 163 | autoNetworkTier: "PREMIUM", |
| 164 | endpointTypes: ["ENDPOINT_TYPE_VM"], |
| 165 | }); |
| 166 | |
| 167 | const updated = yield* stack.deploy(natRouter(60, "STANDARD")); |
| 168 | expect(updated.routerId).toEqual(created.routerId); |
| 169 | expect(updated.nats[0]).toMatchObject({ |
| 170 | tcpTimeWaitTimeoutSec: 60, |
| 171 | autoNetworkTier: "STANDARD", |
| 172 | }); |
| 173 | |
| 174 | const fetched = yield* compute.getRouters({ |
| 175 | project: updated.project, |
| 176 | region: updated.region, |
| 177 | router: updated.routerName, |
| 178 | }); |
| 179 | expect(fetched.nats?.[0]).toMatchObject({ |
| 180 | name: "egress", |
| 181 | tcpTimeWaitTimeoutSec: 60, |
| 182 | autoNetworkTier: "STANDARD", |
| 183 | endpointTypes: ["ENDPOINT_TYPE_VM"], |
| 184 | }); |
| 185 | |
| 186 | yield* stack.destroy(); |
| 187 | |
| 188 | const gone = yield* waitUntilGone(created.project, created.region, created.routerName); |
| 189 | expect(gone).toEqual("gone"); |
| 190 | }).pipe(logLevel), |
| 191 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 180_000 }, |
| 192 | ); |