import { beforeEach, describe, expect, it, vi } from "vitest" const mocks = vi.hoisted(() => ({ authMock: vi.fn(), getUserProfileMock: vi.fn(), redirectMock: vi.fn((target: string) => { throw new Error(`REDIRECT:${target}`) }), })) vi.mock("@/auth", () => ({ auth: mocks.authMock, })) vi.mock("@/modules/users/data-access", () => ({ getUserProfile: mocks.getUserProfileMock, })) vi.mock("next/navigation", () => ({ redirect: mocks.redirectMock, })) import DashboardPage from "@/app/(dashboard)/dashboard/page" describe("dashboard route dispatcher", () => { beforeEach(() => { vi.resetAllMocks() mocks.redirectMock.mockImplementation((target: string) => { throw new Error(`REDIRECT:${target}`) }) }) it("redirects to login when session is missing", async () => { mocks.authMock.mockResolvedValue(null) await expect(DashboardPage()).rejects.toThrow("REDIRECT:/login") }) it("redirects to login when user profile is missing", async () => { mocks.authMock.mockResolvedValue({ user: { id: "u_1" } }) mocks.getUserProfileMock.mockResolvedValue(null) await expect(DashboardPage()).rejects.toThrow("REDIRECT:/login") }) it("redirects admin to admin dashboard", async () => { mocks.authMock.mockResolvedValue({ user: { id: "u_admin" } }) mocks.getUserProfileMock.mockResolvedValue({ role: "admin" }) await expect(DashboardPage()).rejects.toThrow("REDIRECT:/admin/dashboard") }) it("redirects student to student dashboard", async () => { mocks.authMock.mockResolvedValue({ user: { id: "u_student" } }) mocks.getUserProfileMock.mockResolvedValue({ role: "student" }) await expect(DashboardPage()).rejects.toThrow("REDIRECT:/student/dashboard") }) it("redirects parent to parent dashboard", async () => { mocks.authMock.mockResolvedValue({ user: { id: "u_parent" } }) mocks.getUserProfileMock.mockResolvedValue({ role: "parent" }) await expect(DashboardPage()).rejects.toThrow("REDIRECT:/parent/dashboard") }) it("falls back to student dashboard when role is unknown", async () => { mocks.authMock.mockResolvedValue({ user: { id: "u_teacher" } }) mocks.getUserProfileMock.mockResolvedValue({ role: "" }) await expect(DashboardPage()).rejects.toThrow("REDIRECT:/student/dashboard") }) })