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.

4.7 KB
Raw Blame
"""Notification policy uses the owner passed by production queue callers."""

from types import SimpleNamespace
from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.notifications.manager import (
    NotificationManager,
    NotificationReason,
)
from local_deep_research.notifications import queue_helpers
from local_deep_research.web_search_engines import (
    retriever_registry as registry_module,
)


@pytest.fixture
def registry(monkeypatch):
    monkeypatch.delenv("LDR_POLICY_EGRESS_SCOPE", raising=False)
    monkeypatch.setenv("LDR_NOTIFICATIONS_ALLOW_OUTBOUND", "true")
    monkeypatch.setattr(NotificationManager, "_shared_rate_limiter", None)
    registry = registry_module.RetrieverRegistry()
    monkeypatch.setattr(registry_module, "retriever_registry", registry)
    return registry


def _snapshot(wrapped):
    policy = {
        "policy.egress_scope": "adaptive",
        "search.tool": " owner-corpus ",
    }
    if wrapped:
        policy = {key: {"value": value} for key, value in policy.items()}
    return {
        **policy,
        "notifications.service_url": "slack://test-notification",
        "notifications.on_research_completed": True,
        "notifications.on_research_failed": True,
    }


def _send_from_session(monkeypatch, snapshot, owner, event):
    """Keep the queue caller, manager and policy real; replace storage and send."""
    session = MagicMock()
    session.query.return_value.filter_by.return_value.first.return_value = (
        SimpleNamespace(query="test research")
    )
    settings = MagicMock()
    settings.get_settings_snapshot.return_value = snapshot
    results = []
    original_send = NotificationManager.send_notification

    def capture_result(manager, *args, **kwargs):
        result = original_send(manager, *args, **kwargs)
        results.append(result)
        return result

    monkeypatch.setattr(
        NotificationManager, "send_notification", capture_result
    )
    with (
        patch(
            "local_deep_research.settings.SettingsManager",
            return_value=settings,
        ),
        patch("local_deep_research.storage.get_report_storage") as storage,
        patch(
            "local_deep_research.notifications.url_builder.build_notification_url",
            return_value="/research/test-research",
        ),
        patch(
            "local_deep_research.notifications.service.NotificationService.send_event",
            return_value=True,
        ) as dispatch,
    ):
        storage.return_value.get_report.return_value = "test report"
        if event == "completed":
            queue_helpers.send_research_completed_notification_from_session(
                owner, "test-research", session
            )
        else:
            queue_helpers.send_research_failed_notification_from_session(
                owner, "test-research", "test failure", session
            )
    assert len(results) == 1, "the real manager must complete its policy check"
    return results[0], dispatch


@pytest.mark.parametrize("event", ["completed", "failed"])
@pytest.mark.parametrize("wrapped", [False, True], ids=["flat", "wrapped"])
@pytest.mark.parametrize("namespace", ["alice", None], ids=["owned", "shared"])
@pytest.mark.parametrize("is_local", [False, True], ids=["public", "private"])
def test_queue_notification_uses_owner_without_snapshot_metadata(
    monkeypatch, registry, event, wrapped, namespace, is_local
):
    registry.register(
        "owner-corpus", MagicMock(), is_local=is_local, username=namespace
    )
    snapshot = _snapshot(wrapped)
    assert "_username" not in snapshot

    result, dispatch = _send_from_session(monkeypatch, snapshot, "alice", event)

    if is_local:
        assert result.reason is NotificationReason.EGRESS_DENIED
        dispatch.assert_not_called()
    else:
        assert result.reason is NotificationReason.SENT
        dispatch.assert_called_once()


@pytest.mark.parametrize("owner", ["alice", "bob"])
@pytest.mark.parametrize("wrapped", [False, True], ids=["flat", "wrapped"])
def test_owner_namespace_wins_over_shared_and_stale_snapshot_identity(
    monkeypatch, registry, owner, wrapped
):
    registry.register("owner-corpus", MagicMock(), is_local=False)
    registry.register(
        "owner-corpus", MagicMock(), is_local=True, username="alice"
    )
    snapshot = _snapshot(wrapped)
    snapshot["_username"] = "bob" if owner == "alice" else "alice"

    result, dispatch = _send_from_session(
        monkeypatch, snapshot, owner, "completed"
    )

    if owner == "alice":
        assert result.reason is NotificationReason.EGRESS_DENIED
        dispatch.assert_not_called()
    else:
        assert result.reason is NotificationReason.SENT
        dispatch.assert_called_once()