Files
MonitorSwitcher/tests/test_config_manager.py

64 lines
1.9 KiB
Python

import json
import pytest
from pathlib import Path
from config.config_manager import ConfigManager, DEFAULT_CONFIG
def test_load_defaults_when_no_file(tmp_path):
cm = ConfigManager(tmp_path / "config.json")
assert cm.get()["active_profile"] == "pc1_dp"
assert len(cm.get()["profiles"]) == 2
def test_saves_default_config_on_first_run(tmp_path):
path = tmp_path / "config.json"
ConfigManager(path)
assert path.exists()
data = json.loads(path.read_text())
assert data["active_profile"] == "pc1_dp"
def test_load_existing_config(tmp_path):
path = tmp_path / "config.json"
custom = {"active_profile": "custom", "profiles": [], "device_triggers": [], "general": {}}
path.write_text(json.dumps(custom))
cm = ConfigManager(path)
assert cm.get()["active_profile"] == "custom"
def test_fallback_on_corrupt_config(tmp_path):
path = tmp_path / "config.json"
path.write_text("not json {{{")
cm = ConfigManager(path)
assert cm.get()["active_profile"] == "pc1_dp"
def test_get_profile_returns_profile(tmp_path):
cm = ConfigManager(tmp_path / "config.json")
p = cm.get_profile("pc1_dp")
assert p is not None
assert p["name"] == "PC1 \u2014 DP"
def test_get_profile_returns_none_for_missing(tmp_path):
cm = ConfigManager(tmp_path / "config.json")
assert cm.get_profile("nonexistent") is None
def test_set_active_profile_persists(tmp_path):
path = tmp_path / "config.json"
cm = ConfigManager(path)
cm.set_active_profile("pc2_hdmi")
cm2 = ConfigManager(path)
assert cm2.get_active_profile_id() == "pc2_hdmi"
def test_on_change_callback_fires(tmp_path):
cm = ConfigManager(tmp_path / "config.json")
called_with = []
cm.on_change(lambda cfg: called_with.append(cfg["active_profile"]))
cfg = cm.get().copy()
cfg["active_profile"] = "pc2_hdmi"
cm.update(cfg)
assert called_with == ["pc2_hdmi"]