"""
Tests for web/auth/middleware_optimizer.py and related middleware.

Tests cover:
- should_skip_database_middleware() function
- should_skip_queue_checks() function
- should_skip_session_cleanup() function
- Database middleware behavior
- Session cleanup middleware behavior
"""

from flask import Flask


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

    def test_skip_for_static_files(self):
        """Should return True for static file requests."""
        app = Flask(__name__)
        with app.test_request_context("/static/js/app.js"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_database_middleware,
            )

            assert should_skip_database_middleware() is True

    def test_skip_for_favicon(self):
        """Should return True for favicon.ico requests."""
        app = Flask(__name__)
        with app.test_request_context("/favicon.ico"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_database_middleware,
            )

            assert should_skip_database_middleware() is True

    def test_skip_for_robots_txt(self):
        """Should return True for robots.txt requests."""
        app = Flask(__name__)
        with app.test_request_context("/robots.txt"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_database_middleware,
            )

            assert should_skip_database_middleware() is True

    def test_skip_for_health_check(self):
        """Should return True for health check requests."""
        app = Flask(__name__)
        with app.test_request_context("/api/v1/health"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_database_middleware,
            )

            assert should_skip_database_middleware() is True

    def test_skip_for_socket_io(self):
        """Should return True for Socket.IO requests."""
        app = Flask(__name__)
        with app.test_request_context("/socket.io/poll"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_database_middleware,
            )

            assert should_skip_database_middleware() is True

    def test_skip_for_auth_login(self):
        """Should return True for login requests."""
        app = Flask(__name__)
        with app.test_request_context("/auth/login"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_database_middleware,
            )

            assert should_skip_database_middleware() is True

    def test_skip_for_auth_register(self):
        """Should return True for register requests."""
        app = Flask(__name__)
        with app.test_request_context("/auth/register"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_database_middleware,
            )

            assert should_skip_database_middleware() is True

    def test_skip_for_auth_logout(self):
        """Should return True for logout requests."""
        app = Flask(__name__)
        with app.test_request_context("/auth/logout"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_database_middleware,
            )

            assert should_skip_database_middleware() is True

    def test_skip_for_options_request(self):
        """Should return True for OPTIONS (CORS preflight) requests."""
        app = Flask(__name__)
        with app.test_request_context("/api/data", method="OPTIONS"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_database_middleware,
            )

            assert should_skip_database_middleware() is True

    def test_not_skip_for_api_requests(self):
        """Should return False for regular API requests."""
        app = Flask(__name__)
        with app.test_request_context("/api/v1/research"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_database_middleware,
            )

            assert should_skip_database_middleware() is False

    def test_not_skip_for_regular_page_requests(self):
        """Should return False for regular page requests."""
        app = Flask(__name__)
        with app.test_request_context("/dashboard"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_database_middleware,
            )

            assert should_skip_database_middleware() is False


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

    def test_skip_for_get_requests(self):
        """Should return True for GET requests."""
        app = Flask(__name__)
        with app.test_request_context("/api/data", method="GET"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_queue_checks,
            )

            assert should_skip_queue_checks() is True

    def test_not_skip_for_post_requests(self):
        """Should return False for POST requests to regular endpoints."""
        app = Flask(__name__)
        with app.test_request_context("/api/research", method="POST"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_queue_checks,
            )

            assert should_skip_queue_checks() is False

    def test_skip_for_static_post(self):
        """Should return True for POST to static (inherits from database middleware)."""
        app = Flask(__name__)
        with app.test_request_context("/static/upload", method="POST"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_queue_checks,
            )

            assert should_skip_queue_checks() is True

    def test_skip_for_options_post(self):
        """Should return True for OPTIONS method (CORS preflight)."""
        app = Flask(__name__)
        with app.test_request_context("/api/data", method="OPTIONS"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_queue_checks,
            )

            assert should_skip_queue_checks() is True


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

    def test_skip_for_static_files(self):
        """Should always skip for static files."""
        app = Flask(__name__)
        with app.test_request_context("/static/css/app.css"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_session_cleanup,
            )

            # For static files, always skip
            assert should_skip_session_cleanup() is True

    def test_skip_based_on_random_sampling(self):
        """Should skip based on random sampling (1% chance)."""
        app = Flask(__name__)
        with app.test_request_context("/dashboard"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_session_cleanup,
            )

            # Call multiple times - most should skip due to random sampling
            skip_count = sum(should_skip_session_cleanup() for _ in range(100))
            # With 1% chance of running, we expect ~99 skips
            # Allow some variance for randomness
            assert skip_count >= 90  # Should skip at least 90% of the time

    def test_inherits_database_middleware_skips(self):
        """Should skip for paths that skip database middleware."""
        app = Flask(__name__)
        with app.test_request_context("/favicon.ico"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_session_cleanup,
            )

            assert should_skip_session_cleanup() is True


class TestMiddlewareOptimizerIntegration:
    """Integration tests for middleware optimizer."""

    def test_function_imports_work(self):
        """All middleware optimizer functions can be imported."""
        from local_deep_research.web.auth.middleware_optimizer import (
            should_skip_database_middleware,
            should_skip_queue_checks,
            should_skip_session_cleanup,
        )

        assert callable(should_skip_database_middleware)
        assert callable(should_skip_queue_checks)
        assert callable(should_skip_session_cleanup)

    def test_consistent_skip_behavior(self):
        """Database middleware skip implies queue check skip."""
        app = Flask(__name__)

        # Test several paths that should skip database middleware
        skip_paths = ["/static/app.js", "/favicon.ico", "/socket.io/poll"]

        for path in skip_paths:
            with app.test_request_context(path):
                from local_deep_research.web.auth.middleware_optimizer import (
                    should_skip_database_middleware,
                    should_skip_queue_checks,
                )

                db_skip = should_skip_database_middleware()
                if db_skip:
                    # If database is skipped, queue should also be skipped
                    assert should_skip_queue_checks() is True


class TestDatabaseMiddlewarePaths:
    """Tests for specific database middleware path patterns."""

    def test_deep_static_paths(self):
        """Should skip for nested static paths."""
        app = Flask(__name__)
        with app.test_request_context("/static/dist/assets/js/app.chunk.js"):
            from local_deep_research.web.auth.middleware_optimizer import (
                should_skip_database_middleware,
            )

            assert should_skip_database_middleware() is True

    def test_socket_io_websocket_paths(self):
        """Should skip for Socket.IO websocket paths."""
        app = Flask(__name__)
        test_paths = [
            "/socket.io/",
            "/socket.io/poll",
            "/socket.io/websocket",
        ]

        for path in test_paths:
            with app.test_request_context(path):
                from local_deep_research.web.auth.middleware_optimizer import (
                    should_skip_database_middleware,
                )

                assert should_skip_database_middleware() is True, (
                    f"Failed for {path}"
                )

    def test_auth_routes_only_exact_match(self):
        """Should only skip for exact auth paths."""
        app = Flask(__name__)

        # These should skip
        skip_paths = ["/auth/login", "/auth/register", "/auth/logout"]
        for path in skip_paths:
            with app.test_request_context(path):
                from local_deep_research.web.auth.middleware_optimizer import (
                    should_skip_database_middleware,
                )

                assert should_skip_database_middleware() is True, (
                    f"Should skip {path}"
                )

        # These should NOT skip
        no_skip_paths = [
            "/auth/profile",
            "/auth/settings",
            "/auth/login/callback",
        ]
        for path in no_skip_paths:
            with app.test_request_context(path):
                from local_deep_research.web.auth.middleware_optimizer import (
                    should_skip_database_middleware,
                )

                assert should_skip_database_middleware() is False, (
                    f"Should not skip {path}"
                )
