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.9 KB
Raw Blame
"""The news update validators' message helpers and their raising wrappers.

Routes return ``compact_subscription_update_error`` /
``folder_update_error`` output to the client directly instead of echoing
``str()`` of a caught exception (CodeQL py/stack-trace-exposure, alert
#8185), so the helpers and the ``normalize_*`` wrappers must agree.
"""

import re

import pytest

from local_deep_research.news.constants import (
    COMPACT_SUBSCRIPTION_NAME_MAX_LENGTH,
    NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES,
    NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES,
    compact_subscription_update_error,
    folder_update_error,
    normalize_compact_subscription_update,
    normalize_folder_update,
)


@pytest.mark.parametrize(
    ("data", "message"),
    [
        ({"bogus": 1}, "Unsupported subscription update fields: bogus"),
        # The allowlist check runs first, so a mass-assignment attempt is
        # reported as such even when another field is also invalid.
        (
            {"custom_endpoint": "x", "is_active": "yes"},
            "Unsupported subscription update fields: custom_endpoint",
        ),
        ({"is_active": "yes"}, "is_active must be a boolean"),
        ({"status": "gone"}, "status must be 'active' or 'paused'"),
        ({"notes": 3}, "notes must be a string or null"),
        *[
            pytest.param(
                {"refresh_interval_minutes": value},
                "refresh_interval_minutes must be an integer between "
                f"{NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES} and "
                f"{NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES}",
                id=label,
            )
            for label, value in (
                ("below-minimum", NEWS_SUBSCRIPTION_MIN_REFRESH_MINUTES - 1),
                ("above-maximum", NEWS_SUBSCRIPTION_MAX_REFRESH_MINUTES + 1),
                ("boolean-interval", True),
            )
        ],
        (
            {"name": "x" * (COMPACT_SUBSCRIPTION_NAME_MAX_LENGTH + 1)},
            "name exceeds maximum length of "
            f"{COMPACT_SUBSCRIPTION_NAME_MAX_LENGTH} characters",
        ),
    ],
)
def test_compact_subscription_update_messages(data, message):
    assert compact_subscription_update_error(data) == message
    with pytest.raises(ValueError, match=f"^{re.escape(message)}$"):
        normalize_compact_subscription_update(data)


def test_compact_subscription_update_valid():
    data = {"name": "n", "is_active": False, "id": 1}
    assert compact_subscription_update_error(data) is None
    assert normalize_compact_subscription_update(data) == {
        "name": "n",
        "status": "paused",
    }


@pytest.mark.parametrize(
    ("status", "is_active"), [("paused", True), ("active", False)]
)
def test_compact_subscription_explicit_status_takes_precedence(
    status, is_active
):
    data = {"status": status, "is_active": is_active}
    assert compact_subscription_update_error(data) is None
    assert normalize_compact_subscription_update(data) == {"status": status}


@pytest.mark.parametrize(
    ("data", "message"),
    [
        ({"bogus": 1}, "Unsupported folder update fields: bogus"),
        (
            {"to_dict": "x", "sort_order": True},
            "Unsupported folder update fields: to_dict",
        ),
        ({"name": None}, "name must be a string"),
        ({"color": 1}, "color must be a string or null"),
        ({"is_default": 1}, "is_default must be a boolean"),
        ({"sort_order": True}, "sort_order must be an integer"),
    ],
)
def test_folder_update_messages(data, message):
    assert folder_update_error(data) == message
    with pytest.raises(ValueError, match=f"^{re.escape(message)}$"):
        normalize_folder_update(data)


def test_folder_update_valid():
    data = {"name": "n", "sort_order": 2, "id": 7}
    assert folder_update_error(data) is None
    assert normalize_folder_update(data) == {"name": "n", "sort_order": 2}


@pytest.mark.parametrize(
    ("validator", "normalize", "prefix", "invalid_known"),
    [
        (
            compact_subscription_update_error,
            normalize_compact_subscription_update,
            "Unsupported subscription update fields: ",
            {"is_active": "yes", "refresh_interval_minutes": False},
        ),
        (
            folder_update_error,
            normalize_folder_update,
            "Unsupported folder update fields: ",
            {"name": None, "sort_order": True},
        ),
    ],
    ids=["subscription", "folder"],
)
@pytest.mark.parametrize(
    ("keys", "rendered"),
    [
        (["zebra", "alpha"], "alpha, zebra"),
        (["x" * 32], "x" * 32),
        (["x" * 33], "x" * 29 + "..."),
        (["a" * 100_000], "a" * 29 + "..."),
        (["界" * 100_000], "界" * 29 + "..."),
        (
            [f"key_{i:04d}" for i in range(8)],
            "key_0000, key_0001, key_0002, key_0003, "
            "key_0004, key_0005, key_0006, key_0007",
        ),
        (
            [f"key_{i:04d}" for i in range(1_000)],
            "key_0000, key_0001, key_0002, key_0003, "
            "key_0004, key_0005, key_0006, key_0007, ... (+992 more)",
        ),
        (
            [f"{i:04d}" + "x" * 10_000 for i in range(100)],
            ", ".join(f"{i:04d}" + "x" * 25 + "..." for i in range(8))
            + ", ... (+92 more)",
        ),
    ],
    ids=[
        "short-sorted",
        "key-boundary",
        "key-truncated",
        "long-first-key",
        "unicode-long-key",
        "count-boundary",
        "many-keys",
        "many-long-keys",
    ],
)
def test_unsupported_update_fields_bounded(
    validator, normalize, prefix, invalid_known, keys, rendered
):
    expected = prefix + rendered
    for ordered_keys in (keys, list(reversed(keys))):
        data = {key: 1 for key in ordered_keys}
        data.update(invalid_known)
        message = validator(data)
        with pytest.raises(ValueError) as exc:
            normalize(data)
        assert str(exc.value) == message
        assert len(message) <= len(prefix) + 320
        assert message == expected