""" Tests for configuration system. """ import pytest import tempfile import os from pathlib import Path from src.core.config import Config, BackendConfig, ConfigLoader, get_config, reload_config class TestConfigLoader: """Test configuration loading.""" def test_load_default_config(self): """Test loading with no config files (uses defaults).""" config = ConfigLoader.load( config_path=Path("nonexistent.yaml"), env_file=Path("nonexistent.env") ) assert isinstance(config, Config) assert config.active_backend == "wan_t2v_14b" assert len(config.backends) == 0 # No YAML loaded def test_load_yaml_config(self): """Test loading from YAML file.""" yaml_content = """ backends: test_backend: name: "Test Backend" class: "test.TestBackend" model_id: "test/model" vram_gb: 8 dtype: "fp16" enable_vae_slicing: true enable_vae_tiling: false chunking: enabled: true mode: "sequential" max_chunk_seconds: 4 overlap_seconds: 1 active_backend: "test_backend" defaults: fps: 30 checkpoint_db: "test.db" model_cache_dir: "~/test_cache" """ with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: f.write(yaml_content) yaml_path = f.name try: config = ConfigLoader.load(config_path=Path(yaml_path)) assert config.active_backend == "test_backend" assert "test_backend" in config.backends backend = config.get_backend("test_backend") assert backend.name == "Test Backend" assert backend.vram_gb == 8 assert backend.chunking_mode == "sequential" assert config.defaults["fps"] == 30 finally: os.unlink(yaml_path) def test_load_env_file(self, monkeypatch): """Test loading from .env file.""" # Clear any existing env vars first for key in ['ACTIVE_BACKEND', 'MODEL_CACHE_DIR', 'LOG_LEVEL']: monkeypatch.delenv(key, raising=False) env_content = """ ACTIVE_BACKEND=env_backend MODEL_CACHE_DIR=/env/cache LOG_LEVEL=DEBUG """ with tempfile.NamedTemporaryFile(mode='w', suffix='.env', delete=False) as f: f.write(env_content) env_path = f.name try: config = ConfigLoader.load( config_path=Path("nonexistent.yaml"), env_file=Path(env_path) ) assert config.active_backend == "env_backend" assert "/env/cache" in config.model_cache_dir or "\\env\\cache" in config.model_cache_dir assert config.log_level == "DEBUG" finally: os.unlink(env_path) # Clean up env vars for key in ['ACTIVE_BACKEND', 'MODEL_CACHE_DIR', 'LOG_LEVEL']: monkeypatch.delenv(key, raising=False) def test_env_variable_override(self, monkeypatch): """Test that environment variables override config files.""" # Clear env var first monkeypatch.delenv("ACTIVE_BACKEND", raising=False) yaml_content = """ active_backend: yaml_backend model_cache_dir: ~/yaml_cache """ with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: f.write(yaml_content) yaml_path = f.name try: monkeypatch.setenv("ACTIVE_BACKEND", "env_override") config = ConfigLoader.load(config_path=Path(yaml_path)) # Environment variable should override assert config.active_backend == "env_override" # But other settings from YAML should remain assert "yaml_cache" in config.model_cache_dir finally: os.unlink(yaml_path) monkeypatch.delenv("ACTIVE_BACKEND", raising=False) def test_path_expansion(self): """Test that paths are properly expanded.""" config = ConfigLoader.load( config_path=Path("nonexistent.yaml"), env_file=Path("nonexistent.env") ) # Default paths should be expanded assert not config.model_cache_dir.startswith("~") assert not config.checkpoint_db.startswith("~") def test_get_backend_not_found(self): """Test getting a non-existent backend.""" config = Config() with pytest.raises(ValueError, match="not found"): config.get_backend("nonexistent") class TestBackendConfig: """Test backend configuration.""" def test_backend_config_defaults(self): """Test backend config with defaults.""" config = BackendConfig( name="Test", model_class="test.Test", model_id="test/model", vram_gb=12, dtype="fp16" ) assert config.enable_vae_slicing is True assert config.enable_vae_tiling is False assert config.chunking_enabled is True assert config.chunking_mode == "sequential"