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 d1 from "@distilled.cloud/cloudflare/d1"; |
| 6 | import * as Alchemy from "alchemy"; |
| 7 | import { Action } from "alchemy/Action"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as FileSystem from "effect/FileSystem"; |
| 11 | import * as HttpClient from "effect/http/HttpClient"; |
| 12 | import * as Path from "effect/Path"; |
| 13 | import { MinimumLogLevel } from "effect/References"; |
| 14 | import * as Schedule from "effect/Schedule"; |
| 15 | import * as pathe from "pathe"; |
| 16 | |
| 17 | // `dev: true` runs local providers behind the RPC sidecar proxy by default, |
| 18 | // matching the process topology of the real `alchemy dev` command (see |
| 19 | // MakeOptions.sidecar in Test/Core.ts). |
| 20 | const { test, deploy, destroy } = Test.make({ |
| 21 | providers: Cloudflare.providers(), |
| 22 | dev: true, |
| 23 | }); |
| 24 | |
| 25 | // The in-process topology: no RpcProviderProxy, so the RPC-backed local |
| 26 | // provider builds directly in this process with the un-gated |
| 27 | // `localRuntimeServices()` from its dual registration. Still a real |
| 28 | // production path — a programmatic `deploy({ dev: true })` without the |
| 29 | // `alchemy dev` CLI has no sidecar. |
| 30 | const { test: inProcessTest } = Test.make({ |
| 31 | providers: Cloudflare.providers(), |
| 32 | dev: true, |
| 33 | sidecar: false, |
| 34 | }); |
| 35 | |
| 36 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 37 | |
| 38 | const RpcMigrationStack = Alchemy.Stack( |
| 39 | "D1RpcMigrationStack", |
| 40 | { |
| 41 | providers: Cloudflare.providers(), |
| 42 | state: Alchemy.localState(), |
| 43 | }, |
| 44 | Cloudflare.D1.Database("RpcMigratedDB", { |
| 45 | migrations: pathe.resolve(import.meta.dirname, "fixtures/rpc-migrations"), |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{ |
| 50 | status: number; |
| 51 | }> {} |
| 52 | |
| 53 | const getJsonReady = (url: string) => |
| 54 | Effect.gen(function* () { |
| 55 | const client = yield* HttpClient.HttpClient; |
| 56 | const res = yield* client.get(url).pipe( |
| 57 | Effect.flatMap((res) => |
| 58 | res.status === 200 |
| 59 | ? Effect.succeed(res) |
| 60 | : Effect.fail(new WorkerNotReady({ status: res.status })), |
| 61 | ), |
| 62 | Effect.retry({ |
| 63 | while: (e): e is WorkerNotReady => e instanceof WorkerNotReady, |
| 64 | // Cap the backoff: an uncapped exponential over 10 recurs sums to |
| 65 | // ~8.5 minutes and turns a persistent non-200 into an apparent hang. |
| 66 | schedule: Schedule.max([ |
| 67 | Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]), |
| 68 | Schedule.recurs(10), |
| 69 | ]), |
| 70 | }), |
| 71 | ); |
| 72 | return yield* res.json; |
| 73 | }).pipe(Effect.orDie); |
| 74 | |
| 75 | /** |
| 76 | * Regression test for #1007. Under the RPC proxy used by `alchemy dev` (the |
| 77 | * default topology for `dev: true` tests), `localRuntimeServices()` is |
| 78 | * intentionally omitted from the caller because RPC providers consume it in |
| 79 | * the sidecar. D1's local provider is not an RPC provider, so its migration |
| 80 | * lifecycle must provide the standalone gateway runtime explicitly. |
| 81 | */ |
| 82 | test( |
| 83 | "D1 migrations apply with the alchemy dev RPC proxy", |
| 84 | Effect.gen(function* () { |
| 85 | yield* destroy(RpcMigrationStack); |
| 86 | |
| 87 | const database = yield* deploy(RpcMigrationStack); |
| 88 | |
| 89 | expect(database.databaseId).toMatch(/^dev:/); |
| 90 | expect(Object.keys(database.migrationsHashes)).toEqual(["0001_notes.sql"]); |
| 91 | |
| 92 | yield* destroy(RpcMigrationStack); |
| 93 | }).pipe(logLevel), |
| 94 | { |
| 95 | tags: ["provider:cloudflare", "provider:cloudflare:d1", "local"], |
| 96 | timeout: 120_000, |
| 97 | }, |
| 98 | ); |
| 99 | |
| 100 | /** |
| 101 | * Counterpart of the #1007 regression above for the in-process topology: |
| 102 | * without the proxy, the provider's migration lifecycle runs in this |
| 103 | * process and must find the workerd runtime in its own layer (the dual |
| 104 | * registration's `localRuntimeServices()`, real when un-gated). |
| 105 | */ |
| 106 | inProcessTest.provider( |
| 107 | "D1 migrations apply in-process without the RPC proxy", |
| 108 | (stack) => |
| 109 | Effect.gen(function* () { |
| 110 | yield* stack.destroy(); |
| 111 | |
| 112 | const db = yield* stack.deploy( |
| 113 | Cloudflare.D1.Database("InProcessMigratedDB", { |
| 114 | migrations: pathe.resolve(import.meta.dirname, "fixtures/rpc-migrations"), |
| 115 | }), |
| 116 | ); |
| 117 | |
| 118 | expect(db.databaseId).toMatch(/^dev:/); |
| 119 | expect(Object.keys(db.migrationsHashes)).toEqual(["0001_notes.sql"]); |
| 120 | |
| 121 | yield* stack.destroy(); |
| 122 | }).pipe(logLevel), |
| 123 | { |
| 124 | tags: ["provider:cloudflare", "provider:cloudflare:d1", "local"], |
| 125 | timeout: 120_000, |
| 126 | }, |
| 127 | ); |
| 128 | |
| 129 | /** |
| 130 | * Under `alchemy dev` the D1 Database resource is emulated by the local |
| 131 | * provider (a `dev:` id, no cloud API calls) and the worker's `d1` binding |
| 132 | * is lowered onto the local workerd D1 simulator (workerd's real |
| 133 | * `cloudflare-internal:d1-api` over DO SQLite). This exercises DDL, |
| 134 | * prepared statements, and reads through the native `env` binding. |
| 135 | */ |
| 136 | test.provider( |
| 137 | "D1 database binding round-trips against the local simulator", |
| 138 | (stack) => |
| 139 | Effect.gen(function* () { |
| 140 | yield* stack.destroy(); |
| 141 | |
| 142 | const deployed = yield* stack.deploy( |
| 143 | Effect.gen(function* () { |
| 144 | const db = yield* Cloudflare.D1.Database("LocalDB"); |
| 145 | const worker = yield* Cloudflare.Worker("d1-local-worker", { |
| 146 | main: pathe.resolve(import.meta.dirname, "fixtures/d1-local-worker.ts"), |
| 147 | env: { DB: db }, |
| 148 | }); |
| 149 | return { db, worker }; |
| 150 | }), |
| 151 | ); |
| 152 | |
| 153 | // The local provider fabricates a `dev:` id — proof no cloud call ran. |
| 154 | expect(deployed.db.databaseId).toMatch(/^dev:/); |
| 155 | |
| 156 | const body = (yield* getJsonReady(`${deployed.worker.url}/roundtrip`)) as { |
| 157 | names: string[]; |
| 158 | count: number | null; |
| 159 | }; |
| 160 | expect(body.names).toEqual(["alice", "bob"]); |
| 161 | expect(body.count).toBe(2); |
| 162 | |
| 163 | yield* stack.destroy(); |
| 164 | }).pipe(logLevel), |
| 165 | { |
| 166 | tags: ["provider:cloudflare", "provider:cloudflare:d1", "provider:cloudflare:worker", "local"], |
| 167 | timeout: 120_000, |
| 168 | }, |
| 169 | ); |
| 170 | |
| 171 | /** |
| 172 | * Migrations apply against the local simulator: reconcile boots an |
| 173 | * ephemeral gateway workerd and drives the same migration flow the live |
| 174 | * provider uses (Alchemy's `__alchemy_migrations` table, idempotent |
| 175 | * re-application). Verified through the deployed worker's binding — the |
| 176 | * same DO SQLite storage the gateway wrote to. |
| 177 | */ |
| 178 | test.provider( |
| 179 | "D1 migrations apply against the local simulator and re-apply incrementally", |
| 180 | (stack) => |
| 181 | Effect.gen(function* () { |
| 182 | const fs = yield* FileSystem.FileSystem; |
| 183 | const path = yield* Path.Path; |
| 184 | const migrationsDir = yield* fs.makeTempDirectory({ |
| 185 | prefix: "alchemy-d1-local-migrations-", |
| 186 | }); |
| 187 | yield* fs.writeFileString( |
| 188 | path.join(migrationsDir, "0001_users.sql"), |
| 189 | [ |
| 190 | "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);", |
| 191 | "INSERT INTO users (name) VALUES ('seed');", |
| 192 | ].join("\n"), |
| 193 | ); |
| 194 | |
| 195 | yield* stack.destroy(); |
| 196 | |
| 197 | const deploy = Effect.gen(function* () { |
| 198 | const db = yield* Cloudflare.D1.Database("LocalMigratedDB", { |
| 199 | migrations: migrationsDir, |
| 200 | }); |
| 201 | const worker = yield* Cloudflare.Worker("d1-migrations-worker", { |
| 202 | main: pathe.resolve(import.meta.dirname, "fixtures/d1-local-worker.ts"), |
| 203 | env: { DB: db }, |
| 204 | }); |
| 205 | return { db, worker }; |
| 206 | }); |
| 207 | |
| 208 | const v1 = yield* stack.deploy(deploy); |
| 209 | expect(v1.db.databaseId).toMatch(/^dev:/); |
| 210 | expect(v1.db.migrationsTable).toBe("__alchemy_migrations"); |
| 211 | expect(Object.keys(v1.db.migrationsHashes)).toEqual(["0001_users.sql"]); |
| 212 | |
| 213 | const url = v1.worker.url!; |
| 214 | const schema = (yield* getJsonReady(`${url}/tables`)) as { |
| 215 | tables: string[]; |
| 216 | }; |
| 217 | expect(schema.tables).toContain("users"); |
| 218 | expect(schema.tables).toContain("__alchemy_migrations"); |
| 219 | |
| 220 | const users = (yield* getJsonReady(`${url}/users`)) as { |
| 221 | users: string[]; |
| 222 | }; |
| 223 | expect(users.users).toEqual(["seed"]); |
| 224 | |
| 225 | // Add a second migration — the redeploy applies ONLY the new file |
| 226 | // (0001's seed row would fail a re-run with a duplicate table error, |
| 227 | // so a passing redeploy also proves idempotency). |
| 228 | // ~180 KB of small statements: over SQLite's statement length limit |
| 229 | // as a whole, so the migration must not run as one statement. |
| 230 | yield* fs.writeFileString( |
| 231 | path.join(migrationsDir, "0002_posts.sql"), |
| 232 | [ |
| 233 | "CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT NOT NULL);", |
| 234 | ...Array.from( |
| 235 | { length: 150 }, |
| 236 | (_, i) => `INSERT INTO posts (title) VALUES ('post-${i}-${"x".repeat(1200)}');`, |
| 237 | ), |
| 238 | ].join("\n"), |
| 239 | ); |
| 240 | |
| 241 | const v2 = yield* stack.deploy(deploy); |
| 242 | expect(v2.db.databaseId).toBe(v1.db.databaseId); |
| 243 | expect(Object.keys(v2.db.migrationsHashes).sort()).toEqual([ |
| 244 | "0001_users.sql", |
| 245 | "0002_posts.sql", |
| 246 | ]); |
| 247 | |
| 248 | // Alchemy's shape: INTEGER ids, name-keyed. |
| 249 | const migrations = (yield* getJsonReady(`${url}/migrations`)) as { |
| 250 | migrations: Array<{ id: number; name: string }>; |
| 251 | }; |
| 252 | expect(migrations.migrations).toEqual([ |
| 253 | { id: 1, name: "0001_users.sql" }, |
| 254 | { id: 2, name: "0002_posts.sql" }, |
| 255 | ]); |
| 256 | |
| 257 | // Data written by 0001 survived the second deploy (not re-applied). |
| 258 | const usersAfter = (yield* getJsonReady(`${url}/users`)) as { |
| 259 | users: string[]; |
| 260 | }; |
| 261 | expect(usersAfter.users).toEqual(["seed"]); |
| 262 | |
| 263 | yield* stack.destroy(); |
| 264 | }).pipe(logLevel), |
| 265 | { |
| 266 | tags: ["provider:cloudflare", "provider:cloudflare:d1", "provider:cloudflare:worker", "local"], |
| 267 | timeout: 120_000, |
| 268 | }, |
| 269 | ); |
| 270 | |
| 271 | /** |
| 272 | * `importFiles` apply locally too: an import file is multi-statement SQL, |
| 273 | * executed through the gateway with the same hash-skip semantics as the |
| 274 | * cloud import flow. Verified through the worker's binding. |
| 275 | */ |
| 276 | test.provider( |
| 277 | "D1 importFiles apply against the local simulator", |
| 278 | (stack) => |
| 279 | Effect.gen(function* () { |
| 280 | const fs = yield* FileSystem.FileSystem; |
| 281 | const path = yield* Path.Path; |
| 282 | const dir = yield* fs.makeTempDirectory({ |
| 283 | prefix: "alchemy-d1-local-import-", |
| 284 | }); |
| 285 | const importFile = path.join(dir, "seed.sql"); |
| 286 | yield* fs.writeFileString( |
| 287 | importFile, |
| 288 | [ |
| 289 | "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);", |
| 290 | "INSERT INTO users (name) VALUES ('imported');", |
| 291 | // ~180 KB of small statements: over SQLite's statement length |
| 292 | // limit as a whole, so the file must not run as one statement. |
| 293 | ...Array.from( |
| 294 | { length: 150 }, |
| 295 | (_, i) => `INSERT INTO users (name) VALUES ('bulk-${i}-${"x".repeat(1200)}');`, |
| 296 | ), |
| 297 | ].join("\n"), |
| 298 | ); |
| 299 | |
| 300 | yield* stack.destroy(); |
| 301 | |
| 302 | const deployed = yield* stack.deploy( |
| 303 | Effect.gen(function* () { |
| 304 | const db = yield* Cloudflare.D1.Database("LocalImportedDB", { |
| 305 | importFiles: [importFile], |
| 306 | }); |
| 307 | const worker = yield* Cloudflare.Worker("d1-import-worker", { |
| 308 | main: pathe.resolve(import.meta.dirname, "fixtures/d1-local-worker.ts"), |
| 309 | env: { DB: db }, |
| 310 | }); |
| 311 | return { db, worker }; |
| 312 | }), |
| 313 | ); |
| 314 | |
| 315 | expect(deployed.db.databaseId).toMatch(/^dev:/); |
| 316 | expect(Object.keys(deployed.db.importHashes)).toEqual([importFile]); |
| 317 | |
| 318 | const users = (yield* getJsonReady(`${deployed.worker.url}/users`)) as { |
| 319 | users: string[]; |
| 320 | }; |
| 321 | expect(users.users).toHaveLength(151); |
| 322 | expect(users.users).toContain("imported"); |
| 323 | |
| 324 | yield* stack.destroy(); |
| 325 | }).pipe(logLevel), |
| 326 | { |
| 327 | tags: ["provider:cloudflare", "provider:cloudflare:d1", "provider:cloudflare:worker", "local"], |
| 328 | timeout: 120_000, |
| 329 | }, |
| 330 | ); |
| 331 | |
| 332 | /** |
| 333 | * `QueryDatabaseLocal` (the stack-eval capability used in Actions) reaches |
| 334 | * the LOCAL simulator when the database is a `dev:` row: queries tunnel |
| 335 | * through an ephemeral workerd gateway into the same DO SQLite the worker |
| 336 | * binding reads. The Action seeds; the deployed worker's native binding |
| 337 | * observes the seeded rows — proving both transports share one database. |
| 338 | */ |
| 339 | test.provider( |
| 340 | "QueryDatabaseLocal Action seeds the local simulator in dev", |
| 341 | (stack) => |
| 342 | Effect.gen(function* () { |
| 343 | yield* stack.destroy(); |
| 344 | |
| 345 | const deployed = yield* stack.deploy( |
| 346 | Effect.gen(function* () { |
| 347 | const db = yield* Cloudflare.D1.Database("ActionSeededDB"); |
| 348 | |
| 349 | const Seed = Action( |
| 350 | "Seed", |
| 351 | Effect.gen(function* () { |
| 352 | const client = yield* Cloudflare.D1.QueryDatabase(db); |
| 353 | return Effect.fn(function* () { |
| 354 | yield* client.exec( |
| 355 | "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT NOT NULL)", |
| 356 | ); |
| 357 | yield* client.prepare("DELETE FROM users").run(); |
| 358 | yield* client.prepare("INSERT INTO users (name) VALUES (?)").bind("ada").run(); |
| 359 | const rows = yield* client |
| 360 | .prepare("SELECT name FROM users ORDER BY name") |
| 361 | .all<{ name: string }>(); |
| 362 | return { names: rows.results.map((r) => r.name) }; |
| 363 | }); |
| 364 | }).pipe(Effect.provide(Cloudflare.D1.QueryDatabaseLocal)), |
| 365 | ); |
| 366 | const seeded = yield* Seed({}); |
| 367 | |
| 368 | const worker = yield* Cloudflare.Worker("d1-action-worker", { |
| 369 | main: pathe.resolve(import.meta.dirname, "fixtures/d1-local-worker.ts"), |
| 370 | env: { DB: db }, |
| 371 | }); |
| 372 | return { db, worker, seeded }; |
| 373 | }), |
| 374 | ); |
| 375 | |
| 376 | expect(deployed.db.databaseId).toMatch(/^dev:/); |
| 377 | expect(deployed.seeded.names).toEqual(["ada"]); |
| 378 | |
| 379 | // The worker's native binding reads the same simulator storage the |
| 380 | // Action's gateway wrote to. |
| 381 | const users = (yield* getJsonReady(`${deployed.worker.url}/users`)) as { |
| 382 | users: string[]; |
| 383 | }; |
| 384 | expect(users.users).toEqual(["ada"]); |
| 385 | |
| 386 | yield* stack.destroy(); |
| 387 | }).pipe(logLevel), |
| 388 | { |
| 389 | tags: ["provider:cloudflare", "provider:cloudflare:d1", "provider:cloudflare:worker", "local"], |
| 390 | timeout: 120_000, |
| 391 | }, |
| 392 | ); |
| 393 | |
| 394 | /** |
| 395 | * `Alchemy.remote()` opts the database OUT of local emulation: even under |
| 396 | * `alchemy dev` it is created on real Cloudflare (a real UUID, not a `dev:` |
| 397 | * id) and the worker's `d1` binding proxies to it remotely. An out-of-band |
| 398 | * query through the cloud API proves the worker's writes landed in the real |
| 399 | * database, and destroy removes it. |
| 400 | */ |
| 401 | test.provider( |
| 402 | "Alchemy.remote() database runs live in dev", |
| 403 | (stack) => |
| 404 | Effect.gen(function* () { |
| 405 | yield* stack.destroy(); |
| 406 | |
| 407 | const deployed = yield* stack.deploy( |
| 408 | Effect.gen(function* () { |
| 409 | const db = yield* Cloudflare.D1.Database("LiveDevDB").pipe(Alchemy.remote()); |
| 410 | const worker = yield* Cloudflare.Worker("d1-live-worker", { |
| 411 | main: pathe.resolve(import.meta.dirname, "fixtures/d1-local-worker.ts"), |
| 412 | env: { DB: db }, |
| 413 | }); |
| 414 | return { db, worker }; |
| 415 | }), |
| 416 | ); |
| 417 | |
| 418 | // A real UUID — the live provider created it on Cloudflare. |
| 419 | expect(deployed.db.databaseId).not.toMatch(/^dev:/); |
| 420 | |
| 421 | const body = (yield* getJsonReady(`${deployed.worker.url}/roundtrip`)) as { |
| 422 | names: string[]; |
| 423 | }; |
| 424 | expect(body.names).toEqual(["alice", "bob"]); |
| 425 | |
| 426 | // Out-of-band: the rows are visible through the cloud API — the |
| 427 | // remote-proxied binding really hit the live database. |
| 428 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 429 | const queryDb = yield* d1.queryDatabase; |
| 430 | const result = yield* queryDb({ |
| 431 | accountId, |
| 432 | databaseId: deployed.db.databaseId, |
| 433 | sql: "SELECT name FROM users ORDER BY name;", |
| 434 | }); |
| 435 | const names = (result.result[0]?.results ?? []) as Array<{ |
| 436 | name: string; |
| 437 | }>; |
| 438 | expect(names.map((r) => r.name)).toEqual(["alice", "bob"]); |
| 439 | |
| 440 | yield* stack.destroy(); |
| 441 | |
| 442 | // Destroy deleted the real database (stamped live mode). |
| 443 | const gone = yield* d1.getDatabase({ accountId, databaseId: deployed.db.databaseId }).pipe( |
| 444 | Effect.as(false), |
| 445 | Effect.catchTag("DatabaseNotFound", () => Effect.succeed(true)), |
| 446 | ); |
| 447 | expect(gone).toBe(true); |
| 448 | }).pipe(logLevel), |
| 449 | { |
| 450 | tags: ["provider:cloudflare", "provider:cloudflare:d1", "provider:cloudflare:worker", "live"], |
| 451 | timeout: 120_000, |
| 452 | }, |
| 453 | ); |