package com.ruoyi.web.modules.role.support;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.account.support.MerchantEmployeeSupport;
import com.ruoyi.web.modules.role.constant.MerchantRoleConstants;
import com.ruoyi.web.modules.role.mapper.MerchantRoleMapper;

@ExtendWith(MockitoExtension.class)
class MerchantRoleSupportTest
{
    @Mock
    private MerchantEmployeeSupport merchantEmployeeSupport;

    @Mock
    private MerchantRoleMapper merchantRoleMapper;

    @InjectMocks
    private MerchantRoleSupport support;

    @Test
    void assertRoleNameUnique_rejectsDuplicate()
    {
        when(merchantRoleMapper.countRoleNameInShop(101L, "店员", null)).thenReturn(1);

        ServiceException ex = assertThrows(ServiceException.class,
                () -> support.assertRoleNameUnique(101L, "店员", null));

        assertEquals(MerchantRoleConstants.MSG_ROLE_NAME_EXISTS, ex.getMessage());
    }

    @Test
    void assertMenuIdsAssignable_rejectsEmpty()
    {
        ServiceException ex = assertThrows(ServiceException.class,
                () -> support.assertMenuIdsAssignable(new Long[0]));

        assertEquals(MerchantRoleConstants.MSG_MENU_REQUIRED, ex.getMessage());
    }

    @Test
    void assertDeletable_rejectsBoundRole()
    {
        when(merchantRoleMapper.countEmployeeBindByRole(101L, 401L)).thenReturn(2);

        ServiceException ex = assertThrows(ServiceException.class,
                () -> support.assertDeletable(101L, 401L));

        assertEquals(MerchantRoleConstants.MSG_ROLE_BOUND, ex.getMessage());
    }

    @Test
    void generateRoleKey_containsShopId()
    {
        String roleKey = support.generateRoleKey(101L);

        org.junit.jupiter.api.Assertions.assertTrue(roleKey.startsWith("seller_s101_"));
    }
}
