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

main
ts 112 lines 3.94 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 workers from "@distilled.cloud/cloudflare/workers";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8
9 const { test } = Test.make({ providers: Cloudflare.providers() });
10
11 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 const asyncWorkerScript = `export default {
14 async fetch() {
15 return new Response("ok");
16 },
17 };
18 `;
19
20 // Declares the tunnel + VPC service. Yielded in both deploys (same logical
21 // ids), so the first deploy creates them and the second is a no-op reconcile
22 // that keeps them alive while the worker binds.
23 const vpcService = Effect.gen(function* () {
24 const tunnel = yield* Cloudflare.Tunnel.Tunnel("RefTunnel", {
25 ingress: [{ service: "http://localhost:8080" }],
26 adopt: true,
27 });
28 return yield* Cloudflare.VpcService.VpcService("RefSvc", {
29 httpPort: 8080,
30 host: {
31 hostname: "localhost",
32 resolverNetwork: { tunnelId: tunnel.tunnelId },
33 },
34 adopt: true,
35 });
36 });
37
38 // Read the worker's live `vpc_service` bindings out-of-band from the script
39 // settings API.
40 const readVpcBindings = (scriptName: string) =>
41 Effect.gen(function* () {
42 const { accountId } = yield* yield* CloudflareEnvironment;
43 const settings = yield* workers.getScriptScriptAndVersionSetting({
44 accountId,
45 scriptName,
46 });
47 return (settings.bindings ?? []).filter(
48 (b): b is Extract<typeof b, { type: "vpc_service" }> => b.type === "vpc_service",
49 );
50 });
51
52 test.provider(
53 "looks up a vpc service by id/name and binds it to a worker",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 // Create a real VPC service to look up.
59 const svc = yield* stack.deploy(vpcService);
60
61 // Bind the service to a worker three ways — the managed resource
62 // directly, and `lookup` data sources by id and by name. All emit a
63 // `vpc_service` binding. The lookups are also returned as stack
64 // outputs, pinning plan-time Output resolution. The service and its
65 // tunnel are re-declared so they stay deployed.
66 const { byId, byName, worker } = yield* stack.deploy(
67 Effect.gen(function* () {
68 const managed = yield* vpcService;
69 const worker = yield* Cloudflare.Worker("vpc-binding-worker", {
70 script: asyncWorkerScript,
71 env: {
72 SVC_MANAGED: managed,
73 SVC_BY_ID: Cloudflare.VpcService.lookup({
74 serviceId: svc.serviceId,
75 }),
76 SVC_BY_NAME: Cloudflare.VpcService.lookup({
77 name: svc.serviceName,
78 }),
79 },
80 });
81 return {
82 byId: Cloudflare.VpcService.lookup({ serviceId: svc.serviceId }),
83 byName: Cloudflare.VpcService.lookup({ name: svc.serviceName }),
84 worker,
85 };
86 }),
87 );
88
89 // The data source resolves to the service's attributes.
90 expect(byId.serviceId).toEqual(svc.serviceId);
91 expect(byName.serviceId).toEqual(svc.serviceId);
92 expect(byName.serviceName).toEqual(svc.serviceName);
93 expect(byName.httpPort).toEqual(svc.httpPort);
94
95 const vpc = yield* readVpcBindings(worker.workerName);
96 expect(vpc.find((b) => b.name === "SVC_MANAGED")?.serviceId).toEqual(svc.serviceId);
97 expect(vpc.find((b) => b.name === "SVC_BY_ID")?.serviceId).toEqual(svc.serviceId);
98 expect(vpc.find((b) => b.name === "SVC_BY_NAME")?.serviceId).toEqual(svc.serviceId);
99
100 yield* stack.destroy();
101 }).pipe(logLevel),
102 {
103 tags: [
104 "provider:cloudflare",
105 "provider:cloudflare:tunnel",
106 "provider:cloudflare:vpcservice",
107 "provider:cloudflare:worker",
108 "live",
109 ],
110 timeout: 120_000,
111 },
112 );