@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 165 lines 6.33 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 { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as aiSecurity from "@distilled.cloud/cloudflare/ai-security";
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
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
17
18 // AI Security for Apps (Firewall for AI) is entitlement-gated — on the
19 // standard testing account every call fails with "not entitled to access
20 // this resource" (code 13101), surfaced as the typed `AiSecurityNotEntitled`
21 // error. The full lifecycle test below is gated behind an entitled zone id
22 // supplied via env.
23 const entitledZoneId = process.env.CLOUDFLARE_TEST_AI_SECURITY_ZONE_ID;
24
25 const resolveZoneId = Effect.gen(function* () {
26 const { accountId } = yield* yield* CloudflareEnvironment;
27 const zone = yield* findZoneByName({ accountId, name: zoneName });
28 if (!zone) {
29 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
30 }
31 return zone.id;
32 });
33
34 // A freshly minted scoped token propagates eventually-consistently across
35 // Cloudflare's edge — retry the typed `Forbidden` blips on out-of-band calls.
36 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
37
38 const getSettings = (zoneId: string) =>
39 aiSecurity.getAiSecurity({ zoneId }).pipe(
40 Effect.retry({
41 while: (e) => e._tag === "Forbidden",
42 schedule: forbiddenRetrySchedule,
43 times: 8,
44 }),
45 );
46
47 // Normalize the setting to a known baseline so each run starts from the
48 // same cloud state regardless of what a previous run left behind.
49 const setBaseline = (zoneId: string, enabled: boolean) =>
50 aiSecurity.putAiSecurity({ zoneId, enabled }).pipe(
51 Effect.retry({
52 while: (e) => e._tag === "Forbidden",
53 schedule: forbiddenRetrySchedule,
54 times: 8,
55 }),
56 );
57
58 test.provider(
59 "surfaces the typed AiSecurityNotEntitled error on unentitled accounts",
60 (stack) =>
61 Effect.gen(function* () {
62 const zoneId = yield* resolveZoneId;
63
64 yield* stack.destroy();
65
66 // Settings may be entitled independently of Custom Topics. The
67 // unentitled path still has to surface the typed tag (never the
68 // catch-all); an entitled zone returns the singleton instead.
69 const settings = yield* getSettings(zoneId).pipe(
70 Effect.catchTag("AiSecurityNotEntitled", () => Effect.succeed(undefined)),
71 );
72 if (settings !== undefined) {
73 expect(typeof (settings.enabled ?? false)).toBe("boolean");
74 }
75
76 const topicsError = yield* aiSecurity.getCustomTopic({ zoneId }).pipe(
77 Effect.retry({
78 while: (e) => e._tag === "Forbidden",
79 schedule: forbiddenRetrySchedule,
80 times: 8,
81 }),
82 Effect.flip,
83 );
84 expect(topicsError._tag).toEqual("AiSecurityNotEntitled");
85
86 yield* stack.destroy();
87 }).pipe(logLevel),
88 { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] },
89 );
90
91 // Canonical `list()` test (zone-scoped singleton): there is no account-wide
92 // API for this per-zone setting, so `list()` enumerates every zone via
93 // `listAllZones` and reads the singleton in each, skipping zones that reject
94 // the route (AI Security is entitlement-gated). It always returns a well-typed
95 // `Attributes[]` — empty on the unentitled testing account, non-empty and
96 // containing the entitled zone when one is supplied via env.
97 test.provider(
98 "list enumerates the setting across all entitled zones",
99 (stack) =>
100 Effect.gen(function* () {
101 const provider = yield* Provider.findProvider(Cloudflare.AI.SecuritySettings);
102 const all = yield* provider.list();
103
104 expect(Array.isArray(all)).toBe(true);
105 // Every element is the full Attributes shape `read` produces.
106 for (const settings of all) {
107 expect(typeof settings.zoneId).toBe("string");
108 expect(typeof settings.enabled).toBe("boolean");
109 expect(typeof settings.initialEnabled).toBe("boolean");
110 }
111 // On an entitled account, the supplied zone must appear in the result.
112 if (entitledZoneId) {
113 expect(all.some((s) => s.zoneId === entitledZoneId)).toBe(true);
114 }
115
116 yield* stack.destroy();
117 }).pipe(logLevel),
118 { tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"] },
119 );
120
121 test.provider.skipIf(!entitledZoneId)(
122 "pins enabled, updates in place, and restores the original on destroy",
123 (stack) =>
124 Effect.gen(function* () {
125 const zoneId = entitledZoneId!;
126
127 yield* stack.destroy();
128 // Known baseline: AI Security disabled.
129 yield* setBaseline(zoneId, false);
130
131 const created = yield* stack.deploy(
132 Effect.gen(function* () {
133 return yield* Cloudflare.AI.SecuritySettings("AiSecurity", { zoneId, enabled: true });
134 }),
135 );
136
137 expect(created.zoneId).toEqual(zoneId);
138 expect(created.enabled).toEqual(true);
139 // The pre-management value was captured for restore-on-destroy.
140 expect(created.initialEnabled).toEqual(false);
141
142 // Out-of-band verification via the distilled API.
143 const live = yield* getSettings(zoneId);
144 expect(live.enabled ?? false).toEqual(true);
145
146 // Update in place — same singleton, initial value survives.
147 const updated = yield* stack.deploy(
148 Effect.gen(function* () {
149 return yield* Cloudflare.AI.SecuritySettings("AiSecurity", { zoneId, enabled: false });
150 }),
151 );
152 expect(updated.enabled).toEqual(false);
153 expect(updated.initialEnabled).toEqual(false);
154
155 const toggled = yield* getSettings(zoneId);
156 expect(toggled.enabled ?? false).toEqual(false);
157
158 yield* stack.destroy();
159
160 // Destroy restored the value the zone had before we managed it.
161 const restored = yield* getSettings(zoneId);
162 expect(restored.enabled ?? false).toEqual(false);
163 }).pipe(logLevel),
164 { tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"], timeout: 120_000 },
165 );