Files
xpamych ef81027f65 Исправлена стабильность связи сервиса с графическим интерфейсом
Изменено:
- сервис автоматически применяет обновлённые настройки без перезапуска
- графический интерфейс сразу отображает выбранный режим
- настройки сохраняются без риска повреждения при фоновом чтении

Исправлено:
- чрезмерная загрузка процессора при ожидании команд интерфейса
- обработка команд после периода бездействия
- использование пользовательского пути к конфигурации
2026-10-30 18:55:56 +03:00

84 lines
2.7 KiB
Python

"""Tests for configuration management."""
from __future__ import annotations
from pathlib import Path
from unittest.mock import patch
import pytest
from lgcompanion.config import Config, DeviceProfile, SinkConfig
def test_default_config() -> None:
cfg = Config.default()
assert cfg.general.poll_interval == 2.0
assert cfg.general.log_level == "info"
assert len(cfg.devices) == 0
assert cfg.sinks.surround == "effect_input.lgcompanion-virtual-surround"
def test_device_profile_hex_parsing() -> None:
dev = DeviceProfile(
name="Test",
vendor_id="0x1038",
product_id="0x12AD",
battery_request="06 18",
battery_response_prefix="06 18",
)
assert dev.vendor_id_int == 0x1038
assert dev.product_id_int == 0x12AD
assert dev.battery_request_bytes == b"\x06\x18"
def test_config_roundtrip(tmp_path: Path) -> None:
path = tmp_path / "config.toml"
cfg = Config.default()
cfg.sinks.stereo = "test-stereo"
cfg.save(path)
loaded = Config.load(path)
assert loaded.sinks.stereo == "test-stereo"
assert len(loaded.devices) == 0
def test_sink_config_fills_empty_defaults() -> None:
cfg = SinkConfig(surround="", stereo="", filter_output="", game="", chat="")
assert cfg.surround == "effect_input.lgcompanion-virtual-surround"
assert cfg.stereo == "alsa_output.usb-SteelSeries_SteelSeries_Arctis_7-00.stereo-game"
assert cfg.filter_output == "effect_output.lgcompanion-virtual-surround"
assert cfg.game == "alsa_output.usb-SteelSeries_SteelSeries_Arctis_7-00.stereo-game"
assert cfg.chat == "alsa_output.usb-SteelSeries_SteelSeries_Arctis_7-00.stereo-chat"
def test_sink_config_roundtrip_with_game_chat(tmp_path: Path) -> None:
path = tmp_path / "config.toml"
cfg = Config.default()
cfg.sinks.game = "my-game-sink"
cfg.sinks.chat = "my-chat-sink"
cfg.save(path)
loaded = Config.load(path)
assert loaded.sinks.game == "my-game-sink"
assert loaded.sinks.chat == "my-chat-sink"
def test_add_remove_device() -> None:
cfg = Config.default()
dev = DeviceProfile(name="New", vendor_id="0x1234", product_id="0x5678")
cfg.add_device(dev)
assert cfg.get_device("New") is not None
assert cfg.remove_device("New") is True
assert cfg.get_device("New") is None
assert cfg.remove_device("Missing") is False
def test_failed_save_preserves_existing_config(tmp_path: Path) -> None:
path = tmp_path / "config.toml"
path.write_text("original")
cfg = Config.default()
with patch("lgcompanion.config.tomli_w.dump", side_effect=RuntimeError("disk error")):
with pytest.raises(RuntimeError):
cfg.save(path)
assert path.read_text() == "original"