# test_seo_optimizer.py
import pytest

# 导入实际实现的 SEOOptimizer
from app.services.content.seo_optimizer import SEOOptimizer

def test_get_keyword_density():
    """关键词密度计算"""
    optimizer = SEOOptimizer()
    content = "AI医疗AI医疗AI医疗"  # 5个字，AI医疗出现3次
    density = optimizer.get_keyword_density(content, "AI医疗")
    # 密度计算：(3 * 4) / 15 ≈ 0.8 (约80%)
    assert density > 0

def test_adjust_keyword_density():
    """密度调整到推荐范围"""
    optimizer = SEOOptimizer()
    result = optimizer.optimize(
        content="AI医疗是未来发展趋势。随着人工智能技术的不断进步，医疗领域正在经历智能化变革。智能诊断系统能够分析海量医学数据，为医生提供辅助决策支持，提高诊疗效率和准确性，改善患者就医体验，推动医疗资源的优化配置和行业升级，促进整个医疗生态的可持续发展，提升医疗服务质量与管理水平。",
        title="AI医疗",
        platform="zhihu",  # 推荐密度 1-3%
        keyword="AI医疗"
    )
    # 优化后密度应在推荐范围内
    assert result.density >= 1.0
    assert result.density <= 3.0

def test_optimize_keyword_position():
    """关键词位置优化"""
    optimizer = SEOOptimizer()
    result = optimizer.optimize(
        content="这是一篇关于人工智能医疗的文章",
        title="文章标题",
        platform="zhihu",
        keyword="AI医疗"
    )
    # 应建议在标题中添加关键词
    assert result.suggestions is not None
    assert len(result.suggestions) > 0

def test_optimize_multiple_keywords():
    """多关键词处理"""
    optimizer = SEOOptimizer()
    result = optimizer.optimize(
        content="人工智能和机器学习是热门技术",
        title="技术文章",
        platform="zhihu",
        keyword="人工智能"
    )
    assert result.optimized_content is not None

def test_seo_tips_generation():
    """SEO建议生成"""
    optimizer = SEOOptimizer()
    result = optimizer.optimize(
        content="内容",
        title="标题",
        platform="zhihu"
    )
    assert result.tips is not None
    assert len(result.tips) > 0
