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 * as Neon from "@alchemy.run/neon"; |
| 3 | import * as Prisma from "@alchemy.run/prisma"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import Stack from "./fixtures/prisma-orm/stack.ts"; |
| 13 | |
| 14 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 15 | providers: Layer.mergeAll(Cloudflare.providers(), Neon.providers(), Prisma.providers()), |
| 16 | }); |
| 17 | |
| 18 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 19 | |
| 20 | const stack = beforeAll(deploy(Stack), { timeout: 120_000 }); |
| 21 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 22 | |
| 23 | class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{ |
| 24 | status: number; |
| 25 | body: string; |
| 26 | }> { |
| 27 | get message() { |
| 28 | return `Worker returned ${this.status}: ${this.body}`; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** GET with fresh-edge retry: workers.dev serves placeholder errors briefly. */ |
| 33 | const getOk = (url: string) => |
| 34 | Effect.gen(function* () { |
| 35 | const client = yield* HttpClient.HttpClient; |
| 36 | return yield* client.get(url).pipe( |
| 37 | Effect.flatMap((res) => |
| 38 | res.status === 200 |
| 39 | ? Effect.succeed(res) |
| 40 | : res.text.pipe( |
| 41 | Effect.flatMap((body) => |
| 42 | Effect.fail(new WorkerNotReady({ status: res.status, body })), |
| 43 | ), |
| 44 | ), |
| 45 | ), |
| 46 | Effect.timeout("15 seconds"), |
| 47 | Effect.retry({ |
| 48 | while: (error) => |
| 49 | error._tag === "WorkerNotReady" && (error.status === 404 || error.status === 503), |
| 50 | schedule: Schedule.exponential("500 millis"), |
| 51 | times: 6, |
| 52 | }), |
| 53 | ); |
| 54 | }); |
| 55 | |
| 56 | test( |
| 57 | "Prisma client round-trips orm queries through Hyperdrive on workerd", |
| 58 | Effect.gen(function* () { |
| 59 | const { url } = yield* stack; |
| 60 | |
| 61 | const created = yield* getOk(`${url}/widgets/create/gizmo`).pipe( |
| 62 | Effect.flatMap((res) => res.json), |
| 63 | Effect.map((json) => json as { id: number; name: string }), |
| 64 | ); |
| 65 | expect(created.name).toEqual("gizmo"); |
| 66 | expect(created.id).toBeGreaterThan(0); |
| 67 | |
| 68 | // Read-after-write in a separate fetch event (separate pool). |
| 69 | const fetched = yield* getOk(`${url}/widgets/get/${created.id}`).pipe( |
| 70 | Effect.flatMap((res) => res.json), |
| 71 | Effect.map((json) => json as { found: boolean; name: string | null }), |
| 72 | ); |
| 73 | expect(fetched).toEqual({ found: true, name: "gizmo" }); |
| 74 | const prepared = yield* getOk(`${url}/widgets/prepared/${created.id}`).pipe( |
| 75 | Effect.flatMap((res) => res.json), |
| 76 | ); |
| 77 | expect(prepared).toEqual({ found: true, name: "gizmo" }); |
| 78 | const missing = yield* getOk(`${url}/widgets/prepared/0`).pipe( |
| 79 | Effect.flatMap((res) => res.json), |
| 80 | ); |
| 81 | expect(missing).toEqual({ found: false, name: null }); |
| 82 | }).pipe(logLevel), |
| 83 | { |
| 84 | tags: [ |
| 85 | "provider:cloudflare", |
| 86 | "provider:cloudflare:hyperdrive", |
| 87 | "provider:cloudflare:worker", |
| 88 | "provider:neon", |
| 89 | "provider:neon:branch", |
| 90 | "provider:neon:project", |
| 91 | "provider:prisma", |
| 92 | "provider:prisma:orm", |
| 93 | "live", |
| 94 | ], |
| 95 | timeout: 120_000, |
| 96 | }, |
| 97 | ); |
| 98 | |
| 99 | test( |
| 100 | "transactions work inside a single event", |
| 101 | Effect.gen(function* () { |
| 102 | const { url } = yield* stack; |
| 103 | const result = yield* getOk(`${url}/widgets/tx/tx-gizmo`).pipe( |
| 104 | Effect.flatMap((res) => res.json), |
| 105 | Effect.map((json) => json as { name: string | null }), |
| 106 | ); |
| 107 | expect(result.name).toEqual("tx-gizmo"); |
| 108 | }).pipe(logLevel), |
| 109 | { |
| 110 | tags: [ |
| 111 | "provider:cloudflare", |
| 112 | "provider:cloudflare:hyperdrive", |
| 113 | "provider:cloudflare:worker", |
| 114 | "provider:neon", |
| 115 | "provider:neon:branch", |
| 116 | "provider:neon:project", |
| 117 | "provider:prisma", |
| 118 | "provider:prisma:orm", |
| 119 | "live", |
| 120 | ], |
| 121 | timeout: 120_000, |
| 122 | }, |
| 123 | ); |
| 124 | |
| 125 | test( |
| 126 | "sql lane and typed rollback on workerd", |
| 127 | Effect.gen(function* () { |
| 128 | const { url } = yield* stack; |
| 129 | |
| 130 | const sql = yield* getOk(`${url}/widgets/sql`).pipe( |
| 131 | Effect.flatMap((res) => res.json), |
| 132 | Effect.map((json) => json as { count: number }), |
| 133 | ); |
| 134 | expect(sql.count).toBeGreaterThanOrEqual(0); |
| 135 | |
| 136 | const rollback = yield* getOk(`${url}/widgets/rollback/ghost-widget`).pipe( |
| 137 | Effect.flatMap((res) => res.json), |
| 138 | Effect.map((json) => json as { outcome: string; visible: boolean }), |
| 139 | ); |
| 140 | expect(rollback).toEqual({ outcome: "rolled-back", visible: false }); |
| 141 | }).pipe(logLevel), |
| 142 | { |
| 143 | tags: [ |
| 144 | "provider:cloudflare", |
| 145 | "provider:cloudflare:hyperdrive", |
| 146 | "provider:cloudflare:worker", |
| 147 | "provider:neon", |
| 148 | "provider:neon:branch", |
| 149 | "provider:neon:project", |
| 150 | "provider:prisma", |
| 151 | "provider:prisma:orm", |
| 152 | "live", |
| 153 | ], |
| 154 | timeout: 120_000, |
| 155 | }, |
| 156 | ); |
| 157 | |
| 158 | test( |
| 159 | "sequential and concurrent events each get their own pool (no cross-request I/O)", |
| 160 | Effect.gen(function* () { |
| 161 | const { url } = yield* stack; |
| 162 | |
| 163 | const created = yield* getOk(`${url}/widgets/create/pool-widget`).pipe( |
| 164 | Effect.flatMap((res) => res.json), |
| 165 | Effect.map((json) => json as { id: number }), |
| 166 | ); |
| 167 | |
| 168 | const query = getOk(`${url}/widgets/get/${created.id}`).pipe( |
| 169 | Effect.flatMap((res) => res.json), |
| 170 | Effect.map((json) => json as { found: boolean }), |
| 171 | ); |
| 172 | |
| 173 | // Sequential events reuse nothing across requests. |
| 174 | for (let i = 0; i < 3; i++) { |
| 175 | expect((yield* query).found).toBe(true); |
| 176 | } |
| 177 | |
| 178 | // Concurrent events: workerd kills cross-request I/O ("Cannot perform |
| 179 | // I/O on behalf of a different request") and a shared closed pool fails |
| 180 | // with "Cannot use a pool after calling end" — six parallel queries pin |
| 181 | // the per-event pool isolation. |
| 182 | const results = yield* Effect.all( |
| 183 | Array.from({ length: 6 }, () => query), |
| 184 | { |
| 185 | concurrency: "unbounded", |
| 186 | }, |
| 187 | ); |
| 188 | for (const result of results) { |
| 189 | expect(result.found).toBe(true); |
| 190 | } |
| 191 | }).pipe(logLevel), |
| 192 | { |
| 193 | tags: [ |
| 194 | "provider:cloudflare", |
| 195 | "provider:cloudflare:hyperdrive", |
| 196 | "provider:cloudflare:worker", |
| 197 | "provider:neon", |
| 198 | "provider:neon:branch", |
| 199 | "provider:neon:project", |
| 200 | "provider:prisma", |
| 201 | "provider:prisma:orm", |
| 202 | "live", |
| 203 | ], |
| 204 | timeout: 120_000, |
| 205 | }, |
| 206 | ); |