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 networkconnectivity from "@distilled.cloud/gcp/networkconnectivity_v1"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Result from "effect/Result"; |
| 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 | // Transports are Cross-Cloud Interconnect to a partner cloud: they need an |
| 17 | // allowlisted remote profile and a real peer account (the probe asserts the |
| 18 | // NotFound for a missing profile). Set GCP_TEST_TRANSPORT=1 on an entitled |
| 19 | // project. |
| 20 | const runLifecycle = !!process.env.GCP_TEST_TRANSPORT; |
| 21 | |
| 22 | const location = "us-east4"; |
| 23 | const remoteProfile = "aws-us-east-1"; |
| 24 | |
| 25 | const transportName = (project: string, id: string) => |
| 26 | `projects/${project}/locations/${location}/transports/${id}`; |
| 27 | |
| 28 | const waitUntilGone = (name: string) => |
| 29 | networkconnectivity.getProjectsLocationsTransports({ name }).pipe( |
| 30 | Effect.as("found" as const), |
| 31 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 32 | Effect.repeat({ |
| 33 | schedule: Schedule.spaced("2 seconds"), |
| 34 | until: (status) => status === "gone", |
| 35 | times: 10, |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | const probeCreate = (project: string, transportId: string, profileId: string) => |
| 40 | networkconnectivity |
| 41 | .createProjectsLocationsTransports({ |
| 42 | parent: `projects/${project}/locations/${location}`, |
| 43 | transportId, |
| 44 | body: { |
| 45 | network: `projects/${project}/global/networks/default`, |
| 46 | remoteProfile: `projects/${project}/locations/${location}/remoteTransportProfiles/${profileId}`, |
| 47 | bandwidth: "BPS_1G", |
| 48 | remoteAccountId: "123456789012", |
| 49 | description: "alchemy transport probe", |
| 50 | }, |
| 51 | }) |
| 52 | .pipe(Effect.result); |
| 53 | |
| 54 | test.provider( |
| 55 | "getProjectsLocationsTransports on a missing transport fails with a typed tag", |
| 56 | (stack) => |
| 57 | Effect.gen(function* () { |
| 58 | const { project } = yield* GcpEnvironment.current; |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const error = yield* Effect.flip( |
| 62 | networkconnectivity.getProjectsLocationsTransports({ |
| 63 | name: transportName(project, "alchemy-tp-missing"), |
| 64 | }), |
| 65 | ); |
| 66 | expect(error._tag).toEqual("NotFound"); |
| 67 | |
| 68 | const page = yield* networkconnectivity.listProjectsLocationsTransports({ |
| 69 | parent: `projects/${project}/locations/${location}`, |
| 70 | pageSize: 10, |
| 71 | }); |
| 72 | expect((page.transports ?? []).map((transport) => transport.name)).not.toContain( |
| 73 | transportName(project, "alchemy-tp-missing"), |
| 74 | ); |
| 75 | |
| 76 | yield* stack.destroy(); |
| 77 | }).pipe(logLevel), |
| 78 | { |
| 79 | tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"], |
| 80 | timeout: 90_000, |
| 81 | }, |
| 82 | ); |
| 83 | |
| 84 | test.provider( |
| 85 | "createProjectsLocationsTransports with a missing remote profile fails with NotFound", |
| 86 | (stack) => |
| 87 | Effect.gen(function* () { |
| 88 | const { project } = yield* GcpEnvironment.current; |
| 89 | yield* stack.destroy(); |
| 90 | |
| 91 | const result = yield* probeCreate(project, "alchemy-tp-probe", "aws-profile-missing"); |
| 92 | expect(Result.isFailure(result)).toBe(true); |
| 93 | if (Result.isFailure(result)) { |
| 94 | expect(result.failure._tag).toBe("NotFound"); |
| 95 | expect(result.failure.message ?? "").toContain( |
| 96 | "remoteTransportProfiles/aws-profile-missing", |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | yield* stack.destroy(); |
| 101 | }).pipe(logLevel), |
| 102 | { |
| 103 | tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"], |
| 104 | timeout: 90_000, |
| 105 | }, |
| 106 | ); |
| 107 | |
| 108 | test.provider.skipIf(!runLifecycle)( |
| 109 | "create, update, and delete a transport", |
| 110 | (stack) => |
| 111 | Effect.gen(function* () { |
| 112 | yield* stack.destroy(); |
| 113 | |
| 114 | const created = yield* stack.deploy( |
| 115 | Effect.gen(function* () { |
| 116 | const network = yield* GCP.Compute.Network("TransportVpc", { |
| 117 | autoCreateSubnetworks: false, |
| 118 | }); |
| 119 | const transport = yield* GCP.NetworkConnectivity.Transport("Aws", { |
| 120 | location, |
| 121 | network: network.selfLink.as<string>(), |
| 122 | remoteProfile, |
| 123 | bandwidth: "BPS_1G", |
| 124 | remoteAccountId: "123456789012", |
| 125 | advertisedRoutes: ["10.0.0.0/8"], |
| 126 | description: "transport a", |
| 127 | labels: { env: "test" }, |
| 128 | }); |
| 129 | return { network, transport }; |
| 130 | }), |
| 131 | ); |
| 132 | |
| 133 | expect(created.transport.name).toContain("/transports/"); |
| 134 | expect(created.transport.name).toContain(`/locations/${location}/`); |
| 135 | expect(created.transport.transportId).toEqual(expect.any(String)); |
| 136 | expect(created.transport.location).toEqual(location); |
| 137 | expect(created.transport.networkName).toEqual(created.network.networkName); |
| 138 | expect(created.transport.remoteProfileId).toEqual(remoteProfile); |
| 139 | expect(created.transport.bandwidth).toEqual("BPS_1G"); |
| 140 | expect(created.transport.description).toEqual("transport a"); |
| 141 | expect(created.transport.labels).toMatchObject({ env: "test" }); |
| 142 | expect(created.transport.createTime).toEqual(expect.any(String)); |
| 143 | expect(["PENDING_KEY", "PENDING_CONFIG", "ACTIVE", "CREATING"]).toContain( |
| 144 | created.transport.state, |
| 145 | ); |
| 146 | |
| 147 | const fetched = yield* networkconnectivity.getProjectsLocationsTransports({ |
| 148 | name: created.transport.name, |
| 149 | }); |
| 150 | expect(fetched.name).toEqual(created.transport.name); |
| 151 | expect(fetched.labels?.env).toEqual("test"); |
| 152 | expect(fetched.description).toEqual("transport a"); |
| 153 | expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual( |
| 154 | true, |
| 155 | ); |
| 156 | |
| 157 | const updated = yield* stack.deploy( |
| 158 | Effect.gen(function* () { |
| 159 | const network = yield* GCP.Compute.Network("TransportVpc", { |
| 160 | networkName: created.network.networkName, |
| 161 | autoCreateSubnetworks: false, |
| 162 | }); |
| 163 | const transport = yield* GCP.NetworkConnectivity.Transport("Aws", { |
| 164 | transportId: created.transport.transportId, |
| 165 | location, |
| 166 | network: network.selfLink.as<string>(), |
| 167 | remoteProfile, |
| 168 | bandwidth: "BPS_1G", |
| 169 | remoteAccountId: "123456789012", |
| 170 | advertisedRoutes: ["10.0.0.0/8"], |
| 171 | description: "transport b", |
| 172 | labels: { env: "prod", role: "cci" }, |
| 173 | }); |
| 174 | return { network, transport }; |
| 175 | }), |
| 176 | ); |
| 177 | |
| 178 | expect(updated.transport.name).toEqual(created.transport.name); |
| 179 | expect(updated.transport.transportId).toEqual(created.transport.transportId); |
| 180 | expect(updated.transport.description).toEqual("transport b"); |
| 181 | expect(updated.transport.labels).toMatchObject({ |
| 182 | env: "prod", |
| 183 | role: "cci", |
| 184 | }); |
| 185 | |
| 186 | const refetched = yield* networkconnectivity.getProjectsLocationsTransports({ |
| 187 | name: created.transport.name, |
| 188 | }); |
| 189 | expect(refetched.description).toEqual("transport b"); |
| 190 | expect(refetched.labels?.env).toEqual("prod"); |
| 191 | expect(refetched.labels?.role).toEqual("cci"); |
| 192 | |
| 193 | yield* stack.destroy(); |
| 194 | |
| 195 | const gone = yield* waitUntilGone(created.transport.name); |
| 196 | expect(gone).toEqual("gone"); |
| 197 | }).pipe(logLevel, withNetworkSlot), |
| 198 | { |
| 199 | tags: ["provider:gcp", "provider:gcp:networkconnectivity", "live"], |
| 200 | timeout: 120_000, |
| 201 | }, |
| 202 | ); |