Files
alfred/tests/infrastructure/test_subtitle_metadata_store.py
T
francwa e07c9ec77b 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.
2026-05-17 23:38:00 +02:00

172 lines
6.9 KiB
Python

"""Tests for ``alfred.infrastructure.subtitle.metadata_store.SubtitleMetadataStore``.
Subtitle-pipeline view over a per-release ``.alfred/metadata.yaml``.
Coverage:
- ``TestPatternDelegation`` — ``confirmed_pattern`` / ``mark_pattern_confirmed``
delegate to the generic store.
- ``TestAppendHistory`` — entry shape (placed_at, release_group, tracks),
per-track fields (language/type/format/source_file/placed_as/confidence),
type inference from filename pieces (en.sdh.srt → "sdh"),
empty pairs → no-op, season/episode included only when given.
"""
from __future__ import annotations
from pathlib import Path
from alfred.domain.subtitles.entities import SubtitleCandidate
from alfred.domain.subtitles.services.placer import PlacedTrack
from alfred.domain.subtitles.value_objects import (
SubtitleFormat,
SubtitleLanguage,
SubtitleType,
)
from alfred.infrastructure.subtitle.metadata_store import SubtitleMetadataStore
SRT = SubtitleFormat(id="srt", extensions=[".srt"])
FRA = SubtitleLanguage(code="fra", tokens=["fr"])
ENG = SubtitleLanguage(code="eng", tokens=["en"])
def _track(lang=FRA, *, embedded: bool = False, confidence: float = 0.92) -> SubtitleCandidate:
return SubtitleCandidate(
language=lang,
format=SRT,
subtitle_type=SubtitleType.STANDARD,
is_embedded=embedded,
confidence=confidence,
)
def _placed(src_name: str, dest_name: str, dest_dir: Path) -> PlacedTrack:
return PlacedTrack(
source=Path("/in") / src_name,
destination=dest_dir / dest_name,
filename=dest_name,
)
# --------------------------------------------------------------------------- #
# Pattern delegation #
# --------------------------------------------------------------------------- #
class TestPatternDelegation:
def test_confirmed_pattern_initially_none(self, tmp_path):
s = SubtitleMetadataStore(tmp_path)
assert s.confirmed_pattern() is None
def test_mark_then_read_back(self, tmp_path):
s = SubtitleMetadataStore(tmp_path)
s.mark_pattern_confirmed("adjacent", {"media_type": "movie"})
assert s.confirmed_pattern() == "adjacent"
# --------------------------------------------------------------------------- #
# append_history #
# --------------------------------------------------------------------------- #
class TestAppendHistory:
def test_empty_pairs_is_noop(self, tmp_path):
s = SubtitleMetadataStore(tmp_path)
s.append_history([])
assert s.history() == []
# No .alfred dir written either.
assert not (tmp_path / ".alfred").exists()
def test_single_entry_shape(self, tmp_path):
s = SubtitleMetadataStore(tmp_path)
# Two-segment filename (after rsplit on '.', 2) → falls into the
# "standard" branch only when len(parts) != 3. Here we pass a 2-part
# name like ``moviesrt`` with one extension piece via an artificial
# case — easier: use a "Movie.srt" simulation.
p = _placed("input.srt", "Movie.srt", tmp_path)
t = _track(lang=FRA, confidence=0.875)
s.append_history([(p, t)], release_group="GRP")
hist = s.history()
assert len(hist) == 1
entry = hist[0]
assert entry["release_group"] == "GRP"
assert "placed_at" in entry
assert entry["tracks"] == [
{
"language": "fra",
"type": "standard", # 2-part filename → default
"format": "srt",
"is_embedded": False,
"source_file": "input.srt",
"placed_as": "Movie.srt",
"confidence": 0.875,
}
]
def test_type_inferred_from_filename_segments(self, tmp_path):
s = SubtitleMetadataStore(tmp_path)
# The implementation uses ``filename.rsplit('.', 2)`` and reads
# ``parts[1]``. For "Show.eng.sdh.srt" → ["Show.eng", "sdh", "srt"]
# → type="sdh". For "Show.fra.srt" → ["Show", "fra", "srt"]
# → type="fra" (a known quirk — language token leaks into the type
# slot when the filename has exactly three rsplit pieces).
p_sdh = _placed("a.srt", "Show.eng.sdh.srt", tmp_path)
p_forced = _placed("b.srt", "Show.fra.forced.srt", tmp_path)
p_two_part = _placed("c.srt", "Show.srt", tmp_path) # < 3 → "standard"
s.append_history(
[(p_sdh, _track(ENG)), (p_forced, _track(FRA)), (p_two_part, _track(FRA))],
)
tracks = s.history()[0]["tracks"]
assert tracks[0]["type"] == "sdh"
assert tracks[1]["type"] == "forced"
assert tracks[2]["type"] == "standard"
def test_unknown_language_when_track_has_no_language(self, tmp_path):
s = SubtitleMetadataStore(tmp_path)
p = _placed("a.srt", "Show.und.srt", tmp_path)
t = _track(lang=None)
s.append_history([(p, t)])
assert s.history()[0]["tracks"][0]["language"] == "unknown"
def test_embedded_flag_propagated(self, tmp_path):
s = SubtitleMetadataStore(tmp_path)
p = _placed("x.srt", "Show.fra.srt", tmp_path)
t = _track(embedded=True)
s.append_history([(p, t)])
assert s.history()[0]["tracks"][0]["is_embedded"] is True
def test_season_and_episode_present_when_given(self, tmp_path):
s = SubtitleMetadataStore(tmp_path)
p = _placed("x.srt", "Show.S01E03.fra.srt", tmp_path)
s.append_history([(p, _track())], season=1, episode=3)
entry = s.history()[0]
assert entry["season"] == 1
assert entry["episode"] == 3
def test_season_and_episode_absent_when_omitted(self, tmp_path):
s = SubtitleMetadataStore(tmp_path)
p = _placed("x.srt", "Movie.fra.srt", tmp_path)
s.append_history([(p, _track())])
entry = s.history()[0]
assert "season" not in entry
assert "episode" not in entry
def test_confidence_rounded_to_3_decimals(self, tmp_path):
s = SubtitleMetadataStore(tmp_path)
p = _placed("x.srt", "X.fra.srt", tmp_path)
t = _track(confidence=0.123456789)
s.append_history([(p, t)])
assert s.history()[0]["tracks"][0]["confidence"] == 0.123
def test_release_group_appended_to_top_level_groups(self, tmp_path):
s = SubtitleMetadataStore(tmp_path)
p = _placed("x.srt", "X.fra.srt", tmp_path)
s.append_history([(p, _track())], release_group="GRP1")
s.append_history([(p, _track())], release_group="GRP1") # dup
s.append_history([(p, _track())], release_group="GRP2")
# Use the underlying MetadataStore by reading the YAML directly.
from alfred.infrastructure.metadata.store import MetadataStore
groups = MetadataStore(tmp_path).load().get("release_groups", [])
assert groups == ["GRP1", "GRP2"]