WIP: I HATE PYTHON
This commit is contained in:
@@ -1,53 +1,36 @@
|
||||
import threading
|
||||
import time
|
||||
import requests
|
||||
from api_hook_client import ApiHookClient
|
||||
import pytest
|
||||
from unittest.mock import patch, MagicMock
|
||||
from src.api_hook_client import ApiHookClient
|
||||
|
||||
def test_api_ask_client_method(live_gui) -> None:
|
||||
"""
|
||||
Tests the request_confirmation method in ApiHookClient.
|
||||
"""
|
||||
client = ApiHookClient("http://127.0.0.1:8999")
|
||||
# Drain existing events
|
||||
client.get_events()
|
||||
results = {"response": None, "error": None}
|
||||
def test_api_ask_client_method() -> None:
|
||||
"""Tests the request_confirmation method in ApiHookClient."""
|
||||
client = ApiHookClient()
|
||||
# Mock the internal _make_request method
|
||||
with patch.object(client, '_make_request') as mock_make:
|
||||
# Simulate a successful confirmation
|
||||
mock_make.return_value = {"response": True}
|
||||
|
||||
args = {"script": "echo hello", "base_dir": "."}
|
||||
result = client.request_confirmation("run_powershell", args)
|
||||
|
||||
assert result is True
|
||||
mock_make.assert_called_once_with(
|
||||
'POST',
|
||||
'/api/ask',
|
||||
data={'type': 'tool_approval', 'tool': 'run_powershell', 'args': args},
|
||||
timeout=60.0
|
||||
)
|
||||
|
||||
def make_blocking_request() -> None:
|
||||
try:
|
||||
# This call should block until we respond
|
||||
results["response"] = client.request_confirmation(
|
||||
tool_name="powershell",
|
||||
args={"command": "echo hello"}
|
||||
)
|
||||
except Exception as e:
|
||||
results["error"] = str(e)
|
||||
# Start the request in a background thread
|
||||
t = threading.Thread(target=make_blocking_request)
|
||||
t.start()
|
||||
# Poll for the 'ask_received' event
|
||||
request_id = None
|
||||
start_time = time.time()
|
||||
while time.time() - start_time < 5:
|
||||
events = client.get_events()
|
||||
for ev in events:
|
||||
if ev.get("type") == "ask_received":
|
||||
request_id = ev.get("request_id")
|
||||
break
|
||||
if request_id:
|
||||
break
|
||||
time.sleep(0.1)
|
||||
assert request_id is not None, "Timed out waiting for 'ask_received' event"
|
||||
# Respond
|
||||
expected_response = {"approved": True}
|
||||
resp = requests.post(
|
||||
"http://127.0.0.1:8999/api/ask/respond",
|
||||
json={
|
||||
"request_id": request_id,
|
||||
"response": expected_response
|
||||
}
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
t.join(timeout=5)
|
||||
assert not t.is_alive()
|
||||
assert results["error"] is None
|
||||
assert results["response"] == expected_response
|
||||
def test_api_ask_client_rejection() -> None:
|
||||
client = ApiHookClient()
|
||||
with patch.object(client, '_make_request') as mock_make:
|
||||
mock_make.return_value = {"response": False}
|
||||
result = client.request_confirmation("run_powershell", {})
|
||||
assert result is False
|
||||
|
||||
def test_api_ask_client_error() -> None:
|
||||
client = ApiHookClient()
|
||||
with patch.object(client, '_make_request') as mock_make:
|
||||
mock_make.return_value = None
|
||||
result = client.request_confirmation("run_powershell", {})
|
||||
assert result is None
|
||||
|
||||
Reference in New Issue
Block a user