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 { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as zeroTrust from "@distilled.cloud/cloudflare/zero-trust"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 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: Cloudflare.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | // Ride out 403 blips (`Forbidden`) while the harness-minted token |
| 16 | // propagates across Cloudflare's edge. |
| 17 | const getConfiguration = (accountId: string) => |
| 18 | zeroTrust.getGatewayConfiguration({ accountId }).pipe( |
| 19 | Effect.retry({ |
| 20 | while: (e) => e._tag === "Forbidden", |
| 21 | schedule: Schedule.exponential("500 millis"), |
| 22 | times: 8, |
| 23 | }), |
| 24 | ); |
| 25 | |
| 26 | // Seed a deterministic baseline for the blocks under management. Blocks |
| 27 | // that were never set on the account cannot be restored (Cloudflare's |
| 28 | // PATCH ignores `null`), so the capture-and-restore assertion needs the |
| 29 | // blocks to exist with known values before the stack manages them. |
| 30 | const seedBaseline = (accountId: string) => |
| 31 | zeroTrust |
| 32 | .patchGatewayConfiguration({ |
| 33 | accountId, |
| 34 | settings: { |
| 35 | activityLog: { enabled: true }, |
| 36 | protocolDetection: { enabled: false }, |
| 37 | }, |
| 38 | }) |
| 39 | .pipe( |
| 40 | Effect.retry({ |
| 41 | while: (e) => e._tag === "Forbidden", |
| 42 | schedule: Schedule.exponential("500 millis"), |
| 43 | times: 8, |
| 44 | }), |
| 45 | ); |
| 46 | |
| 47 | test.provider( |
| 48 | "manage activityLog and protocolDetection, then restore on destroy", |
| 49 | (stack) => |
| 50 | Effect.gen(function* () { |
| 51 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 52 | |
| 53 | yield* stack.destroy(); |
| 54 | |
| 55 | // Seed and capture a deterministic pre-test state out-of-band so |
| 56 | // we can verify the capture-and-restore behaviour at the end. |
| 57 | yield* seedBaseline(accountId); |
| 58 | const baseline = yield* getConfiguration(accountId); |
| 59 | const baselineActivityLog = baseline.settings?.activityLog?.enabled; |
| 60 | const baselineProtocolDetection = baseline.settings?.protocolDetection?.enabled; |
| 61 | expect(baselineActivityLog).toEqual(true); |
| 62 | expect(baselineProtocolDetection).toEqual(false); |
| 63 | |
| 64 | // Drive both toggles to the opposite of the baseline so the deploy |
| 65 | // is guaranteed to change something. |
| 66 | const flippedActivityLog = false; |
| 67 | const flippedProtocolDetection = true; |
| 68 | |
| 69 | const config = yield* stack.deploy( |
| 70 | Cloudflare.Gateway.Configuration("Gateway", { |
| 71 | settings: { |
| 72 | activityLog: { enabled: flippedActivityLog }, |
| 73 | protocolDetection: { enabled: flippedProtocolDetection }, |
| 74 | }, |
| 75 | }), |
| 76 | ); |
| 77 | expect(config.accountId).toEqual(accountId); |
| 78 | // The pre-management blocks were captured for restore. |
| 79 | expect(Object.keys(config.initialSettings).sort()).toEqual([ |
| 80 | "activityLog", |
| 81 | "protocolDetection", |
| 82 | ]); |
| 83 | |
| 84 | const live = yield* getConfiguration(accountId); |
| 85 | expect(live.settings?.activityLog?.enabled).toEqual(flippedActivityLog); |
| 86 | expect(live.settings?.protocolDetection?.enabled).toEqual(flippedProtocolDetection); |
| 87 | |
| 88 | // Update in place — flip activityLog back; protocolDetection stays. |
| 89 | const updated = yield* stack.deploy( |
| 90 | Cloudflare.Gateway.Configuration("Gateway", { |
| 91 | settings: { |
| 92 | activityLog: { enabled: !flippedActivityLog }, |
| 93 | protocolDetection: { enabled: flippedProtocolDetection }, |
| 94 | }, |
| 95 | }), |
| 96 | ); |
| 97 | // The original capture survives the update. |
| 98 | expect(updated.initialSettings).toEqual(config.initialSettings); |
| 99 | |
| 100 | const liveAfter = yield* getConfiguration(accountId); |
| 101 | expect(liveAfter.settings?.activityLog?.enabled).toEqual(!flippedActivityLog); |
| 102 | |
| 103 | // Destroy restores the captured pre-management values. |
| 104 | yield* stack.destroy(); |
| 105 | const restored = yield* getConfiguration(accountId); |
| 106 | expect(restored.settings?.activityLog?.enabled).toEqual(baselineActivityLog); |
| 107 | expect(restored.settings?.protocolDetection?.enabled).toEqual(baselineProtocolDetection); |
| 108 | }).pipe(logLevel), |
| 109 | { |
| 110 | tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"], |
| 111 | timeout: 120_000, |
| 112 | }, |
| 113 | ); |
| 114 | |
| 115 | test.provider( |
| 116 | "list returns the account Gateway configuration singleton", |
| 117 | (stack) => |
| 118 | Effect.gen(function* () { |
| 119 | yield* stack.destroy(); |
| 120 | |
| 121 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 122 | |
| 123 | const provider = yield* Provider.findProvider(Cloudflare.Gateway.Configuration); |
| 124 | const all = yield* provider.list(); |
| 125 | |
| 126 | // Account-wide singleton: exactly one element for the ambient account. |
| 127 | expect(all.length).toEqual(1); |
| 128 | expect(all[0]!.accountId).toEqual(accountId); |
| 129 | expect(all[0]!.initialSettings).toEqual({}); |
| 130 | |
| 131 | yield* stack.destroy(); |
| 132 | }).pipe(logLevel), |
| 133 | { |
| 134 | tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"], |
| 135 | timeout: 120_000, |
| 136 | }, |
| 137 | ); |