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 resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 6 | import * as resourcemanagerV1 from "@distilled.cloud/gcp/unstable/cloudresourcemanager_v1"; |
| 7 | import { adopt } from "alchemy/AdoptPolicy"; |
| 8 | import { State } from "alchemy/State"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: GCP.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | // Live create returns Forbidden: "The caller does not have permission" |
| 17 | // (resourcemanager.projects.create on the parent organization/folder). |
| 18 | // Set GOOGLE_ORGANIZATION_ID when the credentials can create projects there. |
| 19 | // Set GOOGLE_STANDALONE_PROJECTS=1 when the current project has no organization |
| 20 | // or folder and the credentials are a user's: Google rejects standalone |
| 21 | // project creation by service accounts. |
| 22 | const runStandalone = process.env.GOOGLE_STANDALONE_PROJECTS === "1"; |
| 23 | const runLifecycle = !!process.env.GOOGLE_ORGANIZATION_ID || runStandalone; |
| 24 | |
| 25 | const waitUntilGone = (name: string) => |
| 26 | resourcemanager.getProjects({ name }).pipe( |
| 27 | Effect.map((resource) => |
| 28 | resource.state === "DELETE_REQUESTED" ? ("gone" as const) : ("found" as const), |
| 29 | ), |
| 30 | Effect.catchTag(["NotFound", "ProjectNotFound"], () => Effect.succeed("gone" as const)), |
| 31 | Effect.repeat({ |
| 32 | schedule: Schedule.spaced("1 second"), |
| 33 | until: (status) => status === "gone", |
| 34 | times: 10, |
| 35 | }), |
| 36 | ); |
| 37 | |
| 38 | const resolveParent = Effect.gen(function* () { |
| 39 | const { project } = yield* GcpEnvironment.current; |
| 40 | const resource = yield* resourcemanager.getProjects({ |
| 41 | name: `projects/${project}`, |
| 42 | }); |
| 43 | return resource.parent; |
| 44 | }); |
| 45 | |
| 46 | test.provider( |
| 47 | "getProjects on a missing project fails with a typed tag", |
| 48 | (stack) => |
| 49 | Effect.gen(function* () { |
| 50 | yield* stack.destroy(); |
| 51 | |
| 52 | const error = yield* Effect.flip( |
| 53 | resourcemanager.getProjects({ |
| 54 | name: "projects/alchemy-missing-proj", |
| 55 | }), |
| 56 | ); |
| 57 | expect(error._tag).toEqual("ProjectNotFound"); |
| 58 | |
| 59 | yield* stack.destroy(); |
| 60 | }).pipe(logLevel), |
| 61 | { |
| 62 | tags: ["provider:gcp", "provider:gcp:resourcemanager", "live"], |
| 63 | timeout: 90_000, |
| 64 | }, |
| 65 | ); |
| 66 | |
| 67 | test.provider.skipIf(runLifecycle)( |
| 68 | "createProjects without project-creator IAM fails with Forbidden", |
| 69 | (stack) => |
| 70 | Effect.gen(function* () { |
| 71 | yield* stack.destroy(); |
| 72 | |
| 73 | const parent = yield* resolveParent.pipe( |
| 74 | Effect.catchTag("NotFound", () => Effect.succeed(undefined)), |
| 75 | ); |
| 76 | const error = yield* Effect.flip( |
| 77 | resourcemanager.createProjects({ |
| 78 | body: { |
| 79 | projectId: "alchemy-rm-probe-xxxx", |
| 80 | parent: parent ?? "organizations/0", |
| 81 | displayName: "Alchemy Probe", |
| 82 | }, |
| 83 | }), |
| 84 | ); |
| 85 | expect(error._tag).toEqual("Forbidden"); |
| 86 | |
| 87 | yield* stack.destroy(); |
| 88 | }).pipe(logLevel), |
| 89 | { |
| 90 | tags: ["provider:gcp", "provider:gcp:resourcemanager", "live"], |
| 91 | timeout: 90_000, |
| 92 | }, |
| 93 | ); |
| 94 | |
| 95 | test.provider.skipIf(!runLifecycle)( |
| 96 | "create, update, and delete a project", |
| 97 | (stack) => |
| 98 | Effect.gen(function* () { |
| 99 | yield* stack.destroy(); |
| 100 | |
| 101 | // Standalone when the current project has no organization or folder. |
| 102 | const expectedParent = yield* resolveParent; |
| 103 | if (runStandalone) expect(expectedParent).toBeUndefined(); |
| 104 | |
| 105 | const created = yield* stack.deploy( |
| 106 | Effect.gen(function* () { |
| 107 | return yield* GCP.ResourceManager.Project("Sandbox", { |
| 108 | displayName: "Sandbox", |
| 109 | labels: { env: "test", team: "infra" }, |
| 110 | }); |
| 111 | }), |
| 112 | ); |
| 113 | |
| 114 | expect(created.name).toMatch(/^projects\//); |
| 115 | expect(created.projectId).toEqual(expect.any(String)); |
| 116 | expect(created.projectId.length).toBeGreaterThanOrEqual(6); |
| 117 | expect(created.displayName).toEqual("Sandbox"); |
| 118 | expect(created.labels).toEqual({ env: "test", team: "infra" }); |
| 119 | expect(created.state).toEqual("ACTIVE"); |
| 120 | expect(created.parent).toEqual(expectedParent); |
| 121 | |
| 122 | const fetched = yield* resourcemanager.getProjects({ |
| 123 | name: created.name, |
| 124 | }); |
| 125 | expect(fetched.name).toEqual(created.name); |
| 126 | expect(fetched.projectId).toEqual(created.projectId); |
| 127 | expect(fetched.labels?.env).toEqual("test"); |
| 128 | expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 129 | |
| 130 | // An unchanged redeploy keeps the project where it is. |
| 131 | const redeployed = yield* stack.deploy( |
| 132 | Effect.gen(function* () { |
| 133 | return yield* GCP.ResourceManager.Project("Sandbox", { |
| 134 | projectId: created.projectId, |
| 135 | displayName: "Sandbox", |
| 136 | labels: { env: "test" }, |
| 137 | }); |
| 138 | }), |
| 139 | ); |
| 140 | expect(redeployed.name).toEqual(created.name); |
| 141 | expect(redeployed.parent).toEqual(created.parent); |
| 142 | |
| 143 | // Lose state: reconcile re-observes the project by id instead of |
| 144 | // failing for want of a parent. |
| 145 | yield* Effect.gen(function* () { |
| 146 | const state = yield* yield* State; |
| 147 | yield* state.delete({ stack: stack.name, stage: stack.stage, fqn: "Sandbox" }); |
| 148 | }).pipe(Effect.provide(stack.state)); |
| 149 | |
| 150 | const updated = yield* stack.deploy( |
| 151 | Effect.gen(function* () { |
| 152 | return yield* GCP.ResourceManager.Project("Sandbox", { |
| 153 | projectId: created.projectId, |
| 154 | displayName: "Sandbox prod", |
| 155 | labels: { env: "prod" }, |
| 156 | }); |
| 157 | }), |
| 158 | ); |
| 159 | |
| 160 | expect(updated.name).toEqual(created.name); |
| 161 | expect(updated.projectId).toEqual(created.projectId); |
| 162 | expect(updated.displayName).toEqual("Sandbox prod"); |
| 163 | expect(updated.labels).toEqual({ env: "prod" }); |
| 164 | expect(updated.createTime).toEqual(created.createTime); |
| 165 | expect(updated.parent).toEqual(created.parent); |
| 166 | |
| 167 | const fetchedUpdate = yield* resourcemanager.getProjects({ |
| 168 | name: updated.name, |
| 169 | }); |
| 170 | expect(fetchedUpdate.displayName).toEqual("Sandbox prod"); |
| 171 | expect(fetchedUpdate.labels?.env).toEqual("prod"); |
| 172 | expect(fetchedUpdate.labels?.team).toBeUndefined(); |
| 173 | expect(fetchedUpdate.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 174 | |
| 175 | yield* stack.destroy(); |
| 176 | |
| 177 | const gone = yield* waitUntilGone(updated.name); |
| 178 | expect(gone).toEqual("gone"); |
| 179 | }).pipe(logLevel), |
| 180 | { |
| 181 | tags: ["provider:gcp", "provider:gcp:resourcemanager", "live"], |
| 182 | timeout: 90_000, |
| 183 | }, |
| 184 | ); |
| 185 | |
| 186 | test.provider.skipIf(!runLifecycle)( |
| 187 | "adopting an unowned project requires opt-in and keeps its parent", |
| 188 | (stack) => |
| 189 | Effect.gen(function* () { |
| 190 | yield* stack.destroy(); |
| 191 | |
| 192 | const created = yield* stack.deploy( |
| 193 | Effect.gen(function* () { |
| 194 | return yield* GCP.ResourceManager.Project("Sandbox", { |
| 195 | displayName: "Sandbox", |
| 196 | }); |
| 197 | }), |
| 198 | ); |
| 199 | |
| 200 | // Strip Alchemy's ownership labels out of band and forget the state, |
| 201 | // leaving a project Alchemy doesn't own. v1 `projects.update` replaces |
| 202 | // labels; v3 `projects.patch` would only merge into them. |
| 203 | const foreign = yield* resourcemanagerV1.getProjects({ projectId: created.projectId }); |
| 204 | yield* resourcemanagerV1.updateProjects({ |
| 205 | projectId: created.projectId, |
| 206 | body: { ...foreign, labels: {} }, |
| 207 | }); |
| 208 | const stripped = yield* resourcemanager.getProjects({ name: created.name }).pipe( |
| 209 | Effect.repeat({ |
| 210 | schedule: Schedule.spaced("1 second"), |
| 211 | until: (resource) => resource.labels?.["alchemy-id"] === undefined, |
| 212 | times: 10, |
| 213 | }), |
| 214 | ); |
| 215 | expect(stripped.labels?.["alchemy-id"]).toBeUndefined(); |
| 216 | yield* Effect.gen(function* () { |
| 217 | const state = yield* yield* State; |
| 218 | yield* state.delete({ stack: stack.name, stage: stack.stage, fqn: "Sandbox" }); |
| 219 | }).pipe(Effect.provide(stack.state)); |
| 220 | |
| 221 | const project = Effect.gen(function* () { |
| 222 | return yield* GCP.ResourceManager.Project("Sandbox", { |
| 223 | projectId: created.projectId, |
| 224 | displayName: "Adopted sandbox", |
| 225 | labels: { env: "adopted" }, |
| 226 | }); |
| 227 | }); |
| 228 | const error = yield* stack.plan(project).pipe(Effect.flip); |
| 229 | expect(error._tag).toEqual("OwnedBySomeoneElse"); |
| 230 | |
| 231 | const adopted = yield* stack.deploy(project.pipe(adopt(true))); |
| 232 | expect(adopted.name).toEqual(created.name); |
| 233 | expect(adopted.parent).toEqual(created.parent); |
| 234 | expect(adopted.displayName).toEqual("Adopted sandbox"); |
| 235 | expect(adopted.labels).toMatchObject({ env: "adopted" }); |
| 236 | const observed = yield* resourcemanager.getProjects({ name: adopted.name }); |
| 237 | expect(observed.labels?.["alchemy-id"]).toEqual(expect.any(String)); |
| 238 | |
| 239 | yield* stack.destroy(); |
| 240 | expect(yield* waitUntilGone(adopted.name)).toEqual("gone"); |
| 241 | }).pipe(logLevel), |
| 242 | { |
| 243 | tags: ["provider:gcp", "provider:gcp:resourcemanager", "live"], |
| 244 | timeout: 90_000, |
| 245 | }, |
| 246 | ); |