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 { fromCredentials, GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import { Credentials } from "@distilled.cloud/gcp/Credentials"; |
| 6 | import * as Region from "@distilled.cloud/gcp/Region"; |
| 7 | import * as secretmanager from "@distilled.cloud/gcp/secretmanager_v1"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Redacted from "effect/Redacted"; |
| 11 | import { MinimumLogLevel } from "effect/References"; |
| 12 | |
| 13 | const { test } = Test.make({ |
| 14 | providers: GCP.providers().pipe(Layer.provideMerge(GCP.Region("us-east4"))), |
| 15 | }); |
| 16 | |
| 17 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 18 | |
| 19 | test( |
| 20 | "regional endpoint routing", |
| 21 | Effect.sync(() => { |
| 22 | const base = "https://secretmanager.googleapis.com/"; |
| 23 | expect( |
| 24 | Region.endpointFor(base, "v1/projects/p/locations/us-east1/secrets/s", "required"), |
| 25 | ).toEqual("https://secretmanager.us-east1.rep.googleapis.com/"); |
| 26 | expect(Region.endpointFor(base, "v1/projects/p/secrets/s", "required")).toEqual(base); |
| 27 | expect( |
| 28 | Region.endpointFor( |
| 29 | "https://run.googleapis.com/", |
| 30 | "v2/projects/p/locations/us-east1/services/s", |
| 31 | "required", |
| 32 | ), |
| 33 | ).toEqual("https://run.googleapis.com/"); |
| 34 | expect( |
| 35 | Region.endpointFor( |
| 36 | "https://run.googleapis.com/", |
| 37 | "v2/projects/p/locations/us-east1/services/s", |
| 38 | "prefer", |
| 39 | ), |
| 40 | ).toEqual("https://run.us-east1.rep.googleapis.com/"); |
| 41 | }), |
| 42 | { tags: ["unit", "provider:gcp", "provider:gcp:region", "local"] }, |
| 43 | ); |
| 44 | |
| 45 | const credentialWithRegion = (region: string | undefined) => |
| 46 | Layer.succeed( |
| 47 | Credentials, |
| 48 | Effect.succeed({ |
| 49 | accessToken: Redacted.make("token"), |
| 50 | project: "p", |
| 51 | region, |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | const resolvedRegion = (layer: Layer.Layer<GcpEnvironment>) => |
| 56 | GcpEnvironment.use((env) => Effect.map(env, (e) => e.region)).pipe(Effect.provide(layer)); |
| 57 | |
| 58 | test( |
| 59 | "the credential's region is the default; GCP.Region overrides it", |
| 60 | Effect.gen(function* () { |
| 61 | expect( |
| 62 | yield* resolvedRegion( |
| 63 | fromCredentials().pipe(Layer.provide(credentialWithRegion("asia-east1"))), |
| 64 | ), |
| 65 | ).toEqual("asia-east1"); |
| 66 | expect( |
| 67 | yield* resolvedRegion(fromCredentials().pipe(Layer.provide(credentialWithRegion(undefined)))), |
| 68 | ).toEqual("us-central1"); |
| 69 | expect( |
| 70 | yield* resolvedRegion( |
| 71 | fromCredentials().pipe( |
| 72 | Layer.provide(credentialWithRegion("asia-east1")), |
| 73 | Layer.provide(GCP.Region("europe-west1")), |
| 74 | ), |
| 75 | ), |
| 76 | ).toEqual("europe-west1"); |
| 77 | }), |
| 78 | { tags: ["unit", "provider:gcp", "provider:gcp:region", "local"] }, |
| 79 | ); |
| 80 | |
| 81 | test.provider( |
| 82 | "a stack-level GCP.Region sets the default region; regional secrets reach their endpoint", |
| 83 | (stack) => |
| 84 | Effect.gen(function* () { |
| 85 | yield* stack.destroy(); |
| 86 | |
| 87 | const out = yield* stack.deploy( |
| 88 | Effect.gen(function* () { |
| 89 | // No `location`: placed in the scope's default region. |
| 90 | const queue = yield* GCP.CloudTasks.Queue("RegionQueue", {}); |
| 91 | // A regional secret, also in the default region. |
| 92 | const secret = yield* GCP.SecretManager.LocationsSecret("RegionalSecret", {}); |
| 93 | return { queue: queue.name, secret: secret.name }; |
| 94 | }), |
| 95 | ); |
| 96 | |
| 97 | expect(out.queue).toContain("/locations/us-east4/"); |
| 98 | expect(out.secret).toContain("/locations/us-east4/"); |
| 99 | |
| 100 | // Out-of-band read with plain fetch: distilled routes the regional |
| 101 | // secret to secretmanager.us-east4.rep.googleapis.com. |
| 102 | const live = yield* secretmanager.getProjectsLocationsSecrets({ |
| 103 | name: out.secret, |
| 104 | }); |
| 105 | expect(live.name).toEqual(out.secret); |
| 106 | |
| 107 | yield* stack.destroy(); |
| 108 | }).pipe(logLevel), |
| 109 | { |
| 110 | tags: ["provider:gcp", "provider:gcp:cloudtasks", "provider:gcp:secretmanager", "live"], |
| 111 | timeout: 240_000, |
| 112 | }, |
| 113 | ); |
| 114 | |
| 115 | const withoutOverride = Test.make({ providers: GCP.providers() }); |
| 116 | |
| 117 | withoutOverride.test.provider( |
| 118 | "without GCP.Region, the credential's region is the default", |
| 119 | (stack) => |
| 120 | Effect.gen(function* () { |
| 121 | yield* stack.destroy(); |
| 122 | const out = yield* stack.deploy( |
| 123 | Effect.gen(function* () { |
| 124 | const queue = yield* GCP.CloudTasks.Queue("CredentialsRegionQueue", {}); |
| 125 | return { queue: queue.name, region: yield* GCP.currentRegion }; |
| 126 | }), |
| 127 | ); |
| 128 | // The testing credential sets no region, so the default applies. |
| 129 | expect(out.region).toEqual("us-central1"); |
| 130 | expect(out.queue).toContain("/locations/us-central1/"); |
| 131 | yield* stack.destroy(); |
| 132 | }).pipe(logLevel), |
| 133 | { |
| 134 | tags: ["provider:gcp", "provider:gcp:cloudtasks", "live"], |
| 135 | timeout: 120_000, |
| 136 | }, |
| 137 | ); |