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 managedidentities from "@distilled.cloud/gcp/managedidentities_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 | // Domain peering is cross-project only. Same-project create returns |
| 15 | // `BadRequest` ("DomainPeering should be used when network and domain |
| 16 | // resource belong to different projects"). Full lifecycle needs a |
| 17 | // domain in another project via GCP_TEST_MANAGEDIDENTITIES_DOMAIN. |
| 18 | const runLifecycle = |
| 19 | !process.env.FAST && |
| 20 | process.env.GCP_TEST_MANAGEDIDENTITIES === "1" && |
| 21 | !!process.env.GCP_TEST_MANAGEDIDENTITIES_DOMAIN; |
| 22 | const foreignDomain = process.env.GCP_TEST_MANAGEDIDENTITIES_DOMAIN ?? ""; |
| 23 | |
| 24 | const waitUntilGone = (name: string) => |
| 25 | managedidentities.getProjectsLocationsGlobalPeerings({ name }).pipe( |
| 26 | Effect.as("found" as const), |
| 27 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 28 | Effect.repeat({ |
| 29 | schedule: Schedule.spaced("2 seconds"), |
| 30 | until: (status) => status === "gone", |
| 31 | times: 10, |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | test.provider( |
| 36 | "getProjectsLocationsGlobalPeerings on a missing peering fails with a typed tag", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | const { project } = yield* GcpEnvironment.current; |
| 40 | yield* stack.destroy(); |
| 41 | |
| 42 | const error = yield* Effect.flip( |
| 43 | managedidentities.getProjectsLocationsGlobalPeerings({ |
| 44 | name: `projects/${project}/locations/global/peerings/alchemy-peering-missing`, |
| 45 | }), |
| 46 | ); |
| 47 | expect(error._tag).toEqual("NotFound"); |
| 48 | |
| 49 | yield* stack.destroy(); |
| 50 | }).pipe(logLevel), |
| 51 | { |
| 52 | tags: ["provider:gcp", "provider:gcp:managedidentities", "live"], |
| 53 | timeout: 90_000, |
| 54 | }, |
| 55 | ); |
| 56 | |
| 57 | test.provider.skipIf(runLifecycle)( |
| 58 | "a same-project peering is rejected with BadRequest", |
| 59 | (stack) => |
| 60 | Effect.gen(function* () { |
| 61 | const { project } = yield* GcpEnvironment.current; |
| 62 | const missingDomain = `projects/${project}/locations/global/domains/missing.alch.test`; |
| 63 | yield* stack.destroy(); |
| 64 | |
| 65 | const error = yield* Effect.flip( |
| 66 | stack.deploy( |
| 67 | Effect.gen(function* () { |
| 68 | return yield* GCP.ManagedIdentities.Peering("Spoke", { |
| 69 | domainResource: missingDomain, |
| 70 | authorizedNetwork: "default", |
| 71 | labels: { env: "test" }, |
| 72 | }); |
| 73 | }), |
| 74 | ), |
| 75 | ); |
| 76 | expect(error._tag).toEqual("BadRequest"); |
| 77 | |
| 78 | yield* stack.destroy(); |
| 79 | }).pipe(logLevel), |
| 80 | { |
| 81 | tags: ["provider:gcp", "provider:gcp:managedidentities", "live"], |
| 82 | timeout: 90_000, |
| 83 | }, |
| 84 | ); |
| 85 | |
| 86 | test.provider.skipIf(!runLifecycle)( |
| 87 | "create, update, and delete a domain peering", |
| 88 | (stack) => |
| 89 | Effect.gen(function* () { |
| 90 | yield* stack.destroy(); |
| 91 | |
| 92 | const created = yield* stack.deploy( |
| 93 | Effect.gen(function* () { |
| 94 | return yield* GCP.ManagedIdentities.Peering("Spoke", { |
| 95 | domainResource: foreignDomain, |
| 96 | authorizedNetwork: "default", |
| 97 | labels: { env: "test" }, |
| 98 | }); |
| 99 | }), |
| 100 | ); |
| 101 | |
| 102 | expect(created.name).toContain("/peerings/"); |
| 103 | expect(created.peeringId).toEqual(expect.any(String)); |
| 104 | expect(created.domainResource).toEqual(foreignDomain); |
| 105 | expect(created.labels).toMatchObject({ env: "test" }); |
| 106 | |
| 107 | const fetched = yield* managedidentities.getProjectsLocationsGlobalPeerings({ |
| 108 | name: created.name, |
| 109 | }); |
| 110 | expect(fetched.name).toEqual(created.name); |
| 111 | expect(fetched.labels?.env).toEqual("test"); |
| 112 | |
| 113 | const updated = yield* stack.deploy( |
| 114 | Effect.gen(function* () { |
| 115 | return yield* GCP.ManagedIdentities.Peering("Spoke", { |
| 116 | peeringId: created.peeringId, |
| 117 | domainResource: foreignDomain, |
| 118 | authorizedNetwork: "default", |
| 119 | labels: { env: "prod", role: "peering" }, |
| 120 | }); |
| 121 | }), |
| 122 | ); |
| 123 | |
| 124 | expect(updated.name).toEqual(created.name); |
| 125 | expect(updated.labels).toMatchObject({ |
| 126 | env: "prod", |
| 127 | role: "peering", |
| 128 | }); |
| 129 | |
| 130 | const refetched = yield* managedidentities.getProjectsLocationsGlobalPeerings({ |
| 131 | name: created.name, |
| 132 | }); |
| 133 | expect(refetched.labels?.env).toEqual("prod"); |
| 134 | expect(refetched.labels?.role).toEqual("peering"); |
| 135 | |
| 136 | yield* stack.destroy(); |
| 137 | |
| 138 | const gone = yield* waitUntilGone(created.name); |
| 139 | expect(gone).toEqual("gone"); |
| 140 | }).pipe(logLevel), |
| 141 | { |
| 142 | tags: ["provider:gcp", "provider:gcp:managedidentities", "live"], |
| 143 | timeout: 240_000, |
| 144 | }, |
| 145 | ); |