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 { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as Core from "@alchemy.run/test/Core"; |
| 6 | import * as cloudbuild from "@distilled.cloud/gcp/cloudbuild_v2"; |
| 7 | import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { expectProbe } from "../bindingHost.ts"; |
| 10 | import CloudBuildBindingsHost, { Source } from "./fixtures/bindings-host.ts"; |
| 11 | |
| 12 | const testOptions = { providers: GCP.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "CloudBuildBindings"); |
| 15 | |
| 16 | // Needs a GitHub connection that has completed the OAuth handshake. |
| 17 | const runLifecycle = !!process.env.GCP_TEST_CLOUDBUILD_REPO && !process.env.FAST; |
| 18 | |
| 19 | let baseUrl: string; |
| 20 | let hostAccount: string; |
| 21 | let repositoryName: string; |
| 22 | |
| 23 | /** Every project-level role (and its IAM Condition) `account` holds. */ |
| 24 | const projectGrantsOf = (account: string) => |
| 25 | Effect.gen(function* () { |
| 26 | const { project } = yield* GcpEnvironment.current; |
| 27 | const policy = yield* resourcemanager.getIamPolicyProjects({ |
| 28 | resource: `projects/${project}`, |
| 29 | body: { options: { requestedPolicyVersion: 3 } }, |
| 30 | }); |
| 31 | return (policy.bindings ?? []) |
| 32 | .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${account}`)) |
| 33 | .map((binding) => ({ |
| 34 | role: binding.role, |
| 35 | condition: binding.condition?.expression, |
| 36 | })) |
| 37 | .sort((left, right) => (left.role ?? "").localeCompare(right.role ?? "")); |
| 38 | }); |
| 39 | |
| 40 | /** Cloud Build repository bindings grant on the project. */ |
| 41 | const PROJECT_GRANTS = [ |
| 42 | { role: "roles/cloudbuild.connectionViewer", condition: undefined }, |
| 43 | { role: "roles/cloudbuild.readTokenAccessor", condition: undefined }, |
| 44 | { role: "roles/cloudbuild.tokenAccessor", condition: undefined }, |
| 45 | ]; |
| 46 | |
| 47 | const expectProjectGrants = Effect.gen(function* () { |
| 48 | expect(yield* projectGrantsOf(hostAccount)).toEqual(PROJECT_GRANTS); |
| 49 | }); |
| 50 | |
| 51 | describe.skipIf(!runLifecycle)( |
| 52 | "CloudBuild Bindings", |
| 53 | { |
| 54 | tags: ["provider:gcp", "provider:gcp:cloudbuild", "provider:gcp:run", "live"], |
| 55 | }, |
| 56 | () => { |
| 57 | beforeAll( |
| 58 | Effect.gen(function* () { |
| 59 | yield* sharedStack.destroy(); |
| 60 | const out = yield* sharedStack.deploy( |
| 61 | Effect.gen(function* () { |
| 62 | const host = yield* CloudBuildBindingsHost; |
| 63 | const repository = yield* Source; |
| 64 | return { |
| 65 | uri: host.uri, |
| 66 | serviceAccount: host.serviceAccount, |
| 67 | repository: repository.name, |
| 68 | }; |
| 69 | }), |
| 70 | ); |
| 71 | baseUrl = out.uri!; |
| 72 | hostAccount = out.serviceAccount!; |
| 73 | repositoryName = out.repository; |
| 74 | }), |
| 75 | { timeout: 900_000 }, |
| 76 | ); |
| 77 | |
| 78 | afterAll(sharedStack.destroy(), { timeout: 600_000 }); |
| 79 | |
| 80 | describe("AccessReadToken", () => { |
| 81 | test.provider( |
| 82 | "mints a read token as the host's service account", |
| 83 | (_stack) => |
| 84 | Effect.gen(function* () { |
| 85 | const out = yield* expectProbe<{ |
| 86 | token?: string; |
| 87 | expirationTime?: string; |
| 88 | }>(baseUrl, "accessReadToken"); |
| 89 | expect(out.token?.length ?? 0).toBeGreaterThan(0); |
| 90 | expect(out.expirationTime).toEqual(expect.any(String)); |
| 91 | yield* expectProjectGrants; |
| 92 | }), |
| 93 | { |
| 94 | tags: ["provider:gcp", "provider:gcp:cloudbuild", "live"], |
| 95 | timeout: 600_000, |
| 96 | }, |
| 97 | ); |
| 98 | }); |
| 99 | |
| 100 | describe("AccessReadWriteToken", () => { |
| 101 | test.provider( |
| 102 | "mints a read/write token as the host's service account", |
| 103 | (_stack) => |
| 104 | Effect.gen(function* () { |
| 105 | const out = yield* expectProbe<{ |
| 106 | token?: string; |
| 107 | expirationTime?: string; |
| 108 | }>(baseUrl, "accessReadWriteToken"); |
| 109 | expect(out.token?.length ?? 0).toBeGreaterThan(0); |
| 110 | expect(out.expirationTime).toEqual(expect.any(String)); |
| 111 | yield* expectProjectGrants; |
| 112 | }), |
| 113 | { |
| 114 | tags: ["provider:gcp", "provider:gcp:cloudbuild", "live"], |
| 115 | timeout: 600_000, |
| 116 | }, |
| 117 | ); |
| 118 | }); |
| 119 | |
| 120 | describe("FetchGitRefs", () => { |
| 121 | test.provider( |
| 122 | "lists the repository's branches as the host's service account", |
| 123 | (_stack) => |
| 124 | Effect.gen(function* () { |
| 125 | const out = yield* expectProbe<{ refNames?: string[] }>(baseUrl, "fetchGitRefs"); |
| 126 | const expected = yield* cloudbuild.fetchGitRefsProjectsLocationsConnectionsRepositories( |
| 127 | { repository: repositoryName, refType: "BRANCH" }, |
| 128 | ); |
| 129 | // Every repository has at least its default branch. |
| 130 | expect(out.refNames?.length ?? 0).toBeGreaterThan(0); |
| 131 | expect(out.refNames).toEqual(expected.refNames); |
| 132 | yield* expectProjectGrants; |
| 133 | }), |
| 134 | { |
| 135 | tags: ["provider:gcp", "provider:gcp:cloudbuild", "live"], |
| 136 | timeout: 600_000, |
| 137 | }, |
| 138 | ); |
| 139 | }); |
| 140 | }, |
| 141 | ); |