test_conversation_agent.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """Tests for the compact, data-grounded LLM conversation payload."""
  2. from __future__ import annotations
  3. import json
  4. from pathlib import Path
  5. from src.agents.conversation_agent import ConversationAgent
  6. from src.agents.coordinator import MoneyMirrorCoordinator
  7. from .fakes import FakeRuntime
  8. ROOT = Path(__file__).resolve().parents[1]
  9. def test_conversation_payload_keeps_verified_facts_without_raw_audit_data() -> None:
  10. coordinator = MoneyMirrorCoordinator(":memory:", runtime=FakeRuntime())
  11. try:
  12. report = coordinator.analyze_csv(ROOT / "data" / "sample_01.csv")
  13. payload = ConversationAgent.payload(
  14. report,
  15. [
  16. {"role": "assistant", "content": "开场"},
  17. {"role": "user", "content": "我想控制深夜外卖"},
  18. {"role": "assistant", "content": "请从一个小任务开始"},
  19. ],
  20. )
  21. parsed = json.loads(payload)
  22. facts = parsed["[Verified tool output]"]
  23. assert facts["summary"]["expense"] == 6574
  24. assert facts["patterns"]["late_night"]["count"] >= 3
  25. assert facts["persona"]["primary"]
  26. assert facts["quests"]
  27. assert facts["guided_conversation"][-1]["content"] == "请从一个小任务开始"
  28. # Raw accounting/audit detail remains in the persisted JSON only, not
  29. # in every LLM turn where it can crowd out the facts above.
  30. assert "transactions" not in facts
  31. assert "agent_trace" not in facts
  32. finally:
  33. coordinator.close()
  34. def test_compact_conversation_bounds_history_and_message_length() -> None:
  35. history = [
  36. {"role": "user", "content": f"turn-{index}"}
  37. for index in range(8)
  38. ]
  39. history[-1]["content"] = "x" * 700
  40. compact = ConversationAgent.compact_conversation(history)
  41. assert len(compact) == ConversationAgent.MAX_HISTORY_ITEMS
  42. assert compact[0]["content"] == "turn-2"
  43. assert compact[-1]["content"].endswith("…")
  44. assert len(compact[-1]["content"]) == ConversationAgent.MAX_MESSAGE_CHARS + 1