"""
Copyright (C) 2025 Luciano Guerche

This file is part of rabbitmq-mcp-server.

rabbitmq-mcp-server is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

rabbitmq-mcp-server is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with rabbitmq-mcp-server. If not, see <https://www.gnu.org/licenses/>.
"""

"""Integration tests for server startup."""

import time
from pathlib import Path

import pytest

from config.settings import Settings
from mcp_server.server import MCPServer


class TestServerStartup:
    """Test server startup sequence."""

    @pytest.mark.asyncio
    async def test_server_starts_successfully(self) -> None:
        """Test server starts successfully and loads artifacts."""
        settings = Settings(
            operations_registry_path="data/operations.json",
            embeddings_path="data/embeddings.json",
        )
        server = MCPServer(settings)

        await server.startup()

        # Verify artifacts loaded
        assert len(server.operations_registry) > 0
        assert len(server.embeddings) > 0

    @pytest.mark.asyncio
    async def test_startup_completes_under_one_second(self) -> None:
        """Test server startup completes in <1 second."""
        settings = Settings(
            operations_registry_path="data/operations.json",
            embeddings_path="data/embeddings.json",
        )
        server = MCPServer(settings)

        start_time = time.time()
        await server.startup()
        startup_time = time.time() - start_time

        assert startup_time < 1.0, f"Startup took {startup_time:.2f}s, expected <1s"

    @pytest.mark.asyncio
    async def test_startup_fails_if_operations_missing(self) -> None:
        """Test startup failure logs clear error if operations.json missing."""
        settings = Settings(
            operations_registry_path="data/nonexistent.json",
            embeddings_path="data/embeddings.json",
        )
        server = MCPServer(settings)

        with pytest.raises(FileNotFoundError) as exc_info:
            await server.startup()

        assert "Operations registry not found" in str(exc_info.value)

    @pytest.mark.asyncio
    async def test_startup_fails_if_embeddings_missing(self) -> None:
        """Test startup failure if embeddings.json missing."""
        settings = Settings(
            operations_registry_path="data/operations.json",
            embeddings_path="data/nonexistent.json",
        )
        server = MCPServer(settings)

        with pytest.raises(FileNotFoundError) as exc_info:
            await server.startup()

        assert "Embeddings file not found" in str(exc_info.value)

    @pytest.mark.asyncio
    async def test_operations_registry_loads_into_memory(self) -> None:
        """Test operations.json loads completely into memory."""
        settings = Settings(
            operations_registry_path="data/operations.json",
            embeddings_path="data/embeddings.json",
        )
        server = MCPServer(settings)

        await server.startup()

        # Verify it's a dict and has operations
        assert isinstance(server.operations_registry, dict)
        assert len(server.operations_registry) > 0

    @pytest.mark.asyncio
    async def test_embeddings_load_into_memory(self) -> None:
        """Test embeddings.json loads completely into memory."""
        settings = Settings(
            operations_registry_path="data/operations.json",
            embeddings_path="data/embeddings.json",
        )
        server = MCPServer(settings)

        await server.startup()

        # Verify it's a dict with embeddings
        assert isinstance(server.embeddings, dict)
        assert len(server.embeddings) > 0

    @pytest.mark.asyncio
    async def test_graceful_shutdown(self) -> None:
        """Test graceful shutdown completes."""
        settings = Settings(
            operations_registry_path="data/operations.json",
            embeddings_path="data/embeddings.json",
        )
        server = MCPServer(settings)

        await server.startup()
        await server.shutdown()

        # No exception means success
        assert True
