The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 6 | import * as servicenetworking from "@distilled.cloud/gcp/servicenetworking_v1"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { withNetworkSlot } from "../networkQuota.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: GCP.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | const parent = "services/servicenetworking.googleapis.com"; |
| 17 | |
| 18 | const waitUntilGone = (consumerNetwork: string) => |
| 19 | servicenetworking |
| 20 | .listServicesConnections({ |
| 21 | parent, |
| 22 | network: consumerNetwork, |
| 23 | }) |
| 24 | .pipe( |
| 25 | Effect.map((page) => |
| 26 | (page.connections ?? []).length === 0 ? ("gone" as const) : ("found" as const), |
| 27 | ), |
| 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: 15, |
| 33 | }), |
| 34 | ); |
| 35 | |
| 36 | test.provider( |
| 37 | "listServicesConnections on a missing network returns no connections", |
| 38 | (stack) => |
| 39 | Effect.gen(function* () { |
| 40 | const { project } = yield* GcpEnvironment.current; |
| 41 | |
| 42 | yield* stack.destroy(); |
| 43 | |
| 44 | const resource = yield* resourcemanager.getProjects({ |
| 45 | name: `projects/${project}`, |
| 46 | }); |
| 47 | const projectNumber = resource.name?.split("/").pop() ?? project; |
| 48 | const page = yield* servicenetworking.listServicesConnections({ |
| 49 | parent, |
| 50 | network: `projects/${projectNumber}/global/networks/alchemy-sn-missing`, |
| 51 | }); |
| 52 | expect(page.connections ?? []).toEqual([]); |
| 53 | |
| 54 | yield* stack.destroy(); |
| 55 | }).pipe(logLevel), |
| 56 | { |
| 57 | tags: ["provider:gcp", "provider:gcp:servicenetworking", "live"], |
| 58 | timeout: 90_000, |
| 59 | }, |
| 60 | ); |
| 61 | |
| 62 | test.provider.skipIf(!!process.env.FAST)( |
| 63 | "create, update, and delete a service networking connection", |
| 64 | (stack) => |
| 65 | Effect.gen(function* () { |
| 66 | const { project } = yield* GcpEnvironment.current; |
| 67 | |
| 68 | yield* stack.destroy(); |
| 69 | |
| 70 | const created = yield* stack.deploy( |
| 71 | Effect.gen(function* () { |
| 72 | const network = yield* GCP.Compute.Network("Vpc", { |
| 73 | autoCreateSubnetworks: false, |
| 74 | }); |
| 75 | const range = yield* GCP.Compute.GlobalAddress("PsaRange", { |
| 76 | addressType: "INTERNAL", |
| 77 | purpose: "VPC_PEERING", |
| 78 | network: network.selfLink, |
| 79 | prefixLength: 24, |
| 80 | }); |
| 81 | const extra = yield* GCP.Compute.GlobalAddress("PsaExtra", { |
| 82 | addressType: "INTERNAL", |
| 83 | purpose: "VPC_PEERING", |
| 84 | network: network.selfLink, |
| 85 | prefixLength: 24, |
| 86 | }); |
| 87 | const connection = yield* GCP.ServiceNetworking.Connection("Psa", { |
| 88 | network: network.networkName, |
| 89 | reservedPeeringRanges: [range.addressName], |
| 90 | }); |
| 91 | return { network, range, extra, connection }; |
| 92 | }), |
| 93 | ); |
| 94 | |
| 95 | expect(created.connection.networkName).toEqual(created.network.networkName); |
| 96 | expect(created.connection.service).toEqual(parent); |
| 97 | expect(created.connection.peering).toEqual("servicenetworking-googleapis-com"); |
| 98 | expect(created.connection.reservedPeeringRanges).toEqual([created.range.addressName]); |
| 99 | expect(created.connection.project).toEqual(project); |
| 100 | expect(created.connection.projectNumber).toEqual(expect.any(String)); |
| 101 | |
| 102 | const listed = yield* servicenetworking.listServicesConnections({ |
| 103 | parent, |
| 104 | network: created.connection.network, |
| 105 | }); |
| 106 | const fetched = (listed.connections ?? []).find( |
| 107 | (connection) => (connection.network ?? "") === created.connection.network, |
| 108 | ); |
| 109 | expect(fetched?.peering).toEqual(created.connection.peering); |
| 110 | expect(fetched?.reservedPeeringRanges).toEqual([created.range.addressName]); |
| 111 | |
| 112 | const updated = yield* stack.deploy( |
| 113 | Effect.gen(function* () { |
| 114 | const network = yield* GCP.Compute.Network("Vpc", { |
| 115 | networkName: created.network.networkName, |
| 116 | autoCreateSubnetworks: false, |
| 117 | }); |
| 118 | const range = yield* GCP.Compute.GlobalAddress("PsaRange", { |
| 119 | addressName: created.range.addressName, |
| 120 | addressType: "INTERNAL", |
| 121 | purpose: "VPC_PEERING", |
| 122 | network: network.selfLink, |
| 123 | prefixLength: 24, |
| 124 | }); |
| 125 | const extra = yield* GCP.Compute.GlobalAddress("PsaExtra", { |
| 126 | addressName: created.extra.addressName, |
| 127 | addressType: "INTERNAL", |
| 128 | purpose: "VPC_PEERING", |
| 129 | network: network.selfLink, |
| 130 | prefixLength: 24, |
| 131 | }); |
| 132 | const connection = yield* GCP.ServiceNetworking.Connection("Psa", { |
| 133 | network: network.networkName, |
| 134 | reservedPeeringRanges: [range.addressName, extra.addressName], |
| 135 | }); |
| 136 | return { network, range, extra, connection }; |
| 137 | }), |
| 138 | ); |
| 139 | |
| 140 | expect(updated.connection.network).toEqual(created.connection.network); |
| 141 | expect(updated.connection.peering).toEqual(created.connection.peering); |
| 142 | expect([...updated.connection.reservedPeeringRanges].sort()).toEqual( |
| 143 | [created.range.addressName, created.extra.addressName].sort(), |
| 144 | ); |
| 145 | |
| 146 | const listedUpdated = yield* servicenetworking.listServicesConnections({ |
| 147 | parent, |
| 148 | network: updated.connection.network, |
| 149 | }); |
| 150 | const fetchedUpdated = (listedUpdated.connections ?? [])[0]; |
| 151 | expect([...(fetchedUpdated?.reservedPeeringRanges ?? [])].sort()).toEqual( |
| 152 | [created.range.addressName, created.extra.addressName].sort(), |
| 153 | ); |
| 154 | |
| 155 | yield* stack.destroy(); |
| 156 | |
| 157 | const gone = yield* waitUntilGone(created.connection.network); |
| 158 | expect(gone).toEqual("gone"); |
| 159 | }).pipe(logLevel, withNetworkSlot), |
| 160 | { |
| 161 | tags: ["provider:gcp", "provider:gcp:servicenetworking", "live"], |
| 162 | timeout: 600_000, |
| 163 | }, |
| 164 | ); |