import pytest
import asyncio
import websockets
import json

@pytest.mark.asyncio
@pytest.mark.skip(reason="Requires running WebSocket server on localhost:8001 - integration test")
async def test_rate_limit_enforcement():
    """Test that rate limiting is enforced on WebSocket"""
    uri = "ws://localhost:8001"

    async with websockets.connect(uri) as ws:
        # Init connection
        await ws.send(json.dumps({"type": "init", "userID": "rate_test_user"}))
        response = await ws.recv()

        # Send messages rapidly
        responses = []
        for i in range(35):  # Try to send 35 messages (limit is 30/min)
            await ws.send(json.dumps({"message": "speed", "type": "speed", "value": 1}))
            response = await ws.recv()
            responses.append(response)

        # Should have at least one rate limit error
        errors = [r for r in responses if "rate limit" in r.lower()]
        assert len(errors) > 0
