import { HttpApp } from "@effect/platform"; import { afterAll, beforeAll, describe, expect, test } from "vitest"; import { App, TodoRepositoryLive } from "@/app"; type Todo = { readonly id: string; readonly title: string; readonly completed: boolean; readonly createdAt: string; readonly updatedAt: string; }; type ErrorBody = { readonly error: string; readonly message: string; }; let handler: (request: Request) => Promise; let dispose: () => Promise; const request = (path: string, init?: RequestInit) => handler(new Request(`http://todo.test${path}`, init)); const responseJson = (response: Response) => response.json() as Promise; export const makeTestApp = () => HttpApp.toWebHandlerLayer(App, TodoRepositoryLive); beforeAll(() => { const testApp = makeTestApp(); handler = testApp.handler; dispose = testApp.dispose; }); afterAll(async () => { await dispose(); }); describe("todo api", () => { test("lists seeded todos", async () => { const response = await request("/todos"); const todos = await responseJson>(response); expect(response.status).toBe(200); expect(todos).toHaveLength(3); expect(todos[0]).toMatchObject({ id: "1", title: "Learn Effect", completed: true, }); }); test("gets a todo by id", async () => { const response = await request("/todos/1"); const todo = await responseJson(response); expect(response.status).toBe(200); expect(todo).toMatchObject({ id: "1", title: "Learn Effect", completed: true, }); }); test("returns 404 for a missing todo", async () => { const response = await request("/todos/missing"); const body = await responseJson(response); expect(response.status).toBe(404); expect(body).toMatchObject({ error: "TodoNotFound", message: "Todo missing was not found", }); }); test("creates, updates, and deletes a todo", async () => { const createResponse = await request("/todos", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ title: "Write tests" }), }); const created = await responseJson(createResponse); expect(createResponse.status).toBe(201); expect(created).toMatchObject({ title: "Write tests", completed: false, }); expect(created.id).toEqual(expect.any(String)); const updateResponse = await request(`/todos/${created.id}`, { method: "PATCH", headers: { "content-type": "application/json" }, body: JSON.stringify({ completed: true }), }); const updated = await responseJson(updateResponse); expect(updateResponse.status).toBe(200); expect(updated).toMatchObject({ id: created.id, title: "Write tests", completed: true, }); const deleteResponse = await request(`/todos/${created.id}`, { method: "DELETE", }); expect(deleteResponse.status).toBe(204); const getDeletedResponse = await request(`/todos/${created.id}`); expect(getDeletedResponse.status).toBe(404); }); test("rejects invalid create input", async () => { const response = await request("/todos", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ title: "" }), }); const body = await responseJson(response); expect(response.status).toBe(400); expect(body.error).toBe("BadRequest"); }); test("rejects empty update input", async () => { const response = await request("/todos/1", { method: "PATCH", headers: { "content-type": "application/json" }, body: JSON.stringify({}), }); const body = await responseJson(response); expect(response.status).toBe(400); expect(body.error).toBe("BadRequest"); }); });