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.

5.0 KB
Raw Blame
"""The retry status buckets must agree with the reasons can_retry produces.

``RetryManager._get_resource_status`` classifies a resource by substring-matching
the reason string that ``ResourceStatusTracker.can_retry`` returned. The two
sides drifted: every reason ``can_retry`` builds is capitalized, and the match
was lowercase-only, so every non-retryable resource fell through to
``"unavailable"`` and ``FilterSummary.permanently_failed_count`` /
``temporarily_failed_count`` stayed at zero regardless of state (#6412).

The reason strings here are not written out by hand — they are taken from
``can_retry`` itself, driven through each state, so a reworded reason on either
side fails this test instead of silently emptying a counter again.
"""

from datetime import datetime, timedelta, UTC
from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.library.download_management.retry_manager import (
    RetryManager,
)
from local_deep_research.library.download_management.status_tracker import (
    MAX_TOTAL_RETRIES,
    ResourceStatusTracker,
)


def _status_record(*, cooldown=None, **fields):
    """A ResourceDownloadStatus stand-in with retry-neutral defaults."""
    now = datetime.now(UTC)
    record = MagicMock()
    record.status = "failed"
    record.failure_message = None
    record.failure_type = None
    record.retry_after_timestamp = (
        now + cooldown if cooldown is not None else None
    )
    record.today_retry_count = 0
    record.total_retry_count = 0
    record.last_attempt_at = now
    for name, value in fields.items():
        setattr(record, name, value)
    return record


def _reason_for(status_record):
    """Return the reason a real can_retry() produces for this stored state."""
    with (
        patch(
            "local_deep_research.database.encrypted_db.db_manager"
        ) as mock_db_manager,
        patch("local_deep_research.library.download_management.models.Base"),
    ):
        mock_db_manager.open_user_database.return_value = MagicMock()
        tracker = ResourceStatusTracker("test_user")

    session = MagicMock()
    session.__enter__ = MagicMock(return_value=session)
    session.__exit__ = MagicMock(return_value=False)
    session.query.return_value.filter_by.return_value.first.return_value = (
        status_record
    )

    with patch.object(tracker, "_get_session", return_value=session):
        can_retry, reason = tracker.can_retry(1)

    assert can_retry is False, (
        f"the fixture must describe a non-retryable resource, got {reason!r}"
    )
    return reason


# Each case: the stored state, and the bucket its reason has to land in.
CASES = [
    pytest.param(
        {"status": "permanently_failed", "failure_message": "File not found"},
        "permanently_failed",
        id="permanent-failure",
    ),
    pytest.param(
        {
            "status": "temporarily_failed",
            # A collection-time deadline can expire in the full suite.
            "cooldown": timedelta(hours=2),
        },
        "temporarily_failed",
        id="cooldown-active",
    ),
    pytest.param(
        {"total_retry_count": MAX_TOTAL_RETRIES},
        "permanently_failed",
        id="total-retry-limit",
    ),
    pytest.param(
        {"today_retry_count": 3},
        "unavailable",
        id="daily-retry-limit",
    ),
]


def _manager():
    with (
        patch(
            "local_deep_research.library.download_management.retry_manager.ResourceStatusTracker"
        ) as tracker_cls,
        patch(
            "local_deep_research.library.download_management.retry_manager.FailureClassifier"
        ),
    ):
        tracker_cls.return_value = MagicMock()
        return RetryManager("test_user")


@pytest.mark.parametrize("stored,expected_status", CASES)
def test_status_matches_the_reason_can_retry_actually_returns(
    stored, expected_status
):
    reason = _reason_for(_status_record(**stored))

    assert _manager()._get_resource_status(False, reason) == expected_status, (
        f"can_retry returned {reason!r}"
    )


@pytest.mark.parametrize("stored,expected_status", CASES)
def test_summary_counts_the_state_the_resource_is_really_in(
    stored, expected_status
):
    """The counters the queue-all response reports must follow the same reason."""
    reason = _reason_for(_status_record(**stored))
    manager = _manager()
    manager.status_tracker.can_retry.return_value = (False, reason)

    results = manager.filter_resources([MagicMock(id=1)])
    summary = manager.get_filter_summary(results)

    counts = {
        "permanently_failed": summary.permanently_failed_count,
        "temporarily_failed": summary.temporarily_failed_count,
        "unavailable": summary.available_count,
    }
    assert summary.total_count == 1
    assert summary.downloadable_count == 0
    assert counts.pop(expected_status) == 1, (
        f"{expected_status} was not counted for reason {reason!r}"
    )
    assert set(counts.values()) == {0}, f"double-counted: {counts}"


def test_a_retryable_resource_is_still_available():
    assert _manager()._get_resource_status(True, None) == "available"