import sys
import os
from typing import Any
from unittest.mock import patch

# 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_api_client_has_extensions() -> None:
 client = ApiHookClient()
 # These should exist in the client
 assert hasattr(client, 'select_tab')
 assert hasattr(client, 'select_list_item')

def test_select_tab_integration(live_gui: Any) -> None:
 client = ApiHookClient()
 # In gui_2, select_tab might be implemented as a set_value or a custom action
 response = client.select_tab("operations_tabs", "tab_tool")
 assert response == {'status': 'queued'}

def test_select_list_item_integration(live_gui: Any) -> None:
 client = ApiHookClient()
 response = client.select_list_item("disc_listbox", "Default")
 assert response == {'status': 'queued'}

def test_get_indicator_state_integration(live_gui: Any) -> None:
 client = ApiHookClient()
 # thinking_indicator is usually hidden unless AI is running
 response = client.get_indicator_state("thinking_indicator")
 assert 'shown' in response

def test_app_processes_new_actions() -> None:
 from src import gui_2
 with patch('src.app_controller.AppController.load_config', return_value={}), \
   patch('src.performance_monitor.PerformanceMonitor'), \
   patch('src.session_logger.open_session'), \
   patch('src.session_logger.reset_session'), \
   patch('src.app_controller.AppController._prune_old_logs'), \
   patch('src.app_controller.AppController._load_active_project'):
  app = gui_2.App()
  # Test set_value via _pending_gui_tasks
  # First we need to register a settable field for testing if not present
  app.controller._settable_fields["test_item"] = "ui_ai_input"
  app.controller._pending_gui_tasks.append({
    "action": "set_value",
    "item": "test_item",
    "value": "new_value"
   })
  app.controller._process_pending_gui_tasks()
  assert app.controller.ui_ai_input == "new_value"
