import { afterAll, expect, test } from "bun:test"; import { openDb } from "./db"; import { startServer } from "./app"; // A real server on a free port, with a database in memory. const db = openDb(":memory:"); const { server } = startServer(db, 0); afterAll(() => server.stop(true)); test("health check", async () => { const res = await fetch(new URL("/healthz", server.url)); expect(await res.text()).toBe("ok"); }); test("signing the guestbook", async () => { const res = await fetch(new URL("/api/messages", server.url), { method: "POST", headers: { "Content-Type": "application/json", "X-ExeDev-Email": "ada@example.com" }, body: JSON.stringify({ body: "Hello" }), }); expect(res.status).toBe(201); const list = await (await fetch(new URL("/api/messages", server.url))).json(); expect(list[0]).toMatchObject({ author: "ada", body: "Hello" }); }); test("empty messages are refused", async () => { const res = await fetch(new URL("/api/messages", server.url), { method: "POST", body: "{}" }); expect(res.status).toBe(400); });