"""Tests for web/utils/templates.py."""

from unittest.mock import patch


class TestRenderTemplateWithDefaults:
    """Tests for render_template_with_defaults function."""

    def test_adds_version_to_kwargs(self):
        """Test that version is added to template kwargs."""
        with patch(
            "local_deep_research.web.utils.templates.render_template"
        ) as mock_render:
            with patch(
                "local_deep_research.database.encrypted_db.db_manager"
            ) as mock_db:
                mock_db.has_encryption = False
                mock_render.return_value = "rendered"

                from local_deep_research.web.utils.templates import (
                    render_template_with_defaults,
                )

                render_template_with_defaults("test.html")

                # Check that version was passed
                call_kwargs = mock_render.call_args[1]
                assert "version" in call_kwargs

    def test_adds_has_encryption_to_kwargs(self):
        """Test that has_encryption is added to template kwargs."""
        with patch(
            "local_deep_research.web.utils.templates.render_template"
        ) as mock_render:
            with patch(
                "local_deep_research.database.encrypted_db.db_manager"
            ) as mock_db:
                mock_db.has_encryption = True
                mock_render.return_value = "rendered"

                from local_deep_research.web.utils.templates import (
                    render_template_with_defaults,
                )

                render_template_with_defaults("test.html")

                call_kwargs = mock_render.call_args[1]
                assert call_kwargs["has_encryption"] is True

    def test_passes_encryption_false_when_disabled(self):
        """Test that has_encryption is False when encryption disabled."""
        with patch(
            "local_deep_research.web.utils.templates.render_template"
        ) as mock_render:
            with patch(
                "local_deep_research.database.encrypted_db.db_manager"
            ) as mock_db:
                mock_db.has_encryption = False
                mock_render.return_value = "rendered"

                from local_deep_research.web.utils.templates import (
                    render_template_with_defaults,
                )

                render_template_with_defaults("test.html")

                call_kwargs = mock_render.call_args[1]
                assert call_kwargs["has_encryption"] is False

    def test_passes_template_name_as_first_arg(self):
        """Test that template name is passed as first argument."""
        with patch(
            "local_deep_research.web.utils.templates.render_template"
        ) as mock_render:
            with patch(
                "local_deep_research.database.encrypted_db.db_manager"
            ) as mock_db:
                mock_db.has_encryption = False
                mock_render.return_value = "rendered"

                from local_deep_research.web.utils.templates import (
                    render_template_with_defaults,
                )

                render_template_with_defaults("my_template.html")

                call_args = mock_render.call_args[0]
                assert call_args[0] == "my_template.html"

    def test_passes_custom_kwargs_to_render_template(self):
        """Test that custom kwargs are passed through to render_template."""
        with patch(
            "local_deep_research.web.utils.templates.render_template"
        ) as mock_render:
            with patch(
                "local_deep_research.database.encrypted_db.db_manager"
            ) as mock_db:
                mock_db.has_encryption = False
                mock_render.return_value = "rendered"

                from local_deep_research.web.utils.templates import (
                    render_template_with_defaults,
                )

                render_template_with_defaults(
                    "test.html", title="My Title", data={"key": "value"}
                )

                call_kwargs = mock_render.call_args[1]
                assert call_kwargs["title"] == "My Title"
                assert call_kwargs["data"] == {"key": "value"}

    def test_returns_render_template_result(self):
        """Test that the function returns the result from render_template."""
        with patch(
            "local_deep_research.web.utils.templates.render_template"
        ) as mock_render:
            with patch(
                "local_deep_research.database.encrypted_db.db_manager"
            ) as mock_db:
                mock_db.has_encryption = False
                mock_render.return_value = "<html>Content</html>"

                from local_deep_research.web.utils.templates import (
                    render_template_with_defaults,
                )

                result = render_template_with_defaults("test.html")

                assert result == "<html>Content</html>"

    def test_passes_multiple_positional_args(self):
        """Test that multiple positional arguments are passed through."""
        with patch(
            "local_deep_research.web.utils.templates.render_template"
        ) as mock_render:
            with patch(
                "local_deep_research.database.encrypted_db.db_manager"
            ) as mock_db:
                mock_db.has_encryption = False
                mock_render.return_value = "rendered"

                from local_deep_research.web.utils.templates import (
                    render_template_with_defaults,
                )

                render_template_with_defaults("template.html", "extra_arg")

                call_args = mock_render.call_args[0]
                assert "template.html" in call_args
                assert "extra_arg" in call_args
