refactor(P2/P3): 完成全部7批重构 — 模块化、SSE退避、无障碍、Zod校验、Server组件、Room关系化

批次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缺失导致的测试失败
This commit is contained in:
2026-03-02 20:27:06 +08:00
parent 6bb0e65d4c
commit 4f4220652e
59 changed files with 2369 additions and 1999 deletions
+10 -2
View File
@@ -2,17 +2,25 @@ 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_RESTAURANT } from "@/__tests__/helpers/fixtures";
import { ApiError } from "@/lib/api";
vi.mock("@/lib/auth", () => ({
getAuthUserId: vi.fn().mockResolvedValue("user-1"),
}));
import { GET } from "./route";
import { getAuthUserId } from "@/lib/auth";
const mockCtx = { params: Promise.resolve({}) };
beforeEach(() => {
resetPrismaMock();
vi.mocked(getAuthUserId).mockResolvedValue("user-1");
});
describe("GET /api/user/achievements", () => {
it("returns 401 when no userId", async () => {
it("returns 401 when not authenticated", async () => {
vi.mocked(getAuthUserId).mockRejectedValueOnce(new ApiError("请先登录", 401));
const req = createRequest("/api/user/achievements");
const res = await GET(req, mockCtx);
expect(res.status).toBe(401);
@@ -48,7 +56,7 @@ describe("GET /api/user/achievements", () => {
{ id: "bb-room-1", name: "周末", code: "ABC123" },
] as never);
const req = createRequest("/api/user/achievements?userId=user-1");
const req = createRequest("/api/user/achievements");
const res = await GET(req, mockCtx);
const { status, data } = await parseJsonResponse(res);
+18 -9
View File
@@ -2,17 +2,25 @@ 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_USER, TEST_RESTAURANT } from "@/__tests__/helpers/fixtures";
import { ApiError } from "@/lib/api";
vi.mock("@/lib/auth", () => ({
getAuthUserId: vi.fn().mockResolvedValue("user-1"),
}));
import { GET, POST, DELETE } from "./route";
import { getAuthUserId } from "@/lib/auth";
const mockCtx = { params: Promise.resolve({}) };
beforeEach(() => {
resetPrismaMock();
vi.mocked(getAuthUserId).mockResolvedValue("user-1");
});
describe("GET /api/user/favorite", () => {
it("returns empty array when no userId", async () => {
it("returns empty array when no favorites", async () => {
prismaMock.favorite.findMany.mockResolvedValue([] as never);
const req = createRequest("/api/user/favorite");
const res = await GET(req, mockCtx);
const { data } = await parseJsonResponse(res);
@@ -30,7 +38,7 @@ describe("GET /api/user/favorite", () => {
},
] as never);
const req = createRequest("/api/user/favorite?userId=user-1");
const req = createRequest("/api/user/favorite");
const res = await GET(req, mockCtx);
const { status, data } = await parseJsonResponse(res);
@@ -48,7 +56,7 @@ describe("POST /api/user/favorite", () => {
const req = createRequest("/api/user/favorite", {
method: "POST",
body: { userId: "user-1", restaurant: TEST_RESTAURANT },
body: { restaurant: TEST_RESTAURANT },
});
const res = await POST(req, mockCtx);
const { status, data } = await parseJsonResponse(res);
@@ -70,7 +78,7 @@ describe("POST /api/user/favorite", () => {
const req = createRequest("/api/user/favorite", {
method: "POST",
body: { userId: "user-1", restaurant: TEST_RESTAURANT },
body: { restaurant: TEST_RESTAURANT },
});
const res = await POST(req, mockCtx);
const { data } = await parseJsonResponse(res);
@@ -79,7 +87,8 @@ describe("POST /api/user/favorite", () => {
expect(data.id).toBe("fav-existing");
});
it("returns 401 when no userId", async () => {
it("returns 401 when not authenticated", async () => {
vi.mocked(getAuthUserId).mockRejectedValueOnce(new ApiError("请先登录", 401));
const req = createRequest("/api/user/favorite", {
method: "POST",
body: { restaurant: TEST_RESTAURANT },
@@ -91,7 +100,7 @@ describe("POST /api/user/favorite", () => {
it("returns 400 when no restaurant", async () => {
const req = createRequest("/api/user/favorite", {
method: "POST",
body: { userId: "user-1" },
body: {},
});
const res = await POST(req, mockCtx);
expect(res.status).toBe(400);
@@ -108,7 +117,7 @@ describe("DELETE /api/user/favorite", () => {
const req = createRequest("/api/user/favorite", {
method: "DELETE",
body: { userId: "user-1", favoriteId: "fav-1" },
body: { favoriteId: "fav-1" },
});
const res = await DELETE(req, mockCtx);
const { data } = await parseJsonResponse(res);
@@ -120,7 +129,7 @@ describe("DELETE /api/user/favorite", () => {
const req = createRequest("/api/user/favorite", {
method: "DELETE",
body: { userId: "user-1", favoriteId: "nonexistent" },
body: { favoriteId: "nonexistent" },
});
const res = await DELETE(req, mockCtx);
expect(res.status).toBe(404);
@@ -134,7 +143,7 @@ describe("DELETE /api/user/favorite", () => {
const req = createRequest("/api/user/favorite", {
method: "DELETE",
body: { userId: "user-1", favoriteId: "fav-1" },
body: { favoriteId: "fav-1" },
});
const res = await DELETE(req, mockCtx);
expect(res.status).toBe(404);
+13 -7
View File
@@ -2,17 +2,25 @@ 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_USER, TEST_RESTAURANT } from "@/__tests__/helpers/fixtures";
import { ApiError } from "@/lib/api";
vi.mock("@/lib/auth", () => ({
getAuthUserId: vi.fn().mockResolvedValue("user-1"),
}));
import { GET, POST } from "./route";
import { getAuthUserId } from "@/lib/auth";
const mockCtx = { params: Promise.resolve({}) };
beforeEach(() => {
resetPrismaMock();
vi.mocked(getAuthUserId).mockResolvedValue("user-1");
});
describe("GET /api/user/history", () => {
it("returns empty array when no userId", async () => {
it("returns empty array when no history", async () => {
prismaMock.decision.findMany.mockResolvedValue([] as never);
const req = createRequest("/api/user/history");
const res = await GET(req, mockCtx);
const { data } = await parseJsonResponse(res);
@@ -33,7 +41,7 @@ describe("GET /api/user/history", () => {
},
] as never);
const req = createRequest("/api/user/history?userId=user-1");
const req = createRequest("/api/user/history");
const res = await GET(req, mockCtx);
const { status, data } = await parseJsonResponse(res);
@@ -53,7 +61,6 @@ describe("POST /api/user/history", () => {
const req = createRequest("/api/user/history", {
method: "POST",
body: {
userId: "user-1",
roomId: "room-1",
restaurant: TEST_RESTAURANT,
matchType: "unanimous",
@@ -74,7 +81,6 @@ describe("POST /api/user/history", () => {
const req = createRequest("/api/user/history", {
method: "POST",
body: {
userId: "user-1",
roomId: "room-1",
restaurant: TEST_RESTAURANT,
matchType: "unanimous",
@@ -97,7 +103,6 @@ describe("POST /api/user/history", () => {
const req = createRequest("/api/user/history", {
method: "POST",
body: {
userId: "user-1",
roomId: "room-1",
restaurant: TEST_RESTAURANT,
matchType: "best",
@@ -111,13 +116,14 @@ describe("POST /api/user/history", () => {
it("returns 400 when missing required fields", async () => {
const req = createRequest("/api/user/history", {
method: "POST",
body: { userId: "user-1" },
body: {},
});
const res = await POST(req, mockCtx);
expect(res.status).toBe(400);
});
it("returns 401 when no userId", async () => {
it("returns 401 when not authenticated", async () => {
vi.mocked(getAuthUserId).mockRejectedValueOnce(new ApiError("请先登录", 401));
const req = createRequest("/api/user/history", {
method: "POST",
body: { roomId: "room-1", restaurant: TEST_RESTAURANT, matchType: "best" },
+17 -9
View File
@@ -2,6 +2,11 @@ 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_USER } from "@/__tests__/helpers/fixtures";
import { ApiError } from "@/lib/api";
vi.mock("@/lib/auth", () => ({
getAuthUserId: vi.fn().mockResolvedValue("user-1"),
}));
vi.mock("bcryptjs", () => ({
default: {
@@ -12,12 +17,14 @@ vi.mock("bcryptjs", () => ({
import bcrypt from "bcryptjs";
import { GET, PUT } from "./route";
import { getAuthUserId } from "@/lib/auth";
const mockCtx = { params: Promise.resolve({}) };
beforeEach(() => {
resetPrismaMock();
vi.mocked(bcrypt.compare).mockReset();
vi.mocked(getAuthUserId).mockResolvedValue("user-1");
});
describe("GET /api/user", () => {
@@ -65,7 +72,7 @@ describe("PUT /api/user", () => {
const req = createRequest("/api/user", {
method: "PUT",
body: { userId: "user-1", username: "newname" },
body: { username: "newname" },
});
const res = await PUT(req, mockCtx);
const { status, data } = await parseJsonResponse(res);
@@ -81,7 +88,7 @@ describe("PUT /api/user", () => {
const req = createRequest("/api/user", {
method: "PUT",
body: { userId: "user-1", username: "takenname" },
body: { username: "takenname" },
});
const res = await PUT(req, mockCtx);
expect(res.status).toBe(409);
@@ -94,7 +101,7 @@ describe("PUT /api/user", () => {
const req = createRequest("/api/user", {
method: "PUT",
body: { userId: "user-1", currentPassword: "old", newPassword: "newpass123" },
body: { currentPassword: "old", newPassword: "newpass123" },
});
const res = await PUT(req, mockCtx);
expect(res.status).toBe(200);
@@ -106,7 +113,7 @@ describe("PUT /api/user", () => {
const req = createRequest("/api/user", {
method: "PUT",
body: { userId: "user-1", currentPassword: "wrong", newPassword: "newpass123" },
body: { currentPassword: "wrong", newPassword: "newpass123" },
});
const res = await PUT(req, mockCtx);
expect(res.status).toBe(403);
@@ -117,13 +124,14 @@ describe("PUT /api/user", () => {
const req = createRequest("/api/user", {
method: "PUT",
body: { userId: "user-1", newPassword: "newpass123" },
body: { newPassword: "newpass123" },
});
const res = await PUT(req, mockCtx);
expect(res.status).toBe(400);
});
it("returns 401 when no userId", async () => {
it("returns 401 when not authenticated", async () => {
vi.mocked(getAuthUserId).mockRejectedValueOnce(new ApiError("请先登录", 401));
const req = createRequest("/api/user", {
method: "PUT",
body: { username: "test" },
@@ -138,7 +146,7 @@ describe("PUT /api/user", () => {
const req = createRequest("/api/user", {
method: "PUT",
body: { userId: "user-1", avatar: "🦊" },
body: { avatar: "🦊" },
});
const res = await PUT(req, mockCtx);
const { data } = await parseJsonResponse(res);
@@ -155,7 +163,7 @@ describe("PUT /api/user", () => {
const req = createRequest("/api/user", {
method: "PUT",
body: { userId: "user-1", email: "new@example.com" },
body: { email: "new@example.com" },
});
const res = await PUT(req, mockCtx);
expect(res.status).toBe(200);
@@ -166,7 +174,7 @@ describe("PUT /api/user", () => {
const req = createRequest("/api/user", {
method: "PUT",
body: { userId: "user-1", email: "notanemail" },
body: { email: "notanemail" },
});
const res = await PUT(req, mockCtx);
expect(res.status).toBe(400);