import { describe, it, expect, vi, beforeEach } from "vitest"; // Mock auth client vi.mock("../../lib/auth/client", () => ({ authClient: { useSession: vi.fn(() => ({ data: { session: null, user: null }, isPending: false, })), }, })); vi.mock("next/navigation", () => ({ redirect: vi.fn(), useRouter: vi.fn(() => ({ push: vi.fn(), refresh: vi.fn(), })), })); describe("Platform Dashboard", () => { beforeEach(() => { vi.clearAllMocks(); global.fetch = vi.fn(); }); describe("Authentication", () => { it("should require platform user authentication", () => { // Test that the dashboard checks for platform user role const platformUser = { id: "1", email: "admin@notso.ai", role: "PLATFORM_SUPER_ADMIN", companyId: null, }; const isPlatformRole = (role: string) => ["PLATFORM_SUPER_ADMIN", "PLATFORM_ADMIN", "PLATFORM_SUPPORT"].includes( role ); expect(isPlatformRole(platformUser.role)).toBe(true); expect(platformUser.companyId).toBeNull(); }); it("should not allow regular users", () => { const regularUser = { id: "2", email: "regular@user.com", role: "USER", companyId: "company-123", }; const isPlatformRole = (role: string) => ["PLATFORM_SUPER_ADMIN", "PLATFORM_ADMIN", "PLATFORM_SUPPORT"].includes( role ); expect(isPlatformRole(regularUser.role)).toBe(false); }); }); describe("Dashboard Data Structure", () => { it("should have correct dashboard data structure", () => { const expectedDashboardData = { companies: [ { id: "1", name: "Test Company", status: "ACTIVE", createdAt: "2024-01-01T00:00:00Z", _count: { users: 5 }, }, ], totalCompanies: 1, totalUsers: 5, totalSessions: 100, }; expect(expectedDashboardData).toHaveProperty("companies"); expect(expectedDashboardData).toHaveProperty("totalCompanies"); expect(expectedDashboardData).toHaveProperty("totalUsers"); expect(expectedDashboardData).toHaveProperty("totalSessions"); expect(Array.isArray(expectedDashboardData.companies)).toBe(true); }); it("should support different company statuses", () => { const statuses = ["ACTIVE", "SUSPENDED", "TRIAL"]; statuses.forEach((status) => { const company = { id: "1", name: "Test Company", status, createdAt: new Date().toISOString(), _count: { users: 1 }, }; expect(["ACTIVE", "SUSPENDED", "TRIAL"]).toContain(company.status); }); }); }); describe("Platform Roles", () => { it("should support all platform roles", () => { const roles = [ { role: "PLATFORM_SUPER_ADMIN", canEdit: true }, { role: "PLATFORM_ADMIN", canEdit: true }, { role: "PLATFORM_SUPPORT", canEdit: false }, ]; roles.forEach(({ role, canEdit }) => { const user = { id: "1", email: `${role.toLowerCase()}@notso.ai`, role: role, companyId: null, }; expect(user.role).toBe(role); if (role === "PLATFORM_SUPER_ADMIN" || role === "PLATFORM_ADMIN") { expect(canEdit).toBe(true); } else { expect(canEdit).toBe(false); } }); }); }); describe("API Integration", () => { it("should fetch dashboard data from correct endpoint", async () => { const mockFetch = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ companies: [], totalCompanies: 0, totalUsers: 0, totalSessions: 0, }), }); global.fetch = mockFetch; // Simulate API call await fetch("/api/platform/companies"); expect(mockFetch).toHaveBeenCalledWith("/api/platform/companies"); }); it("should handle API errors", async () => { const mockFetch = vi.fn().mockRejectedValue(new Error("Network error")); global.fetch = mockFetch; try { await fetch("/api/platform/companies"); } catch (error) { expect(error).toBeInstanceOf(Error); expect((error as Error).message).toBe("Network error"); } }); }); });