53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import "dotenv/config";
|
|
import { describe, it, expect, vi } from "vitest";
|
|
import { promises as fs } from "fs";
|
|
import path from "path";
|
|
|
|
const BASE_URL = "http://localhost";
|
|
|
|
// /api/video 업로드 TDD
|
|
// - POST /api/video 로 FormData(file) 를 전송하면 업로드 파일이 uploads 디렉터리에 저장되고
|
|
// id / servedUrl(`/api/video/:id`) 을 반환해야 한다.
|
|
|
|
// Prisma 엔진이 실제로 로드되지 않도록 PrismaClient 를 목 처리한다.
|
|
vi.mock("@/generated/prisma/client", () => {
|
|
class PrismaClient {
|
|
project = {
|
|
upsert: async () => ({ id: "project-mock" }),
|
|
};
|
|
|
|
asset = {
|
|
create: async () => ({ id: "asset-mock" }),
|
|
};
|
|
}
|
|
|
|
return { PrismaClient };
|
|
});
|
|
|
|
describe("/api/video 업로드", () => {
|
|
it("POST /api/video 는 업로드된 비디오 파일을 저장하고 id/servedUrl 을 반환해야 한다", async () => {
|
|
const { POST: handleUpload } = await import("@/app/api/video/route");
|
|
|
|
const formData = new FormData();
|
|
const blob = new Blob(["dummy-video"], { type: "video/mp4" });
|
|
// FormData 에서는 File/Blob 모두 허용되므로, Blob 을 그대로 사용한다.
|
|
formData.append("file", blob as any);
|
|
|
|
const res = await handleUpload(
|
|
new Request(`${BASE_URL}/api/video`, {
|
|
method: "POST",
|
|
body: formData,
|
|
}),
|
|
);
|
|
|
|
expect(res.status).toBe(201);
|
|
const json = (await res.json()) as { id: string; servedUrl?: string | null };
|
|
expect(json.id).toBeTruthy();
|
|
expect(json.servedUrl).toBe(`/api/video/${json.id}`);
|
|
|
|
const filePath = path.join(process.cwd(), "uploads", json.id);
|
|
const stat = await fs.stat(filePath);
|
|
expect(stat.isFile()).toBe(true);
|
|
});
|
|
});
|