77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
from core import ai_fetch
|
|
|
|
|
|
class _FakeMessage:
|
|
def __init__(self, content, annotations=None):
|
|
self.content = content
|
|
self.annotations = annotations
|
|
|
|
|
|
class _FakeChoice:
|
|
def __init__(self, message):
|
|
self.message = message
|
|
|
|
|
|
class _FakeResponse:
|
|
def __init__(self, message):
|
|
self.choices = [_FakeChoice(message)]
|
|
|
|
|
|
def test_fetch_with_ai_returns_result_when_content_sufficient(monkeypatch):
|
|
class _FakeClient:
|
|
class chat:
|
|
class completions:
|
|
@staticmethod
|
|
def create(**kwargs):
|
|
return _FakeResponse(_FakeMessage('x' * 400))
|
|
|
|
monkeypatch.setattr(ai_fetch, 'OpenAI', lambda api_key, base_url: _FakeClient())
|
|
|
|
result = ai_fetch.fetch_with_ai('https://intrepidtravel.com/peru')
|
|
|
|
assert result['success'] is True
|
|
assert result['char_count'] == 400
|
|
|
|
|
|
def test_fetch_with_ai_returns_none_when_content_too_short(monkeypatch):
|
|
class _FakeClient:
|
|
class chat:
|
|
class completions:
|
|
@staticmethod
|
|
def create(**kwargs):
|
|
return _FakeResponse(_FakeMessage('too short'))
|
|
|
|
monkeypatch.setattr(ai_fetch, 'OpenAI', lambda api_key, base_url: _FakeClient())
|
|
|
|
assert ai_fetch.fetch_with_ai('https://intrepidtravel.com/peru') is None
|
|
|
|
|
|
def test_fetch_with_ai_returns_none_on_exception(monkeypatch):
|
|
class _FakeClient:
|
|
class chat:
|
|
class completions:
|
|
@staticmethod
|
|
def create(**kwargs):
|
|
raise Exception('api down')
|
|
|
|
monkeypatch.setattr(ai_fetch, 'OpenAI', lambda api_key, base_url: _FakeClient())
|
|
|
|
assert ai_fetch.fetch_with_ai('https://intrepidtravel.com/peru') is None
|
|
|
|
|
|
def test_fetch_with_ai_appends_citation_content(monkeypatch):
|
|
citation = type('Citation', (), {'content': 'extra citation text'})()
|
|
annotation = type('Annotation', (), {'url_citation': citation})()
|
|
|
|
class _FakeClient:
|
|
class chat:
|
|
class completions:
|
|
@staticmethod
|
|
def create(**kwargs):
|
|
return _FakeResponse(_FakeMessage('x' * 350, annotations=[annotation]))
|
|
|
|
monkeypatch.setattr(ai_fetch, 'OpenAI', lambda api_key, base_url: _FakeClient())
|
|
|
|
result = ai_fetch.fetch_with_ai('https://intrepidtravel.com/peru')
|
|
assert 'extra citation text' in result['text']
|