test_context_ranker.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from data_modules.config import DataModulesConfig
  4. from data_modules.context_ranker import ContextRanker
  5. def test_rank_recent_summaries_prefers_recency_and_hook(tmp_path):
  6. cfg = DataModulesConfig.from_project_root(tmp_path)
  7. ranker = ContextRanker(cfg)
  8. items = [
  9. {"chapter": 8, "summary": "平稳推进"},
  10. {"chapter": 9, "summary": "最后留下悬念?"},
  11. {"chapter": 7, "summary": "老信息"},
  12. ]
  13. ranked = ranker.rank_recent_summaries(items, current_chapter=10)
  14. assert ranked[0]["chapter"] == 9
  15. assert ranked[-1]["chapter"] == 7
  16. def test_rank_appearances_uses_recency_and_frequency(tmp_path):
  17. cfg = DataModulesConfig.from_project_root(tmp_path)
  18. ranker = ContextRanker(cfg)
  19. items = [
  20. {"entity_id": "a", "last_chapter": 9, "total": 1},
  21. {"entity_id": "b", "last_chapter": 8, "total": 8},
  22. {"entity_id": "c", "last_chapter": 9, "total": 3},
  23. ]
  24. ranked = ranker.rank_appearances(items, current_chapter=10)
  25. ids = [item["entity_id"] for item in ranked]
  26. assert ids[0] == "c"
  27. assert ids[-1] in {"a", "b"}
  28. def test_rank_pack_adds_context_contract_meta(tmp_path):
  29. cfg = DataModulesConfig.from_project_root(tmp_path)
  30. ranker = ContextRanker(cfg)
  31. pack = {
  32. "meta": {"chapter": 12},
  33. "core": {"recent_summaries": [{"chapter": 11, "summary": "x"}], "recent_meta": []},
  34. "scene": {"appearing_characters": []},
  35. "global": {},
  36. "story_skeleton": [],
  37. "alerts": {"disambiguation_warnings": [], "disambiguation_pending": []},
  38. }
  39. ranked = ranker.rank_pack(pack, chapter=12)
  40. assert ranked["meta"]["context_contract_version"] == "v2"
  41. assert ranked["meta"]["ranker"]["enabled"] is True