# tests/conftest.py — shared fixtures for Sparrow test suite from __future__ import annotations import os import tempfile import pytest import pytest_asyncio from fastapi.testclient import TestClient from httpx import ASGITransport, AsyncClient # Use mock mode for all tests — no GPU or ffmpeg required os.environ.setdefault("CF_MUSICGEN_MOCK", "1") os.environ.setdefault("CF_STEMS_MOCK", "1") @pytest.fixture def tmp_db(tmp_path): """Temporary SQLite DB path.""" return str(tmp_path / "test.db") @pytest.fixture def tmp_data(tmp_path): """Temporary data directory.""" d = tmp_path / "data" d.mkdir() return str(d) @pytest.fixture def conn(tmp_db): """SQLite connection with migrations applied.""" from app.db.store import get_connection, run_migrations c = get_connection(tmp_db) run_migrations(c) yield c c.close() @pytest.fixture def app(tmp_db, tmp_data): """FastAPI test app with isolated DB and data dir.""" os.environ["SPARROW_DB_PATH"] = tmp_db os.environ["SPARROW_DATA_DIR"] = tmp_data os.environ["SPARROW_ENV"] = "development" from app.main import create_app return create_app() @pytest.fixture def client(app): """Synchronous test client.""" with TestClient(app, raise_server_exceptions=True) as c: yield c @pytest_asyncio.fixture async def async_client(app): """Async test client.""" async with AsyncClient( transport=ASGITransport(app=app), base_url="http://test" ) as c: yield c