feat: remove asyncio dependencies from core api, assistant, and command handlers converting to synchronous execution
This commit is contained in:
+28
-26
@@ -1,62 +1,64 @@
|
||||
import unittest
|
||||
import urllib.error
|
||||
import json
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from pr.core.api import call_api, list_models
|
||||
from rp.core.api import call_api, list_models
|
||||
|
||||
|
||||
class TestApi(unittest.TestCase):
|
||||
|
||||
@patch("pr.core.api.urllib.request.urlopen")
|
||||
@patch("pr.core.api.auto_slim_messages")
|
||||
def test_call_api_success(self, mock_slim, mock_urlopen):
|
||||
@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.read.return_value = (
|
||||
b'{"choices": [{"message": {"content": "response"}}], "usage": {"tokens": 10}}'
|
||||
mock_response.status = 200
|
||||
mock_response.text = (
|
||||
'{"choices": [{"message": {"content": "response"}}], "usage": {"tokens": 10}}'
|
||||
)
|
||||
mock_urlopen.return_value.__enter__.return_value = mock_response
|
||||
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_urlopen.assert_called_once()
|
||||
mock_request.assert_called_once()
|
||||
|
||||
@patch("urllib.request.urlopen")
|
||||
@patch("pr.core.api.auto_slim_messages")
|
||||
def test_call_api_http_error(self, mock_slim, mock_urlopen):
|
||||
@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_urlopen.side_effect = urllib.error.HTTPError(
|
||||
"http://url", 500, "error", None, MagicMock()
|
||||
)
|
||||
mock_request.return_value = {"error": True, "status": 500, "text": "error"}
|
||||
|
||||
result = call_api([], "model", "http://url", "key", False, [])
|
||||
|
||||
self.assertIn("error", result)
|
||||
|
||||
@patch("urllib.request.urlopen")
|
||||
@patch("pr.core.api.auto_slim_messages")
|
||||
def test_call_api_general_error(self, mock_slim, mock_urlopen):
|
||||
@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_urlopen.side_effect = Exception("test error")
|
||||
mock_request.return_value = {"error": True, "exception": "test error"}
|
||||
|
||||
result = call_api([], "model", "http://url", "key", False, [])
|
||||
|
||||
self.assertIn("error", result)
|
||||
|
||||
@patch("urllib.request.urlopen")
|
||||
def test_list_models_success(self, mock_urlopen):
|
||||
@patch("rp.core.http_client.SyncHTTPClient.request")
|
||||
def test_list_models_success(self, mock_request):
|
||||
mock_response = MagicMock()
|
||||
mock_response.read.return_value = b'{"data": [{"id": "model1"}]}'
|
||||
mock_urlopen.return_value.__enter__.return_value = mock_response
|
||||
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("urllib.request.urlopen")
|
||||
def test_list_models_error(self, mock_urlopen):
|
||||
mock_urlopen.side_effect = Exception("error")
|
||||
@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")
|
||||
|
||||
|
||||
@@ -42,7 +42,6 @@ class TestAssistant(unittest.TestCase):
|
||||
@patch("pr.core.assistant.render_markdown")
|
||||
def test_process_response_no_tools(self, mock_render, mock_call):
|
||||
assistant = MagicMock()
|
||||
assistant.messages = MagicMock()
|
||||
assistant.verbose = False
|
||||
assistant.syntax_highlighting = True
|
||||
mock_render.return_value = "rendered"
|
||||
@@ -59,7 +58,6 @@ class TestAssistant(unittest.TestCase):
|
||||
@patch("pr.core.assistant.get_tools_definition")
|
||||
def test_process_response_with_tools(self, mock_tools_def, mock_render, mock_call):
|
||||
assistant = MagicMock()
|
||||
assistant.messages = MagicMock()
|
||||
assistant.verbose = False
|
||||
assistant.syntax_highlighting = True
|
||||
assistant.use_tools = True
|
||||
@@ -92,7 +90,6 @@ class TestAssistant(unittest.TestCase):
|
||||
@patch("pr.core.assistant.get_tools_definition")
|
||||
def test_process_message(self, mock_tools, mock_call):
|
||||
assistant = MagicMock()
|
||||
assistant.messages = MagicMock()
|
||||
assistant.verbose = False
|
||||
assistant.use_tools = True
|
||||
assistant.model = "model"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from pr.core.enhanced_assistant import EnhancedAssistant
|
||||
from rp.core.enhanced_assistant import EnhancedAssistant
|
||||
|
||||
|
||||
def test_enhanced_assistant_init():
|
||||
|
||||
+9
-11
@@ -2,38 +2,36 @@ from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from pr.__main__ import main
|
||||
from rp.__main__ import main
|
||||
|
||||
|
||||
def test_main_version(capsys):
|
||||
with patch("sys.argv", ["pr", "--version"]):
|
||||
with patch("sys.argv", ["rp", "--version"]):
|
||||
with pytest.raises(SystemExit):
|
||||
main()
|
||||
captured = capsys.readouterr()
|
||||
assert "PR Assistant" in captured.out
|
||||
assert "RP Assistant" in captured.out
|
||||
|
||||
|
||||
def test_main_create_config_success(capsys):
|
||||
with patch("pr.core.config_loader.create_default_config", return_value=True):
|
||||
with patch("sys.argv", ["pr", "--create-config"]):
|
||||
with patch("rp.core.config_loader.create_default_config", return_value=True):
|
||||
with patch("sys.argv", ["rp", "--create-config"]):
|
||||
main()
|
||||
captured = capsys.readouterr()
|
||||
assert "Configuration file created" in captured.out
|
||||
|
||||
|
||||
def test_main_create_config_fail(capsys):
|
||||
with patch("pr.core.config_loader.create_default_config", return_value=False):
|
||||
with patch("sys.argv", ["pr", "--create-config"]):
|
||||
with patch("rp.core.config_loader.create_default_config", return_value=False):
|
||||
with patch("sys.argv", ["rp", "--create-config"]):
|
||||
main()
|
||||
captured = capsys.readouterr()
|
||||
assert "Error creating configuration file" in captured.err
|
||||
|
||||
|
||||
def test_main_list_sessions_no_sessions(capsys):
|
||||
with patch("pr.core.session.SessionManager") as mock_sm:
|
||||
with patch("rp.core.session.SessionManager") as mock_sm:
|
||||
mock_instance = mock_sm.return_value
|
||||
mock_instance.list_sessions.return_value = []
|
||||
with patch("sys.argv", ["pr", "--list-sessions"]):
|
||||
with patch("sys.argv", ["rp", "--list-sessions"]):
|
||||
main()
|
||||
captured = capsys.readouterr()
|
||||
assert "No saved sessions found" in captured.out
|
||||
|
||||
Reference in New Issue
Block a user