import pytest
import httpx

@pytest.mark.asyncio
async def test_root_endpoint(http_client):
    """
    Tests if the root endpoint is alive and returns the correct status message.
    """
    print("\n--- Running test_root_endpoint ---")
    response = await http_client.get("/")
    assert response.status_code == 200
    assert response.json() == {"status": "AI Model Hub is running!"}
    print("✅ Root endpoint test passed.")

@pytest.mark.asyncio
async def test_create_speech_stream(http_client):
    """
    Tests the /speech endpoint for a successful audio stream response.
    """
    print("\n--- Running test_create_speech_stream ---")
    url = "/speech"
    payload = {"text": "Hello, world!"}

    # The `stream=True` parameter tells httpx to not read the entire response body
    # at once. We'll handle it manually to check for content.
    async with http_client.stream("POST", url, json=payload) as response:
        assert response.status_code == 200, f"Speech stream request failed. Response: {response.text}"
        assert response.headers.get("content-type") == "audio/wav"

        # Check that the response body is not empty by iterating over chunks.
        content_length = 0
        async for chunk in response.aiter_bytes():
            content_length += len(chunk)
            
        assert content_length > 0
    print("✅ TTS stream test passed.")