Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

66.2 KB
Raw Blame
"""
Tests for NotificationManager.
"""

import pytest
from unittest.mock import patch
from datetime import datetime, timezone

from local_deep_research.notifications.manager import (
    NotificationManager,
    RateLimiter,
    NotificationReason,
    NotificationResult,
)
from local_deep_research.notifications.templates import EventType
from local_deep_research.notifications.exceptions import (
    RateLimitError,
    SecurityBlockError,
    SendError,
    ServiceError,
)


class TestNotificationManagerInit:
    """Tests for NotificationManager initialization."""

    def test_init_with_settings_snapshot(self, mocker):
        """Test initialization with settings snapshot."""
        snapshot = {
            "notifications.service_url": "discord://webhook/token",
            "notifications.on_research_completed": True,
            "notifications.rate_limit_per_hour": 10,
            "notifications.rate_limit_per_day": 50,
        }

        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )

        assert manager._settings_snapshot == snapshot
        assert manager._user_id == "test_user"

    def test_shared_rate_limiter_singleton(self, mocker):
        """Test that rate limiter is shared across instances."""
        # Reset shared rate limiter
        NotificationManager._shared_rate_limiter = None

        snapshot = {
            "notifications.rate_limit_per_hour": 10,
            "notifications.rate_limit_per_day": 50,
        }

        manager1 = NotificationManager(
            settings_snapshot=snapshot, user_id="user1"
        )
        manager2 = NotificationManager(
            settings_snapshot=snapshot, user_id="user2"
        )

        # Both managers should have the same rate limiter instance
        assert manager1._rate_limiter is manager2._rate_limiter
        assert (
            manager1._rate_limiter is NotificationManager._shared_rate_limiter
        )


class TestGetSetting:
    """Tests for _get_setting method."""

    def test_get_setting_from_snapshot(self, mocker):
        """Test getting setting from snapshot."""
        snapshot = {
            "notifications.service_url": "discord://webhook/token",
        }

        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )

        result = manager._get_setting("notifications.service_url")

        assert result == "discord://webhook/token"

    def test_get_setting_returns_default(self, mocker):
        """Test getting setting returns default when not found."""
        snapshot = {}
        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )

        result = manager._get_setting(
            "notifications.nonexistent", default="default_value"
        )

        assert result == "default_value"


class TestSendNotification:
    """Tests for send_notification method."""

    def test_send_notification_success(self, mocker):
        """Test successful notification sending."""
        snapshot = {
            "notifications.service_url": "discord://webhook/token",
            "notifications.on_research_completed": True,
        }

        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )

        # Mock service.send_event
        manager.service.send_event = mocker.MagicMock(return_value=True)

        # Mock rate limiter
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)

        context = {
            "query": "Test query",
            "research_id": "123",
        }

        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context=context,
        )

        assert result.sent is True
        manager._rate_limiter.is_allowed.assert_called_once_with("test_user")
        manager.service.send_event.assert_called_once()

    def test_send_notification_disabled_by_setting(self, mocker):
        """Test notification not sent when disabled by setting."""
        snapshot = {
            "notifications.service_url": "discord://webhook/token",
            "notifications.on_research_completed": False,  # Disabled
        }

        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )
        manager.service.send_event = mocker.MagicMock()

        context = {"query": "Test"}

        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context=context,
        )

        assert result.sent is False
        manager.service.send_event.assert_not_called()

    def test_send_notification_no_service_url(self, mocker):
        """Test notification not sent when service URL missing."""
        snapshot = {
            "notifications.service_url": "",  # Empty
            "notifications.on_research_completed": True,
        }

        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )
        manager.service.send_event = mocker.MagicMock()

        context = {"query": "Test"}

        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context=context,
        )

        assert result.sent is False
        manager.service.send_event.assert_not_called()

    def test_send_notification_respects_rate_limit(self, mocker):
        """Test notification respects rate limiting."""
        snapshot = {
            "notifications.service_url": "discord://webhook/token",
            "notifications.on_research_completed": True,
        }

        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )
        manager.service.send_event = mocker.MagicMock()

        # Mock rate limiter to deny
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=False)

        context = {"query": "Test"}

        with pytest.raises(RateLimitError, match="rate limit exceeded"):
            manager.send_notification(
                event_type=EventType.RESEARCH_COMPLETED,
                context=context,
            )

        manager.service.send_event.assert_not_called()

    def test_send_notification_force_bypasses_rate_limit(self, mocker):
        """Test force=True bypasses rate limiting."""
        snapshot = {
            "notifications.service_url": "discord://webhook/token",
            "notifications.on_research_completed": True,
        }

        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )
        manager.service.send_event = mocker.MagicMock(return_value=True)

        # Mock rate limiter to deny, but force should bypass
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=False)

        context = {"query": "Test"}

        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context=context,
            force=True,  # Bypass rate limit
        )

        assert result.sent is True
        # Rate limiter should still be checked but result ignored
        manager._rate_limiter.is_allowed.assert_called_once()
        manager.service.send_event.assert_called_once()

    def test_send_notification_force_bypasses_disabled_setting(self, mocker):
        """Test force=True bypasses disabled setting."""
        snapshot = {
            "notifications.service_url": "discord://webhook/token",
            "notifications.on_research_completed": False,  # Disabled
        }

        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )
        manager.service.send_event = mocker.MagicMock(return_value=True)
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)

        context = {"query": "Test"}

        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context=context,
            force=True,
        )

        assert result.sent is True
        manager.service.send_event.assert_called_once()

    def test_send_notification_handles_service_failure(self, mocker):
        """Test graceful handling of service failures."""
        snapshot = {
            "notifications.service_url": "discord://webhook/token",
            "notifications.on_research_completed": True,
        }

        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )

        # Mock service to fail
        manager.service.send_event = mocker.MagicMock(return_value=False)
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)

        context = {"query": "Test"}

        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context=context,
        )

        assert result.sent is False

    def test_send_notification_handles_exception(self, mocker):
        """Test graceful handling of exceptions."""
        snapshot = {
            "notifications.service_url": "discord://webhook/token",
            "notifications.on_research_completed": True,
        }

        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )

        # Mock service to raise exception
        manager.service.send_event = mocker.MagicMock(
            side_effect=Exception("Service error")
        )
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)

        context = {"query": "Test"}

        # Should not raise, returns False
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context=context,
        )

        assert result.sent is False


class TestShouldNotify:
    """Tests for _should_notify method."""

    def test_should_notify_enabled(self, mocker):
        """Test notification enabled by default."""
        snapshot = {
            "notifications.on_research_completed": True,
        }

        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )

        result = manager._should_notify(EventType.RESEARCH_COMPLETED)

        assert result is True

    def test_should_notify_disabled(self, mocker):
        """Test notification disabled by setting."""
        snapshot = {
            "notifications.on_research_completed": False,
        }

        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )

        result = manager._should_notify(EventType.RESEARCH_COMPLETED)

        assert result is False

    def test_should_notify_default_false(self, mocker):
        """Test notification defaults to False when setting missing."""
        snapshot = {}
        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )

        result = manager._should_notify(EventType.RESEARCH_COMPLETED)

        # Default is False for missing settings to avoid infinite loops
        assert result is False


class TestTestService:
    """Tests for test_service method."""

    def test_test_service_success(self, mocker):
        """Test successful service test."""
        snapshot = {}
        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )

        manager.service.test_service = mocker.MagicMock(
            return_value={"success": True}
        )

        result = manager.test_service("discord://webhook/token")

        assert result == {"success": True}
        manager.service.test_service.assert_called_once_with(
            "discord://webhook/token"
        )

    def test_test_service_failure(self, mocker):
        """Test failed service test."""
        snapshot = {}
        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )

        manager.service.test_service = mocker.MagicMock(
            return_value={"success": False, "error": "Connection failed"}
        )

        result = manager.test_service("invalid://url")

        assert result["success"] is False
        assert "error" in result


class TestMasterSwitchEnvGate:
    """Tests for the LDR_NOTIFICATIONS_ALLOW_OUTBOUND env-only master switch.

    See SECURITY.md "Notification Webhook SSRF" — outbound notifications
    are off until the operator opts in, because Apprise's DNS-rebinding
    TOCTOU window cannot be closed in code.
    """

    def test_send_notification_returns_false_when_env_unset(
        self, monkeypatch, mocker
    ):
        """If LDR_NOTIFICATIONS_ALLOW_OUTBOUND is unset, send_notification bails early."""
        monkeypatch.delenv("LDR_NOTIFICATIONS_ALLOW_OUTBOUND", raising=False)

        snapshot = {
            "notifications.service_url": "discord://webhook/token",
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )
        manager.service.send_event = mocker.MagicMock(return_value=True)
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)

        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "Test"},
        )

        assert result.sent is False
        manager.service.send_event.assert_not_called()

    def test_send_notification_with_force_does_not_bypass_env_gate(
        self, monkeypatch, mocker
    ):
        """force=True bypasses per-user toggles, never the operator switch."""
        monkeypatch.delenv("LDR_NOTIFICATIONS_ALLOW_OUTBOUND", raising=False)

        snapshot = {"notifications.service_url": "discord://webhook/token"}
        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )
        manager.service.send_event = mocker.MagicMock(return_value=True)

        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "Test"},
            force=True,
        )

        assert result.sent is False
        manager.service.send_event.assert_not_called()

    def test_test_service_returns_disabled_error_when_env_unset(
        self, monkeypatch
    ):
        """test_service should refuse with a clear error when the gate is off."""
        monkeypatch.delenv("LDR_NOTIFICATIONS_ALLOW_OUTBOUND", raising=False)

        manager = NotificationManager(settings_snapshot={}, user_id="test_user")

        result = manager.test_service("discord://webhook/token")

        assert result["success"] is False
        assert "LDR_NOTIFICATIONS_ALLOW_OUTBOUND" in result["error"]


class TestNotificationResultSemantics:
    """Verify that every falsy code path in ``send_notification`` tags the
    result with a distinct :class:`NotificationReason` (issue #4877).
    """

    @pytest.fixture(autouse=True)
    def _fresh_rate_limiter(self):
        """Tests here share user_id "u"; isolate them from the shared
        class-level limiter so ordering / xdist cannot trip
        RateLimitError (issue #5110)."""
        NotificationManager._shared_rate_limiter = None
        yield
        NotificationManager._shared_rate_limiter = None

    def test_result_is_truthy_when_sent(self):
        r = NotificationResult(
            sent=True, reason=NotificationReason.SENT, detail=""
        )
        assert bool(r) is True
        assert r.sent is True

    def test_result_is_falsy_when_dropped(self):
        r = NotificationResult(
            sent=False,
            reason=NotificationReason.UNCONFIGURED,
            detail="x",
        )
        assert bool(r) is False
        assert r.sent is False

    def test_reason_serialization_is_snake_case(self):
        # The ``detail: reason`` log format relies on the value matching
        # the proposed enum names exactly.
        assert NotificationReason.SERVER_DISABLED.value == "server_disabled"
        assert NotificationReason.EVENT_DISABLED.value == "event_disabled"
        assert NotificationReason.UNCONFIGURED.value == "unconfigured"
        assert NotificationReason.EGRESS_DENIED.value == "egress_denied"
        assert NotificationReason.INVALID_URL.value == "invalid_url"
        assert NotificationReason.WEBHOOK_FAILED.value == "webhook_failed"
        assert NotificationReason.EXCEPTION.value == "exception"

    def test_server_disabled(self, monkeypatch, mocker):
        """Operator switch off → ``server_disabled``."""
        monkeypatch.delenv("LDR_NOTIFICATIONS_ALLOW_OUTBOUND", raising=False)
        snapshot = {"notifications.service_url": "discord://x"}
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )
        assert result.reason is NotificationReason.SERVER_DISABLED
        assert "LDR_NOTIFICATIONS_ALLOW_OUTBOUND" in result.detail

    def test_event_disabled(self, mocker):
        """Per-user toggle off → ``event_disabled``."""
        snapshot = {
            "notifications.service_url": "discord://x",
            "notifications.on_research_completed": False,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )
        assert result.reason is NotificationReason.EVENT_DISABLED
        assert "notifications.on_research_completed" in result.detail

    def test_event_disabled_force_bypasses(self, mocker):
        """``force=True`` is allowed to skip the per-user toggle (not the
        operator switch). Ensure we don't tag this case ``event_disabled``
        just because ``_should_notify`` is False."""
        snapshot = {
            "notifications.service_url": "discord://x",
            "notifications.on_research_completed": False,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager.service.send_event = mocker.MagicMock(return_value=True)
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
            force=True,
        )
        assert result.reason is NotificationReason.SENT

    def test_unconfigured(self, mocker):
        snapshot = {
            "notifications.service_url": "",
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )
        assert result.reason is NotificationReason.UNCONFIGURED
        assert "service_url" in result.detail

    def test_egress_denied(self, mocker):
        """All URLs filtered by egress policy → ``egress_denied``."""
        snapshot = {
            "notifications.service_url": "discord://x",
            "notifications.on_research_completed": True,
            "policy.egress_scope": "private_only",
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        with patch.object(
            manager,
            "_filter_urls_by_egress_policy",
            return_value="",
        ):
            result = manager.send_notification(
                event_type=EventType.RESEARCH_COMPLETED,
                context={"query": "q"},
                force=True,
            )
        assert result.reason is NotificationReason.EGRESS_DENIED
        assert "egress" in result.detail

    def test_egress_policy_unevaluable(self, mocker):
        """Policy exists but cannot be evaluated (filter fails closed,
        returning None) → still ``egress_denied``, but the detail must
        say the policy was unevaluable, not that it refused the URLs
        (issue #5110)."""
        snapshot = {
            "notifications.service_url": "discord://x",
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        with patch.object(
            manager,
            "_filter_urls_by_egress_policy",
            return_value=None,
        ):
            result = manager.send_notification(
                event_type=EventType.RESEARCH_COMPLETED,
                context={"query": "q"},
                force=True,
            )
        assert result.reason is NotificationReason.EGRESS_DENIED
        assert "could not be evaluated" in result.detail
        assert "refused" not in result.detail

    def test_unparseable_service_url_is_invalid_url_not_egress_denied(
        self, mocker
    ):
        """A scheme-less service URL is rejected before the egress policy
        is even consulted, so it must be reported as ``invalid_url`` — not
        ``egress_denied``, which would falsely imply the policy was
        evaluated and refused it (issue #5110)."""
        snapshot = {
            "notifications.service_url": "example.com/hook",
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )
        assert result.sent is False
        assert result.reason is NotificationReason.INVALID_URL
        assert "egress" not in result.detail

    def test_unparseable_fragment_in_multi_url_list_is_invalid_url(
        self, mocker
    ):
        """Same as above, but the unparseable fragment trails a
        well-formed URL in a multi-URL configuration."""
        snapshot = {
            "notifications.service_url": "discord://x example.com/hook",
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )
        assert result.sent is False
        assert result.reason is NotificationReason.INVALID_URL
        assert "egress" not in result.detail

    def test_separator_only_service_url_is_invalid_url_not_egress_denied(
        self, mocker
    ):
        """A nonempty setting that parses to zero URLs (separator
        characters only, e.g. ",") must be reported as ``invalid_url``
        — not ``egress_denied``. No URL was ever evaluated, so
        "all configured URLs refused by egress policy" would claim a
        refusal that never happened — the same dishonest-classification
        defect this PR fixes elsewhere (issue #5110).

        Teeth: drop the empty-``url_entries`` guard in
        ``send_notification`` and the flow falls through to the egress
        branch, which returns ``egress_denied`` with "refused" in the
        detail — both assertions fail."""
        snapshot = {
            "notifications.service_url": ",",
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )
        assert result.sent is False
        assert result.reason is NotificationReason.INVALID_URL
        assert "refused" not in result.detail

    def test_whitespace_mixed_malformed_url_is_invalid_url_not_egress_denied(
        self, mocker
    ):
        """A scheme-bearing entry trailed by a bare word —
        ``discord://x garbage`` — is malformed configuration, not a policy
        refusal. The parser must flag ANY token after unencoded whitespace,
        not only scheme-less dotted names like ``example.com/hook``;
        otherwise this shape sails through to the egress filter, which
        under PRIVATE_ONLY refuses the discord scheme and mislabels the
        drop as ``egress_denied`` / "all configured URLs refused by egress
        policy" (issue #5113 follow-up).

        Teeth: weaken ``parse_notification_url_list`` back to dotted-
        name-only trailing-fragment detection and ``garbage`` goes
        undetected — the flow falls through to the egress branch, which
        returns ``egress_denied`` with "refused" in the detail, failing
        both assertions."""
        snapshot = {
            "notifications.service_url": "discord://x garbage",
            "notifications.on_research_completed": True,
            "policy.egress_scope": "private_only",
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )
        assert result.sent is False
        assert result.reason is NotificationReason.INVALID_URL
        assert "refused" not in result.detail

    def test_fragment_drop_log_never_carries_the_fragment(
        self, mocker, capture_loguru, capture_loguru_records
    ):
        r"""The ``invalid_url`` drop log must record NOTHING derived from
        the fragment's content — not even a redacted form.

        ``redact_url_for_log`` keeps ``scheme://host[:port]``, but for the
        token-in-authority Apprise schemes the first authority segment IS
        the secret: ``slack://xoxb-SECRET-TOKEN/T00/B00`` redacts to
        ``slack://xoxb-SECRET-TOKEN``. And when the illegal character only
        TRAILS the entry the parser returns the WHOLE entry as the
        fragment, so the redaction runs on the operator's real,
        credential-bearing service URL. The sibling comment in this same
        code already says "Never log the fragment itself — it may contain
        credentials"; a redaction that preserves the credential does not
        satisfy that.

        Teeth: restore ``fragment=redact_url_for_log(invalid_fragment)``
        in ``NotificationManager.send_notification`` and the bound extra
        carries ``slack://xoxb-SECRET-TOKEN`` — the token assertion fails
        while the positive control still passes. Pass the diagnostics as
        bare loguru kwargs again (no placeholder) and the rendered-message
        assertions fail: no production sink would show them (#5619).
        """
        token = "xoxb-SECRET-TOKEN"
        service_url = f"slack://{token}/T00000/B00000\\"
        snapshot = {
            "notifications.service_url": service_url,
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)

        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )

        assert result.sent is False
        assert result.reason is NotificationReason.INVALID_URL

        logged = capture_loguru.getvalue()
        # Positive control: without this the token assertions below would
        # pass vacuously on an empty log.
        assert "unparseable service URL fragment" in logged
        assert token not in logged
        assert "slack://" not in logged
        # The diagnostic that replaced it is content-free, and it is
        # RENDERED into the message — the only field a production sink
        # shows. (``logged`` above also includes ``{extra}``, so checking
        # it would pass even for an unrendered kwarg.) The trailing
        # backslash makes the whole entry the fragment.
        drops = [
            r
            for r in capture_loguru_records
            if r["message"].startswith(
                "notification dropped: unparseable service URL fragment"
            )
        ]
        assert len(drops) == 1
        assert drops[0]["extra"].get("policy_audit") is True
        rendered = drops[0]["message"]
        assert f"fragment_length={len(service_url)}" in rendered
        assert "entries_parsed=1" in rendered

        # ...and the user-facing detail is content-free too.
        assert token not in result.detail

    def test_egress_filter_fragment_log_never_carries_the_fragment(
        self, capture_loguru
    ):
        r"""Same defect, second call site:
        ``_filter_urls_by_egress_policy`` logged
        ``redact_url_for_log(invalid_fragment)`` too.

        Teeth: restore that call and the bound extra carries
        ``slack://xoxb-SECRET-TOKEN`` while the positive control keeps
        passing.
        """
        token = "xoxb-SECRET-TOKEN"
        snapshot = {
            "notifications.service_url": "unused",
            "policy.egress_scope": "both",
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")

        allowed = manager._filter_urls_by_egress_policy(
            f"slack://{token}/T00000/B00000\\"
        )

        assert allowed == ""
        logged = capture_loguru.getvalue()
        assert "unparseable URL fragment" in logged
        assert token not in logged
        assert "slack://" not in logged

    def test_invalid_url_when_apprise_accepts_no_urls(self, mocker):
        """A falsy return from ``send_event`` means Apprise rejected the
        URL string at parse time (dispatch never attempted) →
        ``invalid_url``, not ``webhook_failed`` (issue #5110)."""
        snapshot = {
            "notifications.service_url": "discord://x",
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager.service.send_event = mocker.MagicMock(return_value=False)
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )
        assert result.reason is NotificationReason.INVALID_URL
        # The detail must carry the corrected claim — Apprise rejected
        # a URL before dispatch completed — not the old "accepted
        # none" wording that was false for a mixed URL list (the valid
        # partition was never tried). Revert the detail correction in
        # send_notification and both assertions fail.
        assert "rejected a configured service URL" in result.detail
        assert "accepted none" not in result.detail

    def test_invalid_url_real_service_contract(self, mocker):
        """Same as above but through the REAL NotificationService. An
        unrecognized scheme fails the SSRF URL validator, which raises
        ServiceError before any network I/O — that too must map to
        ``invalid_url``, not ``exception``.

        Note: this only guards the manager's ``except ServiceError``
        clause, not "the service's invalid-URL contract" broadly. If the
        scheme allowlist regressed instead (e.g. the validator started
        accepting ``not-a-real-scheme://``), the URL would instead fail at
        Apprise's own ``add()`` inside ``_dispatch``, which returns
        ``False`` — and the manager's falsy-``send_event`` branch (see
        ``test_invalid_url_when_apprise_accepts_no_urls``) still yields
        ``invalid_url`` too, so this test would keep passing (for the
        wrong reason) even with that regression."""
        snapshot = {
            "notifications.service_url": "not-a-real-scheme://nope",
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )
        assert result.sent is False
        assert result.reason is NotificationReason.INVALID_URL

    def test_webhook_failed_when_delivery_raises_senderror(self, mocker):
        """The delivery layer signals a real dispatch failure (dead
        webhook, HTTP error, network down) by RAISING SendError — it
        never returns False for this. That contract must map to
        ``webhook_failed``, and the detail must not echo the exception
        message, which wraps URLs/tokens (issue #5110)."""
        snapshot = {
            "notifications.service_url": "discord://x",
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager.service.send_event = mocker.MagicMock(
            side_effect=SendError(
                "Failed to send notification: POST discord://id/secret-token"
            )
        )
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )
        assert result.sent is False
        assert result.reason is NotificationReason.WEBHOOK_FAILED
        assert "secret-token" not in result.detail
        assert "discord://" not in result.detail

    def test_invalid_url_detail_does_not_leak_serviceerror_message(
        self, mocker
    ):
        """A ServiceError from URL security validation maps to
        ``invalid_url``. Its message wraps the offending URL (which can
        embed a token) — the static ``detail`` must never echo it, since
        downstream callers log ``detail`` verbatim (issue #5110)."""
        snapshot = {
            "notifications.service_url": "discord://x",
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager.service.send_event = mocker.MagicMock(
            side_effect=ServiceError(
                "Invalid service URL: discord://id123/secret-token blocked"
            )
        )
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )
        assert result.sent is False
        assert result.reason is NotificationReason.INVALID_URL
        assert "secret-token" not in result.detail
        assert "id123" not in result.detail
        assert "discord://" not in result.detail

    def test_security_block_error_gets_distinct_detail(self, mocker):
        """A ``SecurityBlockError`` (confirmed send-time SSRF/DNS-rebind
        block — a ``ServiceError`` subclass NotificationService.send()
        raises for that case) must still map to ``invalid_url`` like a
        plain ``ServiceError``, but with a detail that says the
        destination was blocked by egress/SSRF protection rather than the
        generic "check your URL/settings" text — the latter is misleading
        for a confirmed security block, not a config mistake."""
        snapshot = {
            "notifications.service_url": "discord://x",
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager.service.send_event = mocker.MagicMock(
            side_effect=SecurityBlockError(
                "Notification send refused: a send-time DNS resolution "
                "was blocked as internal/private/metadata (possible SSRF)"
            )
        )
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )
        assert result.sent is False
        assert result.reason is NotificationReason.INVALID_URL
        assert "blocked by egress/SSRF protection" in result.detail
        # Must not repeat the pre-dispatch-validation wording, which
        # would misleadingly suggest a settings/URL-format mistake.
        assert "LDR_NOTIFICATIONS_ALLOW_PRIVATE_IPS" not in result.detail
        # And must not leak the exception message or any resolved detail.
        assert "DNS resolution" not in result.detail

    def test_exception(self, mocker):
        snapshot = {
            "notifications.service_url": "discord://x",
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager.service.send_event = mocker.MagicMock(
            side_effect=RuntimeError("boom")
        )
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )
        assert result.reason is NotificationReason.EXCEPTION
        assert "RuntimeError" in result.detail

    def test_exception_detail_does_not_leak_message(self, mocker):
        """The raw exception message must not appear in ``detail`` — it
        can contain webhook URLs with embedded tokens, SMTP credentials,
        or payload content that should never be echoed into structured
        results (which downstream callers may log verbatim). Only the
        exception class name is allowed.
        """
        sensitive_message = (
            "POST to discord://id123/secret-token failed: "
            "payload={'password': 'hunter2'}"
        )
        snapshot = {
            "notifications.service_url": "discord://x",
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager.service.send_event = mocker.MagicMock(
            side_effect=RuntimeError(sensitive_message)
        )
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)
        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "q"},
        )
        assert result.reason is NotificationReason.EXCEPTION
        assert "secret-token" not in result.detail
        assert "hunter2" not in result.detail
        assert "discord://" not in result.detail
        assert "RuntimeError" in result.detail

    def test_test_service_distinguishes_unevaluable_policy(self, mocker):
        """test_service must not blame the egress scope when the policy
        could not be evaluated at all (filter returned None) — an
        unevaluable policy is a configuration problem, not a refusal,
        and the "Set Egress Scope to 'Unprotected'" remediation fixes
        the wrong thing.

        Teeth: drop the ``allowed is None`` branch in ``test_service``
        and the None return falls into the refusal branch below it —
        the message becomes "URL refused by egress policy...", failing
        both assertions."""
        snapshot = {"notifications.service_url": "discord://x"}
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        with patch.object(
            manager, "_filter_urls_by_egress_policy", return_value=None
        ):
            result = manager.test_service("discord://x")
        assert result["status"] == "error"
        assert "could not be evaluated" in result["message"]
        assert "refused" not in result["message"]

    @pytest.mark.parametrize(
        ("label", "url"),
        [
            ("scheme-less fragment", "secret.example.com/x discord://a/b"),
            ("no parseable URLs", ","),
        ],
    )
    def test_test_service_reports_unusable_url_as_invalid_not_egress_denied(
        self, mocker, label, url
    ):
        """``test_service`` must draw the same distinction
        ``send_notification`` draws: ``_filter_urls_by_egress_policy``
        returns ``""`` for an unparseable fragment and for a setting with
        no parseable URLs as well as for a real per-URL refusal, so
        mapping every ``""`` onto "Set Egress Scope to 'Unprotected'"
        tells the operator to widen a policy that was never consulted.

        Teeth: drop the ``parse_notification_url_list`` precheck at the
        top of ``test_service`` and both inputs fall through to the
        ``if not allowed`` branch — the message becomes "URL refused by
        egress policy. Set Egress Scope to 'Unprotected'…", failing the
        two assertions below.
        """
        snapshot = {
            "policy.egress_scope": {"value": "unprotected"},
            "search.tool": {"value": "searxng"},
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager.service.test_service = mocker.MagicMock()

        result = manager.test_service(url)

        assert result["status"] == "error", label
        assert "Egress Scope" not in result["message"], label
        assert "refused by egress policy" not in result["message"], label
        manager.service.test_service.assert_not_called()

    def test_rate_limit_still_raises(self, mocker):
        """Backward compat: rate-limit refusal still raises, not returned."""
        snapshot = {
            "notifications.service_url": "discord://x",
            "notifications.on_research_completed": True,
        }
        manager = NotificationManager(settings_snapshot=snapshot, user_id="u")
        manager._rate_limiter.is_allowed = mocker.MagicMock(return_value=False)
        with pytest.raises(RateLimitError):
            manager.send_notification(
                event_type=EventType.RESEARCH_COMPLETED,
                context={"query": "q"},
            )


class TestRateLimiter:
    """Tests for RateLimiter class."""

    def test_allow_first_notification(self):
        """Test first notification is always allowed."""
        limiter = RateLimiter(max_per_hour=10, max_per_day=50)

        result = limiter.is_allowed("user1")

        assert result is True

    def test_allow_within_hourly_limit(self):
        """Test notifications allowed within hourly limit."""
        limiter = RateLimiter(max_per_hour=3, max_per_day=10)

        # Send 3 notifications (at limit)
        assert limiter.is_allowed("user1") is True
        assert limiter.is_allowed("user1") is True
        assert limiter.is_allowed("user1") is True

        # 4th should be denied
        assert limiter.is_allowed("user1") is False

    def test_allow_within_daily_limit(self):
        """Test notifications allowed within daily limit."""
        limiter = RateLimiter(max_per_hour=100, max_per_day=3)

        # Send 3 notifications (at limit)
        assert limiter.is_allowed("user1") is True
        assert limiter.is_allowed("user1") is True
        assert limiter.is_allowed("user1") is True

        # 4th should be denied
        assert limiter.is_allowed("user1") is False

    def test_allow_separate_users(self):
        """Test rate limits are per-user."""
        limiter = RateLimiter(max_per_hour=2, max_per_day=10)

        # User 1 hits limit
        assert limiter.is_allowed("user1") is True
        assert limiter.is_allowed("user1") is True
        assert limiter.is_allowed("user1") is False

        # User 2 should still be allowed
        assert limiter.is_allowed("user2") is True
        assert limiter.is_allowed("user2") is True

    def test_reset_single_user(self):
        """Test resetting rate limit for single user."""
        limiter = RateLimiter(max_per_hour=2, max_per_day=10)

        # Hit limit
        assert limiter.is_allowed("user1") is True
        assert limiter.is_allowed("user1") is True
        assert limiter.is_allowed("user1") is False

        # Reset user1
        limiter.reset("user1")

        # Should be allowed again
        assert limiter.is_allowed("user1") is True

    def test_reset_all_users(self):
        """Test resetting rate limit for all users."""
        limiter = RateLimiter(max_per_hour=2, max_per_day=10)

        # Multiple users hit limits
        limiter.is_allowed("user1")
        limiter.is_allowed("user1")
        limiter.is_allowed("user2")
        limiter.is_allowed("user2")

        # Reset all
        limiter.reset()

        # Both should be allowed again
        assert limiter.is_allowed("user1") is True
        assert limiter.is_allowed("user2") is True

    def test_cleanup_inactive_users(self, mocker):
        """Test periodic cleanup of inactive users."""
        limiter = RateLimiter(
            max_per_hour=10, max_per_day=50, cleanup_interval_hours=1
        )

        # Add some activity
        limiter.is_allowed("user1")

        # Mock datetime to simulate time passing (8 days)
        from datetime import timedelta

        fake_now = datetime.now(timezone.utc) + timedelta(days=8)

        with patch(
            "local_deep_research.notifications.manager.datetime"
        ) as mock_dt:
            mock_dt.now.return_value = fake_now

            # Trigger cleanup by calling allow
            limiter._cleanup_inactive_users_if_needed(fake_now)

            # User1 should be cleaned up
            assert "user1" not in limiter._hourly_counts
            assert "user1" not in limiter._daily_counts

    def test_thread_safety(self, mocker):
        """Test rate limiter is thread-safe."""
        import threading

        limiter = RateLimiter(max_per_hour=100, max_per_day=1000)
        results = []

        def send_notifications():
            for _ in range(10):
                results.append(limiter.is_allowed("user1"))

        # Create 5 threads sending notifications concurrently
        threads = [
            threading.Thread(target=send_notifications) for _ in range(5)
        ]

        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join()

        # All should be allowed (50 total, well under limit)
        assert all(results)
        assert len(results) == 50


class TestIntegration:
    """Integration tests for NotificationManager."""

    @pytest.fixture(autouse=True)
    def reset_rate_limiter(self):
        """Reset shared rate limiter before each test for isolation."""
        NotificationManager._shared_rate_limiter = None
        yield
        NotificationManager._shared_rate_limiter = None

    def test_full_notification_workflow(self, mocker):
        """Test complete notification workflow with settings snapshot."""
        # Simulate background thread scenario
        snapshot = {
            "notifications.service_url": "discord://webhook/token",
            "notifications.on_research_completed": True,
            "notifications.rate_limit_per_hour": 10,
            "notifications.rate_limit_per_day": 50,
        }

        # Create manager with snapshot (no session - thread-safe)
        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )

        # Mock service
        manager.service.send_event = mocker.MagicMock(return_value=True)

        # Send notification
        context = {
            "query": "What is quantum computing?",
            "research_id": "123-abc",
            "summary": "Quantum computing uses quantum mechanics...",
        }

        result = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context=context,
        )

        assert result.sent is True

        # Verify service was called with correct params
        call_args = manager.service.send_event.call_args
        assert call_args[0][0] == EventType.RESEARCH_COMPLETED
        assert call_args[0][1] == context
        assert call_args[1]["service_urls"] == "discord://webhook/token"

    def test_multiple_event_types(self, mocker):
        """Test sending different event types."""
        snapshot = {
            "notifications.service_url": "discord://webhook/token",
            "notifications.on_research_completed": True,
            "notifications.on_research_failed": True,
            "notifications.on_subscription_update": False,  # Disabled
        }

        manager = NotificationManager(
            settings_snapshot=snapshot, user_id="test_user"
        )
        manager.service.send_event = mocker.MagicMock(return_value=True)

        # Research completed - should send
        result1 = manager.send_notification(
            event_type=EventType.RESEARCH_COMPLETED,
            context={"query": "Test"},
            force=True,
        )
        assert result1.sent is True

        # Research failed - should send
        result2 = manager.send_notification(
            event_type=EventType.RESEARCH_FAILED,
            context={"query": "Test", "error": "Failed"},
            force=True,
        )
        assert result2.sent is True

        # Subscription update - should not send (disabled)
        result3 = manager.send_notification(
            event_type=EventType.SUBSCRIPTION_UPDATE,
            context={"subscription_name": "Test"},
        )
        assert result3.sent is False


class TestPerUserRateLimiting:
    """Tests for per-user rate limiting functionality."""

    def test_rate_limiter_set_user_limits(self):
        """Test setting per-user rate limits."""
        limiter = RateLimiter(max_per_hour=10, max_per_day=50)

        # Set custom limits for specific users
        limiter.set_user_limits("user_a", max_per_hour=5, max_per_day=25)
        limiter.set_user_limits("user_b", max_per_hour=20, max_per_day=100)

        # Verify limits are set correctly
        assert limiter.get_user_limits("user_a") == (5, 25)
        assert limiter.get_user_limits("user_b") == (20, 100)
        assert limiter.get_user_limits("user_c") == (10, 50)  # Uses defaults

    def test_rate_limiter_respects_per_user_limits(self):
        """Test that rate limiter enforces different limits per user."""
        limiter = RateLimiter(max_per_hour=10, max_per_day=50)

        # Set conservative limits for user_a
        limiter.set_user_limits("user_a", max_per_hour=2, max_per_day=5)

        # User A should be limited to 2 per hour
        assert limiter.is_allowed("user_a") is True  # 1st
        assert limiter.is_allowed("user_a") is True  # 2nd
        assert limiter.is_allowed("user_a") is False  # 3rd - exceeds limit

        # User B should use default limits (10 per hour)
        for _ in range(10):
            assert limiter.is_allowed("user_b") is True
        assert limiter.is_allowed("user_b") is False  # 11th - exceeds default

    def test_notification_manager_with_user_id(self, mocker):
        """Test NotificationManager configures per-user limits."""
        # Reset shared rate limiter
        NotificationManager._shared_rate_limiter = None

        # User A with conservative limits
        snapshot_a = {
            "notifications.rate_limit_per_hour": 3,
            "notifications.rate_limit_per_day": 10,
        }
        manager_a = NotificationManager(snapshot_a, user_id="user_a")

        # User B with generous limits
        snapshot_b = {
            "notifications.rate_limit_per_hour": 15,
            "notifications.rate_limit_per_day": 50,
        }
        manager_b = NotificationManager(snapshot_b, user_id="user_b")

        # Verify both users have the same rate limiter instance (singleton)
        assert manager_a._rate_limiter is manager_b._rate_limiter

        # Verify per-user limits are configured correctly
        limiter = manager_a._rate_limiter
        assert limiter.get_user_limits("user_a") == (3, 10)
        assert limiter.get_user_limits("user_b") == (15, 50)

    def test_per_user_limits_isolation(self, mocker):
        """Test that users don't interfere with each other's limits."""
        # Reset shared rate limiter
        NotificationManager._shared_rate_limiter = None

        # Create managers for two users
        snapshot_a = {
            "notifications.service_url": "discord://webhook/token",
            "notifications.on_research_completed": True,
            "notifications.rate_limit_per_hour": 2,
            "notifications.rate_limit_per_day": 5,
        }
        manager_a = NotificationManager(snapshot_a, user_id="user_a")

        snapshot_b = {
            "notifications.service_url": "discord://webhook/token",
            "notifications.on_research_completed": True,
            "notifications.rate_limit_per_hour": 5,
            "notifications.rate_limit_per_day": 10,
        }
        manager_b = NotificationManager(snapshot_b, user_id="user_b")

        # Mock service
        manager_a.service.send_event = mocker.MagicMock(return_value=True)
        manager_b.service.send_event = mocker.MagicMock(return_value=True)

        context = {"query": "Test"}

        # User A sends notifications up to their limit (2)
        result1 = manager_a.send_notification(
            EventType.RESEARCH_COMPLETED, context
        )
        assert result1.sent is True

        result2 = manager_a.send_notification(
            EventType.RESEARCH_COMPLETED, context
        )
        assert result2.sent is True

        # User A exceeds limit
        with pytest.raises(RateLimitError):
            manager_a.send_notification(EventType.RESEARCH_COMPLETED, context)

        # User B should still be able to send (not affected by User A)
        for _ in range(5):  # User B has 5/hour limit
            result = manager_b.send_notification(
                EventType.RESEARCH_COMPLETED, context
            )
            assert result.sent is True

        # User B exceeds their own limit
        with pytest.raises(RateLimitError):
            manager_b.send_notification(EventType.RESEARCH_COMPLETED, context)

    def test_manager_configures_user_limits(self, mocker):
        """Test that manager configures per-user limits on initialization."""
        # Reset shared rate limiter
        NotificationManager._shared_rate_limiter = None

        snapshot = {
            "notifications.rate_limit_per_hour": 7,
            "notifications.rate_limit_per_day": 30,
        }

        # Create manager with user_id
        manager = NotificationManager(snapshot, user_id="some_user")

        # Limiter should be created with defaults from snapshot
        limiter = manager._rate_limiter
        assert limiter.max_per_hour == 7
        assert limiter.max_per_day == 30

        # User-specific limits should be configured
        assert limiter.get_user_limits("some_user") == (7, 30)

    def test_updating_user_limits_after_initialization(self):
        """Test that user limits can be updated after manager creation."""
        # Reset shared rate limiter
        NotificationManager._shared_rate_limiter = None

        # Create manager with initial limits
        snapshot = {
            "notifications.rate_limit_per_hour": 5,
            "notifications.rate_limit_per_day": 20,
        }
        manager = NotificationManager(snapshot, user_id="user_a")

        # Verify initial limits
        assert manager._rate_limiter.get_user_limits("user_a") == (5, 20)

        # Update limits directly on rate limiter
        manager._rate_limiter.set_user_limits(
            "user_a", max_per_hour=10, max_per_day=40
        )

        # Verify limits are updated
        assert manager._rate_limiter.get_user_limits("user_a") == (10, 40)

    def test_multiple_managers_same_user_updates_limits(self):
        """Test that creating multiple managers for same user updates limits."""
        # Reset shared rate limiter
        NotificationManager._shared_rate_limiter = None

        # First manager for user_a with 5/hour
        snapshot1 = {"notifications.rate_limit_per_hour": 5}
        manager1 = NotificationManager(snapshot1, user_id="user_a")

        assert manager1._rate_limiter.get_user_limits("user_a") == (5, 50)

        # Second manager for user_a with 10/hour (should update)
        snapshot2 = {"notifications.rate_limit_per_hour": 10}
        manager2 = NotificationManager(snapshot2, user_id="user_a")

        # Both managers should see the updated limits
        assert manager1._rate_limiter.get_user_limits("user_a") == (10, 50)
        assert manager2._rate_limiter.get_user_limits("user_a") == (10, 50)


class TestEgressPolicyURLFilter:
    """_filter_urls_by_egress_policy gates notification dispatch by scope."""

    def _mgr(self, scope, tool="searxng"):
        snap = {
            "policy.egress_scope": {"value": scope},
            "search.tool": {"value": tool},
        }
        return NotificationManager(settings_snapshot=snap, user_id="u")

    def test_private_only_blocks_vendor_schemes_and_public_http(self):
        mgr = self._mgr("private_only", tool="library")
        out = mgr._filter_urls_by_egress_policy(
            "slack://t discord://x http://127.0.0.1/h https://public.example.com/h"
        )
        # Only the LOCAL http webhook survives; vendor schemes + public host dropped.
        assert out == "http://127.0.0.1/h"

    def test_retired_both_coerces_to_adaptive(self):
        # `both` is retired (ADR-0007): it coerces to ADAPTIVE, which with the
        # default public primary (searxng) resolves PUBLIC_ONLY. Vendor schemes
        # still pass (only PRIVATE_ONLY refuses them), but the LOCAL http
        # webhook is now dropped because public_only blocks private hosts.
        mgr = self._mgr("both")
        out = mgr._filter_urls_by_egress_policy("slack://t http://127.0.0.1/h")
        assert "slack://t" in out
        assert "http://127.0.0.1/h" not in out

    def test_unprotected_scope_passes_all_urls(self, monkeypatch):
        # The escape hatch that replaces the old permissive `both`: vendor
        # schemes AND a local http webhook both pass (egress protection off;
        # the hard SSRF/metadata block still applies to metadata IPs). The
        # operator must explicitly enable this dangerous mode.
        monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
        mgr = self._mgr("unprotected")
        out = mgr._filter_urls_by_egress_policy("slack://t http://127.0.0.1/h")
        assert "slack://t" in out
        assert "http://127.0.0.1/h" in out

    def test_unprotected_scope_is_protective_when_gate_disabled(
        self, monkeypatch
    ):
        monkeypatch.delenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", raising=False)
        mgr = self._mgr("unprotected")
        out = mgr._filter_urls_by_egress_policy("slack://t http://127.0.0.1/h")
        assert "slack://t" in out
        assert "http://127.0.0.1/h" not in out

    def test_embedded_comma_survives_mixed_service_filtering(self, monkeypatch):
        monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
        mgr = self._mgr("unprotected")
        json_url = "json://example.com/webhook?field=a,b"
        out = mgr._filter_urls_by_egress_policy(
            f"{json_url}, discord://webhook/token"
        )

        assert out == f"{json_url} discord://webhook/token"

    @pytest.mark.parametrize(
        "service_urls",
        [
            "typo.example.com/x, slack://t/x/y",
            "typo.example.com/x slack://t/x/y",
            "discord://id/token, example.com/webhook",
            "discord://id/token example.com/webhook",
        ],
    )
    def test_scheme_less_fragment_refuses_entire_configuration(
        self, service_urls
    ):
        mgr = self._mgr("unprotected")

        assert mgr._filter_urls_by_egress_policy(service_urls) == ""

    @pytest.mark.parametrize(
        "path, expected_prefix",
        [
            (
                "send_notification",
                "notification dropped: unparseable service URL fragment",
            ),
            (
                "test_service",
                "test notification refused: unparseable service URL fragment",
            ),
            (
                "_filter_urls_by_egress_policy",
                "all notification URLs refused: unparseable URL fragment",
            ),
        ],
    )
    def test_scheme_less_refusal_renders_content_free_diagnostics(
        self, mocker, capture_loguru_records, path, expected_prefix
    ):
        """The scheme-less refusal warning must RENDER its diagnostics.

        Production sinks (stderr, file, database) format only the
        message: loguru kwargs with no placeholder in the template
        never reach an operator (#5619). The diagnostics are
        content-free (fragment length + entry count); the fragment
        itself must stay out of the message AND the bound ``extra``,
        not even in a ``redact_url_for_log`` form — for
        token-in-authority Apprise schemes the first authority segment
        IS the secret, and when the illegal character only trails an
        entry the fragment is the whole entry.

        ``send_notification`` is the live path; ``test_service`` and
        ``_filter_urls_by_egress_policy`` carry the same branch (the
        latter is reached only by a direct caller, since both callers
        parse first and return early).

        Teeth: revert any of the three sites to bare kwargs and its
        ``fragment_length`` / ``entries_parsed`` assertions fail (the
        rendered message is the bare template); render or bind the
        fragment instead and the ``secret.example.com`` /
        ``/private/token`` assertions fail.
        """
        fragment = "secret.example.com/private/token"
        service_urls = f"{fragment} slack://t/x/y"
        mgr = NotificationManager(
            settings_snapshot={
                "policy.egress_scope": {"value": "unprotected"},
                "search.tool": {"value": "searxng"},
                "notifications.service_url": service_urls,
                "notifications.on_research_completed": True,
            },
            user_id="u",
        )
        mgr._rate_limiter.is_allowed = mocker.MagicMock(return_value=True)
        mgr.service.send_event = mocker.MagicMock(return_value=True)
        mgr.service.test_service = mocker.MagicMock(
            return_value={"status": "ok"}
        )

        if path == "send_notification":
            result = mgr.send_notification(
                event_type=EventType.RESEARCH_COMPLETED,
                context={"query": "q"},
            )
            assert result.reason is NotificationReason.INVALID_URL
            mgr.service.send_event.assert_not_called()
        elif path == "test_service":
            assert mgr.test_service(service_urls)["status"] == "error"
            mgr.service.test_service.assert_not_called()
        else:
            assert mgr._filter_urls_by_egress_policy(service_urls) == ""

        warnings = [
            r
            for r in capture_loguru_records
            if r["message"].startswith(expected_prefix)
        ]
        assert len(warnings) == 1
        record = warnings[0]
        # The audit bind still routes the record correctly.
        assert record["extra"].get("policy_audit") is True
        # The content-free diagnostics ARE rendered into the message —
        # the only field production sinks show an operator.
        rendered = record["message"]
        assert f"fragment_length={len(fragment)}" in rendered
        assert "entries_parsed=2" in rendered
        if path == "send_notification":
            assert "user=u" in rendered
            assert f"event={EventType.RESEARCH_COMPLETED.value}" in rendered
        # ...and nothing derived from the fragment's content is, in the
        # message or in any bound field.
        for text in (rendered, str(record["extra"])):
            assert "secret.example.com" not in text
            assert "/private/token" not in text

    def test_egress_refusal_log_never_carries_the_refused_url(
        self, capture_loguru
    ):
        """The ``policy_audit`` line for a refused http(s) URL must carry
        only the redacted ``scheme://host[:port]``.

        This branch sees http(s) URLs straight from
        ``notifications.service_url``. RFC 3986 §3.2.1 allows credentials
        in the userinfo, and for a Slack/Discord-style webhook the PATH is
        the secret — ``https://hooks.example/services/T0/B0/TOKEN`` is a
        bearer credential in its entirety. Logging the entry verbatim put
        both into the audit log (and, via ``database_sink``, into
        ``app_logs``).

        Teeth: revert ``url=redact_url_for_log(url_entry)`` to
        ``url=url_entry`` in ``_filter_urls_by_egress_policy`` and the
        bound ``extra`` carries the password, the userinfo and the whole
        secret path — four of the assertions below fail while the
        positive controls keep passing.
        """
        mgr = self._mgr("public_only")
        # A private host is refused under PUBLIC_ONLY, so this reaches the
        # ``decision.allowed is False`` branch. An IP literal keeps the
        # classification deterministic (no DNS).
        url = "https://ops:[email protected]/services/T0/B0/SECRETTOKEN"

        assert mgr._filter_urls_by_egress_policy(url) == ""

        logged = capture_loguru.getvalue()
        # Positive controls: without these the absence assertions could
        # pass vacuously on an empty log.
        assert "notification URL refused by egress policy" in logged
        assert "127.0.0.1" in logged
        # ...and nothing else from the entry survives.
        assert "PASSWORDSECRET" not in logged
        assert "ops:" not in logged
        assert "SECRETTOKEN" not in logged
        assert "/services/" not in logged

    def test_public_only_blocks_private_http_keeps_public(self):
        mgr = self._mgr("public_only", tool="searxng")
        out = mgr._filter_urls_by_egress_policy(
            "http://127.0.0.1/h https://public.example.com/h"
        )
        assert out == "https://public.example.com/h"

    def test_no_snapshot_passes_through(self):
        mgr = NotificationManager.__new__(NotificationManager)
        mgr._settings_snapshot = None
        assert mgr._filter_urls_by_egress_policy("slack://t") == "slack://t"
        malformed = "typo.example.com/x slack://t/x/y"
        assert mgr._filter_urls_by_egress_policy(malformed) == malformed

    def test_empty_separator_raises_instead_of_merging_entries(self):
        """An empty (or non-string) ``separator`` must be refused before it
        reaches the ``joiner.join(parts)`` rejoin.

        ``joiner = " " if separator == "," else separator`` means a falsy
        non-comma separator becomes ``""``, and ``"".join(parts)`` fuses two
        allowed URLs into one unparseable string — silently losing a
        destination rather than gaining one.

        Teeth: remove the ``if not isinstance(separator, str) or not
        separator: raise ValueError(...)`` guard at the top of
        ``_filter_urls_by_egress_policy`` and this test fails because no
        exception is raised.
        """
        mgr = NotificationManager.__new__(NotificationManager)
        mgr._settings_snapshot = None
        with pytest.raises(ValueError, match="non-empty string"):
            mgr._filter_urls_by_egress_policy("slack://t discord://x", "")