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 resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 6 | import * as cloudsupport from "@distilled.cloud/gcp/cloudsupport_v2"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | // Support event subscriptions need an organization-level Cloud Support role |
| 16 | // (Cloud Customer Care). The testing credentials hold none, so every call is |
| 17 | // rejected with Forbidden ("Permission 'cloudsupport.supportSubscriptions.get' |
| 18 | // denied ..."). Set GCP_TEST_CLOUDSUPPORT=1 on an entitled org to run the |
| 19 | // lifecycle. |
| 20 | const entitled = process.env.GCP_TEST_CLOUDSUPPORT === "1"; |
| 21 | const runLifecycle = entitled && !process.env.FAST; |
| 22 | |
| 23 | const waitUntilGone = (name: string) => |
| 24 | cloudsupport.getOrganizationsSupportEventSubscriptions({ name }).pipe( |
| 25 | Effect.map((subscription) => |
| 26 | subscription.state === "DELETED" ? ("gone" as const) : ("found" as const), |
| 27 | ), |
| 28 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 29 | Effect.repeat({ |
| 30 | schedule: Schedule.spaced("1 second"), |
| 31 | until: (status) => status === "gone", |
| 32 | times: 10, |
| 33 | }), |
| 34 | ); |
| 35 | |
| 36 | /** The organization that owns the testing project. */ |
| 37 | const organizationOf = () => |
| 38 | 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 | "getSupportEventSubscriptions on a missing subscription fails with a typed tag", |
| 48 | (stack) => |
| 49 | Effect.gen(function* () { |
| 50 | yield* stack.destroy(); |
| 51 | |
| 52 | const organization = yield* organizationOf(); |
| 53 | const error = yield* Effect.flip( |
| 54 | cloudsupport.getOrganizationsSupportEventSubscriptions({ |
| 55 | name: `${organization}/supportEventSubscriptions/alchemy-missing`, |
| 56 | }), |
| 57 | ); |
| 58 | expect(error._tag).toEqual(entitled ? "NotFound" : "Forbidden"); |
| 59 | |
| 60 | yield* stack.destroy(); |
| 61 | }).pipe(logLevel), |
| 62 | { |
| 63 | tags: ["provider:gcp", "provider:gcp:cloudsupport", "live"], |
| 64 | timeout: 90_000, |
| 65 | }, |
| 66 | ); |
| 67 | |
| 68 | test.provider.skipIf(entitled)( |
| 69 | "createSupportEventSubscriptions without Cloud Support access fails with Forbidden", |
| 70 | (stack) => |
| 71 | Effect.gen(function* () { |
| 72 | const { project } = yield* GcpEnvironment.current; |
| 73 | yield* stack.destroy(); |
| 74 | |
| 75 | const organization = yield* organizationOf(); |
| 76 | const error = yield* Effect.flip( |
| 77 | cloudsupport.createOrganizationsSupportEventSubscriptions({ |
| 78 | parent: organization, |
| 79 | body: { |
| 80 | pubSubTopic: `projects/${project}/topics/alchemy-missing-topic`, |
| 81 | }, |
| 82 | }), |
| 83 | ); |
| 84 | expect(error._tag).toEqual("Forbidden"); |
| 85 | |
| 86 | yield* stack.destroy(); |
| 87 | }).pipe(logLevel), |
| 88 | { |
| 89 | tags: ["provider:gcp", "provider:gcp:cloudsupport", "live"], |
| 90 | timeout: 90_000, |
| 91 | }, |
| 92 | ); |
| 93 | |
| 94 | test.provider.skipIf(!runLifecycle)( |
| 95 | "create, update, and delete a support event subscription", |
| 96 | (stack) => |
| 97 | Effect.gen(function* () { |
| 98 | yield* stack.destroy(); |
| 99 | |
| 100 | const organization = yield* organizationOf(); |
| 101 | expect(organization.length).toBeGreaterThan(0); |
| 102 | |
| 103 | const created = yield* stack.deploy( |
| 104 | Effect.gen(function* () { |
| 105 | const topic = yield* GCP.PubSub.Topic("SupportEventsA", {}); |
| 106 | const subscription = yield* GCP.CloudSupport.SupportEventSubscription("Events", { |
| 107 | organization, |
| 108 | pubSubTopic: topic.name, |
| 109 | }); |
| 110 | return { topic, subscription }; |
| 111 | }), |
| 112 | ); |
| 113 | |
| 114 | expect(created.subscription.name).toContain("/supportEventSubscriptions/"); |
| 115 | expect(created.subscription.subscriptionId.length).toBeGreaterThan(0); |
| 116 | expect(created.subscription.organization).toEqual(organization); |
| 117 | expect(created.subscription.pubSubTopic).toEqual(created.topic.name); |
| 118 | |
| 119 | const fetched = yield* cloudsupport.getOrganizationsSupportEventSubscriptions({ |
| 120 | name: created.subscription.name, |
| 121 | }); |
| 122 | expect(fetched.name).toEqual(created.subscription.name); |
| 123 | expect(fetched.pubSubTopic).toEqual(created.topic.name); |
| 124 | |
| 125 | const updated = yield* stack.deploy( |
| 126 | Effect.gen(function* () { |
| 127 | const topicA = yield* GCP.PubSub.Topic("SupportEventsA", {}); |
| 128 | const topicB = yield* GCP.PubSub.Topic("SupportEventsB", {}); |
| 129 | const subscription = yield* GCP.CloudSupport.SupportEventSubscription("Events", { |
| 130 | organization, |
| 131 | subscriptionId: created.subscription.subscriptionId, |
| 132 | pubSubTopic: topicB.name, |
| 133 | }); |
| 134 | return { topicA, topicB, subscription }; |
| 135 | }), |
| 136 | ); |
| 137 | |
| 138 | expect(updated.subscription.name).toEqual(created.subscription.name); |
| 139 | expect(updated.subscription.pubSubTopic).toEqual(updated.topicB.name); |
| 140 | |
| 141 | yield* stack.destroy(); |
| 142 | |
| 143 | const gone = yield* waitUntilGone(created.subscription.name); |
| 144 | expect(gone).toEqual("gone"); |
| 145 | }).pipe(logLevel), |
| 146 | { |
| 147 | tags: ["provider:gcp", "provider:gcp:cloudsupport", "live"], |
| 148 | timeout: 90_000, |
| 149 | }, |
| 150 | ); |