File size: 1,169 Bytes
d6282d0
 
 
 
 
 
 
 
 
15ecc00
d6282d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15ecc00
d6282d0
 
 
 
 
 
 
 
15ecc00
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import pytest

from app.core.services.llm_service import GrokLLMService
from app.core.exceptions import LLMProviderRateLimit


class Fake429Resp:
    def __init__(self, text="rate limit", payload=None):
        self.status_code = 429
        self.headers = {}
        self.text = text
        self._payload = payload or {
            "code": "Some resource has been exhausted",
            "error": "out of credits",
        }

    def json(self):
        return self._payload


class FakeAsyncClient:
    def __init__(self, *args, **kwargs):
        self._calls = 0

    async def __aenter__(self):
        return self

    async def __aexit__(self, exc_type, exc, tb):
        return False

    async def post(self, *args, **kwargs):
        self._calls += 1
        return Fake429Resp()


def test_grok_service_raises_rate_limit(monkeypatch):
    monkeypatch.setattr("app.core.services.llm_service.AsyncClient", FakeAsyncClient)

    svc = GrokLLMService()
    svc.api_key = "fake"
    svc.base_url = "https://fake"
    svc.model_name = "grok-test"

    with pytest.raises(LLMProviderRateLimit):
        import asyncio

        asyncio.run(svc.generate("hi there"))