Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138import { 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<Response>;let dispose: () => Promise<void>;
const request = (path: string, init?: RequestInit) => handler(new Request(`http://todo.test${path}`, init));
const responseJson = <A>(response: Response) => response.json() as Promise<A>;
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<ReadonlyArray<Todo>>(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<Todo>(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<ErrorBody>(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<Todo>(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<Todo>(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<ErrorBody>(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<ErrorBody>(response);
expect(response.status).toBe(400); expect(body.error).toBe("BadRequest"); });});