import pytest from ai_typewriter.config import ( ConfigError, ConfigStore, Profile, load_config, math_latex_profile, ) def test_store_creates_defaults_when_missing(tmp_path): path = tmp_path / "nested" / "config.json" store = ConfigStore(path) store.ensure_defaults() assert path.exists() assert len(store.profiles) == 2 names = [p.name for p in store.profiles] assert "Général" in names assert math_latex_profile().name in names store.load() assert store.active_name in names def test_default_math_profile_enables_equation_markers(): p = math_latex_profile() assert p.equation_enabled is True assert p.eq_start_marker == "[EQ]" assert p.eq_end_marker == "[/EQ]" assert p.eq_start_key == "alt+=" assert p.eq_end_key == "right" assert "[EQ]" in p.effective_prompt() def test_crud_upsert_set_active_and_remove(tmp_path): path = tmp_path / "config.json" store = ConfigStore(path) store.ensure_defaults() store.load() names_before = len(store.get_all()) prof = math_latex_profile(name="MaesProfil") store.upsert(prof) store.set_active("MaesProfil") assert store.active().name == "MaesProfil" assert len(store.get_all()) == names_before + 1 prof2 = Profile(name="MaesProfil", provider="openai", model="gpt-4o-mini") store.upsert(prof2) assert store.get("MaesProfil").provider == "openai" store.remove("MaesProfil") with pytest.raises(KeyError): store.get("MaesProfil") def test_remove_last_profile_blocked(tmp_path): store = ConfigStore(tmp_path / "config.json") store.ensure_defaults() store.load() # Il y a 2 profils par défaut : le premier retrait réussit… store.remove(store.get_all()[0].name) assert len(store.get_all()) == 1 # …mais retirer le dernier est interdit. with pytest.raises(ConfigError): store.remove(store.get_all()[0].name) def test_set_active_unknown_raises(tmp_path): store = ConfigStore(tmp_path / "config.json") store.ensure_defaults() store.load() with pytest.raises(KeyError): store.set_active("inexistant") def test_load_config_return_store(tmp_path): store = load_config(str(tmp_path / "config.json")) assert isinstance(store, ConfigStore)