Files
alexei.dolgolyov 530316c2c3 feat(mqtt): multi-broker MQTT + Zigbee2MQTT light target
- New Z2MLightOutputTarget storage, processor, editor and routes for
  Zigbee2MQTT light entities (shares the HA-Light editor UI via the new
  light-target-editor module)
- Replace global MQTTService/MQTTConfig with per-source MQTTManager +
  MQTTRuntime; thread mqtt_source_id through Z2M targets, DIY MQTT
  devices, and the automation engine
- Migrate legacy single-broker YAML/env config to a "Default Broker"
  MQTTSource on startup (core/mqtt/legacy_migration.py) and drop the
  obsolete core/mqtt/mqtt_service.py
- Refresh /api/v1/system integration status to surface every MQTT source
- Extract shared light-target editor and refactor OutputTargetStore +
  output_targets routes around typed factories / auto-registry
- Modal CSS polish, locale strings, and storage/bindable test coverage
2026-05-12 18:06:09 +03:00

106 lines
3.3 KiB
Python

"""Tests for configuration management."""
from pathlib import Path
import pytest
import yaml
from ledgrab.config import (
Config,
ServerConfig,
get_config,
reload_config,
)
class TestDefaultConfig:
def test_default_server_values(self):
config = Config()
assert config.server.host == "0.0.0.0"
assert config.server.port == 8080
assert config.server.log_level == "INFO"
def test_default_storage_paths(self, monkeypatch):
monkeypatch.delenv("LEDGRAB_DATA_DIR", raising=False)
config = Config()
assert config.storage.database_file == "data/ledgrab.db"
def test_data_dir_env_override(self, monkeypatch, tmp_path):
monkeypatch.setenv("LEDGRAB_DATA_DIR", str(tmp_path / "custom"))
# default_data_dir reads the env var, but the module-level default
# was evaluated at import time — so re-import paths() value via the
# helper to confirm the contract.
from importlib import reload
from ledgrab import paths as paths_mod
reload(paths_mod)
assert paths_mod.default_data_dir() == Path(str(tmp_path / "custom"))
def test_default_demo_off(self):
config = Config()
assert config.demo is False
class TestFromYaml:
def test_load_from_yaml(self, tmp_path):
config_data = {
"server": {"host": "127.0.0.1", "port": 9000},
"auth": {"api_keys": {"dev": "secret"}},
}
config_path = tmp_path / "test_config.yaml"
with open(config_path, "w") as f:
yaml.dump(config_data, f)
config = Config.from_yaml(config_path)
assert config.server.host == "127.0.0.1"
assert config.server.port == 9000
assert config.auth.api_keys == {"dev": "secret"}
def test_load_from_yaml_file_not_found(self):
with pytest.raises(FileNotFoundError):
Config.from_yaml("nonexistent.yaml")
class TestEnvironmentVariables:
def test_env_overrides(self, monkeypatch):
monkeypatch.setenv("LEDGRAB_SERVER__HOST", "192.168.1.1")
monkeypatch.setenv("LEDGRAB_SERVER__PORT", "7000")
config = Config()
assert config.server.host == "192.168.1.1"
assert config.server.port == 7000
class TestServerConfig:
def test_creation(self):
sc = ServerConfig(host="localhost", port=8000)
assert sc.host == "localhost"
assert sc.port == 8000
assert sc.log_level == "INFO"
class TestDemoMode:
def test_demo_rewrites_storage_paths(self):
config = Config(demo=True)
db_path = Path(config.storage.database_file)
assert db_path.parent.name == "demo"
assert db_path.name == "ledgrab.db"
assets_path = Path(config.assets.assets_dir)
assert assets_path.parent.name == "demo"
assert assets_path.name == "assets"
def test_non_demo_keeps_original_paths(self, monkeypatch):
monkeypatch.delenv("LEDGRAB_DATA_DIR", raising=False)
config = Config(demo=False)
assert config.storage.database_file == "data/ledgrab.db"
class TestGlobalConfig:
def test_get_config_returns_config(self):
config = get_config()
assert isinstance(config, Config)
def test_reload_config_returns_new_config(self):
config = reload_config()
assert isinstance(config, Config)