The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect } from "bun:test"; |
| 2 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 3 | import * as Drizzle from "@alchemy.run/drizzle"; |
| 4 | import * as Neon from "@alchemy.run/neon"; |
| 5 | import * as Test from "@alchemy.run/test/Bun"; |
| 6 | import * as Alchemy from "alchemy"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 11 | import * as Layer from "effect/Layer"; |
| 12 | import { RpcClient, RpcSerialization } from "effect/rpc"; |
| 13 | import * as Schedule from "effect/Schedule"; |
| 14 | import Stack from "../alchemy.run.ts"; |
| 15 | import { TodoRpcs } from "../src/backend/rpc.ts"; |
| 16 | |
| 17 | describe.each([true, false])("CloudflareTanstackRpcDrizzleExample (dev: %p)", (dev) => { |
| 18 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 19 | providers: Layer.mergeAll(Cloudflare.providers(), Drizzle.providers(), Neon.providers()), |
| 20 | state: Alchemy.localState(), |
| 21 | dev, |
| 22 | }); |
| 23 | |
| 24 | const stack = beforeAll( |
| 25 | deploy(Stack).pipe( |
| 26 | Effect.tap((stack) => |
| 27 | // Fresh workers.dev URLs take a few seconds to start answering, so |
| 28 | // fetch with retries until it's ready before we exercise it in tests. |
| 29 | // Note that the response here will be the RPC server complaining that the |
| 30 | // request is invalid since we're not actually making an RPC call here. |
| 31 | // `filterStatusOk` works because Effect RPC still returns 200, but we may |
| 32 | // need to update this if/when Effect RPC changes its behavior. |
| 33 | HttpClient.get(new URL("/rpc", stack.websiteUrl)).pipe( |
| 34 | Effect.flatMap(HttpClientResponse.filterStatusOk), |
| 35 | Effect.retry({ |
| 36 | schedule: Schedule.max([Schedule.spaced("500 millis"), Schedule.recurs(20)]), |
| 37 | }), |
| 38 | ), |
| 39 | ), |
| 40 | ), |
| 41 | ); |
| 42 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 43 | |
| 44 | const RpcProtocol = stack.pipe( |
| 45 | Effect.map(({ websiteUrl }) => |
| 46 | RpcClient.layerProtocolHttp({ |
| 47 | url: new URL("/rpc", websiteUrl).toString(), |
| 48 | }), |
| 49 | ), |
| 50 | Layer.unwrap, |
| 51 | Layer.provide(FetchHttpClient.layer), |
| 52 | Layer.provide(RpcSerialization.layerJson), |
| 53 | ); |
| 54 | |
| 55 | test( |
| 56 | "deploys and exposes urls + db identifiers", |
| 57 | Effect.gen(function* () { |
| 58 | const { websiteUrl, branchId, hyperdriveId } = yield* stack; |
| 59 | expect(websiteUrl).toBeString(); |
| 60 | expect(branchId).toBeString(); |
| 61 | expect(hyperdriveId).toBeString(); |
| 62 | }), |
| 63 | ); |
| 64 | |
| 65 | test( |
| 66 | "todo CRUD round-trips through the /rpc proxy into Drizzle/Neon", |
| 67 | Effect.gen(function* () { |
| 68 | const client = yield* RpcClient.make(TodoRpcs); |
| 69 | |
| 70 | const created = yield* client.createTodo({ text: "write the example" }); |
| 71 | expect(created.id).toBeNumber(); |
| 72 | expect(created.text).toBe("write the example"); |
| 73 | expect(created.done).toBe(false); |
| 74 | |
| 75 | const afterCreate = yield* client.listTodos(); |
| 76 | expect(afterCreate.some((t) => t.id === created.id)).toBe(true); |
| 77 | |
| 78 | const toggled = yield* client.toggleTodo({ |
| 79 | id: created.id, |
| 80 | done: true, |
| 81 | }); |
| 82 | expect(toggled.done).toBe(true); |
| 83 | |
| 84 | const deletedId = yield* client.deleteTodo({ id: created.id }); |
| 85 | expect(deletedId).toBe(created.id); |
| 86 | |
| 87 | const afterDelete = yield* client.listTodos(); |
| 88 | expect(afterDelete.some((t) => t.id === created.id)).toBe(false); |
| 89 | }).pipe(Effect.provide(RpcProtocol)), |
| 90 | ); |
| 91 | |
| 92 | test( |
| 93 | "toggling a missing todo fails with TodoNotFound", |
| 94 | Effect.gen(function* () { |
| 95 | const client = yield* RpcClient.make(TodoRpcs); |
| 96 | const result = yield* client.toggleTodo({ id: 2_147_483_000, done: true }).pipe(Effect.flip); |
| 97 | expect(result._tag).toBe("TodoNotFound"); |
| 98 | }).pipe(Effect.provide(RpcProtocol)), |
| 99 | ); |
| 100 | }); |