"""敏感词过滤器测试"""
import pytest
from app.services.content.sensitive_filter import (
    SensitiveFilter,
    FoundWord,
    FilterResult
)


class TestSensitiveFilter:
    """敏感词过滤器测试"""

    def test_filter_politics_words(self):
        """政治敏感词过滤"""
        filter_obj = SensitiveFilter()
        result = filter_obj.filter(
            content="这是一个关于台湾问题的分析",
            platform="zhihu"
        )

        assert isinstance(result, FilterResult)
        # 政治敏感词应被替换
        assert "台湾" not in result.filtered_content or "**" in result.filtered_content
        assert len(result.found_words) > 0
        assert result.found_words[0].category == "politics"

    def test_filter_medical_words(self):
        """医疗敏感词过滤"""
        filter_obj = SensitiveFilter()
        result = filter_obj.filter(
            content="这个药品效果很好",
            platform="wechat"
        )

        # 医疗类敏感词应被检测
        assert result.found_words is not None
        assert isinstance(result.found_words, list)

    def test_filter_finance_words(self):
        """金融敏感词过滤"""
        filter_obj = SensitiveFilter()
        result = filter_obj.filter(
            content="年化收益率10%",
            platform="zhihu"
        )

        # 金融敏感词检测
        assert result.found_words is not None
        assert isinstance(result.found_words, list)

    def test_filter_no_sensitive_words(self):
        """无敏感词时返回原内容"""
        filter_obj = SensitiveFilter()
        result = filter_obj.filter(
            content="这是一个正常的产品介绍",
            platform="zhihu"
        )

        assert result.filtered_content == "这是一个正常的产品介绍"
        assert len(result.found_words) == 0
        assert len(result.replacements) == 0

    def test_filter_multiple_categories(self):
        """多分类同时过滤"""
        filter_obj = SensitiveFilter()
        result = filter_obj.filter(
            content="这是内容包含政治和医疗敏感词的内容",
            platform="wechat"
        )

        categories = [w.category for w in result.found_words]
        assert len(set(categories)) >= 1

    def test_add_custom_words(self):
        """添加自定义敏感词"""
        filter_obj = SensitiveFilter()
        filter_obj.add_custom_words("custom", ["自定义敏感词1", "自定义敏感词2"])

        result = filter_obj.filter(
            content="这是一段包含自定义敏感词1的内容",
            platform="zhihu"
        )

        # 自定义敏感词应被替换
        assert "自定义敏感词1" not in result.filtered_content

    def test_filter_result_structure(self):
        """过滤结果结构完整性"""
        filter_obj = SensitiveFilter()
        result = filter_obj.filter(
            content="测试内容",
            platform="zhihu"
        )

        assert isinstance(result, FilterResult)
        assert isinstance(result.filtered_content, str)
        assert isinstance(result.found_words, list)
        assert isinstance(result.replacements, dict)

    def test_found_word_structure(self):
        """发现词汇结构完整性"""
        filter_obj = SensitiveFilter()
        result = filter_obj.filter(
            content="台湾是中华人民共和国不可分割的一部分",
            platform="zhihu"
        )

        if len(result.found_words) > 0:
            word = result.found_words[0]
            assert isinstance(word, FoundWord)
            assert isinstance(word.word, str)
            assert isinstance(word.category, str)
            assert isinstance(word.position, int)
            assert isinstance(word.replacement, str)

    def test_filter_replacement_mapping(self):
        """敏感词替换映射"""
        filter_obj = SensitiveFilter()
        result = filter_obj.filter(
            content="这是一个关于台湾和西藏的内容",
            platform="zhihu"
        )

        # 替换映射应该包含被替换的词
        assert len(result.replacements) > 0
        for original, replacement in result.replacements.items():
            assert len(original) == len(replacement)
            assert replacement == "*" * len(original)

    def test_filter_various_platforms(self):
        """不同平台敏感词过滤"""
        filter_obj = SensitiveFilter()
        platforms = ["zhihu", "wechat", "xiaohongshu", "douyin"]

        for platform in platforms:
            result = filter_obj.filter(
                content="测试内容",
                platform=platform
            )
            assert isinstance(result, FilterResult)
            assert result.filtered_content is not None

    def test_filter_empty_content(self):
        """空内容过滤"""
        filter_obj = SensitiveFilter()
        result = filter_obj.filter(
            content="",
            platform="zhihu"
        )

        assert result.filtered_content == ""
        assert len(result.found_words) == 0

    def test_filter_replacement_char(self):
        """自定义替换字符"""
        filter_obj = SensitiveFilter()
        filter_obj.replacement_char = "#"

        result = filter_obj.filter(
            content="台湾是中华人民共和国的一部分",
            platform="zhihu"
        )

        # 替换字符应该是#
        if len(result.replacements) > 0:
            for original, replacement in result.replacements.items():
                assert replacement == "#" * len(original)

    def test_filter_with_zhihu_platform(self):
        """知乎平台敏感词过滤"""
        filter_obj = SensitiveFilter()
        result = filter_obj.filter(
            content="这是一篇关于华为手机的专业评测",
            platform="zhihu"
        )

        # 正常内容不应有敏感词
        assert result.filtered_content == "这是一篇关于华为手机的专业评测"

    def test_filter_found_words_positions(self):
        """敏感词位置记录"""
        filter_obj = SensitiveFilter()
        result = filter_obj.filter(
            content="台湾位于中国大陆东南沿海",
            platform="zhihu"
        )

        # 检查位置是否正确记录
        if len(result.found_words) > 0:
            for word in result.found_words:
                assert word.position >= 0
                assert result.filtered_content.find(word.replacement) >= 0
