diff --git a/dev/integration/tests/test_copilot_client.py b/dev/integration/tests/test_copilot_client.py new file mode 100644 index 00000000..419d0323 --- /dev/null +++ b/dev/integration/tests/test_copilot_client.py @@ -0,0 +1,61 @@ +import pytest + +from aiohttp.web import Request, Response, Application + +from microsoft_agents.activity import Activity + +from microsoft_agents.copilotstudio.client import ( + CopilotClient, + ConnectionSettings, + PowerPlatformEnvironment +) + +from microsoft_agents.testing.integration.core import ( + AiohttpRunner +) + +def mock_mcs_handler(activity: Activity) -> Awaitable[[Request], Response]: + """Creates a mock handler for MCS endpoint returning the given activity.""" + async def handler(request: Request) -> Response: + activity_data = activity.model_dump_json(exclude_unset=True) + return Response( + body=activity_data + ) + return handler + +def mock_mcs_endpoint( + mocker, + activity: Activity, + path: str, + port: int + ) -> AiohttpRunner: + """Mock MCS responses for testing.""" + + PowerPlatformEnvironment.get_copilot_studio_connection_url = mocker.MagicMock( + return_value=f"http://localhost:{port}{path}" + ) + + app = Application() + app.router.add_post(path, mock_mcs_handler(activity)) + + return AiohttpRunner(app, port=port) + + +@pytest.mark.asyncio +async def test_start_conversation_and_ask_question(mocker): + + activity = Activity( + type="message" + ) + + runner = mock_mcs_endpoint(mocker, activity, "/mcs-endpoint", port=8081) + + await with runner: + settings = ConnectionSettings("environment-id", "agent-id") + client = CopilotClient(settings=settings, token="test-token") + + async for conv_activity in client.start_conversation(): + assert conv_activity.type == "message" + + async for question_activity in client.ask_question("Hello!"): + assert question_activity.type == "message" \ No newline at end of file diff --git a/libraries/microsoft-agents-copilotstudio-client/microsoft_agents/copilotstudio/client/connection_settings.py b/libraries/microsoft-agents-copilotstudio-client/microsoft_agents/copilotstudio/client/connection_settings.py index 6b8e6bc7..2d23b794 100644 --- a/libraries/microsoft-agents-copilotstudio-client/microsoft_agents/copilotstudio/client/connection_settings.py +++ b/libraries/microsoft-agents-copilotstudio-client/microsoft_agents/copilotstudio/client/connection_settings.py @@ -18,6 +18,7 @@ def __init__( cloud: Optional[PowerPlatformCloud], copilot_agent_type: Optional[AgentType], custom_power_platform_cloud: Optional[str], + client_session_defaults: Optional[dict] = None, ) -> None: self.environment_id = environment_id self.agent_identifier = agent_identifier @@ -30,3 +31,4 @@ def __init__( self.cloud = cloud or PowerPlatformCloud.PROD self.copilot_agent_type = copilot_agent_type or AgentType.PUBLISHED self.custom_power_platform_cloud = custom_power_platform_cloud + self.client_session_defaults = client_session_defaults or {} diff --git a/libraries/microsoft-agents-copilotstudio-client/microsoft_agents/copilotstudio/client/copilot_client.py b/libraries/microsoft-agents-copilotstudio-client/microsoft_agents/copilotstudio/client/copilot_client.py index 68c26466..2fe1816b 100644 --- a/libraries/microsoft-agents-copilotstudio-client/microsoft_agents/copilotstudio/client/copilot_client.py +++ b/libraries/microsoft-agents-copilotstudio-client/microsoft_agents/copilotstudio/client/copilot_client.py @@ -28,7 +28,9 @@ def __init__( async def post_request( self, url: str, data: dict, headers: dict ) -> AsyncIterable[Activity]: - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession( + **self.settings.client_session_defaults + ) as session: async with session.post(url, json=data, headers=headers) as response: if response.status != 200: # self.logger(f"Error sending request: {response.status}")