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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.web.modules.category.domain.BizInformationCategory;
import com.ruoyi.web.modules.category.domain.vo.InformationCategoryTreeVo;

@DisplayName("InformationCategoryTreeBuilder")
class InformationCategoryTreeBuilderTest
{
    @Test
    @DisplayName("按 parent_code 组装两级树")
    void build_twoLevelTree()
    {
        BizInformationCategory root = row("001", "养殖标准", null, null);
        BizInformationCategory child = row("001001", "繁育作业", "001", "1");
        List<InformationCategoryTreeVo> tree = InformationCategoryTreeBuilder.build(Arrays.asList(root, child));
        assertEquals(1, tree.size());
        assertEquals("001", tree.get(0).getCode());
        assertEquals("养殖标准", tree.get(0).getName());
        assertEquals(1, tree.get(0).getChildren().size());
        assertEquals("001001", tree.get(0).getChildren().get(0).getCode());
        assertEquals("1", tree.get(0).getChildren().get(0).getCategoryType());
        assertEquals("01", tree.get(0).getModuleId());
        assertEquals("01", tree.get(0).getChildren().get(0).getModuleId());
    }

    @Test
    @DisplayName("父节点不可见未入选时子节点升为根")
    void build_visibleChildWithoutParentAsRoot()
    {
        BizInformationCategory child = row("001013", "疾病决策", "001", "5");
        List<InformationCategoryTreeVo> tree = InformationCategoryTreeBuilder.build(Arrays.asList(child));
        assertEquals(1, tree.size());
        assertEquals("001013", tree.get(0).getCode());
        assertTrue(tree.get(0).getChildren().isEmpty());
    }

    @Test
    @DisplayName("无上级编码的节点作为根")
    void build_orphanAsRoot()
    {
        BizInformationCategory orphan = row("999", "孤立", "404", null);
        List<InformationCategoryTreeVo> tree = InformationCategoryTreeBuilder.build(Arrays.asList(orphan));
        assertEquals(1, tree.size());
        assertEquals("999", tree.get(0).getCode());
        assertTrue(tree.get(0).getChildren().isEmpty());
    }

    private static BizInformationCategory row(String code, String name, String parent, String type)
    {
        BizInformationCategory row = new BizInformationCategory();
        row.setCategoryCode(code);
        row.setCategoryName(name);
        row.setParentCode(parent);
        row.setCategoryType(type);
        row.setModuleId("01");
        return row;
    }
}
