The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { expect } from "bun:test"; |
| 2 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 3 | import * as Planetscale from "@alchemy.run/planetscale"; |
| 4 | import * as Test from "@alchemy.run/test/Bun"; |
| 5 | import * as Alchemy from "alchemy"; |
| 6 | import * as Duration from "effect/Duration"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import Stack from "../alchemy.run.ts"; |
| 13 | import type { Post, User } from "../src/schema.ts"; |
| 14 | |
| 15 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 16 | providers: Layer.mergeAll(Cloudflare.providers(), Planetscale.providers()), |
| 17 | state: Alchemy.localState(), |
| 18 | }); |
| 19 | |
| 20 | const stack = beforeAll(deploy(Stack), { timeout: 600_000 }); |
| 21 | |
| 22 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { |
| 23 | timeout: 600_000, |
| 24 | }); |
| 25 | |
| 26 | test( |
| 27 | "worker exposes a URL, hyperdrive id, and planetscale identifiers", |
| 28 | Effect.gen(function* () { |
| 29 | const { url, databaseId, branchName, hyperdriveId } = yield* stack; |
| 30 | |
| 31 | expect(url).toBeString(); |
| 32 | expect(databaseId).toBeString(); |
| 33 | expect(branchName).toBeString(); |
| 34 | expect(hyperdriveId).toBeString(); |
| 35 | }), |
| 36 | ); |
| 37 | |
| 38 | // Fresh `workers.dev` URLs transiently 404 (route still propagating) or 5xx |
| 39 | // (Hyperdrive/PlanetScale binding still settling). `Test.getWhenReady` fails on |
| 40 | // that cold-start window and retries until the worker answers; the first hit in |
| 41 | // each test rides it, subsequent requests run against the warmed worker. |
| 42 | const { getWhenReady } = Test; |
| 43 | |
| 44 | test( |
| 45 | "worker exposes user CRUD through Drizzle / Hyperdrive / PlanetScale", |
| 46 | Effect.gen(function* () { |
| 47 | const { url } = yield* stack; |
| 48 | const baseUrl = url.replace(/\/+$/, ""); |
| 49 | |
| 50 | const initialResponse = yield* getWhenReady(baseUrl); |
| 51 | expect(initialResponse.status).toBe(200); |
| 52 | |
| 53 | const initialBody = (yield* initialResponse.json) as unknown as { |
| 54 | users: User[]; |
| 55 | }; |
| 56 | expect(Array.isArray(initialBody.users)).toBe(true); |
| 57 | |
| 58 | const createResponse = yield* HttpClient.execute(HttpClientRequest.post(baseUrl)); |
| 59 | expect(createResponse.status).toBe(200); |
| 60 | |
| 61 | const createBody = (yield* createResponse.json) as unknown as { |
| 62 | user: User[]; |
| 63 | }; |
| 64 | expect(createBody.user).toHaveLength(1); |
| 65 | |
| 66 | const [createdUser] = createBody.user; |
| 67 | expect(createdUser.id).toBeNumber(); |
| 68 | expect(createdUser.email).toBeString(); |
| 69 | expect(createdUser.name).toBeString(); |
| 70 | expect(createdUser.createdAt).toBeString(); |
| 71 | |
| 72 | const readResponse = yield* HttpClient.get(`${baseUrl}/${createdUser.id}`); |
| 73 | expect(readResponse.status).toBe(200); |
| 74 | |
| 75 | const readBody = (yield* readResponse.json) as unknown as { |
| 76 | user: User & { posts: Post[] }; |
| 77 | }; |
| 78 | expect(readBody.user).toMatchObject({ |
| 79 | id: createdUser.id, |
| 80 | email: createdUser.email, |
| 81 | name: createdUser.name, |
| 82 | posts: [], |
| 83 | }); |
| 84 | |
| 85 | const invalidReadResponse = yield* HttpClient.get(`${baseUrl}/not-a-user`); |
| 86 | expect(invalidReadResponse.status).toBe(400); |
| 87 | expect(yield* invalidReadResponse.json).toEqual({ |
| 88 | error: "Invalid user ID", |
| 89 | }); |
| 90 | |
| 91 | const methodResponse = yield* HttpClient.execute(HttpClientRequest.patch(baseUrl)); |
| 92 | expect(methodResponse.status).toBe(405); |
| 93 | expect(yield* methodResponse.json).toEqual({ |
| 94 | error: "Method not allowed", |
| 95 | }); |
| 96 | |
| 97 | const deleteResponse = yield* HttpClient.execute( |
| 98 | HttpClientRequest.delete(`${baseUrl}/${createdUser.id}`), |
| 99 | ); |
| 100 | expect(deleteResponse.status).toBe(200); |
| 101 | |
| 102 | const deleteBody = (yield* deleteResponse.json) as unknown as { |
| 103 | user: User; |
| 104 | }; |
| 105 | expect(deleteBody.user).toMatchObject({ |
| 106 | id: createdUser.id, |
| 107 | email: createdUser.email, |
| 108 | name: createdUser.name, |
| 109 | }); |
| 110 | |
| 111 | const finalResponse = yield* HttpClient.get(baseUrl); |
| 112 | expect(finalResponse.status).toBe(200); |
| 113 | const finalBody = (yield* finalResponse.json) as unknown as { |
| 114 | users: User[]; |
| 115 | }; |
| 116 | expect(finalBody.users.some((user) => user.id === createdUser.id)).toBe(false); |
| 117 | }), |
| 118 | { timeout: 20_000 }, |
| 119 | ); |
| 120 | |
| 121 | test( |
| 122 | "worker handles 100 sequential queries spaced 100-500ms apart", |
| 123 | Effect.gen(function* () { |
| 124 | const { url } = yield* stack; |
| 125 | const baseUrl = url.replace(/\/+$/, ""); |
| 126 | |
| 127 | yield* getWhenReady(baseUrl); |
| 128 | |
| 129 | const queryOnce = Effect.gen(function* () { |
| 130 | const response = yield* HttpClient.get(baseUrl); |
| 131 | expect(response.status).toBe(200); |
| 132 | const body = (yield* response.json) as unknown as { users: User[] }; |
| 133 | expect(Array.isArray(body.users)).toBe(true); |
| 134 | }); |
| 135 | |
| 136 | const jitter = Effect.sync(() => Math.floor(Math.random() * 401) + 100).pipe( |
| 137 | Effect.flatMap((ms) => Effect.sleep(Duration.millis(ms))), |
| 138 | ); |
| 139 | |
| 140 | yield* queryOnce.pipe(Effect.zip(jitter), Effect.repeat(Schedule.recurs(99))); |
| 141 | }), |
| 142 | { timeout: 120_000 }, |
| 143 | ); |