import json
import logging

import pytest

logger = logging.getLogger(__name__)


@pytest.mark.asyncio
async def test_twilio_voice_twiml_response_text():

    from rasa.core.channels.twilio_voice import TwilioVoiceInput
    from rasa.core.channels.twilio_voice import TwilioVoiceCollectingOutputChannel

    tv = TwilioVoiceInput("woman")

    output_channel = TwilioVoiceCollectingOutputChannel()

    await output_channel.send_text_message(recipient_id="Chuck Norris", text="Test:")
    assert len(output_channel.messages) == 1
    assert output_channel.messages[0]["text"] == "Test:"

    twiml = tv.build_twilio_voice_response(output_channel.messages)
    assert (
        str(twiml)
        == '<?xml version="1.0" encoding="UTF-8"?><Response><Gather action="/webhooks/twilio_voice/webhook" actionOnEmptyResult="true" input="speech" speechTimeout="auto"><Say voice="woman">Test:</Say></Gather></Response>'
    )


@pytest.mark.asyncio
async def test_twilio_voice_twiml_response_buttons():

    from rasa.core.channels.twilio_voice import TwilioVoiceInput
    from rasa.core.channels.twilio_voice import TwilioVoiceCollectingOutputChannel

    tv = TwilioVoiceInput("woman")

    output_channel = TwilioVoiceCollectingOutputChannel()
    await output_channel.send_text_with_buttons(
        recipient_id="Chuck Norris",
        text="Buttons:",
        buttons=[
            {"title": "Yes", "payload": "/affirm"},
            {"title": "No", "payload": "/deny"},
        ],
    )
    assert len(output_channel.messages) == 3
    message_str = " ".join([m["text"] for m in output_channel.messages])
    assert message_str == "Buttons: Yes No"

    twiml = tv.build_twilio_voice_response(output_channel.messages)
    assert (
        str(twiml)
        == '<?xml version="1.0" encoding="UTF-8"?><Response><Say voice="woman">Buttons:</Say><Pause length="1" /><Say voice="woman">Yes</Say><Pause length="1" /><Gather action="/webhooks/twilio_voice/webhook" actionOnEmptyResult="true" input="speech" speechTimeout="auto"><Say voice="woman">No</Say></Gather></Response>'
    )

@@ -36,16 +36,206 @@
 pytest%0A%0A
+from rasa.shared.exceptions import RasaException%0Afrom rasa.core.channels.twilio_voice import TwilioVoiceInput%0Afrom rasa.core.channels.twilio_voice import TwilioVoiceCollectingOutputChannel%0A%0A
 logger =
@@ -265,37 +265,16 @@
 me__)%0A%0A%0A
-@pytest.mark.asyncio%0A
 async de
@@ -325,179 +325,70 @@
     
-from rasa.core.channels.twilio_voice import TwilioVoiceInput%0A    from rasa.core.channels.twilio_voice import TwilioVoiceCollectingOutputChannel%0A%0A    tv = TwilioVoiceInput(
+tv = TwilioVoiceInput(initial_prompt=%22hello%22, assistant_voice=
 %22wom
@@ -976,29 +976,8 @@
 )%0A%0A%0A
-@pytest.mark.asyncio%0A
 asyn
@@ -1035,179 +1035,70 @@
     
-from rasa.core.channels.twilio_voice import TwilioVoiceInput%0A    from rasa.core.channels.twilio_voice import TwilioVoiceCollectingOutputChannel%0A%0A    tv = TwilioVoiceInput(
+tv = TwilioVoiceInput(initial_prompt=%22hello%22, assistant_voice=
 %22wom
@@ -1976,30 +1976,200 @@
 y%3E%3C/Gather%3E%3C/Response%3E'%0A
-
     )%0A
+%0A%0Aasync def test_twilio_invalid_assistant_voice():%0A%0A    with pytest.raises(RasaException):%0A        tv = TwilioVoiceInput(initial_prompt=%22hello%22, assistant_voice=%22alien%22)%0A

Add check to make sure invalid assistant voices are raising an exception.