174 lines
6.9 KiB
Python
174 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"]
|