| 1 | import pytest |
| 2 | from swarm import Swarm, Agent |
| 3 | from tests.mock_client import MockOpenAIClient, create_mock_response |
| 4 | from unittest.mock import Mock |
| 5 | import json |
| 6 | |
| 7 | DEFAULT_RESPONSE_CONTENT = "sample response content" |
| 8 | |
| 9 | |
| 10 | @pytest.fixture |
| 11 | def mock_openai_client(): |
| 12 | m = MockOpenAIClient() |
| 13 | m.set_response( |
| 14 | create_mock_response({"role": "assistant", "content": DEFAULT_RESPONSE_CONTENT}) |
| 15 | ) |
| 16 | return m |
| 17 | |
| 18 | |
| 19 | def test_run_with_simple_message(mock_openai_client: MockOpenAIClient): |
| 20 | agent = Agent() |
| 21 | # set up client and run |
| 22 | client = Swarm(client=mock_openai_client) |
| 23 | messages = [{"role": "user", "content": "Hello, how are you?"}] |
| 24 | response = client.run(agent=agent, messages=messages) |
| 25 | |
| 26 | # assert response content |
| 27 | assert response.messages[-1]["role"] == "assistant" |
| 28 | assert response.messages[-1]["content"] == DEFAULT_RESPONSE_CONTENT |
| 29 | |
| 30 | |
| 31 | def test_tool_call(mock_openai_client: MockOpenAIClient): |
| 32 | expected_location = "San Francisco" |
| 33 | |
| 34 | # set up mock to record function calls |
| 35 | get_weather_mock = Mock() |
| 36 | |
| 37 | def get_weather(location): |
| 38 | get_weather_mock(location=location) |
| 39 | return "It's sunny today." |
| 40 | |
| 41 | agent = Agent(name="Test Agent", functions=[get_weather]) |
| 42 | messages = [ |
| 43 | {"role": "user", "content": "What's the weather like in San Francisco?"} |
| 44 | ] |
| 45 | |
| 46 | # set mock to return a response that triggers function call |
| 47 | mock_openai_client.set_sequential_responses( |
| 48 | [ |
| 49 | create_mock_response( |
| 50 | message={"role": "assistant", "content": ""}, |
| 51 | function_calls=[ |
| 52 | {"name": "get_weather", "args": {"location": expected_location}} |
| 53 | ], |
| 54 | ), |
| 55 | create_mock_response( |
| 56 | {"role": "assistant", "content": DEFAULT_RESPONSE_CONTENT} |
| 57 | ), |
| 58 | ] |
| 59 | ) |
| 60 | |
| 61 | # set up client and run |
| 62 | client = Swarm(client=mock_openai_client) |
| 63 | response = client.run(agent=agent, messages=messages) |
| 64 | |
| 65 | get_weather_mock.assert_called_once_with(location=expected_location) |
| 66 | assert response.messages[-1]["role"] == "assistant" |
| 67 | assert response.messages[-1]["content"] == DEFAULT_RESPONSE_CONTENT |
| 68 | |
| 69 | |
| 70 | def test_execute_tools_false(mock_openai_client: MockOpenAIClient): |
| 71 | expected_location = "San Francisco" |
| 72 | |
| 73 | # set up mock to record function calls |
| 74 | get_weather_mock = Mock() |
| 75 | |
| 76 | def get_weather(location): |
| 77 | get_weather_mock(location=location) |
| 78 | return "It's sunny today." |
| 79 | |
| 80 | agent = Agent(name="Test Agent", functions=[get_weather]) |
| 81 | messages = [ |
| 82 | {"role": "user", "content": "What's the weather like in San Francisco?"} |
| 83 | ] |
| 84 | |
| 85 | # set mock to return a response that triggers function call |
| 86 | mock_openai_client.set_sequential_responses( |
| 87 | [ |
| 88 | create_mock_response( |
| 89 | message={"role": "assistant", "content": ""}, |
| 90 | function_calls=[ |
| 91 | {"name": "get_weather", "args": {"location": expected_location}} |
| 92 | ], |
| 93 | ), |
| 94 | create_mock_response( |
| 95 | {"role": "assistant", "content": DEFAULT_RESPONSE_CONTENT} |
| 96 | ), |
| 97 | ] |
| 98 | ) |
| 99 | |
| 100 | # set up client and run |
| 101 | client = Swarm(client=mock_openai_client) |
| 102 | response = client.run(agent=agent, messages=messages, execute_tools=False) |
| 103 | print(response) |
| 104 | |
| 105 | # assert function not called |
| 106 | get_weather_mock.assert_not_called() |
| 107 | |
| 108 | # assert tool call is present in last response |
| 109 | tool_calls = response.messages[-1].get("tool_calls") |
| 110 | assert tool_calls is not None and len(tool_calls) == 1 |
| 111 | tool_call = tool_calls[0] |
| 112 | assert tool_call["function"]["name"] == "get_weather" |
| 113 | assert json.loads(tool_call["function"]["arguments"]) == { |
| 114 | "location": expected_location |
| 115 | } |
| 116 | |
| 117 | |
| 118 | def test_handoff(mock_openai_client: MockOpenAIClient): |
| 119 | def transfer_to_agent2(): |
| 120 | return agent2 |
| 121 | |
| 122 | agent1 = Agent(name="Test Agent 1", functions=[transfer_to_agent2]) |
| 123 | agent2 = Agent(name="Test Agent 2") |
| 124 | |
| 125 | # set mock to return a response that triggers the handoff |
| 126 | mock_openai_client.set_sequential_responses( |
| 127 | [ |
| 128 | create_mock_response( |
| 129 | message={"role": "assistant", "content": ""}, |
| 130 | function_calls=[{"name": "transfer_to_agent2"}], |
| 131 | ), |
| 132 | create_mock_response( |
| 133 | {"role": "assistant", "content": DEFAULT_RESPONSE_CONTENT} |
| 134 | ), |
| 135 | ] |
| 136 | ) |
| 137 | |
| 138 | # set up client and run |
| 139 | client = Swarm(client=mock_openai_client) |
| 140 | messages = [{"role": "user", "content": "I want to talk to agent 2"}] |
| 141 | response = client.run(agent=agent1, messages=messages) |
| 142 | |
| 143 | assert response.agent == agent2 |
| 144 | assert response.messages[-1]["role"] == "assistant" |
| 145 | assert response.messages[-1]["content"] == DEFAULT_RESPONSE_CONTENT |