"""
Tests for web/auth/middleware_optimizer.py

Tests cover:
- should_skip_database_middleware - path-based skip logic
- should_skip_queue_checks - method/path skip logic
- should_skip_session_cleanup - probabilistic skip logic
"""

import pytest
from flask import Flask
from unittest.mock import patch


@pytest.fixture
def app():
    """Create a Flask test app."""
    app = Flask(__name__)
    app.config["TESTING"] = True
    return app


class TestShouldSkipDatabaseMiddleware:
    """Tests for should_skip_database_middleware function."""

    def test_skip_static_files(self, app):
        """Test that static file paths are skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context("/static/js/app.js", method="GET"):
            result = should_skip_database_middleware()
            assert result is True

    def test_skip_static_css(self, app):
        """Test that static CSS files are skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context("/static/css/style.css", method="GET"):
            result = should_skip_database_middleware()
            assert result is True

    def test_skip_static_images(self, app):
        """Test that static image files are skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context("/static/images/logo.png", method="GET"):
            result = should_skip_database_middleware()
            assert result is True

    def test_skip_favicon(self, app):
        """Test that favicon.ico is skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context("/favicon.ico", method="GET"):
            result = should_skip_database_middleware()
            assert result is True

    def test_skip_robots_txt(self, app):
        """Test that robots.txt is skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context("/robots.txt", method="GET"):
            result = should_skip_database_middleware()
            assert result is True

    def test_skip_health_endpoint(self, app):
        """Test that health endpoint is skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context("/health", method="GET"):
            result = should_skip_database_middleware()
            assert result is True

    def test_skip_socket_io_polling(self, app):
        """Test that Socket.IO polling paths are skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context(
            "/socket.io/?EIO=4&transport=polling", method="GET"
        ):
            result = should_skip_database_middleware()
            assert result is True

    def test_skip_socket_io_websocket(self, app):
        """Test that Socket.IO websocket paths are skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context(
            "/socket.io/?EIO=4&transport=websocket", method="GET"
        ):
            result = should_skip_database_middleware()
            assert result is True

    def test_skip_auth_login(self, app):
        """Test that auth/login path is skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context("/auth/login", method="POST"):
            result = should_skip_database_middleware()
            assert result is True

    def test_skip_auth_register(self, app):
        """Test that auth/register path is skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context("/auth/register", method="POST"):
            result = should_skip_database_middleware()
            assert result is True

    def test_skip_auth_logout(self, app):
        """Test that auth/logout path is skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context("/auth/logout", method="POST"):
            result = should_skip_database_middleware()
            assert result is True

    def test_skip_options_preflight(self, app):
        """Test that OPTIONS preflight requests are skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context("/api/research", method="OPTIONS"):
            result = should_skip_database_middleware()
            assert result is True

    def test_no_skip_api_endpoint(self, app):
        """Test that API endpoints are not skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context("/api/research", method="GET"):
            result = should_skip_database_middleware()
            assert result is False

    def test_no_skip_api_post(self, app):
        """Test that API POST requests are not skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context("/api/research", method="POST"):
            result = should_skip_database_middleware()
            assert result is False

    def test_no_skip_root_path(self, app):
        """Test that root path is not skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context("/", method="GET"):
            result = should_skip_database_middleware()
            assert result is False

    def test_no_skip_dashboard(self, app):
        """Test that dashboard path is not skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
        )

        with app.test_request_context("/dashboard", method="GET"):
            result = should_skip_database_middleware()
            assert result is False


class TestShouldSkipQueueChecks:
    """Tests for should_skip_queue_checks function."""

    def test_skip_get_requests(self, app):
        """Test that GET requests are skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_queue_checks,
        )

        with app.test_request_context("/api/research", method="GET"):
            result = should_skip_queue_checks()
            assert result is True

    def test_no_skip_post_requests(self, app):
        """Test that POST requests to API are not skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_queue_checks,
        )

        with app.test_request_context("/api/research", method="POST"):
            result = should_skip_queue_checks()
            assert result is False

    def test_no_skip_put_requests(self, app):
        """Test that PUT requests to API are not skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_queue_checks,
        )

        with app.test_request_context("/api/research/123", method="PUT"):
            result = should_skip_queue_checks()
            assert result is False

    def test_no_skip_delete_requests(self, app):
        """Test that DELETE requests to API are not skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_queue_checks,
        )

        with app.test_request_context("/api/research/123", method="DELETE"):
            result = should_skip_queue_checks()
            assert result is False

    def test_skip_static_files_post(self, app):
        """Test that static files are skipped even with POST."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_queue_checks,
        )

        with app.test_request_context("/static/js/app.js", method="POST"):
            result = should_skip_queue_checks()
            assert result is True

    def test_skip_health_post(self, app):
        """Test that health endpoint is skipped even with POST."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_queue_checks,
        )

        with app.test_request_context("/health", method="POST"):
            result = should_skip_queue_checks()
            assert result is True

    def test_skip_socket_io_post(self, app):
        """Test that socket.io is skipped even with POST."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_queue_checks,
        )

        with app.test_request_context(
            "/socket.io/?EIO=4&transport=polling", method="POST"
        ):
            result = should_skip_queue_checks()
            assert result is True

    def test_skip_options_always(self, app):
        """Test that OPTIONS requests are always skipped."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_queue_checks,
        )

        with app.test_request_context("/api/research", method="OPTIONS"):
            result = should_skip_queue_checks()
            assert result is True


class TestShouldSkipSessionCleanup:
    """Tests for should_skip_session_cleanup function."""

    def test_skip_static_files(self, app):
        """Test that static files always skip session cleanup."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_session_cleanup,
        )

        with app.test_request_context("/static/js/app.js", method="GET"):
            with patch("random.randint", return_value=1):  # Would normally run
                result = should_skip_session_cleanup()
            # Static files always skip, regardless of random
            assert result is True

    def test_skip_health_endpoint(self, app):
        """Test that health endpoint always skips session cleanup."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_session_cleanup,
        )

        with app.test_request_context("/health", method="GET"):
            with patch("random.randint", return_value=1):  # Would normally run
                result = should_skip_session_cleanup()
            assert result is True

    def test_skip_99_percent_of_time(self, app):
        """Test that cleanup is skipped 99% of the time (random > 1)."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_session_cleanup,
        )

        with app.test_request_context("/api/research", method="GET"):
            # Test with random values > 1 (should skip)
            for rand_val in [2, 50, 100]:
                with patch("random.randint", return_value=rand_val):
                    result = should_skip_session_cleanup()
                    assert result is True, (
                        f"Expected skip for random={rand_val}"
                    )

    def test_run_cleanup_1_percent(self, app):
        """Test that cleanup runs when random returns 1."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_session_cleanup,
        )

        with app.test_request_context("/api/research", method="GET"):
            with patch("random.randint", return_value=1):
                result = should_skip_session_cleanup()
            # When random returns 1, we should NOT skip (run cleanup)
            assert result is False

    def test_skip_auth_routes(self, app):
        """Test that auth routes skip cleanup."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_session_cleanup,
        )

        with app.test_request_context("/auth/login", method="POST"):
            with patch("random.randint", return_value=1):  # Would normally run
                result = should_skip_session_cleanup()
            assert result is True

    def test_skip_favicon(self, app):
        """Test that favicon skips cleanup."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_session_cleanup,
        )

        with app.test_request_context("/favicon.ico", method="GET"):
            with patch("random.randint", return_value=1):  # Would normally run
                result = should_skip_session_cleanup()
            assert result is True

    def test_skip_socket_io(self, app):
        """Test that socket.io paths skip cleanup."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_session_cleanup,
        )

        with app.test_request_context(
            "/socket.io/?EIO=4&transport=polling", method="GET"
        ):
            with patch("random.randint", return_value=1):  # Would normally run
                result = should_skip_session_cleanup()
            assert result is True

    def test_skip_robots_txt(self, app):
        """Test that robots.txt skips cleanup."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_session_cleanup,
        )

        with app.test_request_context("/robots.txt", method="GET"):
            with patch("random.randint", return_value=1):
                result = should_skip_session_cleanup()
            assert result is True

    def test_skip_options_preflight(self, app):
        """Test that OPTIONS preflight requests skip cleanup."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_session_cleanup,
        )

        with app.test_request_context("/api/research", method="OPTIONS"):
            with patch("random.randint", return_value=1):
                result = should_skip_session_cleanup()
            assert result is True
