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.

38.1 KB
Raw Blame
"""
Tests for NotificationService with Tenacity retry logic.
"""

import pytest
from unittest.mock import patch, MagicMock

from local_deep_research.notifications.service import (
    NotificationService,
    MAX_NOTIFICATION_TARGETS,
    MAX_RETRY_ATTEMPTS,
    INITIAL_RETRY_DELAY,
    RETRY_BACKOFF_MULTIPLIER,
)
from local_deep_research.notifications.templates import EventType
from local_deep_research.notifications.exceptions import (
    SendError,
    ServiceError,
)
from local_deep_research.security.notification_validator import (
    NotificationURLValidator,
)


class TestNotificationServiceInit:
    """Tests for NotificationService initialization."""

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_init_creates_hardened_apprise_instance(self, mock_apprise_class):
        """Test initialization creates Apprise instance."""
        service = NotificationService(outbound_allowed=True)
        assert service.apprise is not None
        mock_apprise_class.assert_called_once()
        assert (
            mock_apprise_class.call_args.kwargs["asset"].http_redirects is False
        )


class TestSendOutboundGate:
    """Tests for the operator-level master switch enforced inside send().

    Defense-in-depth: the gate is also checked in NotificationManager and
    NotificationService.test_service. Re-checking inside send() means a
    direct caller (now or in the future) cannot bypass it. See SECURITY.md
    'Notification Webhook SSRF'.
    """

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_send_returns_false_when_outbound_disallowed(
        self, mock_apprise_class
    ):
        """send() must bail out before adding a service or notifying."""
        mock_apprise_instance = MagicMock()
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(outbound_allowed=False)

        result = service.send(
            title="t",
            body="b",
            service_urls="discord://webhook/token",
        )

        assert result is False
        # Apprise should never be touched once the gate refuses.
        mock_apprise_instance.notify.assert_not_called()
        mock_apprise_instance.add.assert_not_called()

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_send_proceeds_when_outbound_allowed(self, mock_apprise_class):
        """Sanity check: gate open => normal send path runs."""
        mock_apprise_instance = MagicMock()
        mock_apprise_instance.notify.return_value = True
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(outbound_allowed=True)

        result = service.send(
            title="t",
            body="b",
            service_urls="discord://webhook/token",
        )

        assert result is True
        assert mock_apprise_instance.notify.call_count == 1
        assert mock_apprise_class.call_count == 2
        assert all(
            call.kwargs["asset"].http_redirects is False
            for call in mock_apprise_class.call_args_list
        )

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_send_rejects_template_before_apprise_add(self, mock_apprise_class):
        mock_apprise_instance = MagicMock()
        mock_apprise_class.return_value = mock_apprise_instance
        service = NotificationService(outbound_allowed=True)

        with pytest.raises(
            ServiceError,
            match="Blocked unsafe notification parameter: template",
        ):
            service.send(
                title="t",
                body="b",
                service_urls=(
                    "discord://123456789012345678/token"
                    "?template=file%3A%2F%2F%2Fetc%2Fpasswd"
                ),
            )

        assert mock_apprise_class.call_count == 1
        mock_apprise_instance.add.assert_not_called()

    @pytest.mark.parametrize(
        ("url", "blocked_key"),
        [
            pytest.param(
                "https://discord.com/api/webhooks/123456789012345678/token"
                "?template=file%3A%2F%2F%2Fetc%2Fpasswd",
                "template",
                id="native-discord-template",
            ),
            pytest.param(
                "https://hooks.example.com/path?redirect=yes",
                "redirect",
                id="native-https-redirect",
            ),
        ],
    )
    def test_send_rejects_unsafe_native_https_option_before_dns_and_add(
        self, url, blocked_key
    ):
        with patch(
            "local_deep_research.notifications.service.apprise.Apprise"
        ) as mock_apprise_class:
            mock_apprise_instance = MagicMock()
            mock_apprise_class.return_value = mock_apprise_instance
            service = NotificationService(outbound_allowed=True)

            with patch.object(
                NotificationURLValidator, "_resolve_hostname_ips"
            ) as resolver:
                with pytest.raises(
                    ServiceError,
                    match=(
                        "Blocked unsafe notification parameter: " + blocked_key
                    ),
                ):
                    service.send(title="t", body="b", service_urls=url)

        resolver.assert_not_called()
        assert mock_apprise_class.call_count == 1
        mock_apprise_instance.add.assert_not_called()


class TestSendWithTenacity:
    """Tests for send method with Tenacity retry logic."""

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_send_success_first_attempt(self, mock_apprise_class):
        """Test successful notification on first attempt."""
        mock_apprise_instance = MagicMock()
        mock_apprise_instance.notify.return_value = True
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(outbound_allowed=True)

        result = service.send(
            title="Test Title",
            body="Test Body",
            service_urls="discord://webhook/token",
        )

        assert result is True
        assert mock_apprise_instance.notify.call_count == 1

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_send_retries_on_failure_with_tenacity(self, mock_apprise_class):
        """Test Tenacity handles exponential backoff retry automatically."""
        mock_apprise_instance = MagicMock()
        mock_apprise_instance.add.return_value = True
        # Fail twice, succeed on third attempt
        mock_apprise_instance.notify.side_effect = [False, False, True]
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(outbound_allowed=True)

        result = service.send(
            title="Test",
            body="Body",
            service_urls="discord://webhook/token",
        )

        assert result is True
        # Tenacity should have retried 3 times total (1 initial + 2 retries)
        assert mock_apprise_instance.notify.call_count == 3

        # Verify the Tenacity decorator was applied
        assert hasattr(service._send_with_retry, "__wrapped__")

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_send_raises_after_max_retries_with_tenacity(
        self, mock_apprise_class
    ):
        """Test Tenacity raises exception after max retry attempts."""
        mock_apprise_instance = MagicMock()
        mock_apprise_instance.add.return_value = True
        # Always fail
        mock_apprise_instance.notify.return_value = False
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(outbound_allowed=True)

        with pytest.raises(SendError, match="Failed to send notification"):
            service.send(
                title="Test",
                body="Body",
                service_urls="discord://webhook/token",
            )

        # Tenacity should have tried 3 times (MAX_RETRY_ATTEMPTS)
        assert mock_apprise_instance.notify.call_count == MAX_RETRY_ATTEMPTS

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_send_handles_exception_with_retry_with_tenacity(
        self, mock_apprise_class
    ):
        """Test Tenacity handles exception retries automatically."""
        mock_apprise_instance = MagicMock()
        mock_apprise_instance.add.return_value = True
        # Raise exception twice, succeed on third attempt
        mock_apprise_instance.notify.side_effect = [
            Exception("Network error"),
            Exception("Network error"),
            True,
        ]
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(outbound_allowed=True)

        result = service.send(
            title="Test",
            body="Body",
            service_urls="discord://webhook/token",
        )

        assert result is True
        # Tenacity should have retried on exceptions and succeeded
        assert mock_apprise_instance.notify.call_count == 3

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_send_with_no_service_urls(self, mock_apprise_class):
        """Test send returns False when configured instance empty."""
        service = NotificationService(outbound_allowed=True)
        # Don't provide service_urls, use configured instance

        result = service.send(
            title="Test",
            body="Body",
            # No service_urls parameter
        )

        assert result is False


class TestTenacityConfiguration:
    """Tests for Tenacity retry configuration."""

    def test_retry_constants_are_defined(self):
        """Test backward compatibility constants are still available."""
        assert MAX_RETRY_ATTEMPTS == 3
        assert INITIAL_RETRY_DELAY == 0.5
        assert RETRY_BACKOFF_MULTIPLIER == 2

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_tenacity_retry_configuration(self, mock_apprise_class):
        """Test Tenacity is configured with correct retry parameters."""
        mock_apprise_instance = MagicMock()
        mock_apprise_instance.add.return_value = True
        mock_apprise_instance.notify.return_value = False  # Always fail
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(outbound_allowed=True)

        # Verify the retry decorator is applied
        assert hasattr(service._send_with_retry, "__wrapped__")

        # Check that the retry configuration matches our constants
        # (We can't easily inspect the decorator config, so we verify behavior)
        with pytest.raises(SendError):
            service.send(
                title="Test",
                body="Body",
                service_urls="discord://webhook/token",
            )

        # Should have tried exactly MAX_RETRY_ATTEMPTS times
        assert mock_apprise_instance.notify.call_count == MAX_RETRY_ATTEMPTS


class TestSendEvent:
    """Tests for send_event method."""

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_send_event_formats_template(self, mock_apprise_class):
        """Test send_event formats message using template."""
        mock_apprise_instance = MagicMock()
        mock_apprise_instance.notify.return_value = True
        mock_apprise_instance.add.return_value = True
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(outbound_allowed=True)

        # Include all required template variables
        context = {
            "query": "What is quantum computing?",
            "research_id": "123",
            "summary": "Brief summary",
            "url": "http://localhost:5000/research/123",
        }

        result = service.send_event(
            event_type=EventType.RESEARCH_COMPLETED,
            context=context,
            service_urls="discord://webhook/token",
        )

        assert result is True

        # Verify notify was called with formatted message
        call_kwargs = mock_apprise_instance.notify.call_args[1]
        assert "title" in call_kwargs
        assert "body" in call_kwargs
        # Title should contain the query (from template: "Research Completed: {query}")
        assert "quantum computing" in call_kwargs["title"].lower()
        assert "research completed" in call_kwargs["title"].lower()


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

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_test_service_success(self, mock_apprise_class):
        """Test successful service test.

        Also pins the SHAPE of the ``temp_apprise.add`` argument: the
        parsed entry LIST, never the raw setting string. Teeth: revert
        ``temp_apprise.add(list(url_entries))`` to ``temp_apprise.add(url)``
        in ``NotificationService.test_service`` and the argument is a
        ``str``, so the list assertion below fails. That revert matters
        because ``add()`` runs Apprise's own ``parse_urls`` splitter ONLY
        on a ``str``: with a string in the path, LDR's boundary regex and
        Apprise's ``URL_DETECTION_RE`` each get a vote on where entries
        start and they disagree (a LEADING DIGIT starts a new entry for
        Apprise but not for LDR), so ``discord://a/b,7z://x`` is one
        validated entry to LDR and two dispatched targets to Apprise.
        """
        mock_apprise_instance = MagicMock()
        mock_apprise_instance.add.return_value = True
        mock_apprise_instance.notify.return_value = True
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(outbound_allowed=True)

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

        assert result["success"] is True
        assert "message" in result
        # Verify test notification was sent
        mock_apprise_instance.notify.assert_called_once()
        assert mock_apprise_class.call_count == 2
        assert all(
            call.kwargs["asset"].http_redirects is False
            for call in mock_apprise_class.call_args_list
        )
        # Apprise is handed the already-parsed entries, as a list.
        add_arg = mock_apprise_instance.add.call_args[0][0]
        assert add_arg == ["discord://webhook/token"]
        assert isinstance(add_arg, list)

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_test_service_rejects_template_before_apprise_add(
        self, mock_apprise_class
    ):
        mock_apprise_instance = MagicMock()
        mock_apprise_class.return_value = mock_apprise_instance
        service = NotificationService(outbound_allowed=True)

        result = service.test_service(
            "slack://T00000000/B00000000/token"
            "?template=http%3A%2F%2F169.254.169.254%2Fmetadata"
        )

        assert result == {
            "success": False,
            "error": "Blocked unsafe notification parameter: template",
        }
        assert mock_apprise_class.call_count == 1
        mock_apprise_instance.add.assert_not_called()

    def test_test_service_exception(self):
        """Test service test handles exceptions."""
        service = NotificationService(outbound_allowed=True)

        # Mock the internal apprise.Apprise to raise exception
        with patch(
            "local_deep_research.notifications.service.apprise.Apprise"
        ) as mock_apprise:
            mock_instance = MagicMock()
            mock_instance.add.side_effect = Exception("Network error")
            mock_apprise.return_value = mock_instance

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

            assert result["success"] is False
            assert "error" in result
            # Exception should be caught and returned in error field
            assert len(result["error"]) > 0


class TestTestServiceMultiUrl:
    """``test_service()`` must validate EVERY entry of a multi-URL input,
    not the joined string as a single URL (issue #5120).

    ``url`` is one form field, but Apprise's own scheme-aware splitter
    treats it as a list, and commas are legal URL characters. Validating
    it as a single URL checked only the leading entry while Apprise still
    registered — and notified — the rest.
    """

    ATTACK_URL = (
        "discord://123456789012345678/abcdefghijklmnop"
        ",json://169.254.169.254/metadata"
    )

    @patch("local_deep_research.notifications.service.apprise.Apprise.notify")
    def test_comma_smuggled_metadata_url_is_rejected(self, mock_notify):
        """A single-URL validator sees only the valid-looking discord
        entry and lets the smuggled cloud-metadata URL through to
        Apprise. It must be rejected before Apprise is asked to notify.
        """
        service = NotificationService(
            outbound_allowed=True, allow_private_ips=False
        )

        result = service.test_service(self.ATTACK_URL)

        assert result["success"] is False
        assert "169.254.169.254" in result["error"]
        mock_notify.assert_not_called()

    @patch("local_deep_research.notifications.service.apprise.Apprise.notify")
    def test_space_smuggled_metadata_url_is_rejected(self, mock_notify):
        """Whitespace is a separator for Apprise too, so it must be one
        for the validator."""
        service = NotificationService(
            outbound_allowed=True, allow_private_ips=False
        )

        result = service.test_service(self.ATTACK_URL.replace(",", " "))

        assert result["success"] is False
        assert "169.254.169.254" in result["error"]
        mock_notify.assert_not_called()

    def test_single_invalid_url_still_fails_with_validator_message(self):
        """Single-URL behavior is unchanged: a lone cloud-metadata URL
        fails with the real validator's message."""
        service = NotificationService(
            outbound_allowed=True, allow_private_ips=False
        )

        result = service.test_service("json://169.254.169.254/metadata")

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

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_all_valid_multi_url_passes_and_sends(self, mock_apprise_class):
        """Per-entry validation must not regress the legitimate
        multi-URL case: when every entry is a valid vendor URL the test
        notification is still sent."""
        mock_apprise_instance = MagicMock()
        mock_apprise_instance.add.return_value = True
        mock_apprise_instance.notify.return_value = True
        # A bare MagicMock reports len() == 0, which would make the
        # parser-differential guard below compare ``0 > 2`` and pass
        # vacuously. Report the honest registered-target count so this
        # test exercises the guard's non-differential branch for real.
        mock_apprise_instance.__len__.return_value = 2
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(
            outbound_allowed=True, allow_private_ips=False
        )

        result = service.test_service(
            "discord://123456789012345678/abcdefghijklmnop"
            ",mailto://[email protected]"
        )

        assert result["success"] is True
        mock_apprise_instance.notify.assert_called_once()

    @patch(
        "local_deep_research.notifications.service"
        ".NotificationURLValidator.validate_service_url_with_hint",
        return_value=(True, None, False),
    )
    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_comma_inside_one_url_is_not_split(
        self, mock_apprise_class, mock_validate
    ):
        """A comma that is part of a single service URL must not be
        treated as a separator — that is what made the naive split
        unusable in the first place."""
        mock_apprise_instance = MagicMock()
        mock_apprise_instance.add.return_value = True
        mock_apprise_instance.notify.return_value = True
        # See the note in the multi-URL happy path: without an explicit
        # __len__ the parser-differential guard is not exercised at all.
        mock_apprise_instance.__len__.return_value = 1
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(outbound_allowed=True)

        result = service.test_service("discord://webhook/token?fields=a,b")

        assert result["success"] is True
        # One entry -> one validation call, with the comma preserved.
        mock_validate.assert_called_once()
        assert "a,b" in mock_validate.call_args[0][0]

    @patch("local_deep_research.notifications.service.apprise.Apprise.notify")
    def test_target_count_is_capped(self, mock_notify):
        """An unbounded target list would let one authenticated request
        trigger unbounded DNS resolution / outbound fan-out."""
        service = NotificationService(outbound_allowed=True)

        many = ",".join(
            f"discord://12345678901234567{n}/abcdefghijklmnop"
            for n in range(MAX_NOTIFICATION_TARGETS + 1)
        )
        result = service.test_service(many)

        assert result["success"] is False
        assert "Too many notification targets" in result["error"]
        mock_notify.assert_not_called()


class TestTestServiceParserDifferential:
    """``test_service()`` fails closed when Apprise registers MORE
    targets than were validated (issue #5120).

    Regression guard for a VACUOUS test: a bare ``MagicMock`` reports
    ``len() == 0``, so ``len(temp_apprise) > len(url_entries)`` compares
    ``0 > N`` and the fail-closed branch is never entered. Every test
    here sets ``__len__`` explicitly so the comparison is real, and the
    equal/fewer cases pin the guard to ``>`` (an inversion to ``>=`` or
    ``<`` breaks them).

    The validator is stubbed so these tests isolate the differential
    guard itself: every entry is treated as having passed SSRF
    validation, which is exactly the state in which a smuggled extra
    Apprise target would otherwise be notified unvetted.
    """

    TWO_VALID_URLS = (
        "discord://123456789012345678/abcdefghijklmnop"
        ",discord://223456789012345678/abcdefghijklmnop"
    )

    @staticmethod
    def _mock_apprise(mock_apprise_class, registered_targets):
        instance = MagicMock()
        instance.add.return_value = True
        instance.notify.return_value = True
        instance.__len__.return_value = registered_targets
        mock_apprise_class.return_value = instance
        return instance

    @patch(
        "local_deep_research.notifications.service"
        ".NotificationURLValidator.validate_service_url_with_hint",
        return_value=(True, None, False),
    )
    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_more_registered_than_validated_is_refused(
        self, mock_apprise_class, mock_validate
    ):
        """Apprise registering a third target for a two-entry input means
        one destination never passed the SSRF validator. Refuse the send.
        """
        mock_apprise = self._mock_apprise(mock_apprise_class, 3)

        service = NotificationService(
            outbound_allowed=True, allow_private_ips=False
        )

        result = service.test_service(self.TWO_VALID_URLS)

        assert result["success"] is False
        assert "could not be parsed unambiguously" in result["error"]
        mock_apprise.notify.assert_not_called()
        # Two entries went to the validator; Apprise claimed three.
        assert mock_validate.call_count == 2
        assert len(mock_apprise) == 3

    @patch(
        "local_deep_research.notifications.service"
        ".NotificationURLValidator.validate_service_url_with_hint",
        return_value=(True, None, False),
    )
    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_single_entry_expanded_by_apprise_is_refused(
        self, mock_apprise_class, mock_validate
    ):
        """The smuggling shape the guard exists for: one validated entry,
        two registered targets."""
        mock_apprise = self._mock_apprise(mock_apprise_class, 2)

        service = NotificationService(
            outbound_allowed=True, allow_private_ips=False
        )

        result = service.test_service(
            "discord://123456789012345678/abcdefghijklmnop"
        )

        assert result["success"] is False
        assert "could not be parsed unambiguously" in result["error"]
        mock_apprise.notify.assert_not_called()

    @patch(
        "local_deep_research.notifications.service"
        ".NotificationURLValidator.validate_service_url_with_hint",
        return_value=(True, None, False),
    )
    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_equal_counts_still_send(self, mock_apprise_class, mock_validate):
        """Equal counts are the normal case and must NOT be refused —
        pins the comparison to ``>`` rather than ``>=``."""
        mock_apprise = self._mock_apprise(mock_apprise_class, 2)

        service = NotificationService(
            outbound_allowed=True, allow_private_ips=False
        )

        result = service.test_service(self.TWO_VALID_URLS)

        assert result["success"] is True
        mock_apprise.notify.assert_called_once()

    @patch(
        "local_deep_research.notifications.service"
        ".NotificationURLValidator.validate_service_url_with_hint",
        return_value=(True, None, False),
    )
    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_fewer_registered_than_validated_still_sends(
        self, mock_apprise_class, mock_validate
    ):
        """Apprise dropping an entry is harmless: only the smuggling
        direction is refused."""
        mock_apprise = self._mock_apprise(mock_apprise_class, 1)

        service = NotificationService(
            outbound_allowed=True, allow_private_ips=False
        )

        result = service.test_service(self.TWO_VALID_URLS)

        assert result["success"] is True
        mock_apprise.notify.assert_called_once()

    def test_bare_magicmock_len_is_zero(self):
        """Documents WHY every test above sets ``__len__``: an
        unconfigured MagicMock reports zero registered targets, which
        would let the guard pass without ever being evaluated against a
        real count."""
        assert len(MagicMock()) == 0


class TestTestServiceInvalidFragmentRefusal:
    r"""``test_service()`` must REFUSE outright when
    ``parse_notification_url_list`` reports an ``invalid_fragment`` —
    it must never validate the fragment IN PLACE OF the input it was
    carved out of.

    The fragment is cut on ``RFC_FORBIDDEN_URL_CHARS_RE``
    (``[\\\s\x00-\x1f\x7f]`` — backslash and every control byte), but the
    entry boundary regex is ``[,\s]+``. Backslash and the non-``\s``
    control bytes are therefore in the fragment class but NOT in the
    boundary class, so ``a://h1/p\https://h2/q`` stays ONE parsed entry
    whose fragment is ``https://h2/q``. The old code substituted
    ``url_entries = [invalid_fragment]``, which made that fragment a
    dispatchable DECOY:

    * only the decoy was validated by the per-entry loop;
    * ``temp_apprise.add(url)`` then added the RAW ORIGINAL string, so
      the real destination was ``h1``;
    * the smuggle guard compared ``len(temp_apprise) > len(url_entries)``
      — ``1 > 1`` — and stayed silent;
    * the pin / block-private policy (``has_http``, ``block_link_local``)
      was derived from the DECOY's scheme, so a link-local target rode
      out under ``block_link_local=False``.

    Under the old (pre-#5113-follow-up) parser this shape was hard-
    rejected because the backslash tripped ``validate_service_url``'s own
    RFC check, so the substitution is a regression introduced by widening
    the parser — closed here at the consumer, not by weakening the parser.
    """

    # A hostile link-local (metadata-style) target hidden behind
    # a perfectly valid Discord webhook decoy, joined by a backslash.
    BACKSLASH_DECOY_URL = (
        "json://169.254.10.5/x"
        "\\https://discord.com/api/webhooks/123456789/abcdefghijklmnop"
    )

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_backslash_smuggled_decoy_fragment_is_refused(
        self, mock_apprise_class
    ):
        r"""Teeth: restore ``url_entries = [invalid_fragment]`` in
        ``NotificationService.test_service`` and the per-entry loop
        validates only the ``https://discord.com/...`` decoy, which
        passes — so ``temp_apprise.add`` IS reached and
        ``result["success"]`` is True, failing every assertion below.

        With ONLY that revert, ``add(list(url_entries))`` hands Apprise
        the decoy, so the send goes to ``discord.com`` rather than to the
        link-local target. Reaching the raw
        ``json://169.254.10.5/x\https://...`` string — the original
        defect, where the link-local host is what actually gets notified
        — takes the SECOND revert as well: ``add(list(url_entries))``
        back to ``add(url)``. That is the pairing the class docstring
        above describes; either half alone is enough to fail this test.
        """
        mock_apprise_instance = MagicMock()
        mock_apprise_instance.add.return_value = True
        mock_apprise_instance.notify.return_value = True
        mock_apprise_instance.__len__.return_value = 1
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(
            outbound_allowed=True, allow_private_ips=True
        )

        result = service.test_service(self.BACKSLASH_DECOY_URL)

        assert result["success"] is False
        assert "could not be parsed unambiguously" in result["error"]
        # The dispatch side must never be reached: Apprise would have
        # been handed the RAW original string, not the validated decoy.
        mock_apprise_instance.add.assert_not_called()
        mock_apprise_instance.notify.assert_not_called()
        # And nothing derived from the input is echoed back to the user.
        assert "169.254.10.5" not in result["error"]
        assert "discord.com" not in result["error"]

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_trailing_forbidden_byte_is_refused(self, mock_apprise_class):
        r"""The ``else entry`` branch of the parser: when the illegal
        character only TRAILS the entry there is no token after it, so the
        WHOLE entry comes back as the fragment.

        Teeth (narrow, and NOT the ``add``/``notify`` assertions): restore
        ``url_entries = [invalid_fragment]`` and the fragment IS the entry,
        so the loop validates ``"slack://tokenA/tokenB/tokenC/\\"`` — and
        the REAL ``validate_service_url_with_hint`` rejects it on the
        trailing backslash (``RFC_FORBIDDEN_URL_CHARS_RE``) before Apprise
        is touched. So ``add``/``notify`` stay uncalled on the revert too;
        what actually breaks is the MESSAGE assertion: the user would get
        the validator's "URL contains characters that are not allowed"
        instead of this path's "could not be parsed unambiguously", i.e.
        a per-entry complaint about a decoy rather than a refusal of the
        whole input. The ``add``/``notify`` assertions are kept as
        belt-and-braces for a revert that ALSO loosens the validator.
        ``test_fragment_that_would_pass_validation_is_still_refused``
        below is the sibling that keeps teeth on the dispatch itself, by
        stubbing the validator open.
        """
        mock_apprise_instance = MagicMock()
        mock_apprise_instance.add.return_value = True
        mock_apprise_instance.notify.return_value = True
        mock_apprise_instance.__len__.return_value = 1
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(outbound_allowed=True)

        result = service.test_service("slack://tokenA/tokenB/tokenC/\\")

        assert result["success"] is False
        assert "could not be parsed unambiguously" in result["error"]
        mock_apprise_instance.add.assert_not_called()
        mock_apprise_instance.notify.assert_not_called()

    @patch(
        "local_deep_research.notifications.service"
        ".NotificationURLValidator.validate_service_url_with_hint"
    )
    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_fragment_that_would_pass_validation_is_still_refused(
        self, mock_apprise_class, mock_validate
    ):
        """The refusal must NOT depend on the fragment failing
        validation. A scheme-carrying fragment can be a perfectly valid
        URL on its own — that is exactly what makes it a usable decoy.

        The validator is stubbed to accept EVERYTHING here, so the only
        thing that can refuse this input is the fragment check itself.

        Teeth: restore ``url_entries = [invalid_fragment]``; with the
        validator accepting every entry the flow reaches
        ``temp_apprise.add`` and returns success, failing every assertion
        below. (This also pins the weaker fix of merely making the
        validation loop reject fragments: with validation stubbed open,
        that fix would not refuse this input either.)
        """
        mock_validate.return_value = (True, None, False)
        mock_apprise_instance = MagicMock()
        mock_apprise_instance.add.return_value = True
        mock_apprise_instance.notify.return_value = True
        mock_apprise_instance.__len__.return_value = 1
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(outbound_allowed=True)

        result = service.test_service(
            "bark://internal.invalid/key\\https://public.example/hook"
        )

        assert result["success"] is False
        assert "could not be parsed unambiguously" in result["error"]
        mock_validate.assert_not_called()
        mock_apprise_instance.add.assert_not_called()
        mock_apprise_instance.notify.assert_not_called()


class TestSendMultiUrlSsrf:
    """The configured-dispatch path (``send()``) must also validate every
    entry Apprise parses out of a multi-URL string, so a comma/space
    smuggled cloud-metadata target cannot reach dispatch behind a
    valid-looking leading entry. Exercises the REAL validator and the
    REAL parser; only ``notify`` is mocked."""

    ATTACK_URL = (
        "discord://123456789012345678/abcdefghijklmnop"
        ",json://169.254.169.254/metadata"
    )

    @patch("local_deep_research.notifications.service.apprise.Apprise.notify")
    def test_send_rejects_comma_smuggled_metadata_url(self, mock_notify):
        service = NotificationService(
            outbound_allowed=True, allow_private_ips=False
        )

        with pytest.raises(ServiceError):
            service.send("Title", "Body", service_urls=self.ATTACK_URL)

        mock_notify.assert_not_called()

    @patch("local_deep_research.notifications.service.apprise.Apprise.notify")
    def test_send_rejects_space_smuggled_metadata_url(self, mock_notify):
        service = NotificationService(
            outbound_allowed=True, allow_private_ips=False
        )

        with pytest.raises(ServiceError):
            service.send(
                "Title", "Body", service_urls=self.ATTACK_URL.replace(",", " ")
            )

        mock_notify.assert_not_called()


class TestGetServiceType:
    """Tests for get_service_type method."""

    def test_get_service_type_discord(self):
        """Test detecting Discord service."""
        service = NotificationService(outbound_allowed=True)
        service_type = service.get_service_type("discord://webhook/token")
        assert service_type == "discord"

    def test_get_service_type_unknown(self):
        """Test unknown service type."""
        service = NotificationService(outbound_allowed=True)
        service_type = service.get_service_type("unknown://service")
        assert service_type == "unknown"


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

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    def test_complete_notification_flow(self, mock_apprise_class):
        """Test complete notification flow from event to send."""
        mock_apprise_instance = MagicMock()
        mock_apprise_instance.add.return_value = True
        mock_apprise_instance.notify.return_value = True
        mock_apprise_class.return_value = mock_apprise_instance

        service = NotificationService(outbound_allowed=True)

        context = {
            "query": "Test research query",
            "research_id": "test-123",
            "summary": "Test summary",
            "url": "http://localhost:5000/research/test-123",
        }

        result = service.send_event(
            event_type=EventType.RESEARCH_COMPLETED,
            context=context,
            service_urls="discord://webhook/token",
        )

        assert result is True
        mock_apprise_instance.notify.assert_called_once()

        # Verify the formatted message
        call_args = mock_apprise_instance.notify.call_args
        title = call_args[1]["title"]
        body = call_args[1]["body"]

        assert "Test research query" in title
        assert "Test summary" in body
        assert "http://localhost:5000/research/test-123" in body


class TestPartitionUrlsMalformedFragmentLog:
    """``_partition_urls`` refuses a list with a malformed fragment and
    logs content-free diagnostics that production sinks can display."""

    def test_malformed_fragment_log_renders_diagnostics(
        self, capture_loguru_records
    ):
        """Teeth: pass ``fragment_length`` / ``entries_parsed`` as bare
        loguru kwargs again and the rendered-message assertions fail —
        no production sink formats ``extra`` (#5619). Log the fragment
        and the ``secret.example.com`` assertions fail."""
        fragment = "secret.example.com/private/token"

        assert NotificationService._partition_urls(
            f"{fragment} slack://t/x/y"
        ) == ([], [])

        warnings = [
            r
            for r in capture_loguru_records
            if "contains a malformed fragment" in r["message"]
        ]
        assert len(warnings) == 1
        rendered = warnings[0]["message"]
        assert f"fragment_length={len(fragment)}" in rendered
        assert "entries_parsed=2" in rendered
        for text in (rendered, str(warnings[0]["extra"])):
            assert "secret.example.com" not in text
            assert "/private/token" not in text