package com.ruoyi.web.modules.category.facade;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import java.util.Arrays;
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.web.modules.category.constant.CategoryConstants;
import com.ruoyi.web.modules.category.domain.BizGoodsCategory;
import com.ruoyi.web.modules.category.facade.impl.CategoryFacadeImpl;
import com.ruoyi.web.modules.category.mapper.BizGoodsCategoryMapper;

/**
 * 分类 Facade 单元测试
 */
@ExtendWith(MockitoExtension.class)
class CategoryFacadeImplTest
{
    @Mock
    private BizGoodsCategoryMapper categoryMapper;

    @InjectMocks
    private CategoryFacadeImpl categoryFacade;

    @Test
    void isCategoryVisible_whenParentHidden_false()
    {
        BizGoodsCategory l2 = level2(10L, 1L);
        BizGoodsCategory l1 = level1(1L);
        l1.setShowFlag(CategoryConstants.SHOW_NO);
        when(categoryMapper.selectByIdIgnoreShop(10L)).thenReturn(l2);
        when(categoryMapper.selectByIdIgnoreShop(1L)).thenReturn(l1);

        assertFalse(categoryFacade.isCategoryVisible(10L));
    }

    @Test
    void isCategoryVisible_whenBothVisible_true()
    {
        BizGoodsCategory l2 = level2(10L, 1L);
        BizGoodsCategory l1 = level1(1L);
        when(categoryMapper.selectByIdIgnoreShop(10L)).thenReturn(l2);
        when(categoryMapper.selectByIdIgnoreShop(1L)).thenReturn(l1);

        assertTrue(categoryFacade.isCategoryVisible(10L));
    }

    @Test
    void getCategoryPath_level2()
    {
        BizGoodsCategory l2 = level2(10L, 1L);
        l2.setCategoryName("复合肥");
        BizGoodsCategory l1 = level1(1L);
        l1.setCategoryName("肥料");
        when(categoryMapper.selectByIdIgnoreShop(10L)).thenReturn(l2);
        when(categoryMapper.selectByIdIgnoreShop(1L)).thenReturn(l1);

        assertEquals("肥料 > 复合肥", categoryFacade.getCategoryPath(10L));
    }

    private BizGoodsCategory level1(Long id)
    {
        BizGoodsCategory c = new BizGoodsCategory();
        c.setCategoryId(id);
        c.setCategoryLevel(CategoryConstants.LEVEL_ONE);
        c.setShowFlag(CategoryConstants.SHOW_YES);
        c.setCategoryName("一级");
        return c;
    }

    private BizGoodsCategory level2(Long id, Long parentId)
    {
        BizGoodsCategory c = new BizGoodsCategory();
        c.setCategoryId(id);
        c.setParentId(parentId);
        c.setCategoryLevel(CategoryConstants.LEVEL_TWO);
        c.setShowFlag(CategoryConstants.SHOW_YES);
        c.setCategoryName("二级");
        return c;
    }
}
