"""Shared fixtures for advanced_search_system/filters tests.

Prevents filter unit tests from triggering the real journal_quality
data download path when the bundled DB isn't built (e.g. fresh CI
containers). ``_build_or_raise`` would otherwise attempt a multi-
minute OpenAlex + DOAJ + JabRef fetch, which blows the per-test
timeout. Unit tests here should exercise filter logic only — they
mock their own data manager interactions via ``patch.object`` where
they actually need DB behavior.
"""

from __future__ import annotations

import pytest


@pytest.fixture(autouse=True)
def _stub_journal_quality_build(monkeypatch):
    """Replace JournalQualityDB._build_or_raise with a no-op that
    signals missing data. expand_abbreviation() catches this and
    returns None, so the filter falls through to its own scoring
    tiers without touching the network or disk."""

    def _raise_missing(self, path):
        raise FileNotFoundError(
            "journal_quality DB not available in tests; "
            "conftest stubbed _build_or_raise to skip download"
        )

    try:
        from local_deep_research.journal_quality.db import JournalQualityDB

        monkeypatch.setattr(JournalQualityDB, "_build_or_raise", _raise_missing)
    except ImportError:
        # Module not importable in this environment — nothing to stub.
        pass


@pytest.fixture(autouse=True)
def _make_db_ready_probe_pass(monkeypatch):
    """Bypass the filter's ``db_ready`` probe that gates the whole
    scoring path.

    filter_results checks whether the bundled journal_quality.db is
    available — if not, it returns every result tagged QUALITY_PENDING
    without running any scoring tier. In local dev this is fine (the
    file usually exists); in CI the file doesn't exist and every test
    that exercises scoring logic would get the pending short-circuit
    instead of the real code path.

    Patch the probe to always report True. Individual tests can
    override via their own monkeypatch if they specifically want to
    exercise the pending path.
    """
    import pathlib

    real_exists = pathlib.Path.exists
    real_stat = pathlib.Path.stat

    def _exists(self):
        if self.name == "journal_quality.db":
            return True
        return real_exists(self)

    class _FakeStat:
        st_size = 1

    def _stat(self, *a, **kw):
        if self.name == "journal_quality.db":
            return _FakeStat()
        return real_stat(self, *a, **kw)

    monkeypatch.setattr(pathlib.Path, "exists", _exists)
    monkeypatch.setattr(pathlib.Path, "stat", _stat)
