|
import unittest
|
|
import json
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from rp.core.api import call_api, list_models
|
|
|
|
|
|
class TestApi(unittest.TestCase):
|
|
|
|
@patch("rp.core.http_client.SyncHTTPClient.request")
|
|
@patch("rp.core.api.auto_slim_messages")
|
|
def test_call_api_success(self, mock_slim, mock_request):
|
|
mock_slim.return_value = [{"role": "user", "content": "test"}]
|
|
mock_response = MagicMock()
|
|
mock_response.status = 200
|
|
mock_response.text = (
|
|
'{"choices": [{"message": {"content": "response"}}], "usage": {"tokens": 10}}'
|
|
)
|
|
mock_response.json.return_value = json.loads(mock_response.text)
|
|
mock_request.return_value = mock_response
|
|
|
|
result = call_api([], "model", "http://url", "key", True, [{"name": "tool"}])
|
|
|
|
self.assertIn("choices", result)
|
|
mock_request.assert_called_once()
|
|
|
|
@patch("rp.core.http_client.SyncHTTPClient.request")
|
|
@patch("rp.core.api.auto_slim_messages")
|
|
def test_call_api_http_error(self, mock_slim, mock_request):
|
|
mock_slim.return_value = [{"role": "user", "content": "test"}]
|
|
mock_request.return_value = {"error": True, "status": 500, "text": "error"}
|
|
|
|
result = call_api([], "model", "http://url", "key", False, [])
|
|
|
|
self.assertIn("error", result)
|
|
|
|
@patch("rp.core.http_client.SyncHTTPClient.request")
|
|
@patch("rp.core.api.auto_slim_messages")
|
|
def test_call_api_general_error(self, mock_slim, mock_request):
|
|
mock_slim.return_value = [{"role": "user", "content": "test"}]
|
|
mock_request.return_value = {"error": True, "exception": "test error"}
|
|
|
|
result = call_api([], "model", "http://url", "key", False, [])
|
|
|
|
self.assertIn("error", result)
|
|
|
|
@patch("rp.core.http_client.SyncHTTPClient.request")
|
|
def test_list_models_success(self, mock_request):
|
|
mock_response = MagicMock()
|
|
mock_response.status = 200
|
|
mock_response.text = '{"data": [{"id": "model1"}]}'
|
|
mock_response.json.return_value = json.loads(mock_response.text)
|
|
mock_request.return_value = mock_response
|
|
|
|
result = list_models("http://url", "key")
|
|
|
|
self.assertEqual(result, [{"id": "model1"}])
|
|
|
|
@patch("rp.core.http_client.SyncHTTPClient.request")
|
|
def test_list_models_error(self, mock_request):
|
|
mock_request.return_value = {"error": True, "exception": "error"}
|
|
|
|
result = list_models("http://url", "key")
|
|
|
|
self.assertIn("error", result)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|