import pytest
import sys
import os
from unittest.mock import patch

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from src.api_hook_client import ApiHookClient


def test_get_project_switch_status_calls_correct_endpoint() -> None:
 """get_project_switch_status() hits GET /api/project_switch_status."""
 client = ApiHookClient()
 with patch.object(client, "_make_request") as mock_make:
  mock_make.return_value = {"in_progress": False, "path": None, "error": None}
  result = client.get_project_switch_status()
  assert "in_progress" in result
  assert "path" in result
  assert "error" in result
  mock_make.assert_called_once_with("GET", "/api/project_switch_status")


def test_get_project_switch_status_handles_empty_response() -> None:
 """get_project_switch_status() returns safe default when server returns None."""
 client = ApiHookClient()
 with patch.object(client, "_make_request") as mock_make:
  mock_make.return_value = None
  result = client.get_project_switch_status()
  assert result == {"in_progress": False, "path": None, "error": None}


def test_get_project_switch_status_default_is_idle() -> None:
 """get_project_switch_status() returns idle state when server reports idle."""
 client = ApiHookClient()
 with patch.object(client, "_make_request") as mock_make:
  mock_make.return_value = {"in_progress": False, "path": None, "error": None}
  result = client.get_project_switch_status()
  assert result["in_progress"] is False
  assert result["path"] is None
  assert result["error"] is None


def test_live_project_switch_status_endpoint_idle(live_gui) -> None:
 """Live: GET /api/project_switch_status returns well-formed idle response."""
 client = ApiHookClient()
 assert client.wait_for_server(timeout=10)
 status = client.get_project_switch_status()
 assert "in_progress" in status
 assert "path" in status
 assert "error" in status
 assert isinstance(status["in_progress"], bool)
 assert status["in_progress"] is False, "expected no project switch in flight at session start"
 assert status["error"] is None, f"expected no error at session start, got {status['error']!r}"
 # path may be None (no project) or a str (current project loaded) - both are valid idle states
 assert status["path"] is None or isinstance(status["path"], str)
 assert status["error"] is None
