test_agent_logic.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import pytest
  2. from unittest.mock import patch
  3. from hello_agents import SimpleAgent
  4. from app.agent.agent import ParticipantAgent
  5. from app.agent.memory import SharedMemory
  6. def test_memory_operations():
  7. mem = SharedMemory(n_participants=3)
  8. # Check initial state (it's not empty string, contains headers)
  9. initial_str = mem.get_context_str()
  10. assert "【过往总结】" in initial_str
  11. assert "(暂无)" in initial_str
  12. mem.add_message("Alice", "Hi")
  13. # get_context_str returns "Alice: Hi" in format
  14. assert "Alice: Hi" in mem.get_context_str()
  15. mem.add_message("Bob", "Hello")
  16. mem.add_message("Charlie", "Hey")
  17. def test_agent_initialization():
  18. persona = {
  19. "name": "Socrates",
  20. "bio": "Philosopher",
  21. "title": "Thinker",
  22. "theories": ["Method"],
  23. "stance": "Neutral",
  24. "system_prompt": "Be wise."
  25. }
  26. agent = ParticipantAgent("Socrates", persona, n_participants=3, theme="Truth")
  27. assert isinstance(agent, SimpleAgent)
  28. assert agent.name == "Socrates"
  29. # System prompt is taken from persona['system_prompt'] directly
  30. assert "Be wise." in agent.system_prompt
  31. assert "Truth" in agent.theme
  32. assert "Method" in agent.theories
  33. def test_agent_think_listen():
  34. persona = {"name": "Socrates", "bio": "B", "title": "T", "theories": [], "stance": "S", "system_prompt": "P"}
  35. agent = ParticipantAgent("Socrates", persona, n_participants=3, theme="T")
  36. # Mock response for "listen"
  37. with patch.object(agent, "run", return_value='{"decision":"LISTEN","inner_monologue":"I should listen"}'):
  38. thought = agent.think("Context")
  39. assert thought["action"] == "listen"
  40. def test_agent_think_speak():
  41. persona = {"name": "Socrates", "bio": "B", "title": "T", "theories": [], "stance": "S", "system_prompt": "P"}
  42. agent = ParticipantAgent("Socrates", persona, n_participants=3, theme="T")
  43. # Mock response for "speak"
  44. with patch.object(agent, "run", return_value='{"decision":"APPLY_SPEAK","inner_monologue":"I will speak"}'):
  45. thought = agent.think("Context")
  46. assert thought["action"] == "apply_to_speak"
  47. def test_agent_speak_stream():
  48. persona = {"name": "Socrates", "bio": "B", "title": "T", "theories": [], "stance": "S", "system_prompt": "P"}
  49. agent = ParticipantAgent("Socrates", persona, n_participants=3, theme="T")
  50. thought = {"action": "speak", "thought": "T", "target": "All", "previous": "P", "mind": "M", "benefit": "B"}
  51. with patch.object(agent, "stream_run", return_value=iter(["Hel", "lo"])):
  52. chunks = list(agent.speak(thought, "Context"))
  53. assert chunks == ["Hel", "lo"]
  54. def test_agent_think_error_handling():
  55. persona = {"name": "Socrates", "bio": "B", "title": "T", "theories": [], "stance": "S", "system_prompt": "P"}
  56. agent = ParticipantAgent("Socrates", persona, n_participants=3, theme="T")
  57. with patch.object(agent, "run", return_value=""):
  58. thought = agent.think("Context")
  59. assert thought is None
  60. def test_parse_think_response_chinese_apply():
  61. persona = {"name": "Socrates", "bio": "B", "title": "T", "theories": [], "stance": "S", "system_prompt": "P"}
  62. agent = ParticipantAgent("Socrates", persona, n_participants=3, theme="T")
  63. content = """决策:申请发言
  64. 内心独白:我有新观点要补充
  65. 引用理论:博弈论
  66. 前序观点:上一位观点过于理想化
  67. 预期贡献:提供现实约束条件"""
  68. thought = agent._parse_think_response(content)
  69. assert thought["action"] == "apply_to_speak"