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

main
ts 137 lines 5.36 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 emailRouting from "@distilled.cloud/cloudflare/email-routing";
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 import { EMAIL_ROUTING_ZONE, emailRoutingScoped } from "./scope.ts";
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 // The scoped API token the test harness mints propagates eventually-
28 // consistently across Cloudflare's edge — a fresh token intermittently 403s.
29 // Ride out the blips on the test's own out-of-band calls by retrying the
30 // typed `Forbidden` error (part of each email-routing operation's error union
31 // via distilled patches).
32 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
33
34 const getEmailRouting = (zoneId: string) =>
35 emailRouting.getEmailRouting({ zoneId }).pipe(
36 Effect.retry({
37 while: (e) => e._tag === "Forbidden",
38 schedule: forbiddenRetrySchedule,
39 times: 8,
40 }),
41 );
42
43 const setEnabled = (zoneId: string, enabled: boolean) =>
44 (enabled
45 ? emailRouting.enableEmailRouting({ zoneId })
46 : emailRouting.disableEmailRouting({ zoneId })
47 ).pipe(
48 Effect.retry({
49 while: (e) => e._tag === "Forbidden",
50 schedule: forbiddenRetrySchedule,
51 times: 8,
52 }),
53 );
54
55 describe.sequential.skipIf(!emailRoutingScoped)(
56 "EmailRouting",
57 {
58 tags: ["provider:cloudflare", "provider:cloudflare:email", "provider:cloudflare:zone", "live"],
59 },
60 () => {
61 // Canonical `list()` test (zone-scoped singleton): there is no account-wide
62 // API for these per-zone settings, so `list()` enumerates every zone via
63 // `listAllZones` and reads the singleton in each. Assert the result is
64 // non-empty and contains the standing test zone. Capture-and-restore the
65 // zone's `enabled` state so the run leaves no residue.
66 test.provider(
67 "list enumerates email routing across all zones",
68 (stack) =>
69 Effect.gen(function* () {
70 const zoneId = yield* resolveZoneId;
71
72 yield* stack.destroy();
73
74 // Capture the pre-test enabled state to restore at the end.
75 const before = yield* getEmailRouting(zoneId);
76
77 const provider = yield* Provider.findProvider(Cloudflare.Email.Routing);
78 const all = yield* provider.list();
79
80 expect(all.length).toBeGreaterThan(0);
81 const entry = all.find((r) => r.zoneId === zoneId);
82 expect(entry).toBeDefined();
83 expect(entry!.name).toEqual(zoneName);
84
85 // Restore the captured state.
86 yield* setEnabled(zoneId, before.enabled);
87
88 yield* stack.destroy();
89 }).pipe(logLevel),
90 { exclusive: EMAIL_ROUTING_ZONE },
91 );
92
93 // Subaddressing is a zone setting beside `enabled`: deploy it on, then
94 // off, checking the live setting each time. Destroying the stack
95 // disables routing on the zone, so capture-and-restore both settings.
96 test.provider(
97 "subaddressing converges in place",
98 (stack) =>
99 Effect.gen(function* () {
100 const zoneId = yield* resolveZoneId;
101 yield* stack.destroy();
102 const before = yield* getEmailRouting(zoneId);
103
104 const routing = (subaddressing?: boolean) =>
105 Effect.gen(function* () {
106 return yield* Cloudflare.Email.Routing("Routing", { zone: zoneId, subaddressing });
107 });
108
109 const on = yield* stack.deploy(routing(true));
110 expect(on.subaddressing).toEqual(true);
111 expect((yield* getEmailRouting(zoneId)).supportSubaddress).toEqual(true);
112
113 // Unchanged, or omitted (keep the zone's setting), plans no update.
114 expect((yield* stack.plan(routing(true))).resources.Routing?.action).toBe("noop");
115 expect((yield* stack.plan(routing())).resources.Routing?.action).toBe("noop");
116 expect((yield* stack.plan(routing(false))).resources.Routing?.action).toBe("update");
117
118 const off = yield* stack.deploy(routing(false));
119 expect(off.subaddressing).toEqual(false);
120 expect((yield* getEmailRouting(zoneId)).supportSubaddress).toEqual(false);
121
122 yield* stack.destroy();
123 yield* setEnabled(zoneId, before.enabled);
124 yield* emailRouting
125 .edit({ zoneId, supportSubaddress: before.supportSubaddress ?? false })
126 .pipe(
127 Effect.retry({
128 while: (e) => e._tag === "Forbidden",
129 schedule: forbiddenRetrySchedule,
130 times: 8,
131 }),
132 );
133 }).pipe(logLevel),
134 { exclusive: EMAIL_ROUTING_ZONE },
135 );
136 },
137 );