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 { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as acm from "@distilled.cloud/gcp/accesscontextmanager_v1"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | import { logLevel, projectContext, runLifecycle, runProbe } from "./common.ts"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: GCP.providers() }); |
| 10 | |
| 11 | const waitUntilGone = (name: string) => |
| 12 | acm.getAccessPolicies({ name }).pipe( |
| 13 | Effect.as("found" as const), |
| 14 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 15 | Effect.repeat({ |
| 16 | schedule: Schedule.spaced("2 seconds"), |
| 17 | until: (status) => status === "gone", |
| 18 | times: 10, |
| 19 | }), |
| 20 | ); |
| 21 | |
| 22 | test.provider( |
| 23 | "getAccessPolicies on an inaccessible policy fails with Forbidden", |
| 24 | (stack) => |
| 25 | Effect.gen(function* () { |
| 26 | yield* stack.destroy(); |
| 27 | |
| 28 | const error = yield* Effect.flip( |
| 29 | acm.getAccessPolicies({ name: "accessPolicies/123456789012" }), |
| 30 | ); |
| 31 | expect(error._tag).toEqual("Forbidden"); |
| 32 | |
| 33 | yield* stack.destroy(); |
| 34 | }).pipe(logLevel), |
| 35 | { |
| 36 | tags: ["provider:gcp", "provider:gcp:accesscontextmanager", "live"], |
| 37 | timeout: 90_000, |
| 38 | }, |
| 39 | ); |
| 40 | |
| 41 | test.provider.skipIf(!runProbe)( |
| 42 | "createAccessPolicies without org permissions fails with Forbidden", |
| 43 | (stack) => |
| 44 | Effect.gen(function* () { |
| 45 | yield* stack.destroy(); |
| 46 | |
| 47 | const ctx = yield* projectContext(); |
| 48 | const error = yield* Effect.flip( |
| 49 | acm.createAccessPolicies({ |
| 50 | body: { |
| 51 | title: "alchemy-acm-probe", |
| 52 | parent: ctx.organization ?? "organizations/123456789012", |
| 53 | scopes: ctx.projectNumber.length > 0 ? [`projects/${ctx.projectNumber}`] : undefined, |
| 54 | }, |
| 55 | }), |
| 56 | ); |
| 57 | expect(error._tag).toBe("Forbidden"); |
| 58 | |
| 59 | yield* stack.destroy(); |
| 60 | }).pipe(logLevel), |
| 61 | { |
| 62 | tags: ["provider:gcp", "provider:gcp:accesscontextmanager", "live"], |
| 63 | timeout: 90_000, |
| 64 | }, |
| 65 | ); |
| 66 | |
| 67 | test.provider.skipIf(!runLifecycle)( |
| 68 | "create, update, and delete an access policy", |
| 69 | (stack) => |
| 70 | Effect.gen(function* () { |
| 71 | yield* stack.destroy(); |
| 72 | |
| 73 | const ctx = yield* projectContext(); |
| 74 | const scopes = ctx.projectNumber.length > 0 ? [`projects/${ctx.projectNumber}`] : undefined; |
| 75 | |
| 76 | const created = yield* stack.deploy( |
| 77 | Effect.gen(function* () { |
| 78 | return yield* GCP.AccessContextManager.AccessPolicy("Corp", { |
| 79 | title: "corp access policy", |
| 80 | scopes, |
| 81 | }); |
| 82 | }), |
| 83 | ); |
| 84 | |
| 85 | expect(created.name).toMatch(/^accessPolicies\//); |
| 86 | expect(created.policyId).toEqual(expect.any(String)); |
| 87 | expect(created.parent).toMatch(/^organizations\//); |
| 88 | expect(created.title).toEqual("corp access policy"); |
| 89 | |
| 90 | const fetched = yield* acm.getAccessPolicies({ name: created.name }); |
| 91 | expect(fetched.name).toEqual(created.name); |
| 92 | expect(fetched.title).toContain("alchemy-id="); |
| 93 | expect(fetched.title).toContain("corp access policy"); |
| 94 | |
| 95 | const updated = yield* stack.deploy( |
| 96 | Effect.gen(function* () { |
| 97 | return yield* GCP.AccessContextManager.AccessPolicy("Corp", { |
| 98 | title: "corp access policy prod", |
| 99 | scopes, |
| 100 | }); |
| 101 | }), |
| 102 | ); |
| 103 | |
| 104 | expect(updated.name).toEqual(created.name); |
| 105 | expect(updated.title).toEqual("corp access policy prod"); |
| 106 | |
| 107 | const fetchedUpdate = yield* acm.getAccessPolicies({ |
| 108 | name: updated.name, |
| 109 | }); |
| 110 | expect(fetchedUpdate.title).toContain("corp access policy prod"); |
| 111 | |
| 112 | yield* stack.destroy(); |
| 113 | |
| 114 | const gone = yield* waitUntilGone(created.name); |
| 115 | expect(gone).toEqual("gone"); |
| 116 | }).pipe(logLevel), |
| 117 | { |
| 118 | tags: ["provider:gcp", "provider:gcp:accesscontextmanager", "live"], |
| 119 | timeout: 120_000, |
| 120 | }, |
| 121 | ); |