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 | import { withNetworkSlot } from "../networkQuota.ts"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const waitUntilGone = (project: string, region: string, subnetworkName: string) => |
| 15 | compute.getSubnetworks({ project, region, subnetwork: subnetworkName }).pipe( |
| 16 | Effect.as("found" as const), |
| 17 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 18 | Effect.repeat({ |
| 19 | schedule: Schedule.spaced("2 seconds"), |
| 20 | until: (status) => status === "gone", |
| 21 | times: 10, |
| 22 | }), |
| 23 | ); |
| 24 | |
| 25 | test.provider( |
| 26 | "create, update, and delete a subnetwork", |
| 27 | (stack) => |
| 28 | Effect.gen(function* () { |
| 29 | yield* stack.destroy(); |
| 30 | |
| 31 | const created = yield* stack.deploy( |
| 32 | Effect.gen(function* () { |
| 33 | const network = yield* GCP.Compute.Network("Vpc", { |
| 34 | autoCreateSubnetworks: false, |
| 35 | }); |
| 36 | const subnetwork = yield* GCP.Compute.Subnetwork("Subnet", { |
| 37 | network: network.networkName, |
| 38 | region: "us-central1", |
| 39 | ipCidrRange: "10.20.0.0/24", |
| 40 | description: "test subnet", |
| 41 | }); |
| 42 | return { network, subnetwork }; |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | expect(created.subnetwork.subnetworkName).toEqual(expect.any(String)); |
| 47 | expect(created.subnetwork.region).toEqual("us-central1"); |
| 48 | expect(created.subnetwork.ipCidrRange).toEqual("10.20.0.0/24"); |
| 49 | expect(created.subnetwork.privateIpGoogleAccess).toEqual(false); |
| 50 | expect(created.subnetwork.description).toEqual("test subnet"); |
| 51 | expect(created.subnetwork.secondaryIpRanges).toEqual([]); |
| 52 | |
| 53 | const fetched = yield* compute.getSubnetworks({ |
| 54 | project: created.subnetwork.project, |
| 55 | region: created.subnetwork.region, |
| 56 | subnetwork: created.subnetwork.subnetworkName, |
| 57 | }); |
| 58 | expect(fetched.name).toEqual(created.subnetwork.subnetworkName); |
| 59 | expect(fetched.ipCidrRange).toEqual("10.20.0.0/24"); |
| 60 | expect(fetched.privateIpGoogleAccess).toEqual(false); |
| 61 | expect(fetched.description).toContain("alchemy-id="); |
| 62 | expect(fetched.description).toContain("test subnet"); |
| 63 | |
| 64 | const updated = yield* stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | const network = yield* GCP.Compute.Network("Vpc", { |
| 67 | networkName: created.network.networkName, |
| 68 | autoCreateSubnetworks: false, |
| 69 | }); |
| 70 | const subnetwork = yield* GCP.Compute.Subnetwork("Subnet", { |
| 71 | subnetworkName: created.subnetwork.subnetworkName, |
| 72 | network: network.networkName, |
| 73 | region: "us-central1", |
| 74 | ipCidrRange: "10.20.0.0/24", |
| 75 | description: "test subnet", |
| 76 | privateIpGoogleAccess: true, |
| 77 | secondaryIpRanges: [{ rangeName: "pods", ipCidrRange: "10.20.1.0/24" }], |
| 78 | }); |
| 79 | return { network, subnetwork }; |
| 80 | }), |
| 81 | ); |
| 82 | |
| 83 | expect(updated.subnetwork.subnetworkName).toEqual(created.subnetwork.subnetworkName); |
| 84 | expect(updated.subnetwork.privateIpGoogleAccess).toEqual(true); |
| 85 | expect(updated.subnetwork.secondaryIpRanges).toEqual([ |
| 86 | { rangeName: "pods", ipCidrRange: "10.20.1.0/24" }, |
| 87 | ]); |
| 88 | |
| 89 | const fetchedUpdate = yield* compute.getSubnetworks({ |
| 90 | project: created.subnetwork.project, |
| 91 | region: created.subnetwork.region, |
| 92 | subnetwork: created.subnetwork.subnetworkName, |
| 93 | }); |
| 94 | expect(fetchedUpdate.privateIpGoogleAccess).toEqual(true); |
| 95 | expect( |
| 96 | (fetchedUpdate.secondaryIpRanges ?? []).map((range) => ({ |
| 97 | rangeName: range.rangeName, |
| 98 | ipCidrRange: range.ipCidrRange, |
| 99 | })), |
| 100 | ).toEqual([{ rangeName: "pods", ipCidrRange: "10.20.1.0/24" }]); |
| 101 | |
| 102 | yield* stack.destroy(); |
| 103 | |
| 104 | const gone = yield* waitUntilGone( |
| 105 | created.subnetwork.project, |
| 106 | created.subnetwork.region, |
| 107 | created.subnetwork.subnetworkName, |
| 108 | ); |
| 109 | expect(gone).toEqual("gone"); |
| 110 | }).pipe(logLevel, withNetworkSlot), |
| 111 | { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 360_000 }, |
| 112 | ); |