43 lines
1.6 KiB
Python
43 lines
1.6 KiB
Python
"""
|
|
backend/tests/test_hot_faq_aggregator.py
|
|
|
|
测试热点 FAQ 离线聚合的核心规则:问句归一化、代表答案选取。
|
|
"""
|
|
from datetime import datetime
|
|
|
|
from app.services.faq_text_utils import normalize_faq_query
|
|
from app.services.hot_faq_aggregator import HotFaqAggregator, _LogRow
|
|
|
|
|
|
def test_normalize_faq_query_strips_punctuation_and_space():
|
|
"""归一化后「公司几点上班?」与「公司 几点上班」应相同。"""
|
|
assert normalize_faq_query('公司几点上班?') == normalize_faq_query('公司 几点上班')
|
|
|
|
|
|
def test_build_candidate_accepts_divergent_answers_when_clustering_disabled(app, monkeypatch):
|
|
"""答案聚类关闭时,即使答案分歧较大也应生成热点候选。"""
|
|
monkeypatch.setenv('FAQ_HOT_ANSWER_DOMINANCE', '0.6')
|
|
with app.app_context():
|
|
now = datetime.utcnow()
|
|
rows = [
|
|
_LogRow(i, i, 1, '上班时间', f'answer variant {i % 3}', 200, 100, now)
|
|
for i in range(6)
|
|
]
|
|
candidate, reason = HotFaqAggregator._build_candidate(1, 'Agent', '上班时间', rows)
|
|
assert candidate is not None
|
|
assert reason is None
|
|
assert candidate.ask_count_7d == 6
|
|
|
|
|
|
def test_pick_representative_prefers_good_feedback():
|
|
"""有好评时优先选高分记录。"""
|
|
now = datetime.utcnow()
|
|
rows = [
|
|
_LogRow(1, 1, 1, 'q', 'answer A', 100, 10, now, feedback_score=3),
|
|
_LogRow(2, 2, 1, 'q', 'answer B', 100, 10, now, feedback_score=5),
|
|
]
|
|
picked = HotFaqAggregator._pick_representative_answer(rows)
|
|
assert picked is not None
|
|
assert picked.chat_log_id == 2
|
|
assert picked.answer == 'answer B'
|