Files
syops/backend/tests/test_faq_cache.py

167 lines
6.2 KiB
Python

"""
backend/tests/test_faq_cache.py
用途:FAQ 缓存在线匹配规则(精确 + 模糊兜底两阶段)。
逻辑:验证 cache_enabled 决定是否直答,is_enabled 只影响首页展示、不阻断命中;
模糊兜底仅在条目开启 fuzzy_enabled 后参与,且得分受逐条目阈值约束。
"""
from app import db
from app.models import Agent, QuickQuestion
from app.services.faq_cache import FaqCacheService
def _create_agent() -> Agent:
"""写入测试 Agent,供 FAQ 条目关联。"""
agent = Agent(name='faq-cache-agent', type='api', dify_app_id='app-faq-cache', dify_api_key='key-faq-cache')
db.session.add(agent)
db.session.commit()
return agent
def _create_faq(agent_id: int, **overrides) -> QuickQuestion:
"""写入一条全局 FAQ,默认启用缓存且有标准答案。"""
payload = {
'user_id': None,
'agent_id': agent_id,
'title': '公司的企业文化',
'question': '公司的企业文化',
'answer': '客户满意、员工乐意、经营得意',
'aliases': ['企业文化'],
'cache_enabled': True,
'is_enabled': True,
}
payload.update(overrides)
faq = QuickQuestion(**payload)
db.session.add(faq)
db.session.commit()
return faq
def test_try_match_hits_when_cache_on_even_if_not_shown_on_home(app):
"""关闭前台展示时,启用缓存的问句仍应精确直答,不走 Dify。"""
with app.app_context():
agent = _create_agent()
faq = _create_faq(agent.id, is_enabled=False)
hit = FaqCacheService.try_match('公司的企业文化', agent.id)
alias_hit = FaqCacheService.try_match('企业文化', agent.id)
assert hit is not None
assert hit.match_type == 'exact'
assert hit.entry.id == faq.id
assert hit.entry.answer == '客户满意、员工乐意、经营得意'
assert alias_hit is not None
assert alias_hit.entry.id == hit.entry.id
def test_try_match_skips_when_cache_disabled(app):
"""仅在首页展示、未启用缓存时,提问仍应回退 Dify。"""
with app.app_context():
agent = _create_agent()
_create_faq(agent.id, cache_enabled=False, is_enabled=True)
assert FaqCacheService.try_match('公司的企业文化', agent.id) is None
def test_fuzzy_fallback_hits_affix_variant_when_enabled(app):
"""精确未命中时,开启模糊的条目应容忍加前缀(我/请问)后兜底直答。"""
with app.app_context():
agent = _create_agent()
faq = _create_faq(
agent.id,
title='还能问什么',
question='还能问什么',
aliases=['还能问你什么'],
fuzzy_enabled=True,
)
hit = FaqCacheService.try_match('我还能问什么', agent.id)
assert hit is not None
assert hit.match_type == 'fuzzy'
assert hit.entry.id == faq.id
assert hit.match_score is not None and hit.match_score >= 0.8
assert hit.matched_text == '还能问什么'
def test_fuzzy_fallback_not_hit_when_toggle_off(app):
"""未开启模糊匹配的条目,加前缀的近似问法不应命中(维持旧行为)。"""
with app.app_context():
agent = _create_agent()
_create_faq(agent.id, title='还能问什么', question='还能问什么', aliases=None)
assert FaqCacheService.try_match('我还能问什么', agent.id) is None
def test_fuzzy_fallback_respects_per_entry_threshold(app):
"""得分低于条目阈值不应命中;阈值调低后同问法可命中。"""
with app.app_context():
agent = _create_agent()
faq = _create_faq(
agent.id,
title='还能问什么',
question='还能问什么',
aliases=None,
fuzzy_enabled=True,
fuzzy_threshold=0.9,
)
# 「我可以问什么」对「还能问什么」的实际得分约 0.55(中段换字对编辑距离不友好),低于 0.6
assert FaqCacheService.try_match('我可以问什么', agent.id) is None
faq.fuzzy_threshold = 0.5
db.session.commit()
hit = FaqCacheService.try_match('我可以问什么', agent.id)
assert hit is not None
assert hit.match_type == 'fuzzy'
assert hit.match_score is not None and hit.match_score >= 0.5
def test_fuzzy_guard_blocks_real_question_containing_alias(app):
"""真问题只包含短候选短语时覆盖率低,不应被误直答(放行走 Dify)。"""
with app.app_context():
agent = _create_agent()
_create_faq(agent.id, title='怎么用你', question='怎么用你', aliases=None, fuzzy_enabled=True)
assert FaqCacheService.try_match('怎么用你来查mes服务器', agent.id) is None
def test_fuzzy_skips_short_queries_and_candidates(app):
"""归一化后不足 4 字的输入不参与模糊兜底,只能精确命中。"""
with app.app_context():
agent = _create_agent()
_create_faq(
agent.id,
title='帮助中心',
question='帮助中心',
aliases=['帮助'],
fuzzy_enabled=True,
fuzzy_threshold=0.5,
)
# 逐字命中同义问法「帮助」:短问句的精确匹配不受护栏影响
exact = FaqCacheService.try_match('帮助', agent.id)
assert exact is not None
assert exact.match_type == 'exact'
# 「帮助台」不是逐字命中;若无护栏会以 0.75 覆盖率误命中,应被长度护栏拦截
assert FaqCacheService.try_match('帮助台', agent.id) is None
# 长度足够的输入包含完整候选时覆盖率高,应兜底命中
hit = FaqCacheService.try_match('帮助中心怎么打开', agent.id)
assert hit is not None
assert hit.match_type == 'fuzzy'
def test_exact_match_wins_over_fuzzy_phase(app):
"""逐字一致的问法必须标记 exact 命中,即使条目同时开启了模糊。"""
with app.app_context():
agent = _create_agent()
faq = _create_faq(agent.id, fuzzy_enabled=True)
hit = FaqCacheService.try_match('企业文化', agent.id)
assert hit is not None
assert hit.match_type == 'exact'
assert hit.entry.id == faq.id