import pytest
import requests
from unittest.mock import MagicMock, patch
import threading
import time
import json
import sys
import os

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

from api_hook_client import ApiHookClient

def test_get_status_success(live_gui):
    """
    Test that get_status successfully retrieves the server status
    when the live GUI is running.
    """
    client = ApiHookClient()
    status = client.get_status()
    assert status == {'status': 'ok'}

def test_get_project_success(live_gui):
    """
    Test successful retrieval of project data from the live GUI.
    """
    client = ApiHookClient()
    response = client.get_project()
    assert 'project' in response
    # We don't assert specific content as it depends on the environment's active project

def test_get_session_success(live_gui):
    """
    Test successful retrieval of session data.
    """
    client = ApiHookClient()
    response = client.get_session()
    assert 'session' in response
    assert 'entries' in response['session']

def test_post_gui_success(live_gui):
    """
    Test successful posting of GUI data.
    """
    client = ApiHookClient()
    gui_data = {'command': 'set_text', 'id': 'some_item', 'value': 'new_text'}
    response = client.post_gui(gui_data)
    assert response == {'status': 'queued'}

def test_get_performance_success(live_gui):
    """
    Test successful retrieval of performance metrics.
    """
    client = ApiHookClient()
    response = client.get_performance()
    assert "performance" in response

def test_unsupported_method_error():
    """
    Test that calling an unsupported HTTP method raises a ValueError.
    """
    client = ApiHookClient()
    with pytest.raises(ValueError, match="Unsupported HTTP method"):
        client._make_request('PUT', '/some_endpoint', data={'key': 'value'})
