import pytest
import time
from src.api_hook_client import ApiHookClient

@pytest.mark.timeout(30)
def test_change_provider_via_hook(live_gui) -> None:
 """Verify that we can change the current provider via the API hook."""
 client = ApiHookClient()
 if not client.wait_for_server():
  pytest.fail("Server did not become ready")
 
 # Change provider to 'anthropic'
 client.set_value('current_provider', 'anthropic')
 
 # Wait for state to reflect change
 success = False
 state = {}
 for _ in range(20):
  state = client.get_gui_state()
  if state.get('current_provider') == 'anthropic':
   success = True
   break
  time.sleep(0.5)
 
 assert success, f"Provider did not update. Current state: {state}"

@pytest.mark.timeout(30)
def test_set_params_via_custom_callback(live_gui) -> None:
 """Verify we can use custom_callback to set temperature and max_tokens."""
 client = ApiHookClient()
 if not client.wait_for_server():
  pytest.fail("Server did not become ready")
 
 # Set temperature via custom_callback using _set_attr
 client.post_gui({
  "action": "custom_callback",
  "callback": "_set_attr",
  "args": ["temperature", 0.85]
 })
 
 # Set max_tokens via custom_callback using _set_attr
 client.post_gui({
  "action": "custom_callback",
  "callback": "_set_attr",
  "args": ["max_tokens", 1024]
 })
 
 # Verify via get_gui_state
 success = False
 state = {}
 for _ in range(20):
  state = client.get_gui_state()
  if state.get('temperature') == 0.85 and state.get('max_tokens') == 1024:
   success = True
   break
  time.sleep(0.5)
 
 assert success, f"Params did not update via custom_callback. Got: {state}"