42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
import io
|
|
from urllib import request
|
|
|
|
import pytest
|
|
|
|
from review_agent.llm import stream_reply
|
|
from review_agent.models import Conversation
|
|
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
|
|
class FakeStreamingResponse:
|
|
def __iter__(self):
|
|
return iter(
|
|
[
|
|
b'data: {"choices":[{"delta":{"content":"A"}}]}\n\n',
|
|
b"data: not-json\n\n",
|
|
b'data: {"choices":[{"delta":{"content":"B"}}]}\n\n',
|
|
b"data: [DONE]\n\n",
|
|
]
|
|
)
|
|
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, exc_type, exc, traceback):
|
|
return False
|
|
|
|
|
|
def test_stream_reply_skips_malformed_sse_data(monkeypatch, settings, django_user_model):
|
|
settings.LLM_API_KEY = "key"
|
|
settings.LLM_MODEL = "model"
|
|
settings.LLM_BASE_URL = "https://example.test/v1"
|
|
monkeypatch.setattr(request, "urlopen", lambda req, timeout: FakeStreamingResponse())
|
|
user = django_user_model.objects.create_user(username="owner", password="pass")
|
|
conversation = Conversation.objects.create(user=user, title="会话")
|
|
|
|
chunks = list(stream_reply(conversation, "你好"))
|
|
|
|
assert chunks == ["A", "B"]
|