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 cloudbuild from "@distilled.cloud/gcp/cloudbuild_v2"; |
| 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 | const remoteUri = |
| 14 | process.env.GCP_CLOUDBUILD_REMOTE_URI ?? "https://github.com/octocat/Hello-World.git"; |
| 15 | const standingConnection = process.env.GCP_CLOUDBUILD_CONNECTION; |
| 16 | const runLifecycle = !!standingConnection && !process.env.FAST; |
| 17 | |
| 18 | const waitUntilRepositoryGone = (name: string) => |
| 19 | cloudbuild.getProjectsLocationsConnectionsRepositories({ name }).pipe( |
| 20 | Effect.as("found" as const), |
| 21 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 22 | Effect.repeat({ |
| 23 | schedule: Schedule.spaced("1 second"), |
| 24 | until: (status) => status === "gone", |
| 25 | times: 10, |
| 26 | }), |
| 27 | ); |
| 28 | |
| 29 | const waitUntilConnectionGone = (name: string) => |
| 30 | cloudbuild.getProjectsLocationsConnections({ name }).pipe( |
| 31 | Effect.as("found" as const), |
| 32 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 33 | Effect.repeat({ |
| 34 | schedule: Schedule.spaced("1 second"), |
| 35 | until: (status) => status === "gone", |
| 36 | times: 10, |
| 37 | }), |
| 38 | ); |
| 39 | |
| 40 | test.provider( |
| 41 | "get and binding ops on a missing repository are NotFound", |
| 42 | (stack) => |
| 43 | Effect.gen(function* () { |
| 44 | const { project } = yield* GcpEnvironment.current; |
| 45 | const missingRepositoryName = `projects/${project}/locations/us-central1/connections/alchemy-missing-connection/repositories/alchemy-missing-repository`; |
| 46 | yield* stack.destroy(); |
| 47 | |
| 48 | const getError = yield* Effect.flip( |
| 49 | cloudbuild.getProjectsLocationsConnectionsRepositories({ |
| 50 | name: missingRepositoryName, |
| 51 | }), |
| 52 | ); |
| 53 | expect(getError._tag).toBe("NotFound"); |
| 54 | |
| 55 | const readError = yield* Effect.flip( |
| 56 | cloudbuild.accessReadTokenProjectsLocationsConnectionsRepositories({ |
| 57 | repository: missingRepositoryName, |
| 58 | body: {}, |
| 59 | }), |
| 60 | ); |
| 61 | expect(readError._tag).toBe("NotFound"); |
| 62 | |
| 63 | const writeError = yield* Effect.flip( |
| 64 | cloudbuild.accessReadWriteTokenProjectsLocationsConnectionsRepositories({ |
| 65 | repository: missingRepositoryName, |
| 66 | body: {}, |
| 67 | }), |
| 68 | ); |
| 69 | expect(writeError._tag).toBe("NotFound"); |
| 70 | |
| 71 | const refsError = yield* Effect.flip( |
| 72 | cloudbuild.fetchGitRefsProjectsLocationsConnectionsRepositories({ |
| 73 | repository: missingRepositoryName, |
| 74 | refType: "BRANCH", |
| 75 | }), |
| 76 | ); |
| 77 | expect(refsError._tag).toBe("NotFound"); |
| 78 | |
| 79 | yield* stack.destroy(); |
| 80 | }).pipe(logLevel), |
| 81 | { |
| 82 | tags: ["provider:gcp", "provider:gcp:cloudbuild", "live"], |
| 83 | timeout: 90_000, |
| 84 | }, |
| 85 | ); |
| 86 | |
| 87 | test.provider( |
| 88 | "create repository on a pending GitHub connection is BadRequest", |
| 89 | (stack) => |
| 90 | Effect.gen(function* () { |
| 91 | yield* stack.destroy(); |
| 92 | |
| 93 | const connection = yield* stack.deploy( |
| 94 | Effect.gen(function* () { |
| 95 | return yield* GCP.CloudBuild.Connection("Github", { |
| 96 | location: "us-central1", |
| 97 | githubConfig: {}, |
| 98 | }); |
| 99 | }), |
| 100 | ); |
| 101 | |
| 102 | const error = yield* Effect.flip( |
| 103 | cloudbuild.createProjectsLocationsConnectionsRepositories({ |
| 104 | parent: connection.name, |
| 105 | repositoryId: "alchemy-probe-source", |
| 106 | body: { remoteUri }, |
| 107 | }), |
| 108 | ); |
| 109 | expect(error._tag).toBe("BadRequest"); |
| 110 | expect(error.message.toLowerCase()).toContain("installation"); |
| 111 | |
| 112 | yield* stack.destroy(); |
| 113 | |
| 114 | const gone = yield* waitUntilConnectionGone(connection.name); |
| 115 | expect(gone).toEqual("gone"); |
| 116 | }).pipe(logLevel), |
| 117 | { |
| 118 | tags: ["provider:gcp", "provider:gcp:cloudbuild", "live"], |
| 119 | timeout: 90_000, |
| 120 | }, |
| 121 | ); |
| 122 | |
| 123 | test.provider.skipIf(!runLifecycle)( |
| 124 | "create, replace, and delete a repository", |
| 125 | (stack) => |
| 126 | Effect.gen(function* () { |
| 127 | yield* stack.destroy(); |
| 128 | |
| 129 | const created = yield* stack.deploy( |
| 130 | Effect.gen(function* () { |
| 131 | return yield* GCP.CloudBuild.Repository("Source", { |
| 132 | connection: standingConnection!, |
| 133 | remoteUri, |
| 134 | annotations: { env: "test" }, |
| 135 | }); |
| 136 | }), |
| 137 | ); |
| 138 | |
| 139 | expect(created.name).toContain("/repositories/"); |
| 140 | expect(created.repositoryId).toEqual(expect.any(String)); |
| 141 | expect(created.connection).toContain("/connections/"); |
| 142 | expect(created.location).toEqual(expect.any(String)); |
| 143 | expect(created.remoteUri).toEqual(remoteUri); |
| 144 | expect(created.annotations).toMatchObject({ env: "test" }); |
| 145 | |
| 146 | const fetched = yield* cloudbuild.getProjectsLocationsConnectionsRepositories({ |
| 147 | name: created.name, |
| 148 | }); |
| 149 | expect(fetched.name).toEqual(created.name); |
| 150 | expect(fetched.remoteUri).toEqual(remoteUri); |
| 151 | expect(fetched.annotations?.env).toEqual("test"); |
| 152 | expect(fetched.annotations?.["alchemy-id"]).toEqual(expect.any(String)); |
| 153 | |
| 154 | const listed = yield* cloudbuild.listProjectsLocationsConnectionsRepositories({ |
| 155 | parent: created.connection, |
| 156 | }); |
| 157 | expect( |
| 158 | (listed.repositories ?? []).some((repository) => repository.name === created.name), |
| 159 | ).toEqual(true); |
| 160 | |
| 161 | const readToken = yield* cloudbuild.accessReadTokenProjectsLocationsConnectionsRepositories({ |
| 162 | repository: created.name, |
| 163 | body: {}, |
| 164 | }); |
| 165 | expect(readToken.token).toEqual(expect.any(String)); |
| 166 | expect((readToken.token ?? "").length).toBeGreaterThan(0); |
| 167 | |
| 168 | const writeToken = |
| 169 | yield* cloudbuild.accessReadWriteTokenProjectsLocationsConnectionsRepositories({ |
| 170 | repository: created.name, |
| 171 | body: {}, |
| 172 | }); |
| 173 | expect(writeToken.token).toEqual(expect.any(String)); |
| 174 | expect((writeToken.token ?? "").length).toBeGreaterThan(0); |
| 175 | |
| 176 | const refs = yield* cloudbuild.fetchGitRefsProjectsLocationsConnectionsRepositories({ |
| 177 | repository: created.name, |
| 178 | refType: "BRANCH", |
| 179 | }); |
| 180 | // Every repository has at least its default branch. |
| 181 | expect(refs.refNames?.length ?? 0).toBeGreaterThan(0); |
| 182 | |
| 183 | const replaced = yield* stack.deploy( |
| 184 | Effect.gen(function* () { |
| 185 | return yield* GCP.CloudBuild.Repository("Source", { |
| 186 | connection: standingConnection!, |
| 187 | repositoryId: created.repositoryId, |
| 188 | remoteUri, |
| 189 | annotations: { env: "prod", role: "source" }, |
| 190 | }); |
| 191 | }), |
| 192 | ); |
| 193 | |
| 194 | expect(replaced.repositoryId).toEqual(created.repositoryId); |
| 195 | expect(replaced.connection).toEqual(created.connection); |
| 196 | expect(replaced.annotations).toMatchObject({ |
| 197 | env: "prod", |
| 198 | role: "source", |
| 199 | }); |
| 200 | |
| 201 | const fetchedReplace = yield* cloudbuild.getProjectsLocationsConnectionsRepositories({ |
| 202 | name: replaced.name, |
| 203 | }); |
| 204 | expect(fetchedReplace.annotations?.env).toEqual("prod"); |
| 205 | expect(fetchedReplace.annotations?.role).toEqual("source"); |
| 206 | |
| 207 | yield* stack.destroy(); |
| 208 | |
| 209 | const gone = yield* waitUntilRepositoryGone(replaced.name); |
| 210 | expect(gone).toEqual("gone"); |
| 211 | }).pipe(logLevel), |
| 212 | { |
| 213 | tags: ["provider:gcp", "provider:gcp:cloudbuild", "live"], |
| 214 | timeout: 90_000, |
| 215 | }, |
| 216 | ); |