chore: sprint cleanup — language unification, parser unification, fossils removal

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.
This commit is contained in:
2026-05-17 23:38:00 +02:00
parent ba6f016d49
commit e07c9ec77b
99 changed files with 8833 additions and 6533 deletions
+111
View File
@@ -0,0 +1,111 @@
"""Tests for ``alfred.application.torrents.add_torrent.AddTorrentUseCase``.
Wraps ``QBittorrentClient.add_torrent`` with magnet-link validation and
exception translation into an ``AddTorrentResponse`` envelope.
Coverage:
- ``TestValidation`` — empty / non-string / non-magnet rejection.
- ``TestSuccess`` — client returns True → status="ok".
- ``TestAddFailure`` — client returns False → status="error", error="add_failed".
- ``TestErrorTranslation`` — ``QBittorrentAuthError`` → authentication_failed,
``QBittorrentAPIError`` → api_error.
QBittorrentClient is fully mocked.
"""
from __future__ import annotations
from unittest.mock import MagicMock
import pytest
from alfred.application.torrents.add_torrent import AddTorrentUseCase
from alfred.infrastructure.api.qbittorrent.exceptions import (
QBittorrentAPIError,
QBittorrentAuthError,
)
@pytest.fixture
def client():
return MagicMock()
@pytest.fixture
def use_case(client):
return AddTorrentUseCase(client)
VALID_MAGNET = "magnet:?xt=urn:btih:abc"
# --------------------------------------------------------------------------- #
# Validation #
# --------------------------------------------------------------------------- #
class TestValidation:
@pytest.mark.parametrize("bad", ["", None, 42, b"magnet:?x"])
def test_invalid_inputs_return_validation_failed(self, use_case, client, bad):
r = use_case.execute(bad)
assert r.status == "error"
assert r.error == "validation_failed"
client.add_torrent.assert_not_called()
def test_non_magnet_scheme_rejected(self, use_case, client):
r = use_case.execute("http://example.com/torrent")
assert r.status == "error"
assert r.error == "validation_failed"
assert "magnet" in r.message.lower()
client.add_torrent.assert_not_called()
# --------------------------------------------------------------------------- #
# Success #
# --------------------------------------------------------------------------- #
class TestSuccess:
def test_add_success(self, use_case, client):
client.add_torrent.return_value = True
r = use_case.execute(VALID_MAGNET)
assert r.status == "ok"
assert r.error is None
assert "success" in r.message.lower()
client.add_torrent.assert_called_once_with(VALID_MAGNET)
# --------------------------------------------------------------------------- #
# Add failure #
# --------------------------------------------------------------------------- #
class TestAddFailure:
def test_add_returns_false(self, use_case, client):
client.add_torrent.return_value = False
r = use_case.execute(VALID_MAGNET)
assert r.status == "error"
assert r.error == "add_failed"
# --------------------------------------------------------------------------- #
# Error translation #
# --------------------------------------------------------------------------- #
class TestErrorTranslation:
def test_auth_error_translated(self, use_case, client):
client.add_torrent.side_effect = QBittorrentAuthError("bad creds")
r = use_case.execute(VALID_MAGNET)
assert r.status == "error"
assert r.error == "authentication_failed"
# The message is a fixed user-facing string, not the raw exception.
assert "authenticate" in r.message.lower()
def test_api_error_translated(self, use_case, client):
client.add_torrent.side_effect = QBittorrentAPIError("server down")
r = use_case.execute(VALID_MAGNET)
assert r.status == "error"
assert r.error == "api_error"
assert "server down" in r.message