@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 220 lines 7.96 KB
Raw
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 rulesets from "@distilled.cloud/cloudflare/rulesets";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Result from "effect/Result";
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 // A freshly minted scoped token propagates eventually-consistently across
17 // Cloudflare's edge — retry the typed `Forbidden` blips on out-of-band calls.
18 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
19
20 const phase = "http_request_firewall_custom";
21
22 const resolveAccountId = Effect.gen(function* () {
23 const { accountId } = yield* yield* CloudflareEnvironment;
24 return accountId;
25 });
26
27 const getRuleset = (accountId: string, rulesetId: string) =>
28 rulesets.getRulesetForAccount({ accountId, rulesetId }).pipe(
29 Effect.retry({
30 while: (e) => e._tag === "Forbidden",
31 schedule: forbiddenRetrySchedule,
32 times: 8,
33 }),
34 Effect.catchTag("RulesetNotFound", () => Effect.succeed(undefined)),
35 );
36
37 // Account-level custom rulesets require an Enterprise plan. On the standard
38 // testing account every create fails with the typed `PhaseNotEntitled` error
39 // (code 50002: "not entitled to use the phase http_request_firewall_custom").
40 // The test probes once: unentitled accounts assert the typed tag; entitled
41 // accounts run the full lifecycle.
42 test.provider(
43 "custom ruleset lifecycle (typed PhaseNotEntitled on unentitled accounts)",
44 (stack) =>
45 Effect.gen(function* () {
46 const accountId = yield* resolveAccountId;
47
48 yield* stack.destroy();
49
50 const probe = yield* rulesets
51 .createRulesetForAccount({
52 accountId,
53 kind: "custom",
54 name: "alchemy-customruleset-probe",
55 phase,
56 rules: [],
57 })
58 .pipe(
59 Effect.retry({
60 while: (e) => e._tag === "Forbidden",
61 schedule: forbiddenRetrySchedule,
62 times: 8,
63 }),
64 Effect.result,
65 );
66
67 if (Result.isFailure(probe)) {
68 // Unentitled — the distilled call must fail with the typed
69 // entitlement tag, never an untyped catch-all.
70 expect(probe.failure._tag).toEqual("PhaseNotEntitled");
71 yield* stack.destroy();
72 return;
73 }
74
75 // Entitled — clean up the probe ruleset and run the full lifecycle.
76 yield* rulesets
77 .deleteRulesetForAccount({ accountId, rulesetId: probe.success.id })
78 .pipe(Effect.catchTag("RulesetNotFound", () => Effect.void));
79
80 const created = yield* stack.deploy(
81 Effect.gen(function* () {
82 return yield* Cloudflare.Ruleset.CustomRuleset("WafRules", {
83 phase,
84 description: "alchemy custom ruleset v1",
85 rules: [
86 {
87 description: "Block exploit probes",
88 expression: `lower(http.request.uri.path) contains "/.env"`,
89 action: "block",
90 },
91 ],
92 });
93 }),
94 );
95 expect(created.accountId).toEqual(accountId);
96 expect(created.kind).toEqual("custom");
97 expect(created.phase).toEqual(phase);
98 expect(created.rules).toHaveLength(1);
99
100 // Out-of-band verification via the distilled API.
101 const live = yield* getRuleset(accountId, created.rulesetId);
102 expect(live?.description).toEqual("alchemy custom ruleset v1");
103 expect(live?.rules?.[0]).toMatchObject({
104 action: "block",
105 expression: `lower(http.request.uri.path) contains "/.env"`,
106 });
107
108 // Update rules + description in place — same physical ruleset.
109 const updated = yield* stack.deploy(
110 Effect.gen(function* () {
111 return yield* Cloudflare.Ruleset.CustomRuleset("WafRules", {
112 phase,
113 description: "alchemy custom ruleset v2",
114 rules: [
115 {
116 description: "Challenge exploit probes",
117 expression: `lower(http.request.uri.path) contains "/.env"`,
118 action: "managed_challenge",
119 },
120 ],
121 });
122 }),
123 );
124 expect(updated.rulesetId).toEqual(created.rulesetId);
125 expect(updated.description).toEqual("alchemy custom ruleset v2");
126 expect(updated.rules[0]?.action).toEqual("managed_challenge");
127
128 const patched = yield* getRuleset(accountId, created.rulesetId);
129 expect(patched?.rules?.[0]?.action).toEqual("managed_challenge");
130
131 // Destroy and verify the ruleset is gone (typed not-found read).
132 yield* stack.destroy();
133 const gone = yield* getRuleset(accountId, created.rulesetId);
134 expect(gone).toBeUndefined();
135 }).pipe(logLevel),
136 {
137 tags: ["provider:cloudflare", "provider:cloudflare:ruleset", "live"],
138 timeout: 120_000,
139 },
140 );
141
142 // Canonical `list()` test (account-scoped collection). `list()` enumerates the
143 // account's rulesets, filters to `kind: custom`, and hydrates each into the
144 // full `read` Attributes shape. Account-level custom WAF phases require an
145 // Enterprise plan: unentitled accounts (the standard testing account) fail the
146 // create with the typed `PhaseNotEntitled` error, so we still verify `list()`
147 // returns an array without throwing; entitled accounts additionally deploy a
148 // ruleset and assert it appears in the exhaustively-paginated result.
149 test.provider(
150 "list enumerates the deployed custom ruleset",
151 (stack) =>
152 Effect.gen(function* () {
153 const accountId = yield* resolveAccountId;
154
155 yield* stack.destroy();
156
157 const probe = yield* rulesets
158 .createRulesetForAccount({
159 accountId,
160 kind: "custom",
161 name: "alchemy-customruleset-list-probe",
162 phase,
163 rules: [],
164 })
165 .pipe(
166 Effect.retry({
167 while: (e) => e._tag === "Forbidden",
168 schedule: forbiddenRetrySchedule,
169 times: 8,
170 }),
171 Effect.result,
172 );
173
174 if (Result.isFailure(probe)) {
175 // Unentitled — assert the typed entitlement tag, then verify the
176 // provider's `list()` still enumerates without error (returns []).
177 expect(probe.failure._tag).toEqual("PhaseNotEntitled");
178 const provider = yield* Provider.findProvider(Cloudflare.Ruleset.CustomRuleset);
179 const all = yield* provider.list();
180 expect(Array.isArray(all)).toBe(true);
181 yield* stack.destroy();
182 return;
183 }
184
185 // Entitled — clean up the probe and run the full list assertion.
186 yield* rulesets
187 .deleteRulesetForAccount({ accountId, rulesetId: probe.success.id })
188 .pipe(Effect.catchTag("RulesetNotFound", () => Effect.void));
189
190 const deployed = yield* stack.deploy(
191 Effect.gen(function* () {
192 return yield* Cloudflare.Ruleset.CustomRuleset("ListWafRules", {
193 phase,
194 description: "alchemy custom ruleset list",
195 rules: [
196 {
197 description: "Block exploit probes",
198 expression: `lower(http.request.uri.path) contains "/.env"`,
199 action: "block",
200 },
201 ],
202 });
203 }),
204 );
205
206 const provider = yield* Provider.findProvider(Cloudflare.Ruleset.CustomRuleset);
207 const all = yield* provider.list();
208
209 expect(all.some((r) => r.rulesetId === deployed.rulesetId)).toBe(true);
210 const found = all.find((r) => r.rulesetId === deployed.rulesetId);
211 expect(found?.kind).toEqual("custom");
212 expect(found?.rules).toHaveLength(1);
213
214 yield* stack.destroy();
215 }).pipe(logLevel),
216 {
217 tags: ["provider:cloudflare", "provider:cloudflare:ruleset", "live"],
218 timeout: 120_000,
219 },
220 );