"""Pytest configuration and fixtures for opendeviationbar tests.

Fast-only test suite: all tests must complete in < 30s total.
No ClickHouse, no network I/O, no slow markers.
"""

from __future__ import annotations

import gc
import os

import pytest

# Issue #206: Force UTC for all tests to catch timezone-dependent bugs
os.environ["TZ"] = "UTC"


@pytest.fixture(autouse=True)
def gc_after_test():
    """Force GC after each test to prevent memory accumulation."""
    yield
    gc.collect()


@pytest.fixture(autouse=True, scope="session")
def setup_threshold_env_vars():
    """Set threshold env vars for tests (Issue #62).

    Production uses 1000/50/100/1, tests use 1/1/1/1 for flexibility.
    """
    originals = {}
    keys = [
        "OPENDEVIATIONBAR_CRYPTO_MIN_THRESHOLD",
        "OPENDEVIATIONBAR_FOREX_MIN_THRESHOLD",
        "OPENDEVIATIONBAR_EQUITIES_MIN_THRESHOLD",
        "OPENDEVIATIONBAR_UNKNOWN_MIN_THRESHOLD",
    ]
    for key in keys:
        originals[key] = os.environ.get(key)
        os.environ[key] = "1"

    try:
        from opendeviationbar.threshold import clear_threshold_cache

        clear_threshold_cache()
    except ImportError:
        pass

    yield

    for key in keys:
        if originals[key] is not None:
            os.environ[key] = originals[key]
        elif key in os.environ:
            del os.environ[key]

    try:
        from opendeviationbar.threshold import clear_threshold_cache

        clear_threshold_cache()
    except ImportError:
        pass
