import pytest
from unittest.mock import MagicMock, patch
import ai_client

def test_ai_client_send_gemini_cli():
    """
    Verifies that 'ai_client.send' correctly interacts with 'GeminiCliAdapter'
    when the 'gemini_cli' provider is specified.
    """
    test_message = "Hello, this is a test prompt for the CLI adapter."
    test_response = "This is a dummy response from the Gemini CLI."

    # Set provider to gemini_cli
    ai_client.set_provider("gemini_cli", "gemini-2.0-flash")

    # 1. Mock 'ai_client.GeminiCliAdapter' (which we will add)
    with patch('ai_client.GeminiCliAdapter') as MockAdapterClass:
        mock_adapter_instance = MockAdapterClass.return_value
        mock_adapter_instance.send.return_value = {"text": test_response, "tool_calls": []}
        mock_adapter_instance.last_usage = {"total_tokens": 100}
        mock_adapter_instance.last_latency = 0.5
        mock_adapter_instance.session_id = "test-session"

        # Verify that 'events' are emitted correctly
        with patch.object(ai_client.events, 'emit') as mock_emit:
            response = ai_client.send(
                md_content="<context></context>",
                user_message=test_message,
                base_dir="."
            )

            # Check that the adapter's send method was called.
            mock_adapter_instance.send.assert_called()

            # Verify that the expected lifecycle events were emitted.
            emitted_event_names = [call.args[0] for call in mock_emit.call_args_list]
            assert 'request_start' in emitted_event_names
            assert 'response_received' in emitted_event_names

            # Verify that the combined text returned by the adapter is returned by 'ai_client.send'.
            assert response == test_response
