test_streaming_utils.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from agency_swarm.streaming.utils import add_agent_name_to_event
  2. def test_add_agent_name_to_event_dict_basic():
  3. evt = {"event": "text"}
  4. out = add_agent_name_to_event(evt, "AgentA", None)
  5. assert out["agent"] == "AgentA"
  6. assert out["callerAgent"] is None
  7. assert "agent_run_id" not in out and "parent_run_id" not in out
  8. def test_add_agent_name_to_event_dict_structured_with_type_and_run_ids():
  9. evt = {"type": "raw_response_event"}
  10. out = add_agent_name_to_event(evt, "AgentA", "CallerX", agent_run_id="ARID", parent_run_id="PRID")
  11. assert out["agent"] == "AgentA"
  12. assert out["callerAgent"] == "CallerX"
  13. assert out["agent_run_id"] == "ARID"
  14. assert out["parent_run_id"] == "PRID"
  15. def test_add_agent_name_to_event_object_basic_attrs():
  16. # Create a simple object without pre-existing attributes
  17. class SimpleEvent:
  18. pass
  19. evt = SimpleEvent()
  20. # no type attr: run ids should not be attached
  21. out = add_agent_name_to_event(evt, "AgentB", None)
  22. assert out.agent == "AgentB"
  23. assert out.callerAgent is None
  24. assert not hasattr(out, "agent_run_id") and not hasattr(out, "parent_run_id")
  25. def test_add_agent_name_to_event_object_structured_with_type_and_run_ids():
  26. # Create a simple object with type attribute
  27. class StructuredEvent:
  28. def __init__(self):
  29. self.type = "run_item_stream_event"
  30. evt = StructuredEvent()
  31. out = add_agent_name_to_event(evt, "AgentB", "CallerY", agent_run_id="AR2", parent_run_id="PR2")
  32. assert out.agent == "AgentB"
  33. assert out.callerAgent == "CallerY"
  34. assert out.agent_run_id == "AR2"
  35. assert out.parent_run_id == "PR2"
  36. # Note: call_id/item_id extraction is an internal detail and not part of public/docs usage.
  37. # We intentionally do not test these paths here to keep tests aligned with documented behavior.
  38. def test_add_agent_name_to_event_dict_non_destructive_preserves_existing():
  39. """Test that add_agent_name_to_event preserves existing agent/callerAgent values (non-destructive)."""
  40. evt = {"agent": "ExistingAgent", "event": "text"}
  41. out = add_agent_name_to_event(evt, "NewAgent", "NewCaller")
  42. # Existing agent should be preserved
  43. assert out["agent"] == "ExistingAgent"
  44. # callerAgent should be added since it wasn't present
  45. assert out["callerAgent"] == "NewCaller"
  46. def test_add_agent_name_to_event_dict_fills_missing_fields():
  47. """Test that add_agent_name_to_event fills missing agent/callerAgent fields."""
  48. evt = {"event": "text"}
  49. out = add_agent_name_to_event(evt, "AgentX", "CallerY")
  50. assert out["agent"] == "AgentX"
  51. assert out["callerAgent"] == "CallerY"
  52. def test_add_agent_name_to_event_object_non_destructive_preserves_existing():
  53. """Test that add_agent_name_to_event preserves existing attributes on objects."""
  54. class EventWithAgent:
  55. def __init__(self):
  56. self.agent = "ExistingAgent"
  57. evt = EventWithAgent()
  58. out = add_agent_name_to_event(evt, "NewAgent", "NewCaller")
  59. # Existing agent should be preserved
  60. assert out.agent == "ExistingAgent"
  61. # callerAgent should be set since it wasn't present
  62. assert out.callerAgent == "NewCaller"
  63. def test_add_agent_name_to_event_object_fills_missing_attributes():
  64. """Test that add_agent_name_to_event adds missing attributes on objects."""
  65. class EmptyEvent:
  66. pass
  67. evt = EmptyEvent()
  68. out = add_agent_name_to_event(evt, "AgentZ", None)
  69. assert out.agent == "AgentZ"
  70. assert out.callerAgent is None
  71. def test_add_agent_name_to_event_preserves_run_ids():
  72. """Test that add_agent_name_to_event preserves existing run_id fields on structured events."""
  73. evt = {"type": "run_item_stream_event", "agent_run_id": "ExistingRunId", "event": "text"}
  74. out = add_agent_name_to_event(evt, "AgentA", "CallerB", agent_run_id="NewRunId", parent_run_id="NewParentId")
  75. # Existing run_id should be preserved
  76. assert out["agent_run_id"] == "ExistingRunId"
  77. # parent_run_id should be added since it wasn't present
  78. assert out["parent_run_id"] == "NewParentId"