def test_audio_block_conversion(monkeypatch):
    """Test converting a ChatMessage with AudioBlock to OpenAI format."""
    # Mock binary audio data
    mock_audio_data = b"fake_audio_data"
    mock_audio_file = io.BytesIO(mock_audio_data)

    # Create a mock for resolve_audio that returns base64 encoded data
    encoded_data = base64.b64encode(mock_audio_data)
    mock_file = MagicMock()
    mock_file.read.return_value = encoded_data

    # Set up the monkeypatch to replace the resolve_audio method
    def mock_resolve_audio(*args, **kwargs):
        return mock_file

    monkeypatch.setattr(AudioBlock, "resolve_audio", mock_resolve_audio)

    # Create AudioBlock
    audio_block = AudioBlock(audio_data=mock_audio_file, format="mp3")
    message = ChatMessage(role=MessageRole.USER, content=[audio_block])

    # Convert to OpenAI format
    openai_message = to_openailike_message_dict(message)

    # Verify correct format for audio block
    assert openai_message["role"] == "user"
    assert isinstance(openai_message["content"], list)
    assert len(openai_message["content"]) == 1

    audio_content = openai_message["content"][0]
    assert audio_content["type"] == "input_audio"
    assert "data" in audio_content["input_audio"]
    assert audio_content["input_audio"]["format"] == "mp3"
Test converting a ChatMessage with AudioBlock to OpenAI format.
llama-index-integrations/llms/llama-index-llms-litellm/tests/test_utils.py