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.

7.5 KB
Raw Blame
"""Custom separator pipeline and ``send``/``send_event`` forwarding.

Covers a non-default separator staying consistent across parsing, the
manager, and dispatch: embedded-comma data preserved inside custom-separated
entries, ``send``/``send_event`` threading the separator through to
partitioning, scheme-less and IPv6-fragment refusal, and policy-evaluation
failure. Default comma parsing and its performance regressions live in
``test_notification_url_pipeline_default_parser``.
"""

from __future__ import annotations

from unittest.mock import Mock

import pytest

from local_deep_research.notifications.manager import NotificationManager
from local_deep_research.notifications.service import NotificationService
from local_deep_research.notifications.templates import EventType
from local_deep_research.security.notification_validator import (
    NotificationURLValidator,
    parse_notification_url_list,
)


def _unprotected_manager() -> NotificationManager:
    return NotificationManager(
        settings_snapshot={
            "policy.egress_scope": {"value": "unprotected"},
            "search.tool": {"value": "searxng"},
        },
        user_id="u",
    )


def test_custom_separator_is_shared_across_pipeline() -> None:
    # Given
    urls = "slack://token/path|discord://id/token"
    expected = ["slack://token/path", "discord://id/token"]
    manager = _unprotected_manager()

    # When
    parsed, invalid_fragment = parse_notification_url_list(urls, separator="|")
    is_valid, error = NotificationURLValidator.validate_multiple_urls(
        urls, separator="|"
    )
    filtered = manager._filter_urls_by_egress_policy(urls, separator="|")
    strict, lenient = NotificationService._partition_urls(urls, separator="|")

    # Then
    assert invalid_fragment is None
    assert parsed == expected
    assert is_valid is True
    assert error is None
    assert filtered == "|".join(expected)
    assert strict == []
    assert lenient == expected


def test_custom_separator_preserves_embedded_comma_data(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    # Given
    monkeypatch.setattr(
        NotificationURLValidator,
        "_resolve_hostname_ips",
        Mock(return_value=[]),
    )
    comma_bearing_url = (
        "json://example.com/webhook?+X-Hosts=api.example.com,backup.example.com"
    )
    urls = f"{comma_bearing_url}|discord://id/token"
    expected = [comma_bearing_url, "discord://id/token"]

    # When
    parsed, invalid_fragment = parse_notification_url_list(urls, separator="|")
    is_valid, error = NotificationURLValidator.validate_multiple_urls(
        urls, separator="|"
    )
    strict, lenient = NotificationService._partition_urls(urls, separator="|")

    # Then
    assert invalid_fragment is None
    assert parsed == expected
    assert is_valid is True
    assert error is None
    assert strict == []
    assert lenient == expected


def test_manager_output_reuses_custom_separator(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    # Given
    monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
    urls = "slack://token/path|discord://id/token"
    expected = ["slack://token/path", "discord://id/token"]
    manager = _unprotected_manager()

    # When
    filtered = manager._filter_urls_by_egress_policy(urls, separator="|")
    strict, lenient = NotificationService._partition_urls(
        filtered, separator="|"
    )

    # Then
    assert filtered == "|".join(expected)
    assert strict == []
    assert lenient == expected


def test_send_threads_custom_separator_to_dispatch(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    # Given
    service_urls = "json://example.com/hook?field=a,b|discord://id/token"
    lenient_urls = [
        "json://example.com/hook?field=a,b",
        "discord://id/token",
    ]
    service = NotificationService(outbound_allowed=True)
    validate = Mock(return_value=(True, None))
    partition = Mock(return_value=([], lenient_urls))
    dispatch = Mock(return_value=True)
    monkeypatch.setattr(
        NotificationURLValidator, "validate_multiple_urls", validate
    )
    monkeypatch.setattr(service, "_partition_urls", partition)
    monkeypatch.setattr(service, "_dispatch", dispatch)

    # When
    sent = service.send(
        title="title",
        body="body",
        service_urls=service_urls,
        separator="|",
    )

    # Then
    assert sent is True
    validate.assert_called_once_with(
        service_urls,
        allow_private_ips=False,
        separator="|",
    )
    partition.assert_called_once_with(service_urls, "|")
    dispatch.assert_called_once_with(
        "title", "body", [], lenient_urls, None, None
    )


def test_send_event_threads_custom_separator_to_send(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    # Given
    service_urls = "slack://token/path|discord://id/token"
    context = {
        "query": "query",
        "research_id": "research-id",
        "summary": "summary",
        "url": "https://example.com/research/research-id",
    }
    service = NotificationService(outbound_allowed=True)
    send = Mock(return_value=True)
    monkeypatch.setattr(service, "send", send)

    # When
    sent = service.send_event(
        event_type=EventType.RESEARCH_COMPLETED,
        context=context,
        service_urls=service_urls,
        separator="|",
    )

    # Then
    assert sent is True
    send.assert_called_once()
    assert send.call_args.kwargs["service_urls"] == service_urls
    assert send.call_args.kwargs["separator"] == "|"


def test_custom_separator_ipv6_fragment_fails_closed(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    # Given
    monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
    urls = "discord://id/token|[2001:db8::1]:8080/webhook"
    manager = _unprotected_manager()

    # When
    parsed, invalid_fragment = parse_notification_url_list(urls, separator="|")
    is_valid, error = NotificationURLValidator.validate_multiple_urls(
        urls, separator="|"
    )
    filtered = manager._filter_urls_by_egress_policy(urls, separator="|")
    strict, lenient = NotificationService._partition_urls(urls, separator="|")

    # Then
    assert invalid_fragment == "[2001:db8::1]:8080/webhook"
    assert parsed == ["discord://id/token", "[2001:db8::1]:8080/webhook"]
    assert is_valid is False
    assert error is not None
    assert "must have a protocol" in error
    assert filtered == ""
    assert strict == []
    assert lenient == []


def test_custom_separator_preserves_policy_evaluation_failure(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
    manager = _unprotected_manager()
    with monkeypatch.context() as patcher:
        from local_deep_research.security.egress import policy

        patcher.setattr(
            policy,
            "context_from_snapshot",
            Mock(side_effect=ValueError("bad policy")),
        )
        filtered = manager._filter_urls_by_egress_policy(
            "slack://token/path|discord://id/token", separator="|"
        )
    assert filtered is None


def test_send_refuses_custom_separator_fragment_before_dispatch(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    from local_deep_research.notifications.exceptions import ServiceError

    service = NotificationService(outbound_allowed=True)
    dispatch = Mock()
    monkeypatch.setattr(service, "_dispatch", dispatch)
    with pytest.raises(ServiceError):
        service.send(
            "title",
            "body",
            service_urls="slack://token/path|::1/x",
            separator="|",
        )
    dispatch.assert_not_called()