"""Pytest configuration and fixtures. Every test runs with a scrubbed environment, an empty working directory (so a developer's own ``.env`` is never read) and state paths inside ``tmp_path``. Nothing may write to ``/var/log``, ``/var/lib`` or the real home directory. """ from __future__ import annotations from collections.abc import Generator from pathlib import Path import pytest from aegis_gitea_mcp.audit import reset_audit_logger from aegis_gitea_mcp.config import Settings, reset_settings from aegis_gitea_mcp.rate_limit import reset_rate_limiter from aegis_gitea_mcp.request_context import clear_gitea_auth_context def _reset_singletons() -> None: """Reset every module-level singleton.""" reset_settings() reset_audit_logger() reset_rate_limiter() clear_gitea_auth_context() @pytest.fixture(autouse=True) def reset_globals(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]: """Isolate each test: clean env, tmp cwd, tmp state paths, fresh singletons.""" for field_name in Settings.model_fields: monkeypatch.delenv(field_name.upper(), raising=False) monkeypatch.chdir(tmp_path) data_dir = tmp_path / "data" monkeypatch.setenv("DATA_DIR", str(data_dir)) monkeypatch.setenv("AUDIT_LOG_PATH", str(data_dir / "audit.log")) _reset_singletons() yield _reset_singletons() @pytest.fixture def mock_env(monkeypatch: pytest.MonkeyPatch) -> None: """Minimal valid environment: a Gitea URL and a token.""" monkeypatch.setenv("GITEA_URL", "https://gitea.example.com") monkeypatch.setenv("GITEA_TOKEN", "test-token-12345")