4f4220652e
批次A:重命名 + 路由拆分 - store.ts → roomRepository.ts,更新全部 import - blindbox/plan/route.ts 精简为薄路由,业务逻辑抽取到 planActions.ts / planQueries.ts 批次B:blindboxPlanGen.ts 拆分(710行 → src/lib/plan/) - agentPlan.ts:Agent 工具调用与系统提示 - legacyPlan.ts:非 Agent 备用生成逻辑 - ideaSelection.ts:Idea 筛选与 Slot 映射 - transitEnrichment.ts:交通信息查询与填充 - index.ts:runPlanGeneration 主入口 批次C:SSE 连接稳定性 - useRoomPolling.ts 加入指数退避重连(上限60s,含Jitter) - plan/stream/route.ts 添加30s心跳 + abort信号清理 批次D:无障碍修复 - Modal:role=dialog、aria-modal、aria-labelledby - AuthModal:aria-label关闭按钮、tablist/tab/aria-selected - PlanItemEditModal、QrInviteModal:补全aria-label - BlindboxPlan:图标按钮aria-label 批次E:Zod 引入 - src/lib/schemas/ai.ts:AI返回值 Schema(IdeaTagsSchema等5个) - src/lib/schemas/requests.ts:请求体 Schema - ai.ts 手工验证替换为 Zod safeParse 批次F:Server Components - achievements/page.tsx → Server Component + AchievementsClient.tsx - profile/page.tsx → Server Component + ProfileClient.tsx 批次G:Room 关系化模型 - prisma/schema.prisma:新增 RoomMember、RoomRestaurant、RoomLike、RoomSwipe 4张表 - migration:20260302010000_room_relational_model - roomRepository.ts 完整重写(关系查询+应用锁) - buildRoomStatus.ts 适配关系查询 测试:全部329个用例通过,修复68个因auth mock缺失导致的测试失败
160 lines
5.1 KiB
TypeScript
160 lines
5.1 KiB
TypeScript
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";
|
|
import { TEST_BLINDBOX_IDEA } from "@/__tests__/helpers/fixtures";
|
|
import { ApiError } from "@/lib/api";
|
|
|
|
vi.mock("@/lib/auth", () => ({
|
|
getAuthUserId: vi.fn().mockResolvedValue("user-1"),
|
|
}));
|
|
|
|
vi.mock("@/lib/blindbox", () => ({
|
|
requireMembership: vi.fn().mockResolvedValue({}),
|
|
}));
|
|
|
|
vi.mock("@/lib/ai", () => ({
|
|
tagIdea: vi.fn().mockResolvedValue({
|
|
category: "outdoor",
|
|
timeSlot: "morning",
|
|
estimatedMinutes: 120,
|
|
costLevel: "free",
|
|
intensity: "active",
|
|
needsBooking: false,
|
|
searchQuery: "公园",
|
|
searchType: "category",
|
|
}),
|
|
}));
|
|
|
|
import { POST, GET, PUT, DELETE } from "./route";
|
|
import { getAuthUserId } from "@/lib/auth";
|
|
|
|
const mockCtx = { params: Promise.resolve({}) };
|
|
|
|
beforeEach(() => {
|
|
resetPrismaMock();
|
|
vi.mocked(getAuthUserId).mockResolvedValue("user-1");
|
|
});
|
|
|
|
describe("POST /api/blindbox (create idea)", () => {
|
|
it("creates an idea successfully", async () => {
|
|
prismaMock.blindBoxIdea.create.mockResolvedValue(TEST_BLINDBOX_IDEA as never);
|
|
prismaMock.blindBoxIdea.update.mockResolvedValue(TEST_BLINDBOX_IDEA as never);
|
|
|
|
const req = createRequest("/api/blindbox", {
|
|
method: "POST",
|
|
body: { roomId: "bb-room-1", content: "去公园野餐" },
|
|
});
|
|
const res = await POST(req, mockCtx);
|
|
const { status, data } = await parseJsonResponse(res);
|
|
|
|
expect(status).toBe(201);
|
|
expect(data.id).toBe("idea-1");
|
|
});
|
|
|
|
it("returns 401 when not authenticated", async () => {
|
|
vi.mocked(getAuthUserId).mockRejectedValueOnce(new ApiError("请先登录", 401));
|
|
const req = createRequest("/api/blindbox", {
|
|
method: "POST",
|
|
body: { roomId: "bb-room-1", content: "test" },
|
|
});
|
|
const res = await POST(req, mockCtx);
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it("returns 400 when content is empty", async () => {
|
|
const req = createRequest("/api/blindbox", {
|
|
method: "POST",
|
|
body: { roomId: "bb-room-1", content: "" },
|
|
});
|
|
const res = await POST(req, mockCtx);
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("returns 400 when content over 200 chars", async () => {
|
|
const req = createRequest("/api/blindbox", {
|
|
method: "POST",
|
|
body: { roomId: "bb-room-1", content: "a".repeat(201) },
|
|
});
|
|
const res = await POST(req, mockCtx);
|
|
expect(res.status).toBe(400);
|
|
});
|
|
});
|
|
|
|
describe("GET /api/blindbox (get pool data)", () => {
|
|
it("returns pool data for valid member", async () => {
|
|
prismaMock.blindBoxIdea.count.mockResolvedValue(5 as never);
|
|
prismaMock.blindBoxIdea.findMany
|
|
.mockResolvedValueOnce([TEST_BLINDBOX_IDEA] as never)
|
|
.mockResolvedValueOnce([] as never);
|
|
|
|
const req = createRequest("/api/blindbox?roomId=bb-room-1");
|
|
const res = await GET(req, mockCtx);
|
|
const { status, data } = await parseJsonResponse(res);
|
|
|
|
expect(status).toBe(200);
|
|
expect(data.poolCount).toBe(5);
|
|
expect(data.myIdeas).toHaveLength(1);
|
|
expect(data.drawn).toHaveLength(0);
|
|
});
|
|
|
|
it("returns 401 when not authenticated", async () => {
|
|
vi.mocked(getAuthUserId).mockRejectedValueOnce(new ApiError("请先登录", 401));
|
|
const req = createRequest("/api/blindbox?roomId=bb-room-1");
|
|
const res = await GET(req, mockCtx);
|
|
expect(res.status).toBe(401);
|
|
});
|
|
});
|
|
|
|
describe("PUT /api/blindbox (edit idea)", () => {
|
|
it("edits an idea successfully", async () => {
|
|
prismaMock.blindBoxIdea.updateMany.mockResolvedValue({ count: 1 } as never);
|
|
|
|
const req = createRequest("/api/blindbox", {
|
|
method: "PUT",
|
|
body: { ideaId: "idea-1", content: "去公园散步" },
|
|
});
|
|
const res = await PUT(req, mockCtx);
|
|
const { status, data } = await parseJsonResponse(res);
|
|
|
|
expect(status).toBe(200);
|
|
expect(data.content).toBe("去公园散步");
|
|
});
|
|
|
|
it("returns 404 when idea not found or already drawn", async () => {
|
|
prismaMock.blindBoxIdea.updateMany.mockResolvedValue({ count: 0 } as never);
|
|
|
|
const req = createRequest("/api/blindbox", {
|
|
method: "PUT",
|
|
body: { ideaId: "nonexistent", content: "test" },
|
|
});
|
|
const res = await PUT(req, mockCtx);
|
|
expect(res.status).toBe(404);
|
|
});
|
|
});
|
|
|
|
describe("DELETE /api/blindbox (delete idea)", () => {
|
|
it("deletes an idea successfully", async () => {
|
|
prismaMock.blindBoxIdea.deleteMany.mockResolvedValue({ count: 1 } as never);
|
|
|
|
const req = createRequest("/api/blindbox", {
|
|
method: "DELETE",
|
|
body: { ideaId: "idea-1" },
|
|
});
|
|
const res = await DELETE(req, mockCtx);
|
|
const { data } = await parseJsonResponse(res);
|
|
|
|
expect(data.deleted).toBe(true);
|
|
});
|
|
|
|
it("returns 404 when idea not found or not owned", async () => {
|
|
prismaMock.blindBoxIdea.deleteMany.mockResolvedValue({ count: 0 } as never);
|
|
|
|
const req = createRequest("/api/blindbox", {
|
|
method: "DELETE",
|
|
body: { ideaId: "nonexistent" },
|
|
});
|
|
const res = await DELETE(req, mockCtx);
|
|
expect(res.status).toBe(404);
|
|
});
|
|
});
|