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 Drizzle from "@alchemy.run/drizzle"; |
| 4 | import * as Test from "@alchemy.run/test/Bun"; |
| 5 | import * as Alchemy from "alchemy"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import Stack from "../alchemy.run.ts"; |
| 11 | import type { Post, User } from "../src/schema.ts"; |
| 12 | |
| 13 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 14 | providers: Layer.mergeAll(Cloudflare.providers(), Drizzle.providers()), |
| 15 | state: Alchemy.localState(), |
| 16 | }); |
| 17 | |
| 18 | const stack = beforeAll(deploy(Stack)); |
| 19 | |
| 20 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 21 | |
| 22 | // Fresh `workers.dev` URLs transiently 404 while the route propagates. |
| 23 | // `Test.getWhenReady` retries until the worker answers. |
| 24 | const { getWhenReady } = Test; |
| 25 | |
| 26 | test( |
| 27 | "worker exposes a URL and database name", |
| 28 | Effect.gen(function* () { |
| 29 | const { url, databaseName } = yield* stack; |
| 30 | |
| 31 | expect(url).toBeString(); |
| 32 | expect(databaseName).toBeString(); |
| 33 | }), |
| 34 | ); |
| 35 | |
| 36 | test( |
| 37 | "worker exposes user CRUD through Drizzle on D1", |
| 38 | Effect.gen(function* () { |
| 39 | const { url } = yield* stack; |
| 40 | const baseUrl = url.replace(/\/+$/, ""); |
| 41 | |
| 42 | const initialResponse = yield* getWhenReady(baseUrl); |
| 43 | expect(initialResponse.status).toBe(200); |
| 44 | |
| 45 | const initialBody = (yield* initialResponse.json) as unknown as { |
| 46 | users: User[]; |
| 47 | }; |
| 48 | expect(Array.isArray(initialBody.users)).toBe(true); |
| 49 | |
| 50 | const createResponse = yield* HttpClient.execute(HttpClientRequest.post(baseUrl)); |
| 51 | expect(createResponse.status).toBe(200); |
| 52 | |
| 53 | const createBody = (yield* createResponse.json) as unknown as { |
| 54 | user: User[]; |
| 55 | }; |
| 56 | expect(createBody.user).toHaveLength(1); |
| 57 | |
| 58 | const [createdUser] = createBody.user; |
| 59 | expect(createdUser.id).toBeNumber(); |
| 60 | expect(createdUser.email).toBeString(); |
| 61 | expect(createdUser.name).toBeString(); |
| 62 | |
| 63 | const readResponse = yield* HttpClient.get(`${baseUrl}/${createdUser.id}`); |
| 64 | expect(readResponse.status).toBe(200); |
| 65 | |
| 66 | const readBody = (yield* readResponse.json) as unknown as { |
| 67 | user: User & { posts: Post[] }; |
| 68 | }; |
| 69 | expect(readBody.user).toMatchObject({ |
| 70 | id: createdUser.id, |
| 71 | email: createdUser.email, |
| 72 | name: createdUser.name, |
| 73 | posts: [], |
| 74 | }); |
| 75 | |
| 76 | const invalidReadResponse = yield* HttpClient.get(`${baseUrl}/not-a-user`); |
| 77 | expect(invalidReadResponse.status).toBe(400); |
| 78 | expect(yield* invalidReadResponse.json).toEqual({ |
| 79 | error: "Invalid user ID", |
| 80 | }); |
| 81 | |
| 82 | const methodResponse = yield* HttpClient.execute(HttpClientRequest.patch(baseUrl)); |
| 83 | expect(methodResponse.status).toBe(405); |
| 84 | expect(yield* methodResponse.json).toEqual({ |
| 85 | error: "Method not allowed", |
| 86 | }); |
| 87 | |
| 88 | const deleteResponse = yield* HttpClient.execute( |
| 89 | HttpClientRequest.delete(`${baseUrl}/${createdUser.id}`), |
| 90 | ); |
| 91 | expect(deleteResponse.status).toBe(200); |
| 92 | |
| 93 | const deleteBody = (yield* deleteResponse.json) as unknown as { |
| 94 | user: User; |
| 95 | }; |
| 96 | expect(deleteBody.user).toMatchObject({ |
| 97 | id: createdUser.id, |
| 98 | email: createdUser.email, |
| 99 | name: createdUser.name, |
| 100 | }); |
| 101 | |
| 102 | const finalResponse = yield* HttpClient.get(baseUrl); |
| 103 | expect(finalResponse.status).toBe(200); |
| 104 | const finalBody = (yield* finalResponse.json) as unknown as { |
| 105 | users: User[]; |
| 106 | }; |
| 107 | expect(finalBody.users.some((user) => user.id === createdUser.id)).toBe(false); |
| 108 | }), |
| 109 | { timeout: 120_000 }, |
| 110 | ); |