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

main
ts 557 lines 18.6 KB
Raw
1 import * as Fly from "@alchemy.run/fly";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as machines from "@distilled.cloud/fly-io/machines";
5 import * as Provider from "alchemy/Provider";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Result from "effect/Result";
10 import * as Schedule from "effect/Schedule";
11 import { fetchFrom, httpService, nginx } from "./fixtures/flycast.ts";
12
13 const { test } = Test.make({ providers: Fly.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 const waitUntilAppGone = (appName: string) =>
18 machines.getApp({ app_name: appName }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28 const waitUntilIpGone = (appName: string, ip: string) =>
29 machines.listAppIPAssignments({ app_name: appName }).pipe(
30 Effect.map((res) => ((res.ips ?? []).some((item) => item.ip === ip) ? "found" : "gone")),
31 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
32 Effect.repeat({
33 schedule: Schedule.spaced("1 second"),
34 until: (status) => status === "gone",
35 times: 10,
36 }),
37 );
38
39 const listedHas = (appName: string, ip: string) =>
40 machines
41 .listAppIPAssignments({ app_name: appName })
42 .pipe(Effect.map((res) => (res.ips ?? []).find((item) => item.ip === ip)));
43
44 test.provider(
45 "create, update, and delete a v6 assignment",
46 (stack) =>
47 Effect.gen(function* () {
48 yield* stack.destroy();
49
50 const created = yield* stack.deploy(
51 Effect.gen(function* () {
52 const app = yield* Fly.App("IpApp");
53 const ip = yield* Fly.IpAssignment("Public", {
54 app,
55 type: "v6",
56 });
57 return { app, ip };
58 }),
59 );
60
61 expect(created.ip.ip).toEqual(expect.any(String));
62 expect(created.ip.ip).toContain(":");
63 expect(created.ip.type).toEqual("v6");
64 expect(created.ip.appName).toEqual(created.app.appName);
65 expect(created.ip.shared).toEqual(false);
66
67 const fetched = yield* listedHas(created.app.appName, created.ip.ip);
68 expect(fetched).toBeDefined();
69 expect(fetched?.ip).toEqual(created.ip.ip);
70 expect(fetched?.shared).not.toEqual(true);
71
72 const updated = yield* stack.deploy(
73 Effect.gen(function* () {
74 const app = yield* Fly.App("IpApp");
75 const ip = yield* Fly.IpAssignment("Public", {
76 app,
77 type: "v6",
78 });
79 return { app, ip };
80 }),
81 );
82
83 expect(updated.ip.ip).toEqual(created.ip.ip);
84 expect(updated.ip.type).toEqual("v6");
85 expect(updated.ip.appName).toEqual(created.app.appName);
86 expect(updated.app.appId).toEqual(created.app.appId);
87
88 yield* stack.destroy();
89
90 const ipGone = yield* waitUntilIpGone(created.app.appName, created.ip.ip);
91 expect(ipGone).toEqual("gone");
92 const appGone = yield* waitUntilAppGone(created.app.appName);
93 expect(appGone).toEqual("gone");
94 }).pipe(logLevel),
95 {
96 tags: [
97 "provider:fly",
98 "provider:fly:app",
99 "provider:fly:ipassignment",
100 "provider:fly:machine",
101 "live",
102 ],
103 timeout: 120_000,
104 },
105 );
106
107 test.provider(
108 "replace when type changes to shared_v4",
109 (stack) =>
110 Effect.gen(function* () {
111 yield* stack.destroy();
112
113 const created = yield* stack.deploy(
114 Effect.gen(function* () {
115 const app = yield* Fly.App("IpReplaceApp");
116 const ip = yield* Fly.IpAssignment("Public", {
117 app,
118 type: "v6",
119 });
120 return { app, ip };
121 }),
122 );
123
124 expect(created.ip.type).toEqual("v6");
125
126 const replaced = yield* stack.deploy(
127 Effect.gen(function* () {
128 const app = yield* Fly.App("IpReplaceApp");
129 const ip = yield* Fly.IpAssignment("Public", {
130 app,
131 type: "shared_v4",
132 });
133 return { app, ip };
134 }),
135 );
136
137 expect(replaced.ip.ip).not.toEqual(created.ip.ip);
138 expect(replaced.ip.type).toEqual("shared_v4");
139 expect(replaced.ip.shared).toEqual(true);
140 expect(replaced.ip.appName).toEqual(created.app.appName);
141 expect(replaced.ip.ip).not.toContain(":");
142
143 const fetched = yield* listedHas(replaced.app.appName, replaced.ip.ip);
144 expect(fetched).toBeDefined();
145 expect(fetched?.ip).toEqual(replaced.ip.ip);
146 expect(fetched?.ip).not.toContain(":");
147
148 const oldGone = yield* waitUntilIpGone(created.app.appName, created.ip.ip);
149 expect(oldGone).toEqual("gone");
150
151 yield* stack.destroy();
152
153 const ipGone = yield* waitUntilIpGone(replaced.app.appName, replaced.ip.ip);
154 expect(ipGone).toEqual("gone");
155 const appGone = yield* waitUntilAppGone(replaced.app.appName);
156 expect(appGone).toEqual("gone");
157 }).pipe(logLevel),
158 {
159 tags: [
160 "provider:fly",
161 "provider:fly:app",
162 "provider:fly:ipassignment",
163 "provider:fly:machine",
164 "live",
165 ],
166 timeout: 120_000,
167 },
168 );
169
170 /** Fly marks Flycast addresses with a `network`; public addresses have none. */
171 const observedNetwork = (appName: string, ip: string) =>
172 listedHas(appName, ip).pipe(Effect.map((found) => found?.network));
173
174 const tags = [
175 "provider:fly",
176 "provider:fly:app",
177 "provider:fly:ipassignment",
178 "provider:fly:machine",
179 "live",
180 ];
181
182 test.provider(
183 "private_v6, v6, and shared_v4 on one App keep distinct addresses",
184 (stack) =>
185 Effect.gen(function* () {
186 yield* stack.destroy();
187
188 const program = Effect.gen(function* () {
189 const app = yield* Fly.App("IpFlycastApp");
190 const flycast = yield* Fly.IpAssignment("Flycast", {
191 app,
192 type: "private_v6",
193 });
194 const publicV6 = yield* Fly.IpAssignment("PublicV6", {
195 app,
196 type: "v6",
197 });
198 const sharedV4 = yield* Fly.IpAssignment("SharedV4", {
199 app,
200 type: "shared_v4",
201 });
202 return { app, flycast, publicV6, sharedV4 };
203 });
204
205 const created = yield* stack.deploy(program);
206 expect(created.flycast.type).toEqual("private_v6");
207 expect(created.flycast.shared).toBe(false);
208 expect(created.flycast.network).toBeUndefined();
209 expect(yield* observedNetwork(created.app.appName, created.flycast.ip)).toMatchObject({
210 name: "",
211 });
212 expect(yield* observedNetwork(created.app.appName, created.publicV6.ip)).toBeNull();
213 expect(created.publicV6.type).toEqual("v6");
214 expect(created.publicV6.ip).toContain(":");
215 expect(created.sharedV4.type).toEqual("shared_v4");
216 expect(created.sharedV4.shared).toBe(true);
217
218 const listed = yield* machines.listAppIPAssignments({
219 app_name: created.app.appName,
220 });
221 expect((listed.ips ?? []).map(({ ip }) => ip).sort()).toEqual(
222 [created.flycast.ip, created.publicV6.ip, created.sharedV4.ip].sort(),
223 );
224
225 const updated = yield* stack.deploy(program);
226 expect(updated.flycast).toEqual(created.flycast);
227 expect(updated.publicV6).toEqual(created.publicV6);
228 expect(updated.sharedV4).toEqual(created.sharedV4);
229
230 const provider = yield* Provider.findProvider(Fly.IpAssignment);
231 const all = yield* provider.list();
232 const typeOf = (ip: string) => all.find((row) => row.ip === ip)?.type;
233 expect(typeOf(created.flycast.ip)).toEqual("private_v6");
234 expect(typeOf(created.publicV6.ip)).toEqual("v6");
235 expect(typeOf(created.sharedV4.ip)).toEqual("shared_v4");
236
237 yield* stack.destroy();
238 for (const ip of [created.flycast.ip, created.publicV6.ip])
239 expect(yield* waitUntilIpGone(created.app.appName, ip)).toEqual("gone");
240 expect(yield* waitUntilAppGone(created.app.appName)).toEqual("gone");
241 }).pipe(logLevel),
242 { tags, timeout: 120_000 },
243 );
244
245 test.provider(
246 "an existing Flycast address is adopted and a public v6 is still allocated",
247 (stack) =>
248 Effect.gen(function* () {
249 yield* stack.destroy();
250 const app = yield* stack.deploy(Fly.App("IpFlycastAdoptApp"));
251 // Allocate the Flycast address directly through the Fly API.
252 const existing = yield* machines.createAppIPAssignment({
253 app_name: app.appName,
254 type: "private_v6",
255 });
256 expect(existing.network).toMatchObject({ name: "" });
257
258 const deployed = yield* stack.deploy(
259 Effect.gen(function* () {
260 const app = yield* Fly.App("IpFlycastAdoptApp");
261 const flycast = yield* Fly.IpAssignment("Flycast", {
262 app,
263 type: "private_v6",
264 });
265 const publicV6 = yield* Fly.IpAssignment("PublicV6", {
266 app,
267 type: "v6",
268 });
269 return { flycast, publicV6 };
270 }),
271 );
272 expect(deployed.flycast.ip).toEqual(existing.ip);
273 expect(deployed.flycast.type).toEqual("private_v6");
274 expect(deployed.publicV6.type).toEqual("v6");
275 expect(deployed.publicV6.ip).not.toEqual(existing.ip);
276 expect(yield* observedNetwork(app.appName, deployed.publicV6.ip)).toBeNull();
277 const listed = yield* machines.listAppIPAssignments({
278 app_name: app.appName,
279 });
280 expect(listed.ips ?? []).toHaveLength(2);
281
282 yield* stack.destroy();
283 expect(yield* waitUntilAppGone(app.appName)).toEqual("gone");
284 }).pipe(logLevel),
285 { tags, timeout: 120_000 },
286 );
287
288 test.provider(
289 "changing type between private_v6 and v6 replaces the address",
290 (stack) =>
291 Effect.gen(function* () {
292 yield* stack.destroy();
293 const deploy = (type: "private_v6" | "v6") =>
294 stack.deploy(
295 Effect.gen(function* () {
296 const app = yield* Fly.App("IpFlycastReplaceApp");
297 const ip = yield* Fly.IpAssignment("Ip", { app, type });
298 return { app, ip };
299 }),
300 );
301 const flycast = yield* deploy("private_v6");
302 expect(flycast.ip.type).toEqual("private_v6");
303 expect(yield* observedNetwork(flycast.app.appName, flycast.ip.ip)).toBeDefined();
304 const publicV6 = yield* deploy("v6");
305 expect(publicV6.ip.type).toEqual("v6");
306 expect(publicV6.ip.ip).not.toEqual(flycast.ip.ip);
307 expect(yield* observedNetwork(publicV6.app.appName, publicV6.ip.ip)).toBeNull();
308 expect(yield* waitUntilIpGone(flycast.app.appName, flycast.ip.ip)).toEqual("gone");
309 const back = yield* deploy("private_v6");
310 expect(back.ip.type).toEqual("private_v6");
311 expect(back.ip.ip).not.toEqual(publicV6.ip.ip);
312 expect(yield* waitUntilIpGone(publicV6.app.appName, publicV6.ip.ip)).toEqual("gone");
313 yield* stack.destroy();
314 expect(yield* waitUntilAppGone(flycast.app.appName)).toEqual("gone");
315 }).pipe(logLevel),
316 { tags, timeout: 120_000 },
317 );
318
319 test.provider(
320 "Flycast serves another App through the proxy and not the internet",
321 (stack) =>
322 Effect.gen(function* () {
323 yield* stack.destroy();
324
325 const deployed = yield* stack.deploy(
326 Effect.gen(function* () {
327 const backend = yield* Fly.App("FlycastBackend");
328 const flycast = yield* Fly.IpAssignment("BackendFlycast", {
329 app: backend,
330 type: "private_v6",
331 });
332 const web = yield* Fly.Machine("BackendWeb", {
333 app: backend,
334 ...nginx,
335 services: [httpService],
336 });
337 const client = yield* Fly.App("FlycastClient");
338 const caller = yield* Fly.Machine("Caller", {
339 app: client,
340 ...nginx,
341 });
342 return { backend, flycast, web, caller };
343 }),
344 );
345 expect(deployed.flycast.type).toEqual("private_v6");
346 const flycastUrl = `http://${deployed.backend.appName}.flycast/`;
347 expect(yield* fetchFrom(deployed.caller, flycastUrl)).toContain("Welcome to nginx");
348
349 // No public address: the fly.dev hostname does not serve the backend.
350 const publicResult = yield* HttpClient.get(
351 `http://${deployed.backend.appName}.fly.dev/`,
352 ).pipe(
353 Effect.flatMap((response) => response.text),
354 Effect.timeout("10 seconds"),
355 Effect.result,
356 );
357 if (Result.isSuccess(publicResult))
358 expect(publicResult.success).not.toContain("Welcome to nginx");
359
360 // Requests still pass through Fly's proxy, so a stopped Machine autostarts.
361 const target = {
362 app_name: deployed.web.appName,
363 machine_id: deployed.web.machineId,
364 };
365 yield* machines.stopMachine(target);
366 yield* machines.waitMachine({ ...target, state: "stopped", timeout: 30 });
367 expect(yield* fetchFrom(deployed.caller, flycastUrl)).toContain("Welcome to nginx");
368 expect((yield* machines.getMachine(target)).state).toEqual("started");
369
370 yield* stack.destroy();
371 expect(yield* waitUntilAppGone(deployed.backend.appName)).toEqual("gone");
372 expect(yield* waitUntilAppGone(deployed.caller.appName)).toEqual("gone");
373 }).pipe(logLevel),
374 { tags, timeout: 180_000 },
375 );
376
377 test.provider(
378 "network places Flycast on a named network and changing it replaces",
379 (stack) =>
380 Effect.gen(function* () {
381 yield* stack.destroy();
382 const networks = {
383 a: "alchemy-flycast-tenant-a",
384 b: "alchemy-flycast-tenant-b",
385 };
386 const deploy = (network: string | undefined) =>
387 stack.deploy(
388 Effect.gen(function* () {
389 const backend = yield* Fly.App("NetworkBackend");
390 yield* Fly.Machine("BackendWeb", {
391 app: backend,
392 ...nginx,
393 services: [httpService],
394 });
395 const tenantA = yield* Fly.App("TenantA", { network: networks.a });
396 const tenantB = yield* Fly.App("TenantB", { network: networks.b });
397 const callerA = yield* Fly.Machine("CallerA", {
398 app: tenantA,
399 ...nginx,
400 });
401 const flycast = yield* Fly.IpAssignment("BackendFlycast", {
402 app: backend,
403 type: "private_v6",
404 network,
405 });
406 return { backend, tenantA, tenantB, callerA, flycast };
407 }),
408 );
409
410 const onA = yield* deploy(networks.a);
411 expect(onA.tenantA.network).toEqual(networks.a);
412 expect(onA.flycast.network).toEqual(networks.a);
413 expect(yield* observedNetwork(onA.backend.appName, onA.flycast.ip)).toMatchObject({
414 name: networks.a,
415 });
416 expect(yield* fetchFrom(onA.callerA, `http://${onA.backend.appName}.flycast/`)).toContain(
417 "Welcome to nginx",
418 );
419
420 const onB = yield* deploy(networks.b);
421 expect(onB.flycast.ip).not.toEqual(onA.flycast.ip);
422 expect(onB.flycast.network).toEqual(networks.b);
423 expect(yield* waitUntilIpGone(onA.backend.appName, onA.flycast.ip)).toEqual("gone");
424
425 const onDefault = yield* deploy(undefined);
426 expect(onDefault.flycast.ip).not.toEqual(onB.flycast.ip);
427 expect(onDefault.flycast.network).toBeUndefined();
428 expect(yield* waitUntilIpGone(onB.backend.appName, onB.flycast.ip)).toEqual("gone");
429
430 yield* stack.destroy();
431 for (const app of [onA.backend, onA.tenantA, onA.tenantB])
432 expect(yield* waitUntilAppGone(app.appName)).toEqual("gone");
433 }).pipe(logLevel),
434 { tags, timeout: 300_000 },
435 );
436
437 test.provider(
438 "an unknown network is rejected without allocating an address",
439 (stack) =>
440 Effect.gen(function* () {
441 yield* stack.destroy();
442 const app = yield* stack.deploy(Fly.App("IpUnknownNetworkApp"));
443 const failed = yield* stack
444 .deploy(
445 Effect.gen(function* () {
446 const app = yield* Fly.App("IpUnknownNetworkApp");
447 return yield* Fly.IpAssignment("Flycast", {
448 app,
449 type: "private_v6",
450 network: "alchemy-flycast-missing-network",
451 });
452 }),
453 )
454 .pipe(Effect.flip);
455 expect(failed).toMatchObject({ _tag: "NetworkNotFound" });
456 const listed = yield* machines.listAppIPAssignments({
457 app_name: app.appName,
458 });
459 expect(listed.ips ?? []).toEqual([]);
460 yield* stack.destroy();
461 expect(yield* waitUntilAppGone(app.appName)).toEqual("gone");
462 }).pipe(logLevel),
463 { tags, timeout: 120_000 },
464 );
465
466 test.provider(
467 "list enumerates the deployed assignment",
468 (stack) =>
469 Effect.gen(function* () {
470 yield* stack.destroy();
471
472 const deployed = yield* stack.deploy(
473 Effect.gen(function* () {
474 const app = yield* Fly.App("IpListApp");
475 const ip = yield* Fly.IpAssignment("Public", {
476 app,
477 type: "v6",
478 });
479 return { app, ip };
480 }),
481 );
482
483 const provider = yield* Provider.findProvider(Fly.IpAssignment);
484 const all = yield* provider.list();
485 const found = all.find((row) => row.ip === deployed.ip.ip);
486 expect(found).toBeDefined();
487 expect(found?.appName).toEqual(deployed.app.appName);
488 expect(found?.type).toEqual("v6");
489
490 yield* stack.destroy();
491
492 const ipGone = yield* waitUntilIpGone(deployed.app.appName, deployed.ip.ip);
493 expect(ipGone).toEqual("gone");
494 const appGone = yield* waitUntilAppGone(deployed.app.appName);
495 expect(appGone).toEqual("gone");
496 }).pipe(logLevel),
497 {
498 tags: [
499 "provider:fly",
500 "provider:fly:app",
501 "provider:fly:ipassignment",
502 "provider:fly:machine",
503 "live",
504 ],
505 timeout: 120_000,
506 },
507 );
508
509 test.provider(
510 "dedicated v4 is rejected with a typed quota error",
511 (stack) =>
512 Effect.gen(function* () {
513 yield* stack.destroy();
514
515 const app = yield* stack.deploy(
516 Effect.gen(function* () {
517 return yield* Fly.App("IpV4ProbeApp");
518 }),
519 );
520
521 const result = yield* Effect.result(
522 machines.createAppIPAssignment({
523 app_name: app.appName,
524 type: "v4",
525 }),
526 );
527
528 if (Result.isFailure(result)) {
529 expect(result.failure._tag).toEqual("BadRequest");
530 } else {
531 const ip = result.success.ip;
532 if (ip !== undefined && ip.length > 0) {
533 yield* machines
534 .deleteAppIPAssignment({
535 app_name: app.appName,
536 ip,
537 })
538 .pipe(Effect.catchTag("NotFound", () => Effect.void));
539 }
540 }
541
542 yield* stack.destroy();
543
544 const gone = yield* waitUntilAppGone(app.appName);
545 expect(gone).toEqual("gone");
546 }).pipe(logLevel),
547 {
548 tags: [
549 "provider:fly",
550 "provider:fly:app",
551 "provider:fly:ipassignment",
552 "provider:fly:machine",
553 "live",
554 ],
555 timeout: 120_000,
556 },
557 );