Files
jigaido/tests/test_config.py
shokollm 4885be0752 fix: cleanup codebase and sync SPEC with actual permissions
Phase 1: Ruff lint fixes
- Remove unused imports across all files
- Remove unused variables (now_utc, tz, ctx)
- Fix f-string without placeholders
- Fix E402 import order with noqa comments

Phase 2: Remove confusing hard delete from storage
- Removed delete_bounty() from RoomStorage Protocol (never used by app)
- Removed delete_bounty() from JsonFileRoomStorage (was hard delete)
- Removed corresponding tests (hard delete was never used)

Phase 3: Sync SPEC.md with actual code behavior
- Updated overview: admins can add/edit/delete (not 'anyone' + 'creator')
- Updated command table: /add, /edit, /delete are admin only
- Updated error handling messages

Test results: 96 passed (2 hard delete tests removed)
2026-04-09 10:01:02 +00:00

92 lines
3.8 KiB
Python

"""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()