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

main
ts 222 lines 7.18 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 zeroTrust from "@distilled.cloud/cloudflare/zero-trust";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import * as Option from "effect/Option";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Stream from "effect/Stream";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // Deterministic per-test CIDRs. Using 10.99.X.0/24 keeps each test's network
17 // disjoint from the others and from typical lab address space, so reruns and
18 // parallel runs don't collide.
19 const NETWORK_DEFAULT = "10.99.1.0/24";
20 const NETWORK_UPDATE = "10.99.2.0/24";
21 const NETWORK_ADOPT = "10.99.3.0/24";
22 const NETWORK_LIST = "10.99.4.0/24";
23
24 test.provider(
25 "create and delete route with default props",
26 (stack) =>
27 Effect.gen(function* () {
28 const { accountId } = yield* yield* CloudflareEnvironment;
29
30 yield* stack.destroy();
31
32 const { tunnel, route } = yield* stack.deploy(
33 Effect.gen(function* () {
34 const tunnel = yield* Cloudflare.Tunnel.Tunnel("RouteHostTunnel", {
35 adopt: true,
36 });
37 const route = yield* Cloudflare.Tunnel.Route("DefaultRoute", {
38 tunnelId: tunnel.tunnelId,
39 network: NETWORK_DEFAULT,
40 adopt: true,
41 });
42 return { tunnel, route };
43 }),
44 );
45
46 expect(route.routeId).toBeDefined();
47 expect(route.network).toEqual(NETWORK_DEFAULT);
48 expect(route.tunnelId).toEqual(tunnel.tunnelId);
49 expect(route.accountId).toEqual(accountId);
50
51 const actual = yield* zeroTrust.getNetworkRoute({
52 accountId,
53 routeId: route.routeId,
54 });
55 expect(actual.id).toEqual(route.routeId);
56 expect(actual.network).toEqual(NETWORK_DEFAULT);
57 expect(actual.tunnelId).toEqual(tunnel.tunnelId);
58
59 yield* stack.destroy();
60 }).pipe(logLevel),
61 { tags: ["provider:cloudflare", "provider:cloudflare:tunnel", "live"] },
62 );
63
64 test.provider(
65 "updating the comment patches in place",
66 (stack) =>
67 Effect.gen(function* () {
68 const { accountId } = yield* yield* CloudflareEnvironment;
69
70 yield* stack.destroy();
71
72 const initial = yield* stack.deploy(
73 Effect.gen(function* () {
74 const tunnel = yield* Cloudflare.Tunnel.Tunnel("RouteUpdateTunnel", {
75 adopt: true,
76 });
77 const route = yield* Cloudflare.Tunnel.Route("UpdateRoute", {
78 tunnelId: tunnel.tunnelId,
79 network: NETWORK_UPDATE,
80 comment: "v1",
81 adopt: true,
82 });
83 return { tunnel, route };
84 }),
85 );
86
87 expect(initial.route.comment).toEqual("v1");
88
89 const updated = yield* stack.deploy(
90 Effect.gen(function* () {
91 const tunnel = yield* Cloudflare.Tunnel.Tunnel("RouteUpdateTunnel", {
92 adopt: true,
93 });
94 const route = yield* Cloudflare.Tunnel.Route("UpdateRoute", {
95 tunnelId: tunnel.tunnelId,
96 network: NETWORK_UPDATE,
97 comment: "v2",
98 adopt: true,
99 });
100 return { tunnel, route };
101 }),
102 );
103
104 expect(updated.route.routeId).toEqual(initial.route.routeId);
105 expect(updated.route.comment).toEqual("v2");
106
107 const actual = yield* zeroTrust.getNetworkRoute({
108 accountId,
109 routeId: updated.route.routeId,
110 });
111 expect(actual.comment).toEqual("v2");
112
113 yield* stack.destroy();
114 }).pipe(logLevel),
115 { tags: ["provider:cloudflare", "provider:cloudflare:tunnel", "live"] },
116 );
117
118 test.provider(
119 "adopt: takes over a pre-existing route",
120 (stack) =>
121 Effect.gen(function* () {
122 const { accountId } = yield* yield* CloudflareEnvironment;
123
124 yield* stack.destroy();
125
126 // Stand up just the tunnel via the stack so we have a stable tunnelId to
127 // attach a manually-created route to.
128 const { tunnel } = yield* stack.deploy(
129 Effect.gen(function* () {
130 const tunnel = yield* Cloudflare.Tunnel.Tunnel("RouteAdoptTunnel", {
131 adopt: true,
132 });
133 return { tunnel };
134 }),
135 );
136
137 // Manually create the route outside of alchemy's state. The reconcile
138 // path with `adopt: true` should find this and take ownership rather
139 // than failing or creating a duplicate. Route networks are unique
140 // account-wide, so on Conflict (a prior failed run leaked the route)
141 // reuse the existing one — that's an equally valid pre-existing route.
142 const pre = yield* zeroTrust
143 .createNetworkRoute({
144 accountId,
145 tunnelId: tunnel.tunnelId,
146 network: NETWORK_ADOPT,
147 comment: "pre-existing",
148 })
149 .pipe(
150 Effect.catch(() =>
151 zeroTrust.listNetworkRoutes
152 .items({
153 accountId,
154 isDeleted: false,
155 networkSubset: NETWORK_ADOPT,
156 networkSuperset: NETWORK_ADOPT,
157 })
158 .pipe(
159 Stream.filter((r) => r.network === NETWORK_ADOPT),
160 Stream.runHead,
161 Effect.map(Option.getOrUndefined),
162 ),
163 ),
164 );
165 expect(pre?.id).toBeDefined();
166
167 const adopted = yield* stack.deploy(
168 Effect.gen(function* () {
169 const tunnel = yield* Cloudflare.Tunnel.Tunnel("RouteAdoptTunnel", {
170 adopt: true,
171 });
172 const route = yield* Cloudflare.Tunnel.Route("AdoptRoute", {
173 tunnelId: tunnel.tunnelId,
174 network: NETWORK_ADOPT,
175 adopt: true,
176 });
177 return { route };
178 }),
179 );
180
181 expect(adopted.route.routeId).toEqual(pre?.id);
182 expect(adopted.route.network).toEqual(NETWORK_ADOPT);
183
184 yield* stack.destroy();
185 }).pipe(logLevel),
186 { tags: ["provider:cloudflare", "provider:cloudflare:tunnel", "live"] },
187 );
188
189 test.provider(
190 "list enumerates the deployed route",
191 (stack) =>
192 Effect.gen(function* () {
193 const { accountId } = yield* yield* CloudflareEnvironment;
194
195 yield* stack.destroy();
196
197 const { route } = yield* stack.deploy(
198 Effect.gen(function* () {
199 const tunnel = yield* Cloudflare.Tunnel.Tunnel("RouteListTunnel", {
200 adopt: true,
201 });
202 const route = yield* Cloudflare.Tunnel.Route("ListRoute", {
203 tunnelId: tunnel.tunnelId,
204 network: NETWORK_LIST,
205 adopt: true,
206 });
207 return { route };
208 }),
209 );
210
211 const provider = yield* Provider.findProvider(Cloudflare.Tunnel.Route);
212 const all = yield* provider.list();
213
214 const found = all.find((r) => r.routeId === route.routeId);
215 expect(found).toBeDefined();
216 expect(found?.network).toEqual(NETWORK_LIST);
217 expect(found?.accountId).toEqual(accountId);
218
219 yield* stack.destroy();
220 }).pipe(logLevel),
221 { tags: ["provider:cloudflare", "provider:cloudflare:tunnel", "live"] },
222 );