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