Newer
Older
navi-1 / tests / conftest.py
"""Shared pytest fixtures and configuration."""

import asyncio
import os
from typing import Generator

import pytest

# Ensure the project root is importable
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


@pytest.fixture(autouse=True)
def _reset_settings(monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]:
    """Reset navi.config.settings before every test so mutations don't leak."""
    from navi.config import Settings

    # Clear env vars that would leak from the host environment / .env
    for key in ("NAVI_PERSONA", "NAVI_PERSONA_FILE"):
        monkeypatch.delenv(key, raising=False)

    # Re-create settings from defaults (no .env file during tests)
    fresh = Settings(_env_file=None, navi_persona_file="")
    import navi.config as _config_mod

    _config_mod.settings = fresh

    # The token calibration is fitted from real prompt counts and lives in a
    # module-level registry (it has to outlive a ContextCompressor instance —
    # the compression worker builds a fresh one per pass). Clear it per test so
    # one test's observations cannot move another's token estimates.
    from navi.core.token_calibration import reset_calibration

    reset_calibration()
    yield
    _config_mod.settings = fresh
    reset_calibration()


@pytest.fixture
def event_loop():
    """Provide a consistent event loop for async tests."""
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()