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 | import { testName } from "../../names.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | |
| 17 | // The scoped API token the test harness mints propagates eventually- |
| 18 | // consistently across Cloudflare's edge — ride out 403 blips |
| 19 | // (`Forbidden`, declared in the distilled error union) on the test's |
| 20 | // own out-of-band verification calls. |
| 21 | const getService = (accountId: string, serviceId: string) => |
| 22 | connectivity.getDirectoryService({ accountId, serviceId }).pipe( |
| 23 | Effect.retry({ |
| 24 | while: (e) => e._tag === "Forbidden", |
| 25 | schedule: Schedule.exponential("500 millis"), |
| 26 | times: 8, |
| 27 | }), |
| 28 | ); |
| 29 | |
| 30 | class ServiceStillExists extends Data.TaggedError("ServiceStillExists") {} |
| 31 | |
| 32 | const expectGone = (accountId: string, serviceId: string) => |
| 33 | getService(accountId, serviceId).pipe( |
| 34 | Effect.flatMap(() => Effect.fail(new ServiceStillExists())), |
| 35 | // A missing service surfaces as the typed `VpcServiceNotFound` |
| 36 | // (Cloudflare error code 5104) — that's the success condition here. |
| 37 | Effect.catchTag("VpcServiceNotFound", () => Effect.void), |
| 38 | Effect.retry({ |
| 39 | while: (e) => e._tag === "ServiceStillExists", |
| 40 | schedule: Schedule.exponential("500 millis"), |
| 41 | times: 10, |
| 42 | }), |
| 43 | ); |
| 44 | |
| 45 | test.provider( |
| 46 | "tcp service lifecycle: create, update, host switch", |
| 47 | (stack) => |
| 48 | Effect.gen(function* () { |
| 49 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 50 | |
| 51 | yield* stack.destroy(); |
| 52 | |
| 53 | // Create — tcp database service on a tunnel-backed IPv4 host. |
| 54 | const { tunnel, service } = yield* stack.deploy( |
| 55 | Effect.gen(function* () { |
| 56 | const tunnel = yield* Cloudflare.Tunnel.Tunnel("DirSvcTunnel", { |
| 57 | ingress: [{ service: "http://localhost:8080" }], |
| 58 | adopt: true, |
| 59 | }); |
| 60 | const service = yield* Cloudflare.Connectivity.DirectoryService("PgService", { |
| 61 | name: testName("alchemy-connectivity-dirsvc-tcp"), |
| 62 | type: "tcp", |
| 63 | tcpPort: 5432, |
| 64 | appProtocol: "postgresql", |
| 65 | host: { ipv4: "10.10.0.21", network: { tunnelId: tunnel.tunnelId } }, |
| 66 | }); |
| 67 | return { tunnel, service }; |
| 68 | }), |
| 69 | ); |
| 70 | |
| 71 | expect(service.serviceId).toBeDefined(); |
| 72 | expect(service.accountId).toEqual(accountId); |
| 73 | expect(service.name).toEqual(testName("alchemy-connectivity-dirsvc-tcp")); |
| 74 | expect(service.type).toEqual("tcp"); |
| 75 | expect(service.tcpPort).toEqual(5432); |
| 76 | expect(service.appProtocol).toEqual("postgresql"); |
| 77 | expect(service.host).toMatchObject({ |
| 78 | ipv4: "10.10.0.21", |
| 79 | network: { tunnelId: tunnel.tunnelId }, |
| 80 | }); |
| 81 | |
| 82 | // Out-of-band verify against the live API. |
| 83 | const live = yield* getService(accountId, service.serviceId); |
| 84 | expect(live.serviceId).toEqual(service.serviceId); |
| 85 | expect(live.name).toEqual(testName("alchemy-connectivity-dirsvc-tcp")); |
| 86 | |
| 87 | // Update in place — new name and port, same serviceId. |
| 88 | const updated = yield* stack.deploy( |
| 89 | Effect.gen(function* () { |
| 90 | const tunnel = yield* Cloudflare.Tunnel.Tunnel("DirSvcTunnel", { |
| 91 | ingress: [{ service: "http://localhost:8080" }], |
| 92 | adopt: true, |
| 93 | }); |
| 94 | return yield* Cloudflare.Connectivity.DirectoryService("PgService", { |
| 95 | name: testName("alchemy-connectivity-dirsvc-tcp-v2"), |
| 96 | type: "tcp", |
| 97 | tcpPort: 5433, |
| 98 | appProtocol: "postgresql", |
| 99 | host: { ipv4: "10.10.0.21", network: { tunnelId: tunnel.tunnelId } }, |
| 100 | }); |
| 101 | }), |
| 102 | ); |
| 103 | |
| 104 | expect(updated.serviceId).toEqual(service.serviceId); |
| 105 | expect(updated.name).toEqual(testName("alchemy-connectivity-dirsvc-tcp-v2")); |
| 106 | expect(updated.tcpPort).toEqual(5433); |
| 107 | |
| 108 | const liveUpdated = yield* getService(accountId, service.serviceId); |
| 109 | expect(liveUpdated.name).toEqual(testName("alchemy-connectivity-dirsvc-tcp-v2")); |
| 110 | |
| 111 | // Switch the host variant (ipv4 -> hostname) — still an in-place |
| 112 | // update, same serviceId. Keep the tunnel deployed across steps. |
| 113 | const rehosted = yield* stack.deploy( |
| 114 | Effect.gen(function* () { |
| 115 | const tunnel = yield* Cloudflare.Tunnel.Tunnel("DirSvcTunnel", { |
| 116 | ingress: [{ service: "http://localhost:8080" }], |
| 117 | adopt: true, |
| 118 | }); |
| 119 | return yield* Cloudflare.Connectivity.DirectoryService("PgService", { |
| 120 | name: testName("alchemy-connectivity-dirsvc-tcp-v2"), |
| 121 | type: "tcp", |
| 122 | tcpPort: 5433, |
| 123 | appProtocol: "postgresql", |
| 124 | host: { hostname: "db.internal", resolverNetwork: { tunnelId: tunnel.tunnelId } }, |
| 125 | }); |
| 126 | }), |
| 127 | ); |
| 128 | |
| 129 | expect(rehosted.serviceId).toEqual(service.serviceId); |
| 130 | expect(rehosted.host).toMatchObject({ |
| 131 | hostname: "db.internal", |
| 132 | resolverNetwork: { tunnelId: tunnel.tunnelId }, |
| 133 | }); |
| 134 | |
| 135 | // Redeploying identical props is a no-op (same serviceId, no drift). |
| 136 | const noop = yield* stack.deploy( |
| 137 | Effect.gen(function* () { |
| 138 | const tunnel = yield* Cloudflare.Tunnel.Tunnel("DirSvcTunnel", { |
| 139 | ingress: [{ service: "http://localhost:8080" }], |
| 140 | adopt: true, |
| 141 | }); |
| 142 | return yield* Cloudflare.Connectivity.DirectoryService("PgService", { |
| 143 | name: testName("alchemy-connectivity-dirsvc-tcp-v2"), |
| 144 | type: "tcp", |
| 145 | tcpPort: 5433, |
| 146 | appProtocol: "postgresql", |
| 147 | host: { hostname: "db.internal", resolverNetwork: { tunnelId: tunnel.tunnelId } }, |
| 148 | }); |
| 149 | }), |
| 150 | ); |
| 151 | expect(noop.serviceId).toEqual(service.serviceId); |
| 152 | |
| 153 | yield* stack.destroy(); |
| 154 | yield* expectGone(accountId, service.serviceId); |
| 155 | |
| 156 | // Destroy is idempotent — a second destroy of an empty stack is a no-op. |
| 157 | yield* stack.destroy(); |
| 158 | }).pipe(logLevel), |
| 159 | { |
| 160 | tags: [ |
| 161 | "provider:cloudflare", |
| 162 | "provider:cloudflare:connectivity", |
| 163 | "provider:cloudflare:tunnel", |
| 164 | "live", |
| 165 | ], |
| 166 | }, |
| 167 | ); |
| 168 | |
| 169 | test.provider( |
| 170 | "http service with explicit ports and default name", |
| 171 | (stack) => |
| 172 | Effect.gen(function* () { |
| 173 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 174 | |
| 175 | yield* stack.destroy(); |
| 176 | |
| 177 | const service = yield* stack.deploy( |
| 178 | Effect.gen(function* () { |
| 179 | const tunnel = yield* Cloudflare.Tunnel.Tunnel("DirSvcHttpTunnel", { |
| 180 | ingress: [{ service: "http://localhost:8080" }], |
| 181 | adopt: true, |
| 182 | }); |
| 183 | return yield* Cloudflare.Connectivity.DirectoryService("HttpService", { |
| 184 | type: "http", |
| 185 | httpPort: 8080, |
| 186 | httpsPort: 8443, |
| 187 | host: { |
| 188 | hostname: "api.internal", |
| 189 | resolverNetwork: { tunnelId: tunnel.tunnelId, resolverIps: ["10.0.0.53"] }, |
| 190 | }, |
| 191 | }); |
| 192 | }), |
| 193 | ); |
| 194 | |
| 195 | expect(service.serviceId).toBeDefined(); |
| 196 | // Name was omitted — the provider generates one from app/stage/id. |
| 197 | expect(service.name).toBeTruthy(); |
| 198 | expect(service.type).toEqual("http"); |
| 199 | expect(service.httpPort).toEqual(8080); |
| 200 | expect(service.httpsPort).toEqual(8443); |
| 201 | expect(service.host).toMatchObject({ |
| 202 | hostname: "api.internal", |
| 203 | resolverNetwork: { resolverIps: ["10.0.0.53"] }, |
| 204 | }); |
| 205 | |
| 206 | const live = yield* getService(accountId, service.serviceId); |
| 207 | expect(live.serviceId).toEqual(service.serviceId); |
| 208 | |
| 209 | // Update ports in place. |
| 210 | const updated = yield* stack.deploy( |
| 211 | Effect.gen(function* () { |
| 212 | const tunnel = yield* Cloudflare.Tunnel.Tunnel("DirSvcHttpTunnel", { |
| 213 | ingress: [{ service: "http://localhost:8080" }], |
| 214 | adopt: true, |
| 215 | }); |
| 216 | return yield* Cloudflare.Connectivity.DirectoryService("HttpService", { |
| 217 | type: "http", |
| 218 | httpPort: 3000, |
| 219 | httpsPort: 3001, |
| 220 | host: { |
| 221 | hostname: "api.internal", |
| 222 | resolverNetwork: { tunnelId: tunnel.tunnelId, resolverIps: ["10.0.0.53"] }, |
| 223 | }, |
| 224 | }); |
| 225 | }), |
| 226 | ); |
| 227 | |
| 228 | expect(updated.serviceId).toEqual(service.serviceId); |
| 229 | expect(updated.httpPort).toEqual(3000); |
| 230 | expect(updated.httpsPort).toEqual(3001); |
| 231 | |
| 232 | yield* stack.destroy(); |
| 233 | yield* expectGone(accountId, service.serviceId); |
| 234 | }).pipe(logLevel), |
| 235 | { |
| 236 | tags: [ |
| 237 | "provider:cloudflare", |
| 238 | "provider:cloudflare:connectivity", |
| 239 | "provider:cloudflare:tunnel", |
| 240 | "live", |
| 241 | ], |
| 242 | }, |
| 243 | ); |
| 244 | |
| 245 | test.provider( |
| 246 | "list enumerates the deployed directory service", |
| 247 | (stack) => |
| 248 | Effect.gen(function* () { |
| 249 | yield* stack.destroy(); |
| 250 | |
| 251 | const service = yield* stack.deploy( |
| 252 | Effect.gen(function* () { |
| 253 | const tunnel = yield* Cloudflare.Tunnel.Tunnel("DirSvcListTunnel", { |
| 254 | ingress: [{ service: "http://localhost:8080" }], |
| 255 | adopt: true, |
| 256 | }); |
| 257 | return yield* Cloudflare.Connectivity.DirectoryService("ListService", { |
| 258 | name: testName("alchemy-connectivity-dirsvc-list"), |
| 259 | type: "tcp", |
| 260 | tcpPort: 5432, |
| 261 | host: { ipv4: "10.20.0.21", network: { tunnelId: tunnel.tunnelId } }, |
| 262 | }); |
| 263 | }), |
| 264 | ); |
| 265 | |
| 266 | const provider = yield* Provider.findProvider(Cloudflare.Connectivity.DirectoryService); |
| 267 | const all = yield* provider.list(); |
| 268 | |
| 269 | expect(all.some((s) => s.serviceId === service.serviceId)).toBe(true); |
| 270 | const found = all.find((s) => s.serviceId === service.serviceId)!; |
| 271 | expect(found.name).toEqual(testName("alchemy-connectivity-dirsvc-list")); |
| 272 | expect(found.type).toEqual("tcp"); |
| 273 | |
| 274 | yield* stack.destroy(); |
| 275 | yield* expectGone(service.accountId, service.serviceId); |
| 276 | }).pipe(logLevel), |
| 277 | { |
| 278 | tags: [ |
| 279 | "provider:cloudflare", |
| 280 | "provider:cloudflare:connectivity", |
| 281 | "provider:cloudflare:tunnel", |
| 282 | "live", |
| 283 | ], |
| 284 | }, |
| 285 | ); |
| 286 | |
| 287 | test.provider( |
| 288 | "recreates after out-of-band delete", |
| 289 | (stack) => |
| 290 | Effect.gen(function* () { |
| 291 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 292 | |
| 293 | yield* stack.destroy(); |
| 294 | |
| 295 | const service = yield* stack.deploy( |
| 296 | Effect.gen(function* () { |
| 297 | const tunnel = yield* Cloudflare.Tunnel.Tunnel("DirSvcHealTunnel", { |
| 298 | ingress: [{ service: "http://localhost:8080" }], |
| 299 | adopt: true, |
| 300 | }); |
| 301 | return yield* Cloudflare.Connectivity.DirectoryService("HealService", { |
| 302 | name: testName("alchemy-connectivity-dirsvc-heal"), |
| 303 | type: "http", |
| 304 | httpPort: 8080, |
| 305 | host: { hostname: "heal.internal", resolverNetwork: { tunnelId: tunnel.tunnelId } }, |
| 306 | }); |
| 307 | }), |
| 308 | ); |
| 309 | |
| 310 | // Delete the service out-of-band; a redeploy with changed props must |
| 311 | // observe it as missing and recreate instead of failing on a 404. |
| 312 | yield* connectivity.deleteDirectoryService({ accountId, serviceId: service.serviceId }).pipe( |
| 313 | Effect.retry({ |
| 314 | while: (e) => e._tag === "Forbidden", |
| 315 | schedule: Schedule.exponential("500 millis"), |
| 316 | times: 8, |
| 317 | }), |
| 318 | ); |
| 319 | |
| 320 | const healed = yield* stack.deploy( |
| 321 | Effect.gen(function* () { |
| 322 | const tunnel = yield* Cloudflare.Tunnel.Tunnel("DirSvcHealTunnel", { |
| 323 | ingress: [{ service: "http://localhost:8080" }], |
| 324 | adopt: true, |
| 325 | }); |
| 326 | return yield* Cloudflare.Connectivity.DirectoryService("HealService", { |
| 327 | name: testName("alchemy-connectivity-dirsvc-heal"), |
| 328 | type: "http", |
| 329 | httpPort: 9090, |
| 330 | host: { hostname: "heal.internal", resolverNetwork: { tunnelId: tunnel.tunnelId } }, |
| 331 | }); |
| 332 | }), |
| 333 | ); |
| 334 | |
| 335 | expect(healed.serviceId).not.toEqual(service.serviceId); |
| 336 | expect(healed.httpPort).toEqual(9090); |
| 337 | const live = yield* getService(accountId, healed.serviceId); |
| 338 | expect(live.name).toEqual(testName("alchemy-connectivity-dirsvc-heal")); |
| 339 | |
| 340 | yield* stack.destroy(); |
| 341 | yield* expectGone(accountId, healed.serviceId); |
| 342 | }).pipe(logLevel), |
| 343 | { |
| 344 | tags: [ |
| 345 | "provider:cloudflare", |
| 346 | "provider:cloudflare:connectivity", |
| 347 | "provider:cloudflare:tunnel", |
| 348 | "live", |
| 349 | ], |
| 350 | }, |
| 351 | ); |