test: 添加完整测试套件(52 个文件,326 个用例)

基于 Vitest 搭建测试基础设施,覆盖后端纯函数、API 路由、
前端 hooks、UI 组件和页面级集成测试。
This commit is contained in:
2026-02-28 20:19:14 +08:00
parent 11eeec868e
commit 3ccd1262f9
59 changed files with 8131 additions and 3 deletions
@@ -0,0 +1,56 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { prismaMock, resetPrismaMock } from "@/__tests__/helpers/prisma-mock";
import { createRequest, parseJsonResponse } from "@/__tests__/helpers/api-test-utils";
vi.mock("@/lib/blindbox", () => ({
requireMembership: vi.fn().mockResolvedValue({}),
}));
vi.mock("@/lib/ai", () => ({
suggestIdeas: vi.fn().mockResolvedValue(["去爬山", "骑自行车", "看日出", "野餐"]),
}));
import { GET } from "./route";
const mockCtx = { params: Promise.resolve({}) };
beforeEach(() => {
resetPrismaMock();
});
describe("GET /api/blindbox/suggest", () => {
it("returns AI suggestions when enough ideas exist", async () => {
prismaMock.blindBoxIdea.findMany.mockResolvedValue([
{ content: "去公园" },
{ content: "看电影" },
{ content: "吃火锅" },
] as never);
const req = createRequest("/api/blindbox/suggest?roomId=bb-room-1&userId=user-1");
const res = await GET(req, mockCtx);
const { status, data } = await parseJsonResponse(res);
expect(status).toBe(200);
expect(data.suggestions).toHaveLength(4);
expect(data.source).toBe("ai");
});
it("returns empty when less than 2 ideas", async () => {
prismaMock.blindBoxIdea.findMany.mockResolvedValue([
{ content: "去公园" },
] as never);
const req = createRequest("/api/blindbox/suggest?roomId=bb-room-1&userId=user-1");
const res = await GET(req, mockCtx);
const { data } = await parseJsonResponse(res);
expect(data.suggestions).toHaveLength(0);
expect(data.source).toBe("none");
});
it("returns 400 when roomId missing", async () => {
const req = createRequest("/api/blindbox/suggest?userId=user-1");
const res = await GET(req, mockCtx);
expect(res.status).toBe(400);
});
});