"""Per-test database setup for the Cyclone test suite. Every test gets a fresh SQLite DB at ``tmp_path/test.db`` so the suite can run in parallel and never touches the user's ``~/.local/share/cyclone`` database. ``db.init_db()`` is idempotent — tests that already call it explicitly are unaffected (the second call short-circuits). This fixture exists because the SQLAlchemy-backed ``CycloneStore`` requires the engine to be initialized before any DB access, whereas the old in-memory store did not. Adding the init here keeps existing test modules (test_api.py, test_api_835.py, test_api_gets.py, test_api_parse_persists.py) working unchanged. """ from __future__ import annotations import pytest @pytest.fixture(autouse=True) def _auto_init_db(tmp_path, monkeypatch): """Point CYCLONE_DB_URL at a per-test SQLite file and init the schema. Also wires a fresh ``EventBus`` onto ``app.state`` because ``TestClient`` does not invoke the FastAPI lifespan handler unless used as a context manager. The bus is reset between tests so subscribers don't leak. Auth gating is enabled at the router/endpoint level via the ``Depends(matrix_gate)`` wiring in ``cyclone.api``. The auth tests (``test_auth_*``) explicitly flip ``AUTH_DISABLED = False`` and authenticate via the public login route to exercise the real auth path. Every other test — the original test suite predates auth — gets ``AUTH_DISABLED = True`` here so the existing tests keep working without each one having to login first. """ monkeypatch.setenv("CYCLONE_DB_URL", f"sqlite:///{tmp_path}/test.db") from cyclone import db from cyclone.pubsub import EventBus from cyclone.auth import deps db._reset_for_tests() db.init_db() # Re-resolve `app` each fixture invocation because some tests # (test_cors_extra_origins_via_env) call ``importlib.reload`` on # ``cyclone.api`` to mutate the CORS allow-list. If we cached # the app reference at conftest module load, we'd be setting # ``event_bus`` on a stale instance that no test client is # actually using. from cyclone import api as _api_mod _api_mod.app.state.event_bus = EventBus() deps.AUTH_DISABLED = True try: yield finally: deps.AUTH_DISABLED = False _api_mod.app.state.event_bus = None db._reset_for_tests()