File size: 2,141 Bytes
d85712e | 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 | """Local smoke tests with a mocked model; no downloads or expensive inference."""
import importlib
import os
import sys
import time
import types
from unittest.mock import patch
from fastapi.testclient import TestClient
sys.modules.setdefault('llama_cpp', types.SimpleNamespace(Llama=object))
app_module = importlib.import_module('app')
class DummyLlama:
def create_chat_completion(self, messages, **kwargs):
time.sleep(0.02)
return {'choices': [{'message': {'content': 'We close at 3 PM on Saturday.'}}], 'usage': {'completion_tokens': 10}}
def test_api():
with patch.dict(os.environ, {'FUGTH_API_KEY': 'dev-test-key', 'FUGTH_ADMIN_KEY': 'admin-test-key'}):
with patch.object(app_module, 'load_one', return_value=DummyLlama()):
with TestClient(app_module.app) as client:
status = client.get('/health')
assert status.status_code == 200 and status.json()['ready']
assert set(status.json()['loaded_models']) == {'0.5b', '1.5b'}
req = {'model': '0.5b', 'business_name': 'Example', 'message': 'When do you close?', 'context': 'Saturday 3 PM.'}
assert client.post('/v1/chat', json=req).status_code == 401
result = client.post('/v1/chat', headers={'X-API-Key': 'dev-test-key'}, json=req)
assert result.status_code == 200, result.text
assert result.json()['model'] == '0.5b'
assert result.json()['answer'] == 'We close at 3 PM on Saturday.'
assert client.post('/admin/default-model', json={'model': '0.5b'}).status_code == 401
switch = client.post('/admin/default-model', headers={'X-Admin-Key': 'admin-test-key'}, json={'model': '0.5b'})
assert switch.status_code == 200
assert switch.json()['persisted'] is False
req.pop('model')
result2 = client.post('/v1/chat', headers={'X-API-Key': 'dev-test-key'}, json=req)
assert result2.json()['model'] == '0.5b'
if __name__ == '__main__':
test_api()
print('All gateway smoke tests passed.')
|