import sys
import os
import time
from typing import Any

# Ensure project root is in path for imports
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from src.api_hook_client import ApiHookClient

def test_tool_management_state_updates(live_gui: Any) -> None:
 client = ApiHookClient()
 # Wait for hook server to be ready
 assert client.wait_for_server(timeout=30), "Hook server did not respond within 30s"
 
 # Test setting ui_active_tool_preset via custom_callback
 # callback '_set_attr' is defined in AppController._predefined_callbacks
 preset_name = "TestToolPreset"
 client.push_event("custom_callback", {
  "callback": "_set_attr",
  "args": ["ui_active_tool_preset", preset_name]
 })
 
 # Test setting ui_active_bias_profile via custom_callback
 bias_profile = "TestBiasProfile"
 client.push_event("custom_callback", {
  "callback": "_set_attr",
  "args": ["ui_active_bias_profile", bias_profile]
 })
 
 # Give some time for the GUI task to process (it's async via _pending_gui_tasks)
 time.sleep(2.0)
 
 # Verify via get_gui_state
 state = client.get_gui_state()
 
 assert state.get("ui_active_tool_preset") == preset_name, f"Expected {preset_name}, got {state.get('ui_active_tool_preset')}"
 assert state.get("ui_active_bias_profile") == bias_profile, f"Expected {bias_profile}, got {state.get('ui_active_bias_profile')}"

def test_tool_management_gettable_fields(live_gui: Any) -> None:
 client = ApiHookClient()
 assert client.wait_for_server(timeout=30)
 
 # Ensure they are at least present in the state (even if None/empty)
 state = client.get_gui_state()
 assert "ui_active_tool_preset" in state
 assert "ui_active_bias_profile" in state
