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:
@@ -3,6 +3,10 @@ import { prismaMock, resetPrismaMock } from "@/__tests__/helpers/prisma-mock";
|
||||
import { createRequest, createRouteContext, parseJsonResponse } from "@/__tests__/helpers/api-test-utils";
|
||||
import { TEST_BLINDBOX_ROOM, TEST_USER } from "@/__tests__/helpers/fixtures";
|
||||
|
||||
vi.mock("@/lib/auth", () => ({
|
||||
getAuthUserId: vi.fn().mockResolvedValue("user-1"),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/blindbox", () => ({
|
||||
getRoomByCode: vi.fn(),
|
||||
requireMembership: vi.fn().mockResolvedValue({}),
|
||||
@@ -10,12 +14,14 @@ vi.mock("@/lib/blindbox", () => ({
|
||||
|
||||
import { GET, PATCH, DELETE } from "./route";
|
||||
import { getRoomByCode } from "@/lib/blindbox";
|
||||
import { getAuthUserId } from "@/lib/auth";
|
||||
|
||||
const mockGetRoomByCode = vi.mocked(getRoomByCode);
|
||||
|
||||
beforeEach(() => {
|
||||
resetPrismaMock();
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(getAuthUserId).mockResolvedValue("user-1");
|
||||
});
|
||||
|
||||
describe("GET /api/blindbox/room/[code]", () => {
|
||||
@@ -61,7 +67,7 @@ describe("PATCH /api/blindbox/room/[code]", () => {
|
||||
|
||||
const req = createRequest("/api/blindbox/room/ABC123", {
|
||||
method: "PATCH",
|
||||
body: { userId: "user-1", city: "上海", lat: 31.2, lng: 121.4 },
|
||||
body: { city: "上海", lat: 31.2, lng: 121.4 },
|
||||
});
|
||||
const ctx = createRouteContext({ code: "ABC123" });
|
||||
const res = await PATCH(req, ctx);
|
||||
@@ -76,7 +82,7 @@ describe("PATCH /api/blindbox/room/[code]", () => {
|
||||
|
||||
const req = createRequest("/api/blindbox/room/ABC123", {
|
||||
method: "PATCH",
|
||||
body: { userId: "user-1", lat: 999, lng: 121.4 },
|
||||
body: { lat: 999, lng: 121.4 },
|
||||
});
|
||||
const ctx = createRouteContext({ code: "ABC123" });
|
||||
const res = await PATCH(req, ctx);
|
||||
@@ -86,13 +92,11 @@ describe("PATCH /api/blindbox/room/[code]", () => {
|
||||
|
||||
describe("DELETE /api/blindbox/room/[code]", () => {
|
||||
it("deletes room when creator", async () => {
|
||||
// user-1 is creator (TEST_BLINDBOX_ROOM.creatorId = "user-1")
|
||||
prismaMock.blindBoxRoom.findUnique.mockResolvedValue(TEST_BLINDBOX_ROOM as never);
|
||||
prismaMock.blindBoxRoom.delete.mockResolvedValue({} as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/room/ABC123", {
|
||||
method: "DELETE",
|
||||
body: { userId: "user-1" },
|
||||
});
|
||||
const req = createRequest("/api/blindbox/room/ABC123", { method: "DELETE", body: {} });
|
||||
const ctx = createRouteContext({ code: "ABC123" });
|
||||
const res = await DELETE(req, ctx);
|
||||
const { data } = await parseJsonResponse(res);
|
||||
@@ -101,14 +105,13 @@ describe("DELETE /api/blindbox/room/[code]", () => {
|
||||
});
|
||||
|
||||
it("leaves room when not creator", async () => {
|
||||
// Authenticate as user-2 (not creator)
|
||||
vi.mocked(getAuthUserId).mockResolvedValueOnce("user-2");
|
||||
prismaMock.blindBoxRoom.findUnique.mockResolvedValue(TEST_BLINDBOX_ROOM as never);
|
||||
prismaMock.blindBoxMember.findUnique.mockResolvedValue({ id: "member-2" } as never);
|
||||
prismaMock.blindBoxMember.delete.mockResolvedValue({} as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/room/ABC123", {
|
||||
method: "DELETE",
|
||||
body: { userId: "user-2" },
|
||||
});
|
||||
const req = createRequest("/api/blindbox/room/ABC123", { method: "DELETE", body: {} });
|
||||
const ctx = createRouteContext({ code: "ABC123" });
|
||||
const res = await DELETE(req, ctx);
|
||||
const { data } = await parseJsonResponse(res);
|
||||
@@ -117,13 +120,12 @@ describe("DELETE /api/blindbox/room/[code]", () => {
|
||||
});
|
||||
|
||||
it("returns 403 when not a member and not creator", async () => {
|
||||
// Authenticate as stranger (not creator, not member)
|
||||
vi.mocked(getAuthUserId).mockResolvedValueOnce("stranger");
|
||||
prismaMock.blindBoxRoom.findUnique.mockResolvedValue(TEST_BLINDBOX_ROOM as never);
|
||||
prismaMock.blindBoxMember.findUnique.mockResolvedValue(null as never);
|
||||
|
||||
const req = createRequest("/api/blindbox/room/ABC123", {
|
||||
method: "DELETE",
|
||||
body: { userId: "stranger" },
|
||||
});
|
||||
const req = createRequest("/api/blindbox/room/ABC123", { method: "DELETE", body: {} });
|
||||
const ctx = createRouteContext({ code: "ABC123" });
|
||||
const res = await DELETE(req, ctx);
|
||||
expect(res.status).toBe(403);
|
||||
|
||||
Reference in New Issue
Block a user