85 lines
2.9 KiB
Python
85 lines
2.9 KiB
Python
import pytest
|
|
|
|
from conftest import video_bytes
|
|
from errors import PermanentError
|
|
from naming import add_suffix, build_filename, clean_component, detect_video_extension, normalize_key, same_family
|
|
|
|
|
|
def _name(title, **kw):
|
|
defaults = dict(message_id=42, original_filename=None, mime_type="video/mp4", detected_ext=".mp4")
|
|
defaults.update(kw)
|
|
return build_filename(title, **defaults)
|
|
|
|
|
|
def test_title_used_verbatim():
|
|
assert _name("Class 01 Aman sir.mp4") == ("Class 01 Aman sir.mp4", [])
|
|
|
|
|
|
def test_extension_added_when_missing():
|
|
assert _name("Class 01 Aman sir")[0] == "Class 01 Aman sir.mp4"
|
|
# '.5' is not a video extension, so it stays part of the name
|
|
assert _name("Lecture 1.5", detected_ext=".mkv")[0] == "Lecture 1.5.mkv"
|
|
|
|
|
|
def test_extension_from_mime_when_undetected():
|
|
assert _name("Clip", detected_ext=None, mime_type="video/x-matroska")[0] == "Clip.mkv"
|
|
|
|
|
|
def test_compatible_extension_kept():
|
|
assert _name("Talk.mov", detected_ext=".mp4") == ("Talk.mov", [])
|
|
|
|
|
|
def test_mismatch_kept_by_default_but_reported():
|
|
name, warnings = _name("Talk.mp4", detected_ext=".mkv")
|
|
assert name == "Talk.mp4"
|
|
assert any("does not match" in w for w in warnings)
|
|
|
|
|
|
def test_mismatch_append_policy():
|
|
assert _name("Talk.mp4", detected_ext=".mkv", mismatch_policy="append")[0] == "Talk.mp4.mkv"
|
|
|
|
|
|
def test_missing_title_fallbacks():
|
|
name, warnings = _name(None, original_filename="orig_video.mp4")
|
|
assert name == "orig_video.mp4" and warnings
|
|
name, warnings = _name(" ", original_filename=None)
|
|
assert name == "telegram_42.mp4" and warnings
|
|
|
|
|
|
def test_missing_title_fail_policy():
|
|
with pytest.raises(PermanentError):
|
|
_name(None, missing_title_policy="fail")
|
|
|
|
|
|
def test_sanitizing():
|
|
assert clean_component("a/b\\c\x00d\n e") == "a_b_c d e"
|
|
assert clean_component("Misc• Grammar Practice") == "Misc• Grammar Practice"
|
|
assert clean_component('Q: what? "x" <y>|z*', strict=True) == "Q_ what_ _x_ _y__z_"
|
|
assert len(clean_component("x" * 500 + ".mp4")) == 200
|
|
assert clean_component("x" * 500 + ".mp4").endswith(".mp4")
|
|
|
|
|
|
def test_add_suffix():
|
|
assert add_suffix("Class 01 Aman sir.mp4", "(2)") == "Class 01 Aman sir (2).mp4"
|
|
assert add_suffix("Class 01", "[77]") == "Class 01 [77]"
|
|
|
|
|
|
def test_detect_container(tmp_path):
|
|
cases = {
|
|
".mp4": video_bytes(1000),
|
|
".mkv": b"\x1a\x45\xdf\xa3" + b"\x00" * 30 + b"matroska" + b"\x00" * 100,
|
|
".webm": b"\x1a\x45\xdf\xa3" + b"\x00" * 30 + b"webm" + b"\x00" * 100,
|
|
".avi": b"RIFF\x00\x00\x00\x00AVI LIST",
|
|
None: b"hello world, not a video",
|
|
}
|
|
for expected, data in cases.items():
|
|
path = tmp_path / "f"
|
|
path.write_bytes(data)
|
|
assert detect_video_extension(path) == expected
|
|
|
|
|
|
def test_families_and_keys():
|
|
assert same_family(".MP4", ".mov")
|
|
assert not same_family(".mp4", ".mkv")
|
|
assert normalize_key(" Misc• Grammar Practice ") == normalize_key("misc• grammar practice")
|