The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare"; |
| 2 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 3 | import { HyperdriveDevOriginUnsupported } from "@alchemy.run/cloudflare/Hyperdrive"; |
| 4 | import * as Neon from "@alchemy.run/neon"; |
| 5 | import { expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as hyperdrive from "@distilled.cloud/cloudflare/hyperdrive"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as HttpClient from "effect/http/HttpClient"; |
| 11 | import * as Layer from "effect/Layer"; |
| 12 | import * as Redacted from "effect/Redacted"; |
| 13 | import { MinimumLogLevel } from "effect/References"; |
| 14 | import * as Schedule from "effect/Schedule"; |
| 15 | import * as pathe from "pathe"; |
| 16 | import HyperdriveLocalWorker, { LocalHyperdrive } from "./fixtures/local-worker.ts"; |
| 17 | import { outOfBandConfig } from "./fixtures/lookup-config.ts"; |
| 18 | import HyperdriveLookupLocalWorker, { |
| 19 | LocalLookupHyperdrive, |
| 20 | } from "./fixtures/lookup-local-worker.ts"; |
| 21 | import { lookupConfigName } from "./fixtures/lookup.ts"; |
| 22 | import HyperdriveRemoteWorker, { RemoteHyperdrive } from "./fixtures/remote-worker.ts"; |
| 23 | |
| 24 | // `dev: true` runs local providers behind the RPC sidecar proxy by default, |
| 25 | // matching the process topology of the real `alchemy dev` command. Neon has |
| 26 | // a single (mode-agnostic) provider, so the Postgres origin is always real. |
| 27 | const { test } = Test.make({ |
| 28 | providers: Layer.merge(Cloudflare.providers(), Neon.providers()), |
| 29 | dev: true, |
| 30 | }); |
| 31 | |
| 32 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 33 | |
| 34 | class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{ |
| 35 | status: number; |
| 36 | body: string; |
| 37 | }> { |
| 38 | override get message() { |
| 39 | return `worker answered ${this.status}: ${this.body.slice(0, 500)}`; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | const getJsonReady = (url: string) => |
| 44 | Effect.gen(function* () { |
| 45 | const client = yield* HttpClient.HttpClient; |
| 46 | const res = yield* client.get(url).pipe( |
| 47 | Effect.flatMap((res) => |
| 48 | res.status === 200 |
| 49 | ? Effect.succeed(res) |
| 50 | : res.text.pipe( |
| 51 | Effect.flatMap((body) => |
| 52 | Effect.fail(new WorkerNotReady({ status: res.status, body })), |
| 53 | ), |
| 54 | ), |
| 55 | ), |
| 56 | Effect.retry({ |
| 57 | while: (e): e is WorkerNotReady => e instanceof WorkerNotReady, |
| 58 | // Cap the backoff: an uncapped exponential over 10 recurs sums to |
| 59 | // ~8.5 minutes and turns a persistent non-200 into an apparent hang. |
| 60 | schedule: Schedule.max([ |
| 61 | Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]), |
| 62 | Schedule.recurs(10), |
| 63 | ]), |
| 64 | }), |
| 65 | ); |
| 66 | return yield* res.json; |
| 67 | }).pipe(Effect.orDie); |
| 68 | |
| 69 | type QueryBody = { |
| 70 | row: { sum: number; db: string }; |
| 71 | host: string; |
| 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * Under `alchemy dev` the Hyperdrive Connection is emulated by the local |
| 76 | * provider: reconcile mints a `dev:` id with no cloud API call, and the |
| 77 | * worker's `hyperdrive` binding is lowered onto the local runtime's origin |
| 78 | * passthrough pointed at the `dev` origin (a real Neon Postgres). Running |
| 79 | * SQL through the binding proves the whole local loop. |
| 80 | */ |
| 81 | test.provider( |
| 82 | "Hyperdrive binding round-trips SQL through the local passthrough", |
| 83 | (stack) => |
| 84 | Effect.gen(function* () { |
| 85 | yield* stack.destroy(); |
| 86 | |
| 87 | const deployed = yield* stack.deploy( |
| 88 | Effect.gen(function* () { |
| 89 | const project = yield* Neon.Project("HyperdriveLocalProject"); |
| 90 | const connection = yield* LocalHyperdrive; |
| 91 | const worker = yield* HyperdriveLocalWorker; |
| 92 | return { project, connection, worker }; |
| 93 | }), |
| 94 | ); |
| 95 | |
| 96 | // The local provider fabricates a `dev:` id — proof no cloud call |
| 97 | // ran — and the worker serves from the local dev proxy. |
| 98 | expect(deployed.connection.hyperdriveId).toMatch(/^dev:/); |
| 99 | expect(deployed.worker.url).toMatch(/^http:\/\/localhost:\d+$/); |
| 100 | |
| 101 | const body = (yield* getJsonReady(`${deployed.worker.url}/query`)) as QueryBody; |
| 102 | expect(body.row.sum).toBe(2); |
| 103 | // The query really ran against the dev origin's database. |
| 104 | expect(body.row.db).toBe(deployed.project.databaseName); |
| 105 | // The runtime binding passes through to the dev origin host. |
| 106 | expect(body.host).toBe(deployed.project.origin.host); |
| 107 | |
| 108 | yield* stack.destroy(); |
| 109 | }).pipe(logLevel), |
| 110 | { |
| 111 | tags: [ |
| 112 | "provider:cloudflare", |
| 113 | "provider:cloudflare:hyperdrive", |
| 114 | "provider:cloudflare:worker", |
| 115 | "provider:neon", |
| 116 | "provider:neon:project", |
| 117 | "live", |
| 118 | ], |
| 119 | timeout: 180_000, |
| 120 | }, |
| 121 | ); |
| 122 | |
| 123 | /** |
| 124 | * `Alchemy.remote()` opts the Connection OUT of local emulation: even under |
| 125 | * `alchemy dev` the live provider creates a real Hyperdrive config on |
| 126 | * Cloudflare (a real UUID id), the locally-served worker still round-trips |
| 127 | * SQL, and destroy deletes the real config (its state row is stamped live, |
| 128 | * so the live provider handles the delete even in a dev run). |
| 129 | */ |
| 130 | test.provider( |
| 131 | "Alchemy.remote() connection runs live in dev", |
| 132 | (stack) => |
| 133 | Effect.gen(function* () { |
| 134 | yield* stack.destroy(); |
| 135 | |
| 136 | const deployed = yield* stack.deploy( |
| 137 | Effect.gen(function* () { |
| 138 | const project = yield* Neon.Project("HyperdriveRemoteProject"); |
| 139 | const connection = yield* RemoteHyperdrive; |
| 140 | const worker = yield* HyperdriveRemoteWorker; |
| 141 | return { project, connection, worker }; |
| 142 | }), |
| 143 | ); |
| 144 | |
| 145 | // A real Cloudflare id — the live provider created it. |
| 146 | expect(deployed.connection.hyperdriveId).not.toMatch(/^dev:/); |
| 147 | |
| 148 | // Out-of-band: the config exists on real Cloudflare and fronts the |
| 149 | // Neon origin. |
| 150 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 151 | const config = yield* hyperdrive.getConfig({ |
| 152 | accountId, |
| 153 | hyperdriveId: deployed.connection.hyperdriveId, |
| 154 | }); |
| 155 | expect(config.id).toBe(deployed.connection.hyperdriveId); |
| 156 | expect("host" in config.origin ? config.origin.host : undefined).toBe( |
| 157 | deployed.project.origin.host, |
| 158 | ); |
| 159 | |
| 160 | // The locally-served worker's binding still round-trips SQL. |
| 161 | const body = (yield* getJsonReady(`${deployed.worker.url}/query`)) as QueryBody; |
| 162 | expect(body.row.sum).toBe(2); |
| 163 | expect(body.row.db).toBe(deployed.project.databaseName); |
| 164 | |
| 165 | yield* stack.destroy(); |
| 166 | |
| 167 | // Destroy deleted the real config (stamped live mode). |
| 168 | const gone = yield* hyperdrive |
| 169 | .getConfig({ |
| 170 | accountId, |
| 171 | hyperdriveId: deployed.connection.hyperdriveId, |
| 172 | }) |
| 173 | .pipe( |
| 174 | Effect.as(false), |
| 175 | Effect.catchTag("HyperdriveConfigNotFound", () => Effect.succeed(true)), |
| 176 | ); |
| 177 | expect(gone).toBe(true); |
| 178 | }).pipe(logLevel), |
| 179 | { |
| 180 | tags: [ |
| 181 | "provider:cloudflare", |
| 182 | "provider:cloudflare:hyperdrive", |
| 183 | "provider:cloudflare:worker", |
| 184 | "provider:neon", |
| 185 | "provider:neon:project", |
| 186 | "live", |
| 187 | ], |
| 188 | timeout: 180_000, |
| 189 | }, |
| 190 | ); |
| 191 | |
| 192 | /** |
| 193 | * `Hyperdrive.lookup` reads the real configuration from Cloudflare even in |
| 194 | * dev (it is not a resource, so there is nothing to emulate), while the |
| 195 | * locally-served Worker's binding passes through to the lookup's `dev` |
| 196 | * origin. Destroy leaves the configuration in place. |
| 197 | */ |
| 198 | test.provider( |
| 199 | "lookup with a dev origin round-trips SQL through the local passthrough", |
| 200 | (stack) => |
| 201 | Effect.gen(function* () { |
| 202 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 203 | |
| 204 | yield* stack.destroy(); |
| 205 | |
| 206 | const project = yield* stack.deploy(Neon.Project("HyperdriveLookupLocalProject")); |
| 207 | |
| 208 | yield* Effect.scoped( |
| 209 | Effect.gen(function* () { |
| 210 | const created = yield* outOfBandConfig( |
| 211 | accountId, |
| 212 | lookupConfigName("local"), |
| 213 | project.origin, |
| 214 | ); |
| 215 | |
| 216 | const { effectWorker, asyncWorker } = yield* stack.deploy( |
| 217 | Effect.gen(function* () { |
| 218 | yield* Neon.Project("HyperdriveLookupLocalProject"); |
| 219 | // Through `Cloudflare.Hyperdrive.Connect`. |
| 220 | const effectWorker = yield* HyperdriveLookupLocalWorker; |
| 221 | // Through `env`. |
| 222 | const asyncWorker = yield* Cloudflare.Worker("HyperdriveLookupLocalAsyncWorker", { |
| 223 | main: pathe.resolve(import.meta.dirname, "fixtures/lookup-async-worker.ts"), |
| 224 | env: { HD: LocalLookupHyperdrive }, |
| 225 | }); |
| 226 | return { effectWorker, asyncWorker }; |
| 227 | }), |
| 228 | ); |
| 229 | |
| 230 | for (const url of [`${effectWorker.url}/query`, asyncWorker.url!]) { |
| 231 | expect(url).toMatch(/^http:\/\/localhost:\d+/); |
| 232 | const body = (yield* getJsonReady(url)) as QueryBody; |
| 233 | expect(body.row.sum).toBe(2); |
| 234 | expect(body.row.db).toBe(project.databaseName); |
| 235 | expect(body.host).toBe(project.origin.host); |
| 236 | } |
| 237 | |
| 238 | yield* stack.destroy(); |
| 239 | |
| 240 | const survived = yield* hyperdrive.getConfig({ accountId, hyperdriveId: created.id }); |
| 241 | expect(survived.id).toBe(created.id); |
| 242 | }), |
| 243 | ); |
| 244 | }).pipe(logLevel), |
| 245 | { |
| 246 | tags: [ |
| 247 | "provider:cloudflare", |
| 248 | "provider:cloudflare:hyperdrive", |
| 249 | "provider:cloudflare:worker", |
| 250 | "provider:neon", |
| 251 | "provider:neon:project", |
| 252 | "live", |
| 253 | ], |
| 254 | timeout: 180_000, |
| 255 | }, |
| 256 | ); |
| 257 | |
| 258 | /** |
| 259 | * A looked-up configuration's origin password is never returned by the API, |
| 260 | * so a dev run binding a lookup without a `dev` origin must die with the |
| 261 | * typed `HyperdriveDevOriginUnsupported` defect. The lookup itself must |
| 262 | * resolve first, so it targets a real out-of-band configuration. |
| 263 | */ |
| 264 | test.provider( |
| 265 | "lookup without a dev origin dies with HyperdriveDevOriginUnsupported", |
| 266 | (stack) => |
| 267 | Effect.gen(function* () { |
| 268 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 269 | |
| 270 | yield* stack.destroy(); |
| 271 | |
| 272 | const project = yield* stack.deploy(Neon.Project("HyperdriveLookupLocalProject")); |
| 273 | |
| 274 | const defect = yield* Effect.scoped( |
| 275 | Effect.gen(function* () { |
| 276 | const created = yield* outOfBandConfig( |
| 277 | accountId, |
| 278 | `${lookupConfigName("local")}-nodev`, |
| 279 | project.origin, |
| 280 | ); |
| 281 | return yield* stack |
| 282 | .deploy( |
| 283 | Effect.gen(function* () { |
| 284 | yield* Neon.Project("HyperdriveLookupLocalProject"); |
| 285 | return yield* Cloudflare.Worker("HyperdriveLookupNoDevWorker", { |
| 286 | main: pathe.resolve(import.meta.dirname, "fixtures/vpc-async-worker.ts"), |
| 287 | env: { |
| 288 | HD: Cloudflare.Hyperdrive.lookup("HyperdriveLookupNoDev", { |
| 289 | hyperdriveId: created.id, |
| 290 | }), |
| 291 | }, |
| 292 | }); |
| 293 | }), |
| 294 | ) |
| 295 | .pipe( |
| 296 | Effect.flatMap(() => Effect.die(new Error("expected the dev deploy to die"))), |
| 297 | Effect.catchDefect((defect) => Effect.succeed(defect)), |
| 298 | ); |
| 299 | }), |
| 300 | ); |
| 301 | |
| 302 | expect(defect).toBeInstanceOf(HyperdriveDevOriginUnsupported); |
| 303 | expect(defect).toMatchObject({ |
| 304 | _tag: "HyperdriveDevOriginUnsupported", |
| 305 | logicalId: "HyperdriveLookupNoDev", |
| 306 | origin: "lookup", |
| 307 | }); |
| 308 | |
| 309 | yield* stack.destroy(); |
| 310 | }).pipe(logLevel), |
| 311 | { |
| 312 | tags: [ |
| 313 | "provider:cloudflare", |
| 314 | "provider:cloudflare:hyperdrive", |
| 315 | "provider:cloudflare:worker", |
| 316 | "provider:neon", |
| 317 | "provider:neon:project", |
| 318 | "live", |
| 319 | ], |
| 320 | timeout: 180_000, |
| 321 | }, |
| 322 | ); |
| 323 | |
| 324 | /** |
| 325 | * A Workers VPC origin is only reachable from Cloudflare's network, so a dev |
| 326 | * run binding it without a `dev` override must die with the typed |
| 327 | * `HyperdriveDevOriginUnsupported` defect instead of starting a Worker that |
| 328 | * can never connect. |
| 329 | */ |
| 330 | test.provider( |
| 331 | "VPC Service origin without a dev override dies with HyperdriveDevOriginUnsupported", |
| 332 | (stack) => |
| 333 | Effect.gen(function* () { |
| 334 | yield* stack.destroy(); |
| 335 | |
| 336 | const defect = yield* stack |
| 337 | .deploy( |
| 338 | Effect.gen(function* () { |
| 339 | const connection = yield* Cloudflare.Hyperdrive.Connection( |
| 340 | "HyperdriveVpcDevConnection", |
| 341 | { |
| 342 | origin: { |
| 343 | scheme: "postgres", |
| 344 | serviceId: "00000000-0000-4000-8000-000000000000", |
| 345 | database: "postgres", |
| 346 | user: "postgres", |
| 347 | password: Redacted.make("unused"), |
| 348 | }, |
| 349 | }, |
| 350 | ); |
| 351 | return yield* Cloudflare.Worker("HyperdriveVpcDevWorker", { |
| 352 | main: pathe.resolve(import.meta.dirname, "fixtures/vpc-async-worker.ts"), |
| 353 | env: { HD: connection }, |
| 354 | }); |
| 355 | }), |
| 356 | ) |
| 357 | .pipe( |
| 358 | Effect.flatMap(() => Effect.die(new Error("expected the dev deploy to die"))), |
| 359 | Effect.catchDefect((defect) => Effect.succeed(defect)), |
| 360 | ); |
| 361 | |
| 362 | expect(defect).toBeInstanceOf(HyperdriveDevOriginUnsupported); |
| 363 | expect(defect).toMatchObject({ |
| 364 | _tag: "HyperdriveDevOriginUnsupported", |
| 365 | logicalId: "HyperdriveVpcDevConnection", |
| 366 | origin: "vpc", |
| 367 | }); |
| 368 | |
| 369 | yield* stack.destroy(); |
| 370 | }).pipe(logLevel), |
| 371 | { |
| 372 | tags: [ |
| 373 | "provider:cloudflare", |
| 374 | "provider:cloudflare:hyperdrive", |
| 375 | "provider:cloudflare:worker", |
| 376 | "local", |
| 377 | ], |
| 378 | timeout: 120_000, |
| 379 | }, |
| 380 | ); |