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 { expect } from "bun:test"; |
| 2 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 3 | import * as Neon from "@alchemy.run/neon"; |
| 4 | import * as Prisma from "@alchemy.run/prisma"; |
| 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 HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Schema from "effect/Schema"; |
| 12 | import Stack from "../alchemy.run.ts"; |
| 13 | |
| 14 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 15 | providers: Layer.mergeAll(Cloudflare.providers(), Prisma.providers(), Neon.providers()), |
| 16 | state: Alchemy.localState(), |
| 17 | }); |
| 18 | |
| 19 | const stack = beforeAll(deploy(Stack)); |
| 20 | |
| 21 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 22 | |
| 23 | // Fresh `workers.dev` URLs transiently 404/5xx while the route and the |
| 24 | // Hyperdrive/Neon bindings settle; getWhenReady retries until the worker |
| 25 | // answers. |
| 26 | const { getWhenReady } = Test; |
| 27 | |
| 28 | interface UserRow { |
| 29 | id: string; |
| 30 | email: string; |
| 31 | name: string; |
| 32 | posts?: unknown[]; |
| 33 | } |
| 34 | |
| 35 | test( |
| 36 | "worker exposes a URL, hyperdrive id, and neon branch id", |
| 37 | Effect.gen(function* () { |
| 38 | const { url, branchId, hyperdriveId } = yield* stack; |
| 39 | |
| 40 | expect(url).toBeString(); |
| 41 | expect(branchId).toBeString(); |
| 42 | expect(hyperdriveId).toBeString(); |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | test( |
| 47 | "worker exposes user CRUD through Prisma ORM / Hyperdrive / Neon", |
| 48 | Effect.gen(function* () { |
| 49 | const { url } = yield* stack; |
| 50 | const baseUrl = url.replace(/\/+$/, ""); |
| 51 | |
| 52 | const initialResponse = yield* getWhenReady(baseUrl); |
| 53 | expect(initialResponse.status).toBe(200); |
| 54 | const initialBody = (yield* initialResponse.json) as unknown as { |
| 55 | users: UserRow[]; |
| 56 | }; |
| 57 | expect(Array.isArray(initialBody.users)).toBe(true); |
| 58 | |
| 59 | const createResponse = yield* HttpClient.execute(HttpClientRequest.post(baseUrl)); |
| 60 | expect(createResponse.status).toBe(200); |
| 61 | const { user: createdUser } = (yield* createResponse.json) as unknown as { |
| 62 | user: UserRow; |
| 63 | }; |
| 64 | expect(createdUser.id).toBeString(); |
| 65 | expect(createdUser.email).toBeString(); |
| 66 | |
| 67 | const listedResponse = yield* HttpClient.get(baseUrl); |
| 68 | const listed = yield* Schema.decodeUnknownEffect( |
| 69 | Schema.Struct({ |
| 70 | users: Schema.Array(Schema.Struct({ id: Schema.String })), |
| 71 | }), |
| 72 | )(yield* listedResponse.json); |
| 73 | expect(listed.users.some((user) => user.id === createdUser.id)).toBe(true); |
| 74 | |
| 75 | const readResponse = yield* HttpClient.get(`${baseUrl}/${createdUser.id}`); |
| 76 | expect(readResponse.status).toBe(200); |
| 77 | const readBody = (yield* readResponse.json) as unknown as { |
| 78 | user: UserRow; |
| 79 | }; |
| 80 | expect(readBody.user).toMatchObject({ |
| 81 | id: createdUser.id, |
| 82 | email: createdUser.email, |
| 83 | posts: [], |
| 84 | }); |
| 85 | |
| 86 | const missingResponse = yield* HttpClient.get( |
| 87 | `${baseUrl}/00000000-0000-0000-0000-000000000000`, |
| 88 | ); |
| 89 | expect(missingResponse.status).toBe(200); |
| 90 | expect(yield* missingResponse.json).toEqual({ user: null }); |
| 91 | |
| 92 | const deleteResponse = yield* HttpClient.execute( |
| 93 | HttpClientRequest.delete(`${baseUrl}/${createdUser.id}`), |
| 94 | ); |
| 95 | expect(deleteResponse.status).toBe(200); |
| 96 | const deleteBody = (yield* deleteResponse.json) as unknown as { |
| 97 | user: UserRow | null; |
| 98 | }; |
| 99 | expect(deleteBody.user).toMatchObject({ id: createdUser.id }); |
| 100 | |
| 101 | const finalResponse = yield* HttpClient.get(baseUrl); |
| 102 | expect(finalResponse.status).toBe(200); |
| 103 | const finalBody = (yield* finalResponse.json) as unknown as { |
| 104 | users: UserRow[]; |
| 105 | }; |
| 106 | expect(finalBody.users.some((user) => user.id === createdUser.id)).toBe(false); |
| 107 | }), |
| 108 | { timeout: 120_000 }, |
| 109 | ); |