"""Tests for configuration module.""" from tuimble.config import ( AudioConfig, Config, PttConfig, ServerConfig, _load_section, load_config, ) def test_default_config(): cfg = Config() assert cfg.server.host == "localhost" assert cfg.server.port == 64738 assert cfg.audio.sample_rate == 48000 assert cfg.ptt.mode == "toggle" def test_server_config(): srv = ServerConfig(host="mumble.example.com", port=12345) assert srv.host == "mumble.example.com" assert srv.port == 12345 def test_ptt_config_defaults(): ptt = PttConfig() assert ptt.key == "f4" assert ptt.backend == "auto" def test_audio_gain_defaults(): acfg = AudioConfig() assert acfg.input_gain == 1.0 assert acfg.output_gain == 1.0 def test_audio_gain_custom(): acfg = AudioConfig(input_gain=0.5, output_gain=1.5) assert acfg.input_gain == 0.5 assert acfg.output_gain == 1.5 def test_server_cert_defaults(): srv = ServerConfig() assert srv.certfile == "" assert srv.keyfile == "" def test_server_cert_custom(): srv = ServerConfig(certfile="/path/cert.pem", keyfile="/path/key.pem") assert srv.certfile == "/path/cert.pem" assert srv.keyfile == "/path/key.pem" # -- _load_section tests ----------------------------------------------------- def test_load_section_filters_unknown_keys(): """Unknown keys are silently dropped, valid keys are kept.""" result = _load_section(ServerConfig, { "host": "example.com", "typo_field": "oops", "another_bad": 42, }) assert result.host == "example.com" assert result.port == 64738 # default preserved def test_load_section_empty_dict(): result = _load_section(PttConfig, {}) assert result.key == "f4" assert result.mode == "toggle" def test_load_section_all_valid(): result = _load_section(PttConfig, {"key": "space", "mode": "hold"}) assert result.key == "space" assert result.mode == "hold" def test_load_config_missing_file(tmp_path): """Missing config file returns defaults.""" cfg = load_config(tmp_path / "nonexistent.toml") assert cfg.server.host == "localhost" def test_load_config_with_unknown_keys(tmp_path): """Config file with unknown keys loads without error.""" toml = tmp_path / "config.toml" toml.write_text( '[server]\nhost = "example.com"\nbogus = true\n' '[ptt]\nfuture_option = "x"\n' ) cfg = load_config(toml) assert cfg.server.host == "example.com" assert cfg.ptt.key == "f4" # default, not overwritten