import "dotenv/config"; import { describe, it, expect, vi } from "vitest"; // PrismaClient를 실제 DB 대신 메모리 기반 저장소를 사용하는 목으로 대체한다. // 이렇게 하면 CI/로컬 어디에서도 DATABASE_URL 이나 실제 DB 없이 API 테스트를 실행할 수 있다. const inMemoryProjects: any[] = []; vi.mock("@/generated/prisma/client", () => { class PrismaClientMock { project = { create: async ({ data }: any) => { const project = { id: inMemoryProjects.length + 1, ...data }; inMemoryProjects.push(project); return project; }, findUnique: async ({ where: { slug } }: any) => { return inMemoryProjects.find((p) => p.slug === slug) ?? null; }, }; } return { PrismaClient: PrismaClientMock }; }); const BASE_URL = "http://localhost"; describe("/api/projects", () => { it("POST /api/projects 로 프로젝트를 생성하고 GET /api/projects/[slug] 로 조회할 수 있어야 한다", async () => { const payload = { title: "테스트 프로젝트", slug: `test-slug-${Date.now()}`, contentJson: [ { id: "blk_test", type: "text", props: { text: "JSON에서 온 텍스트", align: "left", size: "base" }, }, ], }; const { POST: createProject } = await import("@/app/api/projects/route"); const createResponse = await createProject( new Request(`${BASE_URL}/api/projects`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }), ); if (createResponse.status !== 201) { // 디버깅을 위해 에러 응답을 한번 출력해 둔다. // eslint-disable-next-line no-console console.error("createResponse status", createResponse.status, await createResponse.text()); } expect(createResponse.status).toBe(201); const created = (await createResponse.json()) as any; expect(created.title).toBe(payload.title); expect(created.slug).toBe(payload.slug); const { GET: getProjectBySlug } = await import("@/app/api/projects/[slug]/route"); const getResponse = await getProjectBySlug( new Request(`${BASE_URL}/api/projects/${payload.slug}`), { params: { slug: payload.slug } } as any, ); expect(getResponse.status).toBe(200); const fetched = (await getResponse.json()) as any; expect(fetched.slug).toBe(payload.slug); expect(fetched.contentJson).toEqual(payload.contentJson); }); });