test_gate2_query_50plus.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """M9E:Gate 2 query 级 50+ 判定——yes 保留/no 丢弃/拿不准从宽;judge 解析;游走标签复用。"""
  2. import pytest
  3. from content_agent.business_modules.search_intent import _gate2_keep
  4. from content_agent.integrations import query_variant
  5. from content_agent.integrations.query_variant import OpenRouterQueryVariantClient
  6. class _Judge:
  7. def __init__(self, verdict):
  8. self.verdict = verdict
  9. self.calls: list[str] = []
  10. def judge_query_fifty_plus(self, query_text):
  11. self.calls.append(query_text)
  12. return self.verdict
  13. class _RaisingJudge:
  14. def judge_query_fifty_plus(self, query_text):
  15. raise RuntimeError("llm down")
  16. class _NoJudge:
  17. pass
  18. def test_gate2_keeps_yes_and_drops_no():
  19. assert _gate2_keep("广场舞", _Judge(True)) is True
  20. assert _gate2_keep("二次元", _Judge(False)) is False
  21. def test_gate2_keeps_on_error_and_missing_method():
  22. assert _gate2_keep("x", _RaisingJudge()) is True # 拿不准从宽放过
  23. assert _gate2_keep("x", _NoJudge()) is True # mock client 无 judge → 放过
  24. def test_judge_parses_no_as_drop_and_else_as_keep(monkeypatch):
  25. client = OpenRouterQueryVariantClient(api_key="k", model="m")
  26. def fake_post(url, headers, json, timeout):
  27. import httpx
  28. answer = {"choices": [{"message": {"content": fake_post.reply}}]}
  29. return httpx.Response(200, json=answer, request=httpx.Request("POST", url))
  30. monkeypatch.setattr(query_variant.httpx, "post", fake_post)
  31. fake_post.reply = "no"
  32. assert client.judge_query_fifty_plus("二次元") is False
  33. fake_post.reply = "yes"
  34. assert client.judge_query_fifty_plus("广场舞") is True
  35. fake_post.reply = "不确定"
  36. assert client.judge_query_fifty_plus("模糊") is True # 非 no 一律放行
  37. def test_judge_keeps_on_http_exception(monkeypatch):
  38. client = OpenRouterQueryVariantClient(api_key="k", model="m")
  39. def boom(*args, **kwargs):
  40. raise RuntimeError("network")
  41. monkeypatch.setattr(query_variant.httpx, "post", boom)
  42. assert client.judge_query_fifty_plus("x") is True