@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 155 lines 5.46 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 schemaValidation from "@distilled.cloud/cloudflare/schema-validation";
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 const resolveZoneId = Effect.gen(function* () {
19 const { accountId } = yield* yield* CloudflareEnvironment;
20 const zone = yield* findZoneByName({ accountId, name: zoneName });
21 if (!zone) {
22 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
23 }
24 return zone.id;
25 });
26
27 // Retry 403 blips while the harness-minted scoped token propagates.
28 const getOverrideOob = (zoneId: string, operationId: string) =>
29 schemaValidation.getSettingOperation({ zoneId, operationId }).pipe(
30 Effect.retry({
31 while: (e) => e._tag === "Forbidden",
32 schedule: Schedule.exponential("500 millis"),
33 times: 8,
34 }),
35 );
36
37 test.provider(
38 "sets a per-operation override, updates it in place, and clears it on destroy",
39 (stack) =>
40 Effect.gen(function* () {
41 const zoneId = yield* resolveZoneId;
42
43 yield* stack.destroy();
44
45 const program = (mitigationAction: Cloudflare.SchemaValidation.OperationMitigationAction) =>
46 Effect.gen(function* () {
47 const op = yield* Cloudflare.ApiShield.Operation("TestOp", {
48 zoneId,
49 method: "GET",
50 host: zoneName,
51 endpoint: "/alchemy-sv-operation-setting-test",
52 });
53 const override = yield* Cloudflare.SchemaValidation.OperationSetting("TestOverride", {
54 zoneId,
55 operationId: op.operationId,
56 mitigationAction,
57 });
58 return { op, override };
59 });
60
61 // "log" is plan-gated on this zone (UnentitledMitigationAction,
62 // code 11400) — exercise the entitled "block"/"none" actions.
63 const first = yield* stack.deploy(program("block"));
64 expect(first.override.zoneId).toEqual(zoneId);
65 expect(first.override.operationId).toEqual(first.op.operationId);
66 expect(first.override.mitigationAction).toEqual("block");
67
68 const live = yield* getOverrideOob(zoneId, first.op.operationId);
69 expect(live.mitigationAction).toEqual("block");
70
71 // The PUT is a true upsert — flipping the action updates in place.
72 const second = yield* stack.deploy(program("none"));
73 expect(second.override.operationId).toEqual(first.op.operationId);
74 expect(second.override.mitigationAction).toEqual("none");
75
76 const liveUpdated = yield* getOverrideOob(zoneId, first.op.operationId);
77 expect(liveUpdated.mitigationAction).toEqual("none");
78
79 const operationId = first.op.operationId;
80 yield* stack.destroy();
81
82 // Destroy cleared the override (the operation itself is destroyed
83 // too, which cascades — either way the override is gone).
84 const gone = yield* schemaValidation
85 .getSettingOperation({ zoneId, operationId })
86 .pipe(Effect.flip);
87 expect(gone._tag).toEqual("OperationNotFound");
88 }).pipe(logLevel),
89 {
90 tags: [
91 "provider:cloudflare",
92 "provider:cloudflare:apishield",
93 "provider:cloudflare:schemavalidation",
94 "provider:cloudflare:zone",
95 "live",
96 ],
97 timeout: 120_000,
98 },
99 );
100
101 // Canonical `list()` test (zone-scoped collection): there is no account-wide
102 // API for per-operation overrides, so `list()` enumerates every zone via
103 // `listAllZones` and exhaustively paginates each zone's operation settings.
104 // Deploy one override and assert it appears in the result, keyed by its
105 // (zoneId, operationId) identity.
106 test.provider(
107 "list enumerates the deployed per-operation override",
108 (stack) =>
109 Effect.gen(function* () {
110 const zoneId = yield* resolveZoneId;
111
112 yield* stack.destroy();
113
114 const deployed = yield* stack.deploy(
115 Effect.gen(function* () {
116 const op = yield* Cloudflare.ApiShield.Operation("ListOp", {
117 zoneId,
118 method: "GET",
119 host: zoneName,
120 endpoint: "/alchemy-sv-operation-setting-list-test",
121 });
122 const override = yield* Cloudflare.SchemaValidation.OperationSetting("ListOverride", {
123 zoneId,
124 operationId: op.operationId,
125 mitigationAction: "block",
126 });
127 return { op, override };
128 }),
129 );
130
131 const provider = yield* Provider.findProvider(Cloudflare.SchemaValidation.OperationSetting);
132 const all = yield* provider.list();
133
134 expect(
135 all.some(
136 (o) =>
137 o.zoneId === zoneId &&
138 o.operationId === deployed.op.operationId &&
139 o.mitigationAction === "block",
140 ),
141 ).toBe(true);
142
143 yield* stack.destroy();
144 }).pipe(logLevel),
145 {
146 tags: [
147 "provider:cloudflare",
148 "provider:cloudflare:apishield",
149 "provider:cloudflare:schemavalidation",
150 "provider:cloudflare:zone",
151 "live",
152 ],
153 timeout: 120_000,
154 },
155 );