test_memory_writer.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from data_modules.config import DataModulesConfig
  4. from data_modules.memory.store import ScratchpadManager
  5. from data_modules.memory.writer import MemoryWriter
  6. def _cfg(tmp_path):
  7. cfg = DataModulesConfig.from_project_root(tmp_path)
  8. cfg.ensure_dirs()
  9. return cfg
  10. def test_writer_stage2_mapping(tmp_path):
  11. cfg = _cfg(tmp_path)
  12. writer = MemoryWriter(cfg)
  13. result = {
  14. "entities_new": [{"suggested_id": "yaolao", "name": "药老", "type": "角色", "tier": "重要"}],
  15. "state_changes": [{"entity_id": "xiaoyan", "field": "realm", "old": "斗者", "new": "斗师"}],
  16. "relationships_new": [{"from": "xiaoyan", "to": "yaolao", "type": "师徒", "description": "收徒"}],
  17. "chapter_meta": {"hook": {"content": "三年之约将至", "type": "悬念钩", "strength": "strong"}},
  18. }
  19. summary = writer.update_from_chapter_result(12, result)
  20. assert summary["items_added"] >= 4
  21. store = ScratchpadManager(cfg)
  22. chars = store.query(category="character_state", status="active")
  23. assert any(x.subject == "xiaoyan" and x.field == "realm" for x in chars)
  24. rels = store.query(category="relationship", status="active")
  25. assert any(x.subject == "xiaoyan" and x.field == "yaolao" for x in rels)
  26. def test_writer_stage4_memory_facts_mapping(tmp_path):
  27. cfg = _cfg(tmp_path)
  28. writer = MemoryWriter(cfg)
  29. result = {
  30. "memory_facts": {
  31. "timeline_events": [{"event": "萧炎离开天云宗", "chapter": 100, "time_hint": "三年之约前夕"}],
  32. "world_rules": [{"rule": "修炼体系九境", "scope": "global", "domain": "修炼体系", "field": "境界划分"}],
  33. "open_loops": [{"content": "三年之约", "status": "active", "urgency": 80}],
  34. "reader_promises": [{"content": "纳兰嫣然会出场", "type": "encounter", "target": "纳兰嫣然"}],
  35. }
  36. }
  37. summary = writer.update_from_chapter_result(100, result)
  38. assert summary["items_added"] >= 4
  39. store = ScratchpadManager(cfg)
  40. assert store.query(category="timeline", status="active")
  41. assert store.query(category="world_rule", status="active")
  42. assert store.query(category="open_loop", status="active")
  43. assert store.query(category="reader_promise", status="active")