geo/backend/tests/test_rule_validator.py

106 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# test_rule_validator.py
import pytest
from app.services.distribution.platform_rules import PLATFORM_RULES
from app.services.content.rule_validator import RuleValidator, ValidationIssue, ValidationResult, AI_Pattern
def test_validate_title_length_pass():
"""标题长度符合规则时返回passed"""
validator = RuleValidator()
result = validator.validate(
content="这是一篇关于AI医疗的深度分析文章...",
title="AI医疗的发展趋势与未来展望", # 符合知乎10-30要求
platform="zhihu"
)
assert result.is_valid == True
assert any("标题长度合规" in p or "合规" in p for p in result.passed)
def test_validate_title_length_fail():
"""标题长度超出限制时返回issue"""
validator = RuleValidator()
result = validator.validate(
content="内容",
title="这个标题太长了超过了三十个字符的限制了哈哈哈哈哈哈", # 超过微信公众号22字限制
platform="wechat" # 微信公众号限制22字
)
assert result.is_valid == False
assert any("超过" in i.message for i in result.issues if i.severity == "high")
def test_validate_content_length_pass():
"""内容长度符合规则时返回passed"""
validator = RuleValidator()
result = validator.validate(
content="A" * 1500, # 1500字符合知乎500-50000要求
title="测试标题",
platform="zhihu"
)
assert result.score >= 80
def test_validate_content_length_fail():
"""内容超长返回issue"""
validator = RuleValidator()
result = validator.validate(
content="A" * 30000, # 30000字微信公众号限制20000
title="测试标题",
platform="wechat"
)
assert any("超过" in i.message for i in result.issues if i.severity == "high")
def test_detect_ai_patterns_banned_words():
"""检测禁用词"""
validator = RuleValidator()
result = validator.detect_ai_patterns(
content="首先,其次,最后,总而言之,总之,总之",
platform="zhihu"
)
assert len(result) > 0
assert any("首先" in r.pattern or "总之" in r.pattern for r in result)
def test_detect_ai_patterns_banned_structures():
"""检测禁用结构"""
validator = RuleValidator()
result = validator.detect_ai_patterns(
content="第一,观点一。第二,观点二。第三,观点三。",
platform="zhihu"
)
assert len(result) > 0
def test_validate_zhihu_specific_rules():
"""知乎特定规则"""
validator = RuleValidator()
result = validator.validate(
content="这是一个专业回答",
title="专业回答",
platform="zhihu"
)
# 知乎应检查营销用语
assert result.score > 0
def test_validate_wechat_specific_rules():
"""微信公众号特定规则"""
validator = RuleValidator()
result = validator.validate(
content="点击购买,限时优惠",
title="限时优惠",
platform="wechat"
)
# 微信公众号应检测诱导分享
assert any("诱导" in i.message or "营销" in i.message for i in result.issues)
def test_validate_xiaohongshu_rules():
"""小红书特定规则"""
validator = RuleValidator()
result = validator.validate(
content="微信公众号搜索xxx获取更多内容",
title="种草笔记",
platform="xiaohongshu"
)
# 小红书应检测跨平台引流
assert any("引流" in i.message or "平台" in i.message for i in result.issues)
def test_get_optimization_tips():
"""获取优化建议"""
validator = RuleValidator()
tips = validator.get_optimization_tips("zhihu")
assert len(tips) > 0
assert any(isinstance(tip, str) for tip in tips)