"""FlowDeck — pytest fixtures and configuration.""" import os import tempfile from pathlib import Path import pytest @pytest.fixture def client(): """FastAPI TestClient with a fresh temporary SQLite database.""" db_file = tempfile.NamedTemporaryFile(suffix=".db", delete=False) db_path = db_file.name db_file.close() backup_dir = tempfile.mkdtemp(prefix="fd_backups_") # Set env BEFORE importing app modules (config reads at import time) os.environ["DATABASE_URL"] = f"sqlite:///{db_path}" os.environ["APP_SECRET_KEY"] = "test-secret-for-tests" os.environ["RATE_LIMIT_ENABLED"] = "false" os.environ["BACKUP_ENABLED"] = "true" os.environ["BACKUP_DIR"] = backup_dir os.environ["PROJECT_SYNC_ENABLED"] = "false" # Force config reload by clearing the cached Settings instance import app.config app.config.settings = app.config.Settings() from app.db import init_db from app.main import app init_db() yield TestClient(app) # Cleanup try: os.unlink(db_path) except PermissionError: pass # Windows: file may still be open in another thread for p in Path(backup_dir).glob("*.db"): try: p.unlink() except PermissionError: pass try: Path(backup_dir).rmdir() except OSError: pass from fastapi.testclient import TestClient