The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Data from "effect/Data"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as HttpClient from "effect/http/HttpClient"; |
| 7 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 8 | import type * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import Stack from "./fixtures/stack.ts"; |
| 12 | |
| 13 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 14 | providers: Cloudflare.providers(), |
| 15 | state: Cloudflare.state(), |
| 16 | }); |
| 17 | |
| 18 | const HOOK_TIMEOUT = 300_000; |
| 19 | const TEST_TIMEOUT = 120_000; |
| 20 | |
| 21 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 22 | |
| 23 | class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{ |
| 24 | status: number; |
| 25 | body: string; |
| 26 | }> {} |
| 27 | |
| 28 | // Bounded spaced schedule — caps total wait so a genuine failure surfaces |
| 29 | // fast instead of an uncapped exponential blowing past the test timeout |
| 30 | // while riding out fresh-workers.dev cold-start propagation. |
| 31 | const ready = Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(45)]); |
| 32 | |
| 33 | /** Retry an HTTP call until it returns 200 (rides out cold-start 404s). */ |
| 34 | const untilOk = <E, R>(eff: Effect.Effect<HttpClientResponse.HttpClientResponse, E, R>) => |
| 35 | eff.pipe( |
| 36 | Effect.flatMap((res) => |
| 37 | res.status === 200 |
| 38 | ? Effect.succeed(res) |
| 39 | : res.text.pipe( |
| 40 | Effect.flatMap((body) => Effect.fail(new WorkerNotReady({ status: res.status, body }))), |
| 41 | ), |
| 42 | ), |
| 43 | Effect.retry({ |
| 44 | while: (e): e is WorkerNotReady => e instanceof WorkerNotReady, |
| 45 | schedule: ready, |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | class RowsMismatch extends Data.TaggedError("RowsMismatch")<{ |
| 50 | actual: string; |
| 51 | }> {} |
| 52 | |
| 53 | const retryRows = <A, E, R>(eff: Effect.Effect<A, E, R>) => |
| 54 | eff.pipe( |
| 55 | Effect.retry({ |
| 56 | while: (e: E) => e instanceof RowsMismatch, |
| 57 | schedule: ready, |
| 58 | }), |
| 59 | ); |
| 60 | |
| 61 | /** |
| 62 | * Drive the full D1 client surface against ONE deployed worker over `fetch`: |
| 63 | * `exec` (/init) → `batch` (/seed) → `prepare.bind.run` (/users POST) → |
| 64 | * `prepare.bind.all` (/users GET) → `prepare.bind.first` (/users/:id) → |
| 65 | * `raw` (/raw). Both workers share one physical database; each stamps its |
| 66 | * rows with a distinct `style`, so the row-count assertions stay independent. |
| 67 | */ |
| 68 | const exercise = (base: string) => |
| 69 | Effect.gen(function* () { |
| 70 | // exec — CREATE TABLE. Don't assert exact count (0 on a re-run), only shape. |
| 71 | const initRes = yield* untilOk(HttpClient.execute(HttpClientRequest.post(`${base}/init`))); |
| 72 | const initBody = (yield* initRes.json) as { |
| 73 | count: number; |
| 74 | duration: number; |
| 75 | }; |
| 76 | expect(typeof initBody.count).toBe("number"); |
| 77 | expect(typeof initBody.duration).toBe("number"); |
| 78 | |
| 79 | // batch — three inserts in one transactional call. |
| 80 | const seedRes = yield* untilOk(HttpClient.execute(HttpClientRequest.post(`${base}/seed`))); |
| 81 | expect(yield* seedRes.json).toMatchObject({ batches: 3, success: true }); |
| 82 | |
| 83 | // prepare.bind.run — single insert. |
| 84 | const insertRes = yield* untilOk( |
| 85 | HttpClient.execute( |
| 86 | HttpClientRequest.post(`${base}/users`).pipe( |
| 87 | HttpClientRequest.bodyJsonUnsafe({ id: 4, name: "dave" }), |
| 88 | ), |
| 89 | ), |
| 90 | ); |
| 91 | expect(yield* insertRes.json).toMatchObject({ |
| 92 | success: true, |
| 93 | meta: { changes: 1 }, |
| 94 | }); |
| 95 | |
| 96 | // prepare.all — SELECT all rows for this style. D1 read-after-write is |
| 97 | // eventually consistent across edge locations, so retry until all four |
| 98 | // seeded/inserted rows are visible. |
| 99 | const expected = [ |
| 100 | { id: 1, name: "alice" }, |
| 101 | { id: 2, name: "bob" }, |
| 102 | { id: 3, name: "carol" }, |
| 103 | { id: 4, name: "dave" }, |
| 104 | ]; |
| 105 | const allBody = yield* untilOk(HttpClient.get(`${base}/users`)).pipe( |
| 106 | Effect.flatMap((res) => res.json), |
| 107 | Effect.flatMap((body) => { |
| 108 | const b = body as { |
| 109 | success: boolean; |
| 110 | results: Array<{ id: number; name: string }>; |
| 111 | }; |
| 112 | return b.results.length === expected.length |
| 113 | ? Effect.succeed(b) |
| 114 | : Effect.fail(new RowsMismatch({ actual: JSON.stringify(b.results) })); |
| 115 | }), |
| 116 | retryRows, |
| 117 | ); |
| 118 | expect(allBody.success).toBe(true); |
| 119 | expect(allBody.results).toEqual(expected); |
| 120 | |
| 121 | // prepare.bind.first — SELECT one row. |
| 122 | const oneRes = yield* untilOk(HttpClient.get(`${base}/users/2`)); |
| 123 | expect(yield* oneRes.json).toEqual({ row: { id: 2, name: "bob" } }); |
| 124 | |
| 125 | // raw — direct runtime D1Database access (the Better Auth / Drizzle hatch). |
| 126 | // Retry until the count reflects all four rows (eventual consistency). |
| 127 | const rawBody = yield* untilOk(HttpClient.get(`${base}/raw`)).pipe( |
| 128 | Effect.flatMap((res) => res.json), |
| 129 | Effect.flatMap((body) => { |
| 130 | const b = body as { count: number }; |
| 131 | return b.count === expected.length |
| 132 | ? Effect.succeed(b) |
| 133 | : Effect.fail(new RowsMismatch({ actual: JSON.stringify(b) })); |
| 134 | }), |
| 135 | retryRows, |
| 136 | ); |
| 137 | expect(rawBody.count).toBe(4); |
| 138 | }); |
| 139 | |
| 140 | /** |
| 141 | * End-to-end test of `Cloudflare.D1.QueryDatabase(...)` against a real |
| 142 | * Cloudflare Worker + D1 database, covering BOTH invocation styles against |
| 143 | * one shared database: |
| 144 | * |
| 145 | * - effect-worker: `yield* Cloudflare.D1.QueryDatabase(db)` in Init with |
| 146 | * `Cloudflare.D1.QueryDatabaseBinding` provided to the worker effect; |
| 147 | * - async-worker: the database declared on the Worker `env` (resolved to the |
| 148 | * native `cf.D1Database` via `InferEnv`) and used from a plain async fetch. |
| 149 | * |
| 150 | * The stack lives in `fixtures/stack.ts` so it can also be inspected directly, |
| 151 | * e.g. `alchemy logs --tail --stage test --config ./test/Cloudflare/D1/fixtures/stack.ts`. |
| 152 | */ |
| 153 | const stack = beforeAll(deploy(Stack), { timeout: HOOK_TIMEOUT }); |
| 154 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { |
| 155 | timeout: HOOK_TIMEOUT, |
| 156 | }); |
| 157 | |
| 158 | // ── effect-worker ── `yield* Cloudflare.D1.QueryDatabase(db)` + ConnectionBinding. |
| 159 | test( |
| 160 | "effect-worker: QueryDatabase exercises the full client surface", |
| 161 | Effect.gen(function* () { |
| 162 | const out = yield* stack; |
| 163 | yield* exercise(out.effectWorkerUrl); |
| 164 | }).pipe(logLevel), |
| 165 | { |
| 166 | tags: ["provider:cloudflare", "provider:cloudflare:d1", "provider:cloudflare:worker", "live"], |
| 167 | timeout: TEST_TIMEOUT, |
| 168 | }, |
| 169 | ); |
| 170 | |
| 171 | // ── async-worker ── D1 declared on `env`, native `env.DB` used from async fetch. |
| 172 | test( |
| 173 | "async-worker: env-declared D1 binding exercises the full surface", |
| 174 | Effect.gen(function* () { |
| 175 | const out = yield* stack; |
| 176 | yield* exercise(out.asyncWorkerUrl); |
| 177 | }).pipe(logLevel), |
| 178 | { |
| 179 | tags: ["provider:cloudflare", "provider:cloudflare:d1", "provider:cloudflare:worker", "live"], |
| 180 | timeout: TEST_TIMEOUT, |
| 181 | }, |
| 182 | ); |