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 { 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 agentidentity from "@distilled.cloud/gcp/agentidentity_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 | const location = "us-central1"; |
| 15 | |
| 16 | // The Agent Identity API is not enabled in the testing project. Set |
| 17 | // GCP_TEST_AGENT_IDENTITY=1 on a project where it is. |
| 18 | const entitled = !!process.env.GCP_TEST_AGENT_IDENTITY; |
| 19 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | agentidentity.getProjectsLocationsAuthProviders({ name }).pipe( |
| 22 | Effect.map((item) => (item.deleted === true ? ("gone" as const) : ("found" as const))), |
| 23 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 24 | Effect.repeat({ |
| 25 | schedule: Schedule.spaced("1 second"), |
| 26 | until: (status) => status === "gone", |
| 27 | times: 10, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | test.provider.skipIf(!entitled)( |
| 32 | "getProjectsLocationsAuthProviders on a missing provider fails with a typed tag", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | const { project } = yield* GcpEnvironment.current; |
| 36 | yield* stack.destroy(); |
| 37 | |
| 38 | const error = yield* Effect.flip( |
| 39 | agentidentity.getProjectsLocationsAuthProviders({ |
| 40 | name: `projects/${project}/locations/${location}/authProviders/alchemy-missing-auth`, |
| 41 | }), |
| 42 | ); |
| 43 | expect(error._tag).toEqual("NotFound"); |
| 44 | |
| 45 | yield* stack.destroy(); |
| 46 | }).pipe(logLevel), |
| 47 | { |
| 48 | tags: ["provider:gcp", "provider:gcp:agentidentity", "live"], |
| 49 | timeout: 90_000, |
| 50 | }, |
| 51 | ); |
| 52 | |
| 53 | test.provider.skipIf(entitled)( |
| 54 | "getProjectsLocationsAuthProviders fails with ServiceDisabled while the API is disabled", |
| 55 | (stack) => |
| 56 | Effect.gen(function* () { |
| 57 | const { project } = yield* GcpEnvironment.current; |
| 58 | yield* stack.destroy(); |
| 59 | |
| 60 | const error = yield* Effect.flip( |
| 61 | agentidentity.getProjectsLocationsAuthProviders({ |
| 62 | name: `projects/${project}/locations/${location}/authProviders/alchemy-missing-auth`, |
| 63 | }), |
| 64 | ); |
| 65 | expect(["ServiceDisabled", "NotFound"]).toContain(error._tag); |
| 66 | |
| 67 | yield* stack.destroy(); |
| 68 | }).pipe(logLevel), |
| 69 | { |
| 70 | tags: ["provider:gcp", "provider:gcp:agentidentity", "live"], |
| 71 | timeout: 90_000, |
| 72 | }, |
| 73 | ); |
| 74 | |
| 75 | test.provider.skipIf(!entitled)( |
| 76 | "create, update, and delete an Agent Identity auth provider", |
| 77 | (stack) => |
| 78 | Effect.gen(function* () { |
| 79 | yield* stack.destroy(); |
| 80 | |
| 81 | const created = yield* stack.deploy( |
| 82 | Effect.gen(function* () { |
| 83 | return yield* GCP.AgentIdentity.AuthProvider("Maps", { |
| 84 | location, |
| 85 | description: "alchemy maps key", |
| 86 | labels: { env: "test" }, |
| 87 | allowedScopes: ["https://www.googleapis.com/auth/cloud-platform"], |
| 88 | authProviderTypeParams: { |
| 89 | apiKey: { apiKey: "alchemy-test-api-key" }, |
| 90 | }, |
| 91 | }); |
| 92 | }), |
| 93 | ); |
| 94 | |
| 95 | expect(created.name).toContain("/authProviders/"); |
| 96 | expect(created.authProviderId).toEqual(expect.any(String)); |
| 97 | expect(created.location).toEqual(location); |
| 98 | expect(created.description).toEqual("alchemy maps key"); |
| 99 | expect(created.labels).toMatchObject({ env: "test" }); |
| 100 | expect(created.disabled).toEqual(false); |
| 101 | expect(created.authProviderTypeParams?.apiKey).toBeDefined(); |
| 102 | |
| 103 | const fetched = yield* agentidentity.getProjectsLocationsAuthProviders({ |
| 104 | name: created.name, |
| 105 | }); |
| 106 | expect(fetched.name).toEqual(created.name); |
| 107 | expect(fetched.description).toEqual("alchemy maps key"); |
| 108 | expect(fetched.labels?.env).toEqual("test"); |
| 109 | expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 110 | |
| 111 | const updated = yield* stack.deploy( |
| 112 | Effect.gen(function* () { |
| 113 | return yield* GCP.AgentIdentity.AuthProvider("Maps", { |
| 114 | authProviderId: created.authProviderId, |
| 115 | location, |
| 116 | description: "alchemy maps key v2", |
| 117 | labels: { env: "prod", role: "maps" }, |
| 118 | allowedScopes: [ |
| 119 | "https://www.googleapis.com/auth/cloud-platform", |
| 120 | "https://www.googleapis.com/auth/userinfo.email", |
| 121 | ], |
| 122 | authProviderTypeParams: { |
| 123 | apiKey: { apiKey: "alchemy-test-api-key" }, |
| 124 | }, |
| 125 | }); |
| 126 | }), |
| 127 | ); |
| 128 | |
| 129 | expect(updated.name).toEqual(created.name); |
| 130 | expect(updated.authProviderId).toEqual(created.authProviderId); |
| 131 | expect(updated.description).toEqual("alchemy maps key v2"); |
| 132 | expect(updated.labels).toMatchObject({ env: "prod", role: "maps" }); |
| 133 | |
| 134 | const fetchedUpdate = yield* agentidentity.getProjectsLocationsAuthProviders({ |
| 135 | name: created.name, |
| 136 | }); |
| 137 | expect(fetchedUpdate.description).toEqual("alchemy maps key v2"); |
| 138 | expect(fetchedUpdate.labels?.env).toEqual("prod"); |
| 139 | expect(fetchedUpdate.labels?.role).toEqual("maps"); |
| 140 | expect(fetchedUpdate.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 141 | |
| 142 | yield* stack.destroy(); |
| 143 | |
| 144 | const gone = yield* waitUntilGone(created.name); |
| 145 | expect(gone).toEqual("gone"); |
| 146 | }).pipe(logLevel), |
| 147 | { |
| 148 | tags: ["provider:gcp", "provider:gcp:agentidentity", "live"], |
| 149 | timeout: 90_000, |
| 150 | }, |
| 151 | ); |