@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 158 lines 6.15 KB
Raw
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 // Regional Tiered Cache is Enterprise-only. On the testing account's zone
18 // every GET/PATCH fails with Cloudflare code 1135 ("Sorry, this zone setting
19 // is not available for your plan type."), surfaced as the typed
20 // `SettingUnavailableForPlan` error. The full lifecycle test below is gated
21 // behind an Enterprise zone id supplied via env.
22 const enterpriseZoneId = process.env.CLOUDFLARE_TEST_ENTERPRISE_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 getRegionalTieredCache = (zoneId: string) =>
36 cache.getRegionalTieredCache({ 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.patchRegionalTieredCache({ 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 Regional Tiered Cache singleton; run them serially so they don't corrupt each other's captured `initialValue` under the global concurrent test config.
54 describe.sequential(
55 "RegionalTieredCache",
56 { tags: ["provider:cloudflare", "provider:cloudflare:cache", "live"] },
57 () => {
58 test.provider(
59 "surfaces the typed SettingUnavailableForPlan error on non-Enterprise zones",
60 (stack) =>
61 Effect.gen(function* () {
62 const zoneId = yield* resolveZoneId;
63
64 yield* stack.destroy();
65
66 // The standard testing zone is plan-gated — both the out-of-band
67 // distilled call and a deploy must fail with the typed tag.
68 const error = yield* cache.getRegionalTieredCache({ 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(!enterpriseZoneId)(
84 "enables Regional Tiered Cache and restores the original value on destroy",
85 (stack) =>
86 Effect.gen(function* () {
87 const zoneId = enterpriseZoneId!;
88
89 yield* stack.destroy();
90 // Known baseline: Regional Tiered Cache defaults to "off".
91 yield* setBaseline(zoneId, "off");
92
93 const setting = yield* stack.deploy(
94 Effect.gen(function* () {
95 return yield* Cloudflare.Cache.RegionalTieredCache("RegionalCache", { 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* getRegionalTieredCache(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.RegionalTieredCache("RegionalCache", {
112 zoneId,
113 enabled: false,
114 });
115 }),
116 );
117 expect(updated.value).toEqual("off");
118 expect(updated.initialValue).toEqual("off");
119
120 yield* stack.destroy();
121
122 // Destroy restored the value the setting had before we managed it.
123 const restored = yield* getRegionalTieredCache(zoneId);
124 expect(restored.value).toEqual("off");
125 }).pipe(logLevel),
126 );
127
128 // Canonical `list()` test (zone-scoped singleton): there is no account-wide
129 // API for this per-zone setting, so `list()` enumerates every zone via
130 // `listAllZones` and reads the singleton in each. Regional Tiered Cache is
131 // Enterprise-only, so non-entitled zones surface `SettingUnavailableForPlan`
132 // and are skipped — on a non-Enterprise account the result is well-formed
133 // (often empty). The "contains the test zone" assertion only holds when an
134 // Enterprise zone id is supplied via env.
135 test.provider("list enumerates the setting across entitled zones", (stack) =>
136 Effect.gen(function* () {
137 yield* stack.destroy();
138
139 const provider = yield* Provider.findProvider(Cloudflare.Cache.RegionalTieredCache);
140 const all = yield* provider.list();
141
142 // Always well-formed: an array whose entries match the Attributes shape.
143 expect(Array.isArray(all)).toBe(true);
144 for (const row of all) {
145 expect(typeof row.zoneId).toBe("string");
146 expect(typeof row.value).toBe("string");
147 }
148
149 // On an Enterprise account, the entitled zone is enumerated.
150 if (enterpriseZoneId) {
151 expect(all.some((s) => s.zoneId === enterpriseZoneId)).toBe(true);
152 }
153
154 yield* stack.destroy();
155 }).pipe(logLevel),
156 );
157 },
158 );