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 Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 3 | import { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup"; |
| 4 | import { describe, expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as cache from "@distilled.cloud/cloudflare/cache"; |
| 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 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 16 | |
| 17 | // Cache Reserve is a usage-billed add-on. On the testing account's zone |
| 18 | // every GET/PATCH fails with "Sorry, this zone setting is not available for |
| 19 | // your plan type.", surfaced as the typed `SettingUnavailableForPlan` error. |
| 20 | // The full lifecycle test below is gated behind an entitled zone id |
| 21 | // supplied via env. |
| 22 | const entitledZoneId = process.env.CLOUDFLARE_TEST_CACHE_RESERVE_ZONE_ID; |
| 23 | |
| 24 | const resolveZoneId = Effect.gen(function* () { |
| 25 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 26 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 27 | if (!zone) { |
| 28 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 29 | } |
| 30 | return zone.id; |
| 31 | }); |
| 32 | |
| 33 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 34 | |
| 35 | const getCacheReserve = (zoneId: string) => |
| 36 | cache.getCacheReserve({ zoneId }).pipe( |
| 37 | Effect.retry({ |
| 38 | while: (e) => e._tag === "Forbidden", |
| 39 | schedule: forbiddenRetrySchedule, |
| 40 | times: 8, |
| 41 | }), |
| 42 | ); |
| 43 | |
| 44 | const setBaseline = (zoneId: string, value: "on" | "off") => |
| 45 | cache.patchCacheReserve({ zoneId, value }).pipe( |
| 46 | Effect.retry({ |
| 47 | while: (e) => e._tag === "Forbidden", |
| 48 | schedule: forbiddenRetrySchedule, |
| 49 | times: 8, |
| 50 | }), |
| 51 | ); |
| 52 | |
| 53 | // Both cases mutate the same zone-level Cache Reserve singleton; run them serially so they don't corrupt each other's captured `initialValue` under the global concurrent test config. |
| 54 | describe.sequential( |
| 55 | "Reserve", |
| 56 | { tags: ["provider:cloudflare", "provider:cloudflare:cache", "live"] }, |
| 57 | () => { |
| 58 | test.provider( |
| 59 | "surfaces the typed SettingUnavailableForPlan error on unentitled zones", |
| 60 | (stack) => |
| 61 | Effect.gen(function* () { |
| 62 | const zoneId = yield* resolveZoneId; |
| 63 | |
| 64 | yield* stack.destroy(); |
| 65 | |
| 66 | // The standard testing zone lacks the Cache Reserve subscription — |
| 67 | // the distilled call must fail with the typed entitlement tag. |
| 68 | const error = yield* cache.getCacheReserve({ zoneId }).pipe( |
| 69 | Effect.retry({ |
| 70 | while: (e) => e._tag === "Forbidden", |
| 71 | schedule: forbiddenRetrySchedule, |
| 72 | times: 8, |
| 73 | }), |
| 74 | Effect.flip, |
| 75 | ); |
| 76 | expect(error._tag).toEqual("SettingUnavailableForPlan"); |
| 77 | |
| 78 | yield* stack.destroy(); |
| 79 | }).pipe(logLevel), |
| 80 | { tags: ["provider:cloudflare:zone"] }, |
| 81 | ); |
| 82 | |
| 83 | test.provider.skipIf(!entitledZoneId)( |
| 84 | "enables Cache Reserve and restores the original value on destroy", |
| 85 | (stack) => |
| 86 | Effect.gen(function* () { |
| 87 | const zoneId = entitledZoneId!; |
| 88 | |
| 89 | yield* stack.destroy(); |
| 90 | // Known baseline: Cache Reserve defaults to "off". |
| 91 | yield* setBaseline(zoneId, "off"); |
| 92 | |
| 93 | const setting = yield* stack.deploy( |
| 94 | Effect.gen(function* () { |
| 95 | return yield* Cloudflare.Cache.Reserve("Reserve", { zoneId }); |
| 96 | }), |
| 97 | ); |
| 98 | |
| 99 | expect(setting.zoneId).toEqual(zoneId); |
| 100 | expect(setting.value).toEqual("on"); |
| 101 | // The pre-management value was captured for restore-on-destroy. |
| 102 | expect(setting.initialValue).toEqual("off"); |
| 103 | |
| 104 | // Out-of-band verification via the distilled API. |
| 105 | const live = yield* getCacheReserve(zoneId); |
| 106 | expect(live.value).toEqual("on"); |
| 107 | |
| 108 | // Update in place — same singleton, initialValue survives. |
| 109 | const updated = yield* stack.deploy( |
| 110 | Effect.gen(function* () { |
| 111 | return yield* Cloudflare.Cache.Reserve("Reserve", { zoneId, enabled: false }); |
| 112 | }), |
| 113 | ); |
| 114 | expect(updated.value).toEqual("off"); |
| 115 | expect(updated.initialValue).toEqual("off"); |
| 116 | |
| 117 | yield* stack.destroy(); |
| 118 | |
| 119 | // Destroy restored the value the setting had before we managed it. |
| 120 | const restored = yield* getCacheReserve(zoneId); |
| 121 | expect(restored.value).toEqual("off"); |
| 122 | }).pipe(logLevel), |
| 123 | ); |
| 124 | |
| 125 | // Canonical `list()` test (zone-scoped singleton): there is no account-wide |
| 126 | // API for this per-zone setting, so `list()` enumerates every zone via |
| 127 | // `listAllZones` and reads the singleton in each. Cache Reserve is an |
| 128 | // entitlement-gated add-on, so unentitled zones are filtered out via the |
| 129 | // typed `SettingUnavailableForPlan` skip — on the standard testing account |
| 130 | // the result is therefore an array (possibly empty) and must not throw. |
| 131 | test.provider("list enumerates Cache Reserve across all zones", (stack) => |
| 132 | Effect.gen(function* () { |
| 133 | yield* stack.destroy(); |
| 134 | |
| 135 | const provider = yield* Provider.findProvider(Cloudflare.Cache.Reserve); |
| 136 | const all = yield* provider.list(); |
| 137 | |
| 138 | // Enumeration + typed entitlement skip succeed: a plain array, every |
| 139 | // element carrying the full Attributes shape. |
| 140 | expect(Array.isArray(all)).toBe(true); |
| 141 | for (const entry of all) { |
| 142 | expect(typeof entry.zoneId).toBe("string"); |
| 143 | expect(typeof entry.value).toBe("string"); |
| 144 | } |
| 145 | |
| 146 | yield* stack.destroy(); |
| 147 | }).pipe(logLevel), |
| 148 | ); |
| 149 | |
| 150 | // On an entitled account, the entitled zone's singleton is returned by |
| 151 | // `list()`. Gated behind the same env var as the lifecycle test. |
| 152 | test.provider.skipIf(!entitledZoneId)( |
| 153 | "list includes the entitled zone's Cache Reserve setting", |
| 154 | (stack) => |
| 155 | Effect.gen(function* () { |
| 156 | const zoneId = entitledZoneId!; |
| 157 | |
| 158 | const provider = yield* Provider.findProvider(Cloudflare.Cache.Reserve); |
| 159 | const all = yield* provider.list(); |
| 160 | |
| 161 | expect(all.some((s) => s.zoneId === zoneId)).toBe(true); |
| 162 | |
| 163 | // `stack` is unused (the singleton always exists on entitled zones), |
| 164 | // but keep the destroy bookend so the harness state stays clean. |
| 165 | yield* stack.destroy(); |
| 166 | }).pipe(logLevel), |
| 167 | ); |
| 168 | }, |
| 169 | ); |