The best way to control Claude Code, Codex and more from any computer or phone
| 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 connectivity from "@distilled.cloud/cloudflare/connectivity"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | test.provider( |
| 17 | "create, update, delete vpc service", |
| 18 | (stack) => |
| 19 | Effect.gen(function* () { |
| 20 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 21 | |
| 22 | yield* stack.destroy(); |
| 23 | |
| 24 | const { tunnel, service } = yield* stack.deploy( |
| 25 | Effect.gen(function* () { |
| 26 | const tunnel = yield* Cloudflare.Tunnel.Tunnel("VpcTunnel", { |
| 27 | ingress: [{ service: "http://localhost:8080" }], |
| 28 | adopt: true, |
| 29 | }); |
| 30 | const service = yield* Cloudflare.VpcService.VpcService("VpcSvc", { |
| 31 | httpPort: 8080, |
| 32 | host: { hostname: "localhost", resolverNetwork: { tunnelId: tunnel.tunnelId } }, |
| 33 | adopt: true, |
| 34 | }); |
| 35 | return { tunnel, service }; |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | expect(service.serviceId).toBeDefined(); |
| 40 | expect(service.serviceType).toEqual("http"); |
| 41 | expect(service.httpPort).toEqual(8080); |
| 42 | expect(service.host).toMatchObject({ |
| 43 | hostname: "localhost", |
| 44 | resolverNetwork: { tunnelId: tunnel.tunnelId }, |
| 45 | }); |
| 46 | |
| 47 | const fetched = yield* connectivity.getDirectoryService({ |
| 48 | accountId, |
| 49 | serviceId: service.serviceId, |
| 50 | }); |
| 51 | expect(fetched.serviceId).toEqual(service.serviceId); |
| 52 | expect(fetched.httpPort).toEqual(8080); |
| 53 | |
| 54 | const updated = yield* stack.deploy( |
| 55 | Effect.gen(function* () { |
| 56 | const tunnel = yield* Cloudflare.Tunnel.Tunnel("VpcTunnel", { |
| 57 | ingress: [{ service: "http://localhost:8080" }], |
| 58 | adopt: true, |
| 59 | }); |
| 60 | return yield* Cloudflare.VpcService.VpcService("VpcSvc", { |
| 61 | httpPort: 3000, |
| 62 | httpsPort: 3001, |
| 63 | host: { hostname: "localhost", resolverNetwork: { tunnelId: tunnel.tunnelId } }, |
| 64 | adopt: true, |
| 65 | }); |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | expect(updated.serviceId).toEqual(service.serviceId); |
| 70 | expect(updated.httpPort).toEqual(3000); |
| 71 | expect(updated.httpsPort).toEqual(3001); |
| 72 | |
| 73 | const fetchedUpdated = yield* connectivity.getDirectoryService({ |
| 74 | accountId, |
| 75 | serviceId: service.serviceId, |
| 76 | }); |
| 77 | expect(fetchedUpdated.httpPort).toEqual(3000); |
| 78 | expect(fetchedUpdated.httpsPort).toEqual(3001); |
| 79 | |
| 80 | yield* stack.destroy(); |
| 81 | |
| 82 | yield* waitForServiceToBeDeleted(service.serviceId, accountId); |
| 83 | }).pipe(logLevel), |
| 84 | { |
| 85 | tags: [ |
| 86 | "provider:cloudflare", |
| 87 | "provider:cloudflare:connectivity", |
| 88 | "provider:cloudflare:tunnel", |
| 89 | "provider:cloudflare:vpcservice", |
| 90 | "live", |
| 91 | ], |
| 92 | }, |
| 93 | ); |
| 94 | |
| 95 | test.provider( |
| 96 | "create vpc service with ipv4 host", |
| 97 | (stack) => |
| 98 | Effect.gen(function* () { |
| 99 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 100 | |
| 101 | yield* stack.destroy(); |
| 102 | |
| 103 | const service = yield* stack.deploy( |
| 104 | Effect.gen(function* () { |
| 105 | const tunnel = yield* Cloudflare.Tunnel.Tunnel("Ipv4Tunnel", { |
| 106 | ingress: [{ service: "http://localhost:8080" }], |
| 107 | adopt: true, |
| 108 | }); |
| 109 | return yield* Cloudflare.VpcService.VpcService("Ipv4Svc", { |
| 110 | httpPort: 8080, |
| 111 | host: { ipv4: "192.168.1.100", network: { tunnelId: tunnel.tunnelId } }, |
| 112 | adopt: true, |
| 113 | }); |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | expect(service.host).toMatchObject({ ipv4: "192.168.1.100" }); |
| 118 | expect("ipv6" in service.host).toBe(false); |
| 119 | |
| 120 | const fetched = yield* connectivity.getDirectoryService({ |
| 121 | accountId, |
| 122 | serviceId: service.serviceId, |
| 123 | }); |
| 124 | expect((fetched.host as { ipv4?: string }).ipv4).toEqual("192.168.1.100"); |
| 125 | |
| 126 | yield* stack.destroy(); |
| 127 | yield* waitForServiceToBeDeleted(service.serviceId, accountId); |
| 128 | }).pipe(logLevel), |
| 129 | { |
| 130 | tags: [ |
| 131 | "provider:cloudflare", |
| 132 | "provider:cloudflare:connectivity", |
| 133 | "provider:cloudflare:tunnel", |
| 134 | "provider:cloudflare:vpcservice", |
| 135 | "live", |
| 136 | ], |
| 137 | }, |
| 138 | ); |
| 139 | |
| 140 | test.provider( |
| 141 | "create vpc service with dual-stack host", |
| 142 | (stack) => |
| 143 | Effect.gen(function* () { |
| 144 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 145 | |
| 146 | yield* stack.destroy(); |
| 147 | |
| 148 | const service = yield* stack.deploy( |
| 149 | Effect.gen(function* () { |
| 150 | const tunnel = yield* Cloudflare.Tunnel.Tunnel("DualStackTunnel", { |
| 151 | ingress: [{ service: "http://localhost:8080" }], |
| 152 | adopt: true, |
| 153 | }); |
| 154 | return yield* Cloudflare.VpcService.VpcService("DualStackSvc", { |
| 155 | httpPort: 8080, |
| 156 | host: { |
| 157 | ipv4: "192.168.1.101", |
| 158 | ipv6: "2001:db8::1", |
| 159 | network: { tunnelId: tunnel.tunnelId }, |
| 160 | }, |
| 161 | adopt: true, |
| 162 | }); |
| 163 | }), |
| 164 | ); |
| 165 | |
| 166 | expect(service.host).toMatchObject({ ipv4: "192.168.1.101", ipv6: "2001:db8::1" }); |
| 167 | |
| 168 | yield* stack.destroy(); |
| 169 | yield* waitForServiceToBeDeleted(service.serviceId, accountId); |
| 170 | }).pipe(logLevel), |
| 171 | { |
| 172 | tags: [ |
| 173 | "provider:cloudflare", |
| 174 | "provider:cloudflare:connectivity", |
| 175 | "provider:cloudflare:tunnel", |
| 176 | "provider:cloudflare:vpcservice", |
| 177 | "live", |
| 178 | ], |
| 179 | }, |
| 180 | ); |
| 181 | |
| 182 | test.provider( |
| 183 | "list enumerates the deployed vpc service", |
| 184 | (stack) => |
| 185 | Effect.gen(function* () { |
| 186 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 187 | |
| 188 | yield* stack.destroy(); |
| 189 | |
| 190 | const service = yield* stack.deploy( |
| 191 | Effect.gen(function* () { |
| 192 | const tunnel = yield* Cloudflare.Tunnel.Tunnel("ListTunnel", { |
| 193 | ingress: [{ service: "http://localhost:8080" }], |
| 194 | adopt: true, |
| 195 | }); |
| 196 | return yield* Cloudflare.VpcService.VpcService("ListSvc", { |
| 197 | httpPort: 8080, |
| 198 | host: { hostname: "localhost", resolverNetwork: { tunnelId: tunnel.tunnelId } }, |
| 199 | adopt: true, |
| 200 | }); |
| 201 | }), |
| 202 | ); |
| 203 | |
| 204 | const provider = yield* Provider.findProvider(Cloudflare.VpcService.VpcService); |
| 205 | const all = yield* provider.list(); |
| 206 | |
| 207 | const found = all.find((s) => s.serviceId === service.serviceId); |
| 208 | expect(found).toBeDefined(); |
| 209 | expect(found?.serviceName).toEqual(service.serviceName); |
| 210 | expect(found?.accountId).toEqual(accountId); |
| 211 | |
| 212 | yield* stack.destroy(); |
| 213 | yield* waitForServiceToBeDeleted(service.serviceId, accountId); |
| 214 | }).pipe(logLevel), |
| 215 | { |
| 216 | tags: [ |
| 217 | "provider:cloudflare", |
| 218 | "provider:cloudflare:connectivity", |
| 219 | "provider:cloudflare:tunnel", |
| 220 | "provider:cloudflare:vpcservice", |
| 221 | "live", |
| 222 | ], |
| 223 | }, |
| 224 | ); |
| 225 | |
| 226 | const waitForServiceToBeDeleted = Effect.fn(function* (serviceId: string, accountId: string) { |
| 227 | yield* connectivity.getDirectoryService({ accountId, serviceId }).pipe( |
| 228 | Effect.flatMap(() => Effect.fail(new VpcServiceStillExists())), |
| 229 | Effect.retry({ |
| 230 | while: (e): e is VpcServiceStillExists => e instanceof VpcServiceStillExists, |
| 231 | schedule: Schedule.exponential(100), |
| 232 | }), |
| 233 | Effect.catch(() => Effect.void), |
| 234 | ); |
| 235 | }); |
| 236 | |
| 237 | class VpcServiceStillExists extends Data.TaggedError("VpcServiceStillExists") {} |