"""
Basic integration tests for Godot MCP Server.

Tests the basic functionality of the MCP server including
project detection and info retrieval.
"""

import json
from pathlib import Path


from godot_mcp.server import (
    GodotServer,
    project_get_info,
    project_list,
    validate_path,
)


class TestValidatePath:
    """Tests for the validate_path utility function."""

    def test_valid_path(self) -> None:
        """Test that normal paths are valid."""
        assert validate_path("/some/normal/path") is True
        assert validate_path("relative/path") is True

    def test_invalid_path_traversal(self) -> None:
        """Test that path traversal attempts are rejected."""
        assert validate_path("../parent") is False
        assert validate_path("some/../path") is False

    def test_invalid_empty_path(self) -> None:
        """Test that empty paths are rejected."""
        assert validate_path("") is False
        assert validate_path(None) is False  # type: ignore


class TestTempProject:
    """Tests using temporary Godot projects."""

    def test_temp_project_has_project_file(self, temp_godot_project: Path) -> None:
        """Test that the temp project fixture creates a valid project.godot."""
        project_file = temp_godot_project / "project.godot"
        assert project_file.exists()
        content = project_file.read_text()
        assert "config/name" in content
        assert "Test Project" in content

    def test_get_project_info_on_temp_project(
        self, temp_godot_project: Path, godot_client: GodotServer
    ) -> None:
        """
        Test that get_project_info works on the temporary project.

        This validates that the test harness is correctly set up.
        """
        result = project_get_info(str(temp_godot_project))

        # The result should be valid JSON
        data = json.loads(result)

        # Check basic structure
        assert "name" in data
        assert "path" in data
        assert "structure" in data

        # Name should match what we set in project.godot
        assert data["name"] == "Test Project"
        assert data["path"] == str(temp_godot_project)

    def test_list_projects_finds_temp_project(self, temp_godot_project: Path) -> None:
        """Test that list_projects can find the temporary project."""
        # List in the parent directory
        parent_dir = temp_godot_project.parent
        result = project_list(str(parent_dir), recursive=False)

        data = json.loads(result)

        # Should find at least one project
        assert len(data) >= 1

        # One of them should be our temp project
        project_names = [p["name"] for p in data]
        assert temp_godot_project.name in project_names


class TestGodotServer:
    """Tests for the GodotServer class."""

    def test_godot_server_instance(self, godot_client: GodotServer) -> None:
        """Test that we can get a GodotServer instance."""
        assert godot_client is not None
        assert isinstance(godot_client, GodotServer)

    def test_camel_to_snake_conversion(self, godot_client: GodotServer) -> None:
        """Test the camelCase to snake_case conversion."""
        assert godot_client.camel_to_snake("scenePath") == "scene_path"
        assert godot_client.camel_to_snake("nodeType") == "node_type"
        assert godot_client.camel_to_snake("parentNodePath") == "parent_node_path"
        assert godot_client.camel_to_snake("XMLParser") == "xml_parser"

    def test_normalize_parameters(self, godot_client: GodotServer) -> None:
        """Test parameter normalization from camelCase to snake_case."""
        params = {
            "scenePath": "test.tscn",
            "nodeType": "Node2D",
            "parentNodePath": "root",
        }
        result = godot_client.normalize_parameters(params)

        assert result["scene_path"] == "test.tscn"
        assert result["node_type"] == "Node2D"
        assert result["parent_node_path"] == "root"

    def test_normalize_empty_parameters(self, godot_client: GodotServer) -> None:
        """Test that empty parameters are handled correctly."""
        assert godot_client.normalize_parameters({}) == {}
        assert godot_client.normalize_parameters(None) is None  # type: ignore
