import { render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { PERMISSIONS } from "@/app/permissions";
import { DashboardWorkspaces } from "./DashboardWorkspaces.jsx";

const can = vi.fn();

vi.mock("@/app/auth/AuthProvider.jsx", () => ({
    useAuth: () => ({ can }),
}));

describe("DashboardWorkspaces", () => {
    beforeEach(() => {
        can.mockReset();
    });

    test("shows each workspace only when its existing permission is granted", () => {
        can.mockImplementation((permission) => permission === PERMISSIONS.financeView);

        render(<DashboardWorkspaces />);

        expect(screen.getByRole("link", { name: /财务工作台/ })).toHaveAttribute("href", "/finance/");
        expect(screen.queryByRole("link", { name: /运营工作台/ })).not.toBeInTheDocument();
    });

    test("routes the operations workspace to the Social BI document entry", () => {
        can.mockImplementation((permission) => permission === PERMISSIONS.overviewView);

        render(<DashboardWorkspaces />);

        expect(screen.getByRole("link", { name: /运营工作台/ })).toHaveAttribute("href", "/databi/social/");
        expect(screen.queryByRole("link", { name: /财务工作台/ })).not.toBeInTheDocument();
    });

    test("shows the finance document entry to withdrawal operations reviewers", () => {
        can.mockImplementation((permission) => permission === PERMISSIONS.operationsWithdrawalAudit);

        render(<DashboardWorkspaces />);

        expect(screen.getByRole("link", { name: /财务工作台/ })).toHaveAttribute("href", "/finance/");
    });

    test.each([PERMISSIONS.financeWithdrawalView, PERMISSIONS.financeWithdrawalAudit])(
        "shows the finance document entry to finance withdrawal roles with %s",
        (grantedPermission) => {
            can.mockImplementation((permission) => permission === grantedPermission);

            render(<DashboardWorkspaces />);

            expect(screen.getByRole("link", { name: /财务工作台/ })).toHaveAttribute("href", "/finance/");
        },
    );
});
