The best way to control Claude Code, Codex and more from any computer or phone

main
ts 153 lines 5.77 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 originTls from "@distilled.cloud/cloudflare/origin-tls-client-auth";
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 const resolveZoneId = Effect.gen(function* () {
18 const { accountId } = yield* yield* CloudflareEnvironment;
19 const zone = yield* findZoneByName({ accountId, name: zoneName });
20 if (!zone) {
21 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
22 }
23 return zone.id;
24 });
25
26 // The scoped API token the test harness mints propagates eventually-
27 // consistently across Cloudflare's edge — a fresh token intermittently 403s
28 // with "Unable to authenticate request". Ride out the blips on the test's
29 // own out-of-band calls by retrying the typed `Forbidden` error (part of
30 // each operation's error union via distilled patches).
31 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
32
33 const getSetting = (zoneId: string) =>
34 originTls.getSetting({ zoneId }).pipe(
35 Effect.retry({
36 while: (e) => e._tag === "Forbidden",
37 schedule: forbiddenRetrySchedule,
38 times: 8,
39 }),
40 );
41
42 // Normalize the singleton to a known baseline so each run starts from the
43 // same cloud state regardless of what a previous (possibly interrupted) run
44 // left behind.
45 const setBaseline = (zoneId: string, enabled: boolean) =>
46 originTls.putSetting({ zoneId, enabled }).pipe(
47 Effect.retry({
48 while: (e) => e._tag === "Forbidden",
49 schedule: forbiddenRetrySchedule,
50 times: 8,
51 }),
52 );
53
54 // Both cases toggle the same zone-level AOP singleton; run them serially so they don't corrupt each other's captured `initialEnabled` under the global concurrent test config.
55 describe.sequential(
56 "Setting",
57 {
58 tags: [
59 "provider:cloudflare",
60 "provider:cloudflare:origintlsclientauth",
61 "provider:cloudflare:zone",
62 "live",
63 ],
64 },
65 () => {
66 test.provider("enables AOP and restores the original value on destroy", (stack) =>
67 Effect.gen(function* () {
68 const zoneId = yield* resolveZoneId;
69
70 yield* stack.destroy();
71 // Known baseline: zone-level AOP defaults to disabled.
72 yield* setBaseline(zoneId, false);
73
74 const setting = yield* stack.deploy(
75 Cloudflare.OriginTlsClientAuth.Setting("Aop", { zoneId, enabled: true }),
76 );
77
78 expect(setting.zoneId).toEqual(zoneId);
79 expect(setting.enabled).toEqual(true);
80 // The pre-management value is captured for restore-on-destroy.
81 expect(setting.initialEnabled).toEqual(false);
82
83 const observed = yield* getSetting(zoneId);
84 expect(observed.enabled).toEqual(true);
85
86 yield* stack.destroy();
87
88 // Destroy restores the captured baseline, not a hardcoded default.
89 const restored = yield* getSetting(zoneId);
90 expect(restored.enabled).toEqual(false);
91 }).pipe(logLevel),
92 );
93
94 test.provider("updates the enabled flag in place", (stack) =>
95 Effect.gen(function* () {
96 const zoneId = yield* resolveZoneId;
97
98 yield* stack.destroy();
99 yield* setBaseline(zoneId, false);
100
101 const enabled = yield* stack.deploy(
102 Cloudflare.OriginTlsClientAuth.Setting("AopUpdate", { zoneId, enabled: true }),
103 );
104 expect(enabled.enabled).toEqual(true);
105 expect(enabled.initialEnabled).toEqual(false);
106
107 // In-place update — the singleton is never replaced.
108 const disabled = yield* stack.deploy(
109 Cloudflare.OriginTlsClientAuth.Setting("AopUpdate", { zoneId, enabled: false }),
110 );
111 expect(disabled.enabled).toEqual(false);
112 // The original baseline survives the update.
113 expect(disabled.initialEnabled).toEqual(false);
114
115 const observed = yield* getSetting(zoneId);
116 expect(observed.enabled).toEqual(false);
117
118 yield* stack.destroy();
119
120 const restored = yield* getSetting(zoneId);
121 expect(restored.enabled).toEqual(false);
122 }).pipe(logLevel),
123 );
124
125 // Canonical `list()` test (zone-scoped singleton): there is no account-wide
126 // API for this per-zone setting, so `list()` enumerates every zone via
127 // `listAllZones` and reads the singleton in each. Assert the result is
128 // non-empty and contains the standing test zone.
129 test.provider("list enumerates the setting across all zones", (stack) =>
130 Effect.gen(function* () {
131 const zoneId = yield* resolveZoneId;
132
133 const provider = yield* Provider.findProvider(Cloudflare.OriginTlsClientAuth.Setting);
134 // Ride out fresh-token 403 blips on the account-wide enumeration, like
135 // every other out-of-band call in this suite.
136 const all = yield* provider.list().pipe(
137 Effect.retry({
138 while: (e) => e._tag === "Forbidden",
139 schedule: forbiddenRetrySchedule,
140 times: 8,
141 }),
142 );
143
144 expect(all.length).toBeGreaterThan(0);
145 expect(all.some((s) => s.zoneId === zoneId)).toBe(true);
146
147 // `stack` is unused here (the singleton always exists on every zone),
148 // but keep the destroy bookend so the harness state stays clean.
149 yield* stack.destroy();
150 }).pipe(logLevel),
151 );
152 },
153 );