test_config.py (3298B)
1 """T011 — Tests for config loading.""" 2 3 import json 4 from pathlib import Path 5 6 import pytest 7 8 from app.config import DashboardConfig 9 10 11 class TestConfigLoading: 12 """Load config from JSON file with defaults.""" 13 14 def test_loads_from_json_file(self, tmp_config_dir: Path) -> None: 15 """Reads values from data/config.json.""" 16 config_file = tmp_config_dir / "config.json" 17 config_file.write_text(json.dumps({ 18 "hermes_base_url": "http://example.com:9999", 19 "hermes_api_key": "sk-test-123", 20 "data_dir": "/tmp/mydata", 21 })) 22 config = DashboardConfig.from_file(str(config_file)) 23 assert config.hermes_base_url == "http://example.com:9999" 24 assert config.hermes_api_key == "sk-test-123" 25 assert config.data_dir == "/tmp/mydata" 26 27 def test_missing_file_uses_defaults(self, tmp_config_dir: Path) -> None: 28 """When config file doesn't exist, default values are used.""" 29 config = DashboardConfig.from_file(str(tmp_config_dir / "nonexistent.json")) 30 assert config.hermes_base_url == "http://localhost:8642" 31 assert config.data_dir == "data/" 32 33 def test_partial_config_fills_defaults(self, tmp_config_dir: Path) -> None: 34 """Only provided fields override defaults; rest stay default.""" 35 config_file = tmp_config_dir / "config.json" 36 config_file.write_text(json.dumps({"hermes_api_key": "sk-abc"})) 37 config = DashboardConfig.from_file(str(config_file)) 38 assert config.hermes_api_key == "sk-abc" 39 assert config.hermes_base_url == "http://localhost:8642" 40 41 def test_empty_json_uses_defaults(self, tmp_config_dir: Path) -> None: 42 """Empty JSON object uses all defaults.""" 43 config_file = tmp_config_dir / "config.json" 44 config_file.write_text(json.dumps({})) 45 config = DashboardConfig.from_file(str(config_file)) 46 assert config.hermes_base_url == "http://localhost:8642" 47 48 def test_invalid_json_raises(self, tmp_config_dir: Path) -> None: 49 """Malformed JSON raises an error.""" 50 config_file = tmp_config_dir / "config.json" 51 config_file.write_text("not valid json {{{") 52 with pytest.raises(Exception): 53 DashboardConfig.from_file(str(config_file)) 54 55 56 class TestConfigDefaults: 57 """Default values for all config fields.""" 58 59 def test_default_hermes_api_key_is_empty(self, tmp_config_dir: Path) -> None: 60 config = DashboardConfig.from_file(str(tmp_config_dir / "missing.json")) 61 assert config.hermes_api_key == "" 62 63 def test_default_data_dir(self, tmp_config_dir: Path) -> None: 64 config = DashboardConfig.from_file(str(tmp_config_dir / "missing.json")) 65 assert config.data_dir == "data/" 66 67 def test_from_file_with_all_fields(self, tmp_config_dir: Path) -> None: 68 config_file = tmp_config_dir / "full.json" 69 config_file.write_text(json.dumps({ 70 "hermes_base_url": "http://custom:9999", 71 "hermes_api_key": "sk-full", 72 "data_dir": "/custom/data", 73 })) 74 config = DashboardConfig.from_file(str(config_file)) 75 assert config.hermes_base_url == "http://custom:9999" 76 assert config.hermes_api_key == "sk-full" 77 assert config.data_dir == "/custom/data" 78