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

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;

@DisplayName("LivestockKnowledgeValidation")
class LivestockKnowledgeValidationTest
{

    @Test
    @DisplayName("知识标题长度")
    void knowledgeTitle()
    {
        assertThrows(ServiceException.class, () -> LivestockKnowledgeValidation.validateKnowledgeTitle(""));
        assertThrows(ServiceException.class, () -> LivestockKnowledgeValidation.validateKnowledgeTitle(
                "一二三四五六七八九十一二三四五六七八九十一"));
        assertDoesNotThrow(() -> LivestockKnowledgeValidation.validateKnowledgeTitle("标题"));
    }

    @Test
    @DisplayName("知识简介长度")
    void introduction()
    {
        assertDoesNotThrow(() -> LivestockKnowledgeValidation.validateIntroduction(null));
        String fifty = repeat('字', 50);
        assertDoesNotThrow(() -> LivestockKnowledgeValidation.validateIntroduction(fifty));
        assertThrows(ServiceException.class,
                () -> LivestockKnowledgeValidation.validateIntroduction(fifty + "多"));
    }

    @Test
    @DisplayName("知识主题枚举")
    void knowledgeTopic()
    {
        assertThrows(ServiceException.class, () -> LivestockKnowledgeValidation.validateKnowledgeTopic(0));
        assertThrows(ServiceException.class, () -> LivestockKnowledgeValidation.validateKnowledgeTopic(6));
        assertDoesNotThrow(() -> LivestockKnowledgeValidation.validateKnowledgeTopic(3));
    }

    @Test
    @DisplayName("正文附件")
    void contentAttachment()
    {
        String path = Constants.RESOURCE_PREFIX + "/upload/a.pdf";
        assertDoesNotThrow(() -> LivestockKnowledgeValidation.validateContentAttachment("a.pdf", path));
        assertThrows(ServiceException.class,
                () -> LivestockKnowledgeValidation.validateContentAttachment("a.exe", path));
        assertThrows(ServiceException.class,
                () -> LivestockKnowledgeValidation.validateContentAttachment("a.pdf", "/bad/path"));
    }

    @Test
    @DisplayName("入库状态规则")
    void kbRules()
    {
        assertDoesNotThrow(() -> {
            org.junit.jupiter.api.Assertions.assertTrue(LivestockKnowledgeRules.canEdit(0));
            org.junit.jupiter.api.Assertions.assertFalse(LivestockKnowledgeRules.canEdit(1));
            org.junit.jupiter.api.Assertions.assertTrue(LivestockKnowledgeRules.canSyncKb(0));
            org.junit.jupiter.api.Assertions.assertFalse(LivestockKnowledgeRules.canSyncKb(1));
            org.junit.jupiter.api.Assertions.assertTrue(LivestockKnowledgeRules.canRemoveKb(1));
            org.junit.jupiter.api.Assertions.assertFalse(LivestockKnowledgeRules.canRemoveKb(0));
        });
    }

    @Test
    @DisplayName("正文后缀白名单")
    void contentExtension()
    {
        assertDoesNotThrow(() -> {
            org.junit.jupiter.api.Assertions.assertTrue(
                    LivestockKnowledgeRules.isContentExtensionAllowed("doc.pdf"));
            org.junit.jupiter.api.Assertions.assertTrue(
                    LivestockKnowledgeRules.isContentExtensionAllowed("readme.md"));
            org.junit.jupiter.api.Assertions.assertTrue(
                    LivestockKnowledgeRules.isContentExtensionAllowed("slides.pptx"));
            org.junit.jupiter.api.Assertions.assertFalse(
                    LivestockKnowledgeRules.isContentExtensionAllowed("legacy.ppt"));
            org.junit.jupiter.api.Assertions.assertFalse(
                    LivestockKnowledgeRules.isContentExtensionAllowed("x.exe"));
        });
    }

    private static String repeat(char c, int n)
    {
        StringBuilder sb = new StringBuilder(n);
        for (int i = 0; i < n; i++)
        {
            sb.append(c);
        }
        return sb.toString();
    }
}
