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.0 KB
Raw Blame
"""Default comma-boundary parsing, performance, and validation alignment.

Covers embedded-comma preservation, whitespace-boundary normalization,
strict/lenient scheme partitioning, malformed-fragment refusal, and the
linear-time scan across the parser, validator, manager and service for the
default "," separator. The custom-separator pipeline and ``send``/
``send_event`` forwarding are exercised in
``test_notification_url_pipeline_custom_separator``.
"""

from __future__ import annotations

import subprocess
import sys

import pytest

from local_deep_research.notifications.manager import NotificationManager
from local_deep_research.notifications.service import NotificationService
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_parser_preserves_embedded_comma_boundary() -> None:
    # Given
    urls = "json://example.com/webhook?field=a,b, discord://webhook/token"
    expected = [
        "json://example.com/webhook?field=a,b",
        "discord://webhook/token",
    ]

    # When
    parsed, invalid_fragment = parse_notification_url_list(urls)

    # Then
    assert invalid_fragment is None
    assert parsed == expected


def test_validator_accepts_embedded_comma_boundary() -> None:
    # Given
    urls = "json://example.com/webhook?field=a,b, discord://webhook/token"

    # When
    is_valid, error = NotificationURLValidator.validate_multiple_urls(urls)

    # Then
    assert is_valid is True
    assert error is None


def test_manager_preserves_embedded_comma_boundary(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    # Given
    monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
    urls = "json://example.com/webhook?field=a,b, discord://webhook/token"
    expected = [
        "json://example.com/webhook?field=a,b",
        "discord://webhook/token",
    ]
    manager = _unprotected_manager()

    # When
    filtered = manager._filter_urls_by_egress_policy(urls)

    # Then
    assert filtered == " ".join(expected)
    filtered_entries, invalid_fragment = parse_notification_url_list(filtered)
    assert invalid_fragment is None
    assert filtered_entries == expected


def test_service_preserves_embedded_comma_boundary() -> None:
    # Given
    urls = "json://example.com/webhook?field=a,b, discord://webhook/token"
    expected = [
        "json://example.com/webhook?field=a,b",
        "discord://webhook/token",
    ]

    # When
    strict, lenient = NotificationService._partition_urls(urls)

    # Then
    assert strict == []
    assert lenient == expected


def test_partition_preserves_strict_and_lenient_urls() -> None:
    # Given
    embedded_comma_url = "json://example.com/webhook?field=a,b"
    service_urls = (
        f"https://public.example.com/hook,{embedded_comma_url},"
        "discord://id/token"
    )

    # When
    strict_urls, lenient_urls = NotificationService._partition_urls(
        service_urls
    )

    # Then
    assert strict_urls == ["https://public.example.com/hook"]
    assert lenient_urls == [embedded_comma_url, "discord://id/token"]


def test_invalid_fragment_refuses_entire_partition() -> None:
    # Given
    service_urls = "discord://id/token,example.com/webhook"

    # When
    partitions = NotificationService._partition_urls(service_urls)

    # Then
    assert partitions == ([], [])


@pytest.mark.parametrize(
    "delimiter",
    ["\v", "\f", "\u00a0", "\u2003", ",\v \f\u00a0\u2003"],
    ids=["vertical-tab", "form-feed", "nbsp", "em-space", "mixed"],
)
def test_default_parser_normalizes_all_whitespace_boundaries(
    delimiter: str,
) -> None:
    # Given
    urls = f"slack://token/path{delimiter}discord://id/token"
    expected = ["slack://token/path", "discord://id/token"]

    # When
    parsed, invalid_fragment = parse_notification_url_list(urls)

    # Then
    assert invalid_fragment is None
    assert parsed == expected


@pytest.mark.parametrize(
    "delimiter",
    ["\v", "\f", "\u00a0", "\u2003", ",\v \f\u00a0\u2003"],
    ids=["vertical-tab", "form-feed", "nbsp", "em-space", "mixed"],
)
def test_partition_normalizes_all_whitespace_boundaries(delimiter: str) -> None:
    # Given
    urls = f"slack://token/path{delimiter}discord://id/token"
    expected = ["slack://token/path", "discord://id/token"]

    # When
    strict, lenient = NotificationService._partition_urls(urls)

    # Then
    assert strict == []
    assert lenient == expected


def test_default_separator_scan_is_linear_for_long_non_boundary_run() -> None:
    # Given
    probe = (
        "from local_deep_research.security.notification_validator "
        "import parse_notification_url_list\n"
        'separator_run = ", \\t" * 49_152\n'
        'urls = f"discord://id/token?payload=start{separator_run}opaque"\n'
        "parsed, invalid_fragment = parse_notification_url_list(urls)\n"
        "assert invalid_fragment is not None\n"
        "assert parsed == [urls]\n"
    )

    # When
    completed = subprocess.run(
        [sys.executable, "-c", probe],
        check=True,
        # Generous: the probe is ~3s of interpreter start-up plus a
        # sub-millisecond scan. A quadratic scan over the 49k-separator
        # run would take minutes, so a loose bound still fails closed
        # while leaving headroom for a loaded CI runner.
        timeout=60,
    )

    # Then
    assert completed.returncode == 0


def test_manager_to_service_scan_is_linear_for_long_non_boundary_run() -> None:
    # Given
    probe = (
        "import os\n"
        'os.environ["LDR_POLICY_ALLOW_UNPROTECTED_EGRESS"] = "true"\n'
        "from local_deep_research.notifications.manager import "
        "NotificationManager\n"
        "from local_deep_research.notifications.service import "
        "NotificationService\n"
        'separator_run = ", \\t" * 49_152\n'
        'urls = f"discord://id/token?payload=start{separator_run}opaque"\n'
        "manager = NotificationManager(\n"
        "    settings_snapshot={\n"
        '        "policy.egress_scope": {"value": "unprotected"},\n'
        '        "search.tool": {"value": "searxng"},\n'
        "    },\n"
        '    user_id="subprocess-probe",\n'
        ")\n"
        "filtered = manager._filter_urls_by_egress_policy(urls)\n"
        "strict, lenient = NotificationService._partition_urls(urls)\n"
        'assert filtered == ""\n'
        "assert strict == []\n"
        "assert lenient == []\n"
    )

    # When
    completed = subprocess.run(
        [sys.executable, "-c", probe],
        check=True,
        # Generous: the probe is ~3s of interpreter start-up plus a
        # sub-millisecond scan. A quadratic scan over the 49k-separator
        # run would take minutes, so a loose bound still fails closed
        # while leaving headroom for a loaded CI runner.
        timeout=60,
    )

    # Then
    assert completed.returncode == 0