The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 3 | import { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as iam from "@distilled.cloud/cloudflare/iam"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { testName } from "../../names.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | |
| 17 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 18 | |
| 19 | // Deterministic names — the same on every run (never Date.now()/random). |
| 20 | const RG_NAME = testName("alchemy-iam-rg-crud"); |
| 21 | const RG_NAME_RENAMED = testName("alchemy-iam-rg-crud-renamed"); |
| 22 | |
| 23 | // The scoped API token the test harness mints propagates eventually- |
| 24 | // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`, |
| 25 | // declared in the distilled error union) on out-of-band verification calls. |
| 26 | const getResourceGroup = (accountId: string, resourceGroupId: string) => |
| 27 | iam.getResourceGroup({ accountId, resourceGroupId }).pipe( |
| 28 | Effect.retry({ |
| 29 | while: (e) => e._tag === "Forbidden", |
| 30 | schedule: Schedule.exponential("500 millis"), |
| 31 | times: 8, |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | // A destroyed resource group answers GET with the typed |
| 36 | // `ResourceGroupNotFound` (404). |
| 37 | const expectGone = (accountId: string, resourceGroupId: string) => |
| 38 | getResourceGroup(accountId, resourceGroupId).pipe( |
| 39 | Effect.asSome, |
| 40 | Effect.catchTag("ResourceGroupNotFound", () => Effect.succeedNone), |
| 41 | Effect.repeat({ |
| 42 | schedule: Schedule.exponential("500 millis"), |
| 43 | until: (g) => g._tag === "None", |
| 44 | times: 8, |
| 45 | }), |
| 46 | Effect.map((g) => expect(g._tag).toEqual("None")), |
| 47 | ); |
| 48 | |
| 49 | test.provider( |
| 50 | "create, verify out-of-band, update name+scope in place, destroy", |
| 51 | (stack) => |
| 52 | Effect.gen(function* () { |
| 53 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 54 | const accountScopeKey = `com.cloudflare.api.account.${accountId}`; |
| 55 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 56 | if (!zone) { |
| 57 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 58 | } |
| 59 | const zoneObjectKey = `com.cloudflare.api.account.zone.${zone.id}`; |
| 60 | |
| 61 | yield* stack.destroy(); |
| 62 | |
| 63 | // Create — scoped to the whole account. |
| 64 | const v1 = yield* stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | return yield* Cloudflare.Iam.ResourceGroup("Rg", { |
| 67 | name: RG_NAME, |
| 68 | scope: { |
| 69 | key: accountScopeKey, |
| 70 | objects: [{ key: "*" }], |
| 71 | }, |
| 72 | }); |
| 73 | }), |
| 74 | ); |
| 75 | |
| 76 | expect(v1.resourceGroupId).toBeTruthy(); |
| 77 | expect(v1.accountId).toEqual(accountId); |
| 78 | expect(v1.name).toEqual(RG_NAME); |
| 79 | expect(v1.scope.key).toEqual(accountScopeKey); |
| 80 | expect(v1.scope.objects).toEqual([{ key: "*" }]); |
| 81 | |
| 82 | // Out-of-band verification via the distilled API. |
| 83 | const live = yield* getResourceGroup(accountId, v1.resourceGroupId); |
| 84 | expect(live.name).toEqual(RG_NAME); |
| 85 | |
| 86 | // In-place update — rename and narrow the scope to a single zone. |
| 87 | // Same resource group (no replacement). |
| 88 | const v2 = yield* stack.deploy( |
| 89 | Effect.gen(function* () { |
| 90 | return yield* Cloudflare.Iam.ResourceGroup("Rg", { |
| 91 | name: RG_NAME_RENAMED, |
| 92 | scope: { |
| 93 | key: accountScopeKey, |
| 94 | objects: [{ key: zoneObjectKey }], |
| 95 | }, |
| 96 | }); |
| 97 | }), |
| 98 | ); |
| 99 | |
| 100 | expect(v2.resourceGroupId).toEqual(v1.resourceGroupId); |
| 101 | expect(v2.name).toEqual(RG_NAME_RENAMED); |
| 102 | expect(v2.scope.objects).toEqual([{ key: zoneObjectKey }]); |
| 103 | |
| 104 | const updated = yield* getResourceGroup(accountId, v2.resourceGroupId); |
| 105 | expect(updated.name).toEqual(RG_NAME_RENAMED); |
| 106 | |
| 107 | // No-op deploy — same desired state, same identity, reconcile |
| 108 | // observes the in-sync state and applies nothing. |
| 109 | const v3 = yield* stack.deploy( |
| 110 | Effect.gen(function* () { |
| 111 | return yield* Cloudflare.Iam.ResourceGroup("Rg", { |
| 112 | name: RG_NAME_RENAMED, |
| 113 | scope: { |
| 114 | key: accountScopeKey, |
| 115 | objects: [{ key: zoneObjectKey }], |
| 116 | }, |
| 117 | }); |
| 118 | }), |
| 119 | ); |
| 120 | expect(v3.resourceGroupId).toEqual(v1.resourceGroupId); |
| 121 | |
| 122 | yield* stack.destroy(); |
| 123 | |
| 124 | yield* expectGone(accountId, v1.resourceGroupId); |
| 125 | }).pipe(logLevel), |
| 126 | { |
| 127 | tags: ["provider:cloudflare", "provider:cloudflare:iam", "provider:cloudflare:zone", "live"], |
| 128 | timeout: 120_000, |
| 129 | }, |
| 130 | ); |
| 131 | |
| 132 | test.provider( |
| 133 | "list enumerates the deployed resource group", |
| 134 | (stack) => |
| 135 | Effect.gen(function* () { |
| 136 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 137 | const accountScopeKey = `com.cloudflare.api.account.${accountId}`; |
| 138 | |
| 139 | yield* stack.destroy(); |
| 140 | |
| 141 | const deployed = yield* stack.deploy( |
| 142 | Effect.gen(function* () { |
| 143 | return yield* Cloudflare.Iam.ResourceGroup("ListRg", { |
| 144 | scope: { |
| 145 | key: accountScopeKey, |
| 146 | objects: [{ key: "*" }], |
| 147 | }, |
| 148 | }); |
| 149 | }), |
| 150 | ); |
| 151 | |
| 152 | const provider = yield* Provider.findProvider(Cloudflare.Iam.ResourceGroup); |
| 153 | const all = yield* provider.list(); |
| 154 | |
| 155 | // Each element is the full `read` Attributes shape, usable by delete. |
| 156 | expect(all.some((g) => g.resourceGroupId === deployed.resourceGroupId)).toBe(true); |
| 157 | |
| 158 | yield* stack.destroy(); |
| 159 | |
| 160 | yield* expectGone(accountId, deployed.resourceGroupId); |
| 161 | }).pipe(logLevel), |
| 162 | { |
| 163 | tags: ["provider:cloudflare", "provider:cloudflare:iam", "live"], |
| 164 | timeout: 120_000, |
| 165 | }, |
| 166 | ); |