import pytest
import time
from concurrent.futures import ThreadPoolExecutor
from src.api_hook_client import ApiHookClient

def test_preset_windows_opening(live_gui):
 """Test opening Preset Manager, Tool Preset Manager, and Persona Editor via custom_callback."""
 client = ApiHookClient()
 assert client.wait_for_server(timeout=15)
 
 # Push custom_callback events to set window visibility flags
 # These rely on the _set_attr predefined callback in AppController
 windows = [
  "show_preset_manager_window",
  "show_tool_preset_manager_window",
  "show_persona_editor_window"
 ]
 
 for window in windows:
  client.push_event("custom_callback", {
   "callback": "_set_attr",
   "args": [window, True]
  })
 
 # Wait 1 second as requested
 time.sleep(1.0)
 
 # Verify the app is still responsive
 status = client.get_status()
 assert status.get("status") == "ok"

def test_api_hook_under_load(live_gui):
 """Verify the API Hook can still respond under load."""
 client = ApiHookClient()
 assert client.wait_for_server(timeout=15)
 
 def make_request(_):
  return client.get_status()
 
 # Send 20 parallel requests using a thread pool
 with ThreadPoolExecutor(max_workers=10) as executor:
  results = list(executor.map(make_request, range(20)))
 
 for res in results:
  assert res is not None
  assert res.get("status") == "ok"