package net.micode.myapplication;

import com.example.myapplication.service.EnhancedKeywordClassifier;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 * 测试增强关键词分类器的功能
 */
public class EnhancedKeywordClassifierTest {

    @Test
    public void testClassifyProgramming() {
        String text = "这是一个关于Java程序设计的基础教程，包含变量声明、方法定义和类创建的内容。";
        EnhancedKeywordClassifier classifier = new EnhancedKeywordClassifier();
        EnhancedKeywordClassifier.ClassificationResult result = classifier.classifyWithSegmentation(text);
        
        assertNotNull(result);
        assertEquals("程序设计", result.category);
        assertTrue(result.confidence > 0.5);
        assertNotNull(result.chapter);
    }

    @Test
    public void testClassifyDataStructure() {
        String text = "在数据结构中，我们学习数组、链表、栈和队列的原理和实现。";
        EnhancedKeywordClassifier classifier = new EnhancedKeywordClassifier();
        EnhancedKeywordClassifier.ClassificationResult result = classifier.classifyWithSegmentation(text);
        
        assertNotNull(result);
        assertEquals("数据结构", result.category);
        assertTrue(result.confidence > 0.3);
        assertNotNull(result.chapter);
    }

    @Test
    public void testClassifyAdvancedMath() {
        String text = "高等数学中的微积分，包括导数、积分和极限的计算方法。";
        EnhancedKeywordClassifier classifier = new EnhancedKeywordClassifier();
        EnhancedKeywordClassifier.ClassificationResult result = classifier.classifyWithSegmentation(text);
        
        assertNotNull(result);
        assertEquals("高等数学", result.category);
        assertTrue(result.confidence > 0.3);
        assertNotNull(result.chapter);
    }

    @Test
    public void testClassifyAI() {
        String text = "人工智能课程涵盖机器学习算法、深度学习网络和自然语言处理技术。";
        EnhancedKeywordClassifier classifier = new EnhancedKeywordClassifier();
        EnhancedKeywordClassifier.ClassificationResult result = classifier.classifyWithSegmentation(text);
        
        assertNotNull(result);
        assertEquals("人工智能", result.category);
        assertTrue(result.confidence > 0.3);
        assertNotNull(result.chapter);
    }

    @Test
    public void testClassifyIoT() {
        String text = "物联网技术涉及传感器网络、RFID标签和智能设备的互联互通。";
        EnhancedKeywordClassifier classifier = new EnhancedKeywordClassifier();
        EnhancedKeywordClassifier.ClassificationResult result = classifier.classifyWithSegmentation(text);
        
        assertNotNull(result);
        assertEquals("物联网", result.category);
        assertTrue(result.confidence > 0.3);
        assertNotNull(result.chapter);
    }

    @Test
    public void testClassifyEnglish() {
        String text = "大学英语课程包括英语语法、词汇扩展和学术写作技巧。";
        EnhancedKeywordClassifier classifier = new EnhancedKeywordClassifier();
        EnhancedKeywordClassifier.ClassificationResult result = classifier.classifyWithSegmentation(text);
        
        assertNotNull(result);
        assertEquals("大学英语", result.category);
        assertTrue(result.confidence > 0.3);
        assertNotNull(result.chapter);
    }

    @Test
    public void testConfidenceCalculation() {
        String text = "Java程序设计基础教程，包含变量、方法、类和对象等核心概念";
        EnhancedKeywordClassifier classifier = new EnhancedKeywordClassifier();
        EnhancedKeywordClassifier.ClassificationResult result = classifier.classifyWithSegmentation(text);
        
        assertNotNull(result);
        assertTrue("置信度应该在0-1之间", result.confidence >= 0 && result.confidence <= 1);
        assertTrue("高关键词密度应该有较高置信度", result.confidence > 0.6);
    }

    @Test
    public void testChapterAssignment() {
        String text = "数据库系统设计涉及关系模型和SQL查询优化技术";
        EnhancedKeywordClassifier classifier = new EnhancedKeywordClassifier();
        EnhancedKeywordClassifier.ClassificationResult result = classifier.classifyWithSegmentation(text);
        
        assertNotNull(result);
        assertEquals("数据库系统", result.category);
        assertNotNull("章节不应该为空", result.chapter);
        assertTrue("章节描述应该包含关键词", result.chapter.contains("数据库"));
    }

    @Test
    public void testEdgeCaseEmptyText() {
        EnhancedKeywordClassifier classifier = new EnhancedKeywordClassifier();
        EnhancedKeywordClassifier.ClassificationResult result = classifier.classifyWithSegmentation("");
        
        assertNotNull(result);
        assertEquals("默认分类", result.category);
        assertEquals(0.0, result.confidence, 0.01);
    }

    @Test
    public void testEdgeCaseNullText() {
        EnhancedKeywordClassifier classifier = new EnhancedKeywordClassifier();
        EnhancedKeywordClassifier.ClassificationResult result = classifier.classifyWithSegmentation(null);
        
        assertNotNull(result);
        assertEquals("默认分类", result.category);
        assertEquals(0.0, result.confidence, 0.01);
    }
}