Download test_gateway.py from fugthchat/MEPLIFEOS: direct link, hf CLI and curl.
- Browser
- Download file 2.14 kB
-
https://huggingface.co/spaces/fugthchat/MEPLIFEOS/resolve/main/test_gateway.py
- Command line
-
hf download hf://spaces/fugthchat/MEPLIFEOS/test_gateway.py
-
curl -L -o test_gateway.py https://huggingface.co/spaces/fugthchat/MEPLIFEOS/resolve/main/test_gateway.py
2.14 kB
| """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.') | |