test_agent_run_id.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from unittest.mock import AsyncMock, patch
  2. import pytest
  3. @pytest.mark.asyncio
  4. @patch("agents.Runner.run", new_callable=AsyncMock)
  5. async def test_runner_input_strips_agent_run_id_non_stream(mock_runner_run, minimal_agent, mock_thread_manager):
  6. class DummyRunResult:
  7. new_items = []
  8. final_output = "done"
  9. captured_input = {}
  10. async def _fake_run(**kwargs):
  11. captured_input["input"] = kwargs.get("input", [])
  12. return DummyRunResult()
  13. mock_runner_run.side_effect = _fake_run
  14. mock_thread_manager.add_messages(
  15. [
  16. {
  17. "role": "user",
  18. "content": "hello",
  19. "agent": "TestAgent",
  20. "callerAgent": None,
  21. "timestamp": 1,
  22. "agent_run_id": "agent_run_PRE",
  23. }
  24. ]
  25. )
  26. await minimal_agent.get_response("Next")
  27. assert "input" in captured_input
  28. assert all("agent_run_id" not in m for m in captured_input["input"])
  29. @pytest.mark.asyncio
  30. @patch("agents.Runner.run_streamed")
  31. async def test_runner_input_strips_agent_run_id_stream(mock_run_streamed, minimal_agent, mock_thread_manager):
  32. mock_thread_manager.add_messages(
  33. [
  34. {
  35. "type": "function_call",
  36. "agent": "TestAgent",
  37. "callerAgent": None,
  38. "name": "send_message",
  39. "arguments": "{}",
  40. "call_id": "call_ABC",
  41. "id": "fc_ABC",
  42. "status": "in_progress",
  43. "timestamp": 1,
  44. "agent_run_id": "agent_run_PRE_STREAM",
  45. }
  46. ]
  47. )
  48. async def dummy_stream():
  49. if False:
  50. yield # pragma: no cover
  51. return
  52. class DummyStreamedResult:
  53. def stream_events(self):
  54. return dummy_stream()
  55. captured = {}
  56. def _run_streamed_side_effect(**kwargs):
  57. captured["input"] = kwargs.get("input", [])
  58. return DummyStreamedResult()
  59. mock_run_streamed.side_effect = _run_streamed_side_effect
  60. async for _ in minimal_agent.get_response_stream("Hello streaming"):
  61. pass
  62. assert "input" in captured
  63. assert all("agent_run_id" not in m for m in captured["input"])