"""Тест для #2445 D2: scrape_catalog_objects() должен конструировать BrowserSession
с throttled concurrency/jitter (settings.scrape_kn_*), а НЕ с модульным дефолтом
stealth.py (concurrency=8, jitter 600-1500ms).

Этот scraper ходит по тому же /сервисы/* path family, что вызвал DOM.РФ WAF
hard-ban 2026-05-24 (#2443) — без явного override BrowserSession тихо унаследовал
бы дефолт, углубляя риск повторного бана.
"""

from __future__ import annotations

from datetime import date
from unittest.mock import AsyncMock, MagicMock, patch

import pytest

from app.core.config import settings


class _FakeBrowserSession:
    """Async context manager stub — избегаем реального Playwright/network."""

    def __init__(self, *args: object, **kwargs: object) -> None:
        self.init_kwargs = kwargs
        self.warm_up = AsyncMock()

    async def __aenter__(self) -> _FakeBrowserSession:
        return self

    async def __aexit__(self, *exc: object) -> None:
        return None


@pytest.mark.asyncio
async def test_scrape_catalog_objects_passes_throttled_browsersession_kwargs() -> None:
    from app.services.scrapers import domrf_catalog_object as mod

    captured: dict[str, object] = {}

    def _factory(*args: object, **kwargs: object) -> _FakeBrowserSession:
        captured.update(kwargs)
        return _FakeBrowserSession(*args, **kwargs)

    db = MagicMock()

    with (
        patch.object(mod, "BrowserSession", side_effect=_factory) as mock_cls,
        patch.object(mod, "scrape_catalog_object", new=AsyncMock(return_value=True)),
    ):
        stats = await mod.scrape_catalog_objects(
            db=db,
            obj_ids=[65136],
            snapshot_date=date(2026, 5, 17),
            region_code=66,
        )

    mock_cls.assert_called_once()
    assert captured["concurrency"] == settings.scrape_kn_browser_concurrency
    assert captured["jitter_min_ms"] == settings.scrape_kn_request_jitter_min_ms
    assert captured["jitter_max_ms"] == settings.scrape_kn_request_jitter_max_ms
    # Sanity: эти throttled значения — НЕ модульный дефолт stealth.py.
    assert captured["concurrency"] != 8
    assert stats["succeeded"] == 1
