"""Tests for config.py — configuration management.""" import json import os from pathlib import Path from unittest.mock import patch from config import Config, DEFAULT_DATA_DIR class TestConfigDataDir: def test_default_data_dir(self, tmp_path): """Test that default data_dir is ~/.jigaido when no config exists.""" with patch.dict(os.environ, {}, clear=True): with patch("pathlib.Path.exists", return_value=False): cfg = Config() assert cfg.data_dir == DEFAULT_DATA_DIR def test_env_override_data_dir(self, tmp_path): """Test that JIGAIDO_DATA_DIR env var overrides config file.""" env_dir = "/custom/env/data/dir" with patch.dict(os.environ, {"JIGAIDO_DATA_DIR": env_dir}, clear=False): cfg = Config() assert cfg.data_dir == Path(env_dir) def test_config_file_data_dir(self, tmp_path): """Test that config file is read when JIGAIDO_DATA_DIR not set.""" config_dir = tmp_path / ".jigaido" config_dir.mkdir() config_file = config_dir / "config.json" config_file.write_text(json.dumps({"data_dir": "/custom/config/data"})) with patch.dict(os.environ, {}, clear=True): with patch("pathlib.Path.expanduser", return_value=config_file): cfg = Config() assert cfg.data_dir == Path("/custom/config/data") def test_bot_token_from_env(self): """Test that bot_token is read from JIGAIDO_BOT_TOKEN env var.""" with patch.dict( os.environ, {"JIGAIDO_BOT_TOKEN": "test_token_123"}, clear=False ): cfg = Config() assert cfg.bot_token == "test_token_123" def test_bot_token_none_when_not_set(self): """Test that bot_token is None when JIGAIDO_BOT_TOKEN not set and no config file.""" with patch.dict(os.environ, {}, clear=True): with patch("pathlib.Path.exists", return_value=False): cfg = Config() assert cfg.bot_token is None def test_bot_token_from_config_file(self): """Test that bot_token is read from config file when env var not set.""" config_dir = Path.home() / ".jigaido" config_file = config_dir / "config.json" with patch.dict(os.environ, {}, clear=True): with patch("pathlib.Path.expanduser", return_value=config_file): with patch("pathlib.Path.exists", return_value=True): with patch("builtins.open", create=True) as mock_open: mock_open.return_value.__enter__ = lambda s: s mock_open.return_value.__exit__ = lambda *a: None mock_open.return_value.read = lambda: ( '{"JIGAIDO_BOT_TOKEN": "config_token"}' ) cfg = Config() assert cfg.bot_token == "config_token" class TestConfigEnsureDataDir: def test_ensure_data_dir_creates_directory(self, tmp_path): """Test that ensure_data_dir creates the directory if it doesn't exist.""" data_dir = tmp_path / "test_data_dir" with patch.object(Config, "__init__", lambda self: None): cfg = Config() cfg.data_dir = data_dir assert not data_dir.exists() cfg.ensure_data_dir() assert data_dir.exists() assert data_dir.is_dir() def test_ensure_data_dir_does_nothing_if_exists(self, tmp_path): """Test that ensure_data_dir doesn't fail if directory already exists.""" data_dir = tmp_path / "existing_dir" data_dir.mkdir() with patch.object(Config, "__init__", lambda self: None): cfg = Config() cfg.data_dir = data_dir cfg.ensure_data_dir() assert data_dir.exists()