"""SEO优化器测试"""
import pytest
from app.services.content.seo_optimizer import SEOOptimizer, OptimizationResult


class TestSEOOptimizer:
    """SEO优化器测试"""

    def test_get_keyword_density_basic(self):
        """关键词密度基本计算"""
        optimizer = SEOOptimizer()
        content = "华为手机华为手机华为手机"
        density = optimizer.get_keyword_density(content, "华为手机")

        assert density > 0
        assert isinstance(density, float)

    def test_get_keyword_density_zero_content(self):
        """空内容密度计算"""
        optimizer = SEOOptimizer()
        density = optimizer.get_keyword_density("", "华为")

        assert density == 0.0

    def test_get_keyword_density_zero_keyword(self):
        """空关键词密度计算"""
        optimizer = SEOOptimizer()
        density = optimizer.get_keyword_density("内容", "")

        assert density == 0.0

    def test_get_keyword_density_no_match(self):
        """关键词不匹配时密度"""
        optimizer = SEOOptimizer()
        content = "这是一篇关于苹果的文章"
        density = optimizer.get_keyword_density(content, "华为")

        assert density == 0.0

    def test_optimize_with_keyword(self):
        """带关键词的SEO优化"""
        optimizer = SEOOptimizer()
        result = optimizer.optimize(
            content="华为手机是知名的手机品牌，华为手机性能出色。",
            title="华为手机评测",
            platform="zhihu",
            keyword="华为手机"
        )

        assert isinstance(result, OptimizationResult)
        assert isinstance(result.density, float)
        assert isinstance(result.suggestions, list)
        assert isinstance(result.tips, list)

    def test_optimize_without_keyword(self):
        """不带关键词的SEO优化"""
        optimizer = SEOOptimizer()
        result = optimizer.optimize(
            content="这是一篇关于手机评测的文章",
            title="手机评测",
            platform="zhihu"
        )

        assert result.optimized_content is not None
        assert result.density == 0.0

    def test_optimize_density_too_low(self):
        """关键词密度过低建议"""
        optimizer = SEOOptimizer()
        result = optimizer.optimize(
            content="这是一篇简短的内容",
            title="标题",
            platform="zhihu",
            keyword="华为手机"
        )

        # 密度过低应有建议
        if result.density < 1.0:
            assert len(result.suggestions) > 0

    def test_optimize_density_too_high(self):
        """关键词密度过高建议"""
        optimizer = SEOOptimizer()
        result = optimizer.optimize(
            content="华为华为华为华为华为华为华为华为华为华为",
            title="华为",
            platform="zhihu",
            keyword="华为"
        )

        # 密度过高应有建议
        if result.density > 3.0:
            assert any("超过" in s for s in result.suggestions)

    def test_optimize_keyword_in_title(self):
        """关键词在标题中的检查"""
        optimizer = SEOOptimizer()
        result = optimizer.optimize(
            content="手机内容",
            title="华为手机评测",
            platform="zhihu",
            keyword="华为手机"
        )

        # 关键词在标题中应有通过的建议
        assert isinstance(result.suggestions, list)

    def test_optimize_keyword_in_first_paragraph(self):
        """关键词在前100字的检查"""
        optimizer = SEOOptimizer()
        result = optimizer.optimize(
            content="华为手机是本文的主题...",
            title="评测",
            platform="zhihu",
            keyword="华为手机"
        )

        assert isinstance(result.suggestions, list)

    def test_optimize_different_platforms(self):
        """不同平台SEO优化"""
        optimizer = SEOOptimizer()
        platforms = ["zhihu", "wechat", "xiaohongshu", "baijiahao"]

        for platform in platforms:
            result = optimizer.optimize(
                content="测试内容",
                title="测试标题",
                platform=platform,
                keyword="测试"
            )
            assert isinstance(result, OptimizationResult)

    def test_optimization_result_structure(self):
        """优化结果结构完整性"""
        optimizer = SEOOptimizer()
        result = optimizer.optimize(
            content="华为手机是知名品牌",
            title="华为评测",
            platform="zhihu",
            keyword="华为"
        )

        assert isinstance(result, OptimizationResult)
        assert isinstance(result.optimized_content, str)
        assert isinstance(result.density, float)
        assert isinstance(result.suggestions, list)
        assert isinstance(result.tips, list)

    def test_optimize_returns_suggestions(self):
        """优化建议列表返回"""
        optimizer = SEOOptimizer()
        result = optimizer.optimize(
            content="内容过短",
            title="标题",
            platform="zhihu",
            keyword="关键词"
        )

        assert isinstance(result.suggestions, list)

    def test_optimize_returns_tips(self):
        """优化提示列表返回"""
        optimizer = SEOOptimizer()
        result = optimizer.optimize(
            content="测试内容",
            title="标题",
            platform="zhihu"
        )

        assert isinstance(result.tips, list)

    def test_density_calculation_accuracy(self):
        """密度计算准确性"""
        optimizer = SEOOptimizer()
        # "华为" 4个字符，出现2次 = 8 / 总字符数 * 100
        content = "华为手机华为"
        total_chars = len(content)  # 6
        keyword = "华为"
        keyword_count = content.count(keyword)  # 2
        expected_density = (len(keyword) * keyword_count) / total_chars * 100

        actual_density = optimizer.get_keyword_density(content, keyword)

        assert abs(actual_density - round(expected_density, 2)) < 0.1

    def test_optimize_empty_content(self):
        """空内容优化"""
        optimizer = SEOOptimizer()
        result = optimizer.optimize(
            content="",
            title="标题",
            platform="zhihu",
            keyword="关键词"
        )

        assert result.density == 0.0

    def test_optimize_invalid_platform(self):
        """无效平台优化"""
        optimizer = SEOOptimizer()
        result = optimizer.optimize(
            content="测试内容",
            title="标题",
            platform="invalid_platform",
            keyword="关键词"
        )

        # 无效平台应返回空建议和提示
        assert isinstance(result.suggestions, list)
        assert isinstance(result.tips, list)
