"""
Tests for web/routes/research_routes_orm.py

Tests cover:
- ORM helper functions (check_research_status_orm, update_research_status_orm, update_progress_log_orm)
- Research endpoints (start_research, terminate, delete, clear_history, history, get_research)
"""

from unittest.mock import MagicMock, patch

from flask import Flask


class TestCheckResearchStatusOrm:
    """Tests for check_research_status_orm helper function."""

    def test_returns_status_for_existing_research(self):
        """Should return status for existing research."""
        mock_research = MagicMock()
        mock_research.status = "in_progress"

        mock_db_session = MagicMock()
        mock_db_session.query.return_value.filter_by.return_value.first.return_value = mock_research

        with patch(
            "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
        ) as mock_get_session:
            mock_get_session.return_value.__enter__ = MagicMock(
                return_value=mock_db_session
            )
            mock_get_session.return_value.__exit__ = MagicMock(
                return_value=False
            )

            from local_deep_research.web.routes.research_routes_orm import (
                check_research_status_orm,
            )

            result = check_research_status_orm("research_123")
            assert result == "in_progress"

    def test_returns_none_for_nonexistent_research(self):
        """Should return None for nonexistent research."""
        mock_db_session = MagicMock()
        mock_db_session.query.return_value.filter_by.return_value.first.return_value = None

        with patch(
            "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
        ) as mock_get_session:
            mock_get_session.return_value.__enter__ = MagicMock(
                return_value=mock_db_session
            )
            mock_get_session.return_value.__exit__ = MagicMock(
                return_value=False
            )

            from local_deep_research.web.routes.research_routes_orm import (
                check_research_status_orm,
            )

            result = check_research_status_orm("nonexistent_123")
            assert result is None


class TestUpdateResearchStatusOrm:
    """Tests for update_research_status_orm helper function."""

    def test_returns_true_when_research_updated(self):
        """Should return True when research is updated."""
        mock_research = MagicMock()
        mock_db_session = MagicMock()
        mock_db_session.query.return_value.filter_by.return_value.first.return_value = mock_research

        with patch(
            "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
        ) as mock_get_session:
            mock_get_session.return_value.__enter__ = MagicMock(
                return_value=mock_db_session
            )
            mock_get_session.return_value.__exit__ = MagicMock(
                return_value=False
            )

            from local_deep_research.web.routes.research_routes_orm import (
                update_research_status_orm,
            )

            result = update_research_status_orm("research_123", "completed")
            assert result is True
            assert mock_research.status == "completed"
            mock_db_session.commit.assert_called_once()

    def test_returns_false_when_research_not_found(self):
        """Should return False when research is not found."""
        mock_db_session = MagicMock()
        mock_db_session.query.return_value.filter_by.return_value.first.return_value = None

        with patch(
            "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
        ) as mock_get_session:
            mock_get_session.return_value.__enter__ = MagicMock(
                return_value=mock_db_session
            )
            mock_get_session.return_value.__exit__ = MagicMock(
                return_value=False
            )

            from local_deep_research.web.routes.research_routes_orm import (
                update_research_status_orm,
            )

            result = update_research_status_orm("nonexistent_123", "completed")
            assert result is False
            mock_db_session.commit.assert_not_called()


class TestUpdateProgressLogOrm:
    """Tests for update_progress_log_orm helper function."""

    def test_returns_true_when_progress_log_updated(self):
        """Should return True when progress log is updated."""
        mock_research = MagicMock()
        mock_db_session = MagicMock()
        mock_db_session.query.return_value.filter_by.return_value.first.return_value = mock_research

        new_log = [{"time": "2024-01-01", "progress": 50}]

        with patch(
            "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
        ) as mock_get_session:
            mock_get_session.return_value.__enter__ = MagicMock(
                return_value=mock_db_session
            )
            mock_get_session.return_value.__exit__ = MagicMock(
                return_value=False
            )

            from local_deep_research.web.routes.research_routes_orm import (
                update_progress_log_orm,
            )

            result = update_progress_log_orm("research_123", new_log)
            assert result is True
            assert mock_research.progress_log == new_log
            mock_db_session.commit.assert_called_once()

    def test_returns_false_when_research_not_found(self):
        """Should return False when research is not found."""
        mock_db_session = MagicMock()
        mock_db_session.query.return_value.filter_by.return_value.first.return_value = None

        with patch(
            "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
        ) as mock_get_session:
            mock_get_session.return_value.__enter__ = MagicMock(
                return_value=mock_db_session
            )
            mock_get_session.return_value.__exit__ = MagicMock(
                return_value=False
            )

            from local_deep_research.web.routes.research_routes_orm import (
                update_progress_log_orm,
            )

            result = update_progress_log_orm("nonexistent_123", [])
            assert result is False


class TestTerminateResearchEndpoint:
    """Tests for /api/terminate/<research_id> endpoint."""

    def test_returns_404_for_nonexistent_research(self):
        """Should return 404 when research doesn't exist."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_db_session = MagicMock()
        mock_db_session.query.return_value.filter_by.return_value.first.return_value = None

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
        ):
            mock_get_session.return_value.__enter__ = MagicMock(
                return_value=mock_db_session
            )
            mock_get_session.return_value.__exit__ = MagicMock(
                return_value=False
            )
            mock_db_manager.connections.get.return_value = MagicMock()

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.post("/api/terminate/nonexistent_123")
                assert response.status_code == 404
                assert response.json["status"] == "error"
                assert "not found" in response.json["message"]

    def test_returns_400_for_non_in_progress_research(self):
        """Should return 400 when research is not in progress."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_research = MagicMock()
        mock_research.status = "completed"

        mock_db_session = MagicMock()
        mock_db_session.query.return_value.filter_by.return_value.first.return_value = mock_research

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
        ):
            mock_get_session.return_value.__enter__ = MagicMock(
                return_value=mock_db_session
            )
            mock_get_session.return_value.__exit__ = MagicMock(
                return_value=False
            )
            mock_db_manager.connections.get.return_value = MagicMock()

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.post("/api/terminate/research_123")
                assert response.status_code == 400
                assert "not in progress" in response.json["message"]


class TestDeleteResearchEndpoint:
    """Tests for /api/delete/<research_id> endpoint."""

    def test_returns_404_for_nonexistent_research(self):
        """Should return 404 when research doesn't exist."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_db_session = MagicMock()
        mock_db_session.query.return_value.filter_by.return_value.first.return_value = None

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
        ):
            mock_get_session.return_value = mock_db_session
            mock_db_manager.connections.get.return_value = MagicMock()

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.delete("/api/delete/nonexistent_123")
                assert response.status_code == 404
                assert response.json["status"] == "error"

    def test_deletes_research_and_report_file(self):
        """Should delete research and associated report file."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_research = MagicMock()
        mock_research.report_path = "/tmp/report.md"

        mock_db_session = MagicMock()
        mock_db_session.query.return_value.filter_by.return_value.first.return_value = mock_research

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
            patch(
                "local_deep_research.web.routes.research_routes_orm.Path"
            ) as mock_path,
        ):
            mock_get_session.return_value = mock_db_session
            mock_db_manager.connections.get.return_value = MagicMock()
            mock_path_instance = MagicMock()
            mock_path_instance.exists.return_value = True
            mock_path.return_value = mock_path_instance

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.delete("/api/delete/research_123")
                assert response.status_code == 200
                assert response.json["status"] == "success"
                mock_db_session.delete.assert_called_once_with(mock_research)

    def test_handles_exception_returns_500(self):
        """Should return 500 on database error."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_db_session = MagicMock()
        mock_db_session.query.side_effect = Exception("Database error")

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
        ):
            mock_get_session.return_value = mock_db_session
            mock_db_manager.connections.get.return_value = MagicMock()

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.delete("/api/delete/research_123")
                assert response.status_code == 500


class TestClearHistoryEndpoint:
    """Tests for /api/clear_history endpoint."""

    def test_clears_all_research_records(self):
        """Should delete all research records."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_db_session = MagicMock()
        mock_db_session.query.return_value.all.return_value = []
        mock_db_session.query.return_value.delete.return_value = 5

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
        ):
            mock_get_session.return_value = mock_db_session
            mock_db_manager.connections.get.return_value = MagicMock()

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.post("/api/clear_history")
                assert response.status_code == 200
                assert response.json["status"] == "success"
                assert "5" in response.json["message"]

    def test_deletes_report_files(self):
        """Should delete associated report files."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_research = MagicMock()
        mock_research.report_path = "/tmp/report.md"

        mock_db_session = MagicMock()
        mock_db_session.query.return_value.all.return_value = [mock_research]
        mock_db_session.query.return_value.delete.return_value = 1

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
            patch(
                "local_deep_research.web.routes.research_routes_orm.Path"
            ) as mock_path,
        ):
            mock_get_session.return_value = mock_db_session
            mock_db_manager.connections.get.return_value = MagicMock()
            mock_path_instance = MagicMock()
            mock_path_instance.exists.return_value = True
            mock_path.return_value = mock_path_instance

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.post("/api/clear_history")
                assert response.status_code == 200
                mock_path_instance.unlink.assert_called_once()

    def test_handles_exception_returns_500(self):
        """Should return 500 on database error."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_db_session = MagicMock()
        mock_db_session.query.return_value.all.side_effect = Exception(
            "DB error"
        )

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
        ):
            mock_get_session.return_value = mock_db_session
            mock_db_manager.connections.get.return_value = MagicMock()

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.post("/api/clear_history")
                assert response.status_code == 500


class TestHistoryEndpoint:
    """Tests for /api/history endpoint."""

    def test_returns_paginated_history(self):
        """Should return paginated history."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_research = MagicMock()
        mock_research.id = "research_123"
        mock_research.query = "test query"
        mock_research.mode = "quick"
        mock_research.status = "completed"
        mock_research.created_at = "2024-01-01T00:00:00"
        mock_research.completed_at = "2024-01-01T01:00:00"
        mock_research.duration_seconds = 3600
        mock_research.report_path = "/tmp/report.md"
        mock_research.research_meta = {}
        mock_research.progress = 100
        mock_research.title = "Test Research"

        mock_query = MagicMock()
        mock_query.count.return_value = 1
        mock_query.offset.return_value.limit.return_value.all.return_value = [
            mock_research
        ]

        mock_db_session = MagicMock()
        mock_db_session.query.return_value.order_by.return_value = mock_query

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
        ):
            mock_get_session.return_value = mock_db_session
            mock_db_manager.connections.get.return_value = MagicMock()

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.get("/api/history")
                assert response.status_code == 200
                assert "history" in response.json
                assert "total" in response.json
                assert "page" in response.json
                assert "per_page" in response.json
                assert "total_pages" in response.json

    def test_uses_default_pagination_values(self):
        """Should use default pagination values."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_query = MagicMock()
        mock_query.count.return_value = 0
        mock_query.offset.return_value.limit.return_value.all.return_value = []

        mock_db_session = MagicMock()
        mock_db_session.query.return_value.order_by.return_value = mock_query

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
        ):
            mock_get_session.return_value = mock_db_session
            mock_db_manager.connections.get.return_value = MagicMock()

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.get("/api/history")
                assert response.status_code == 200
                assert response.json["page"] == 1
                assert response.json["per_page"] == 50

    def test_accepts_pagination_parameters(self):
        """Should accept pagination parameters."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_query = MagicMock()
        mock_query.count.return_value = 100
        mock_query.offset.return_value.limit.return_value.all.return_value = []

        mock_db_session = MagicMock()
        mock_db_session.query.return_value.order_by.return_value = mock_query

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
        ):
            mock_get_session.return_value = mock_db_session
            mock_db_manager.connections.get.return_value = MagicMock()

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.get("/api/history?page=2&per_page=10")
                assert response.status_code == 200
                assert response.json["page"] == 2
                assert response.json["per_page"] == 10

    def test_handles_exception_returns_500(self):
        """Should return 500 on database error."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_db_session = MagicMock()
        mock_db_session.query.side_effect = Exception("Database error")

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
        ):
            mock_get_session.return_value = mock_db_session
            mock_db_manager.connections.get.return_value = MagicMock()

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.get("/api/history")
                assert response.status_code == 500


class TestGetResearchEndpoint:
    """Tests for /api/research/<research_id> endpoint."""

    def test_returns_research_details(self):
        """Should return research details."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_research = MagicMock()
        mock_research.id = "research_123"
        mock_research.query = "test query"
        mock_research.mode = "quick"
        mock_research.status = "completed"
        mock_research.created_at = "2024-01-01T00:00:00"
        mock_research.completed_at = "2024-01-01T01:00:00"
        mock_research.duration_seconds = 3600
        mock_research.report_path = "/tmp/report.md"
        mock_research.research_meta = {}
        mock_research.progress_log = []
        mock_research.progress = 100
        mock_research.title = "Test Research"

        mock_db_session = MagicMock()
        mock_db_session.query.return_value.filter_by.return_value.first.return_value = mock_research

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
            patch(
                "local_deep_research.web.routes.research_routes_orm.active_research",
                {},
            ),
        ):
            mock_get_session.return_value.__enter__ = MagicMock(
                return_value=mock_db_session
            )
            mock_get_session.return_value.__exit__ = MagicMock(
                return_value=False
            )
            mock_db_manager.connections.get.return_value = MagicMock()

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.get("/api/research/research_123")
                assert response.status_code == 200
                assert response.json["id"] == "research_123"
                assert response.json["query"] == "test query"

    def test_returns_404_for_nonexistent_research(self):
        """Should return 404 when research doesn't exist."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_db_session = MagicMock()
        mock_db_session.query.return_value.filter_by.return_value.first.return_value = None

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
        ):
            mock_get_session.return_value.__enter__ = MagicMock(
                return_value=mock_db_session
            )
            mock_get_session.return_value.__exit__ = MagicMock(
                return_value=False
            )
            mock_db_manager.connections.get.return_value = MagicMock()

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.get("/api/research/nonexistent_123")
                assert response.status_code == 404

    def test_includes_logs_for_active_research(self):
        """Should include logs for active research."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        mock_research = MagicMock()
        mock_research.id = "research_123"
        mock_research.query = "test query"
        mock_research.mode = "quick"
        mock_research.status = "in_progress"
        mock_research.created_at = "2024-01-01T00:00:00"
        mock_research.completed_at = None
        mock_research.duration_seconds = None
        mock_research.report_path = None
        mock_research.research_meta = {}
        mock_research.progress_log = []
        mock_research.progress = 50
        mock_research.title = "Test Research"

        mock_db_session = MagicMock()
        mock_db_session.query.return_value.filter_by.return_value.first.return_value = mock_research

        active_logs = [{"time": "2024-01-01", "message": "Starting"}]

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
            patch(
                "local_deep_research.web.routes.research_routes_orm.active_research",
                {"research_123": {"log": active_logs}},
            ),
        ):
            mock_get_session.return_value.__enter__ = MagicMock(
                return_value=mock_db_session
            )
            mock_get_session.return_value.__exit__ = MagicMock(
                return_value=False
            )
            mock_db_manager.connections.get.return_value = MagicMock()

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.get("/api/research/research_123")
                assert response.status_code == 200
                assert "logs" in response.json
                assert response.json["logs"] == active_logs

    def test_handles_exception_returns_500(self):
        """Should return 500 on database error."""
        app = Flask(__name__)
        app.secret_key = "test"
        app.config["WTF_CSRF_ENABLED"] = False

        with (
            patch(
                "local_deep_research.web.routes.research_routes_orm.get_user_db_session"
            ) as mock_get_session,
            patch(
                "local_deep_research.web.auth.decorators.db_manager"
            ) as mock_db_manager,
        ):
            mock_get_session.return_value.__enter__ = MagicMock(
                side_effect=Exception("DB error")
            )
            mock_get_session.return_value.__exit__ = MagicMock(
                return_value=False
            )
            mock_db_manager.connections.get.return_value = MagicMock()

            from local_deep_research.web.routes.research_routes_orm import (
                research_bp,
            )

            app.register_blueprint(research_bp)

            with app.test_client() as client:
                with client.session_transaction() as sess:
                    sess["username"] = "testuser"

                response = client.get("/api/research/research_123")
                assert response.status_code == 500
