flexigo-support-bot / tests /llm /test_cloudflare.py
victor34593993's picture
deploy flexigo support bot
187966e verified
Raw
History Blame Contribute Delete
1.11 kB
from __future__ import annotations
import httpx
import pytest
import respx
from app.llm.base import RateLimitError
from app.llm.cloudflare import CloudflareProvider
ACCT = "acct123"
URL = f"https://api.cloudflare.com/client/v4/accounts/{ACCT}/ai/v1/chat/completions"
@respx.mock
async def test_cloudflare_returns_content_and_sends_bearer():
route = respx.post(URL).mock(
return_value=httpx.Response(
200, json={"choices": [{"finish_reason": "stop", "message": {"content": "ok"}}]}
)
)
p = CloudflareProvider(account_id=ACCT, api_token="tok")
r = await p.chat(messages=[{"role": "user", "content": "x"}], tools=[], model="@cf/m")
assert r.content == "ok"
assert route.calls.last.request.headers["Authorization"] == "Bearer tok"
@respx.mock
async def test_cloudflare_429_raises_ratelimit():
respx.post(URL).mock(return_value=httpx.Response(429, text="limit"))
p = CloudflareProvider(account_id=ACCT, api_token="tok")
with pytest.raises(RateLimitError):
await p.chat(messages=[{"role": "user", "content": "x"}], tools=[], model="@cf/m")