e07c9ec77b
Several weeks of work accumulated without being committed. Grouped here for clarity; see CHANGELOG.md [Unreleased] for the user-facing summary. Highlights ---------- P1 #2 — ISO 639-2/B canonical migration - New Language VO + LanguageRegistry (alfred/domain/shared/knowledge/). - iso_languages.yaml as single source of truth for language codes. - SubtitleKnowledgeBase now delegates lookup to LanguageRegistry; subtitles.yaml only declares subtitle-specific tokens (vostfr, vf, vff, …). - SubtitlePreferences default → ["fre", "eng"]; subtitle filenames written as {iso639_2b}.srt (legacy fr.srt still read via alias). - Scanner: dropped _LANG_KEYWORDS / _SDH_TOKENS / _FORCED_TOKENS / SUBTITLE_EXTENSIONS hardcoded dicts. - Fixed: 'hi' token no longer marks SDH (conflicted with Hindi alias). - Added settings.min_movie_size_bytes (was a module constant). P1 #3 — Release parser unification + data-driven tokenizer - parse_release() is now the single source of truth for release-name parsing. - alfred/knowledge/release/separators.yaml declares the token separators used by the tokenizer (., space, [, ], (, ), _). New conventions can be added without code changes. - Tokenizer now splits on any configured separator instead of name.split('.'). Releases like 'The Father (2020) [1080p] [WEBRip] [5.1] [YTS.MX]' parse via the direct path without sanitization fallback. - Site-tag extraction always runs first; well-formedness only rejects truly forbidden chars. - _parse_season_episode() extended with NxNN / NxNNxNN alt forms. - Removed dead helpers: _sanitize, _normalize. Domain cleanup - Deleted fossil services with zero production callers: alfred/domain/movies/services.py alfred/domain/tv_shows/services.py alfred/domain/subtitles/services.py (replaced by subtitles/services/ package) alfred/domain/subtitles/repositories.py - Split monolithic subtitle services into a package (identifier, matcher, placer, pattern_detector, utils) + dedicated knowledge/ package. - MediaInfo split into dedicated package (alfred/domain/shared/media/: audio, video, subtitle, info, matching). Persistence cleanup - Removed dead JSON repositories (movie/subtitle/tvshow_repository.py). Tests - Major expansion of the test suite organized to mirror the source tree. - Removed obsolete *_edge_cases test files superseded by structured tests. - Suite: 990 passed, 8 skipped. Misc - .gitignore: exclude env_backup/ and *.bak. - Adjustments across agent/llm, app.py, application/filesystem, and infrastructure/filesystem to align with the new domain layout.
205 lines
7.1 KiB
Python
205 lines
7.1 KiB
Python
"""Tests for ``alfred.settings.Settings`` validation.
|
|
|
|
Covers the field-level validators that ship today:
|
|
|
|
- ``llm_temperature`` — must be within [0, 2].
|
|
- ``max_tool_iterations`` — must be positive.
|
|
- ``request_timeout`` — must be positive.
|
|
|
|
URL fields (``deepseek_base_url``, ``tmdb_base_url``) are *not* currently
|
|
URL-validated; tests document that contract explicitly so a future
|
|
regression that silently drops the validator would be caught.
|
|
"""
|
|
|
|
import pytest
|
|
|
|
from alfred.settings import ConfigurationError, Settings
|
|
|
|
|
|
class TestConfigValidation:
|
|
"""Critical tests for config validation."""
|
|
|
|
def test_invalid_temperature_raises_error(self):
|
|
"""Verify invalid temperature is rejected."""
|
|
with pytest.raises(ConfigurationError, match="Temperature"):
|
|
Settings(llm_temperature=3.0) # > 2.0
|
|
|
|
with pytest.raises(ConfigurationError, match="Temperature"):
|
|
Settings(llm_temperature=-0.1) # < 0.0
|
|
|
|
def test_valid_temperature_accepted(self):
|
|
"""Verify valid temperature is accepted."""
|
|
# Should not raise
|
|
Settings(llm_temperature=0.0)
|
|
Settings(llm_temperature=1.0)
|
|
Settings(llm_temperature=2.0)
|
|
|
|
def test_invalid_max_iterations_raises_error(self):
|
|
"""Verify invalid max_iterations is rejected."""
|
|
with pytest.raises(ConfigurationError, match="max_tool_iterations"):
|
|
Settings(max_tool_iterations=0) # < 1
|
|
|
|
with pytest.raises(ConfigurationError, match="max_tool_iterations"):
|
|
Settings(max_tool_iterations=100) # > 20
|
|
|
|
def test_valid_max_iterations_accepted(self):
|
|
"""Verify valid max_iterations is accepted."""
|
|
# Should not raise
|
|
Settings(max_tool_iterations=1)
|
|
Settings(max_tool_iterations=10)
|
|
Settings(max_tool_iterations=20)
|
|
|
|
def test_invalid_timeout_raises_error(self):
|
|
"""Verify invalid timeout is rejected."""
|
|
with pytest.raises(ConfigurationError, match="request_timeout"):
|
|
Settings(request_timeout=0) # < 1
|
|
|
|
with pytest.raises(ConfigurationError, match="request_timeout"):
|
|
Settings(request_timeout=500) # > 300
|
|
|
|
def test_valid_timeout_accepted(self):
|
|
"""Verify valid timeout is accepted."""
|
|
# Should not raise
|
|
Settings(request_timeout=1)
|
|
Settings(request_timeout=30)
|
|
Settings(request_timeout=300)
|
|
|
|
def test_deepseek_url_accepted_verbatim(self):
|
|
"""``deepseek_base_url`` is currently not URL-validated.
|
|
|
|
Documents the actual contract: any non-empty string is accepted, and
|
|
the burden of producing a valid URL falls on the caller. If URL
|
|
validation is introduced later, this test should be replaced with
|
|
``test_invalid_deepseek_url_raises_error``.
|
|
"""
|
|
for url in (
|
|
"https://api.deepseek.com",
|
|
"http://localhost:8000",
|
|
"not-a-url", # currently accepted — see docstring
|
|
"ftp://invalid.com",
|
|
):
|
|
s = Settings(deepseek_base_url=url)
|
|
assert s.deepseek_base_url == url
|
|
|
|
def test_tmdb_url_accepted_verbatim(self):
|
|
"""``tmdb_base_url`` is currently not URL-validated (see deepseek test)."""
|
|
for url in (
|
|
"https://api.themoviedb.org/3",
|
|
"http://localhost:3000",
|
|
"not-a-url",
|
|
):
|
|
s = Settings(tmdb_base_url=url)
|
|
assert s.tmdb_base_url == url
|
|
|
|
|
|
class TestConfigChecks:
|
|
"""Tests for configuration check methods."""
|
|
|
|
def test_is_deepseek_configured_with_key(self):
|
|
"""Verify is_deepseek_configured returns True with API key."""
|
|
settings = Settings(
|
|
deepseek_api_key="test-key", deepseek_base_url="https://api.test.com"
|
|
)
|
|
|
|
assert settings.is_deepseek_configured() is True
|
|
|
|
def test_is_deepseek_configured_without_key(self):
|
|
"""Verify is_deepseek_configured returns False without API key."""
|
|
settings = Settings(
|
|
deepseek_api_key="", deepseek_base_url="https://api.test.com"
|
|
)
|
|
|
|
assert settings.is_deepseek_configured() is False
|
|
|
|
def test_is_deepseek_configured_without_url(self):
|
|
"""Verify is_deepseek_configured returns False without URL."""
|
|
# This will fail validation, so we can't test it directly
|
|
# The validation happens in __post_init__
|
|
pass
|
|
|
|
def test_is_tmdb_configured_with_key(self):
|
|
"""Verify is_tmdb_configured returns True with API key."""
|
|
settings = Settings(
|
|
tmdb_api_key="test-key", tmdb_base_url="https://api.test.com"
|
|
)
|
|
|
|
assert settings.is_tmdb_configured() is True
|
|
|
|
def test_is_tmdb_configured_without_key(self):
|
|
"""Verify is_tmdb_configured returns False without API key."""
|
|
settings = Settings(tmdb_api_key="", tmdb_base_url="https://api.test.com")
|
|
|
|
assert settings.is_tmdb_configured() is False
|
|
|
|
|
|
class TestConfigDefaults:
|
|
"""Tests for configuration defaults."""
|
|
|
|
def test_default_temperature(self):
|
|
"""Verify default temperature is reasonable."""
|
|
settings = Settings()
|
|
|
|
assert 0.0 <= settings.llm_temperature <= 2.0
|
|
|
|
def test_default_max_iterations(self):
|
|
"""Verify default max_iterations is reasonable."""
|
|
settings = Settings()
|
|
|
|
assert 1 <= settings.max_tool_iterations <= 20
|
|
|
|
def test_default_timeout(self):
|
|
"""Verify default timeout is reasonable."""
|
|
settings = Settings()
|
|
|
|
assert 1 <= settings.request_timeout <= 300
|
|
|
|
def test_default_urls_are_valid(self):
|
|
"""Verify default URLs are valid."""
|
|
settings = Settings()
|
|
|
|
assert settings.deepseek_base_url.startswith(("http://", "https://"))
|
|
assert settings.tmdb_base_url.startswith(("http://", "https://"))
|
|
|
|
|
|
class TestConfigEnvironmentVariables:
|
|
"""Tests for environment variable loading."""
|
|
|
|
def test_loads_temperature_from_env(self, monkeypatch):
|
|
"""Verify temperature is loaded from environment."""
|
|
monkeypatch.setenv("LLM_TEMPERATURE", "0.5")
|
|
|
|
settings = Settings()
|
|
|
|
assert settings.llm_temperature == 0.5
|
|
|
|
def test_loads_max_iterations_from_env(self, monkeypatch):
|
|
"""Verify max_iterations is loaded from environment."""
|
|
monkeypatch.setenv("MAX_TOOL_ITERATIONS", "10")
|
|
|
|
settings = Settings()
|
|
|
|
assert settings.max_tool_iterations == 10
|
|
|
|
def test_loads_timeout_from_env(self, monkeypatch):
|
|
"""Verify timeout is loaded from environment."""
|
|
monkeypatch.setenv("REQUEST_TIMEOUT", "60")
|
|
|
|
settings = Settings()
|
|
|
|
assert settings.request_timeout == 60
|
|
|
|
def test_loads_deepseek_url_from_env(self, monkeypatch):
|
|
"""Verify DeepSeek URL is loaded from environment."""
|
|
monkeypatch.setenv("DEEPSEEK_BASE_URL", "https://custom.api.com")
|
|
|
|
settings = Settings()
|
|
|
|
assert settings.deepseek_base_url == "https://custom.api.com"
|
|
|
|
def test_invalid_env_value_raises_error(self, monkeypatch):
|
|
"""Verify invalid environment value raises error."""
|
|
monkeypatch.setenv("LLM_TEMPERATURE", "invalid")
|
|
|
|
with pytest.raises(ValueError):
|
|
Settings()
|