clipwright/tests/test_profiles.py

214 lines
7.6 KiB
Python

"""Channel profile loader (PLAN #1 v0).
Covers the happy path, the defaults for a minimal file, the failure modes a
bad ``channels/<slug>.json`` produces, slug hardening, and the
``CHANNEL_PROFILE`` env fallback. Also asserts the two seeded profiles load.
"""
import json
import pytest
from clipwright.pipeline import profiles
FULL = {
"name": "Full Channel",
"language": "pt",
"tone": "calm",
"title_rules": {
"max_chars": 55,
"mobile_chars": 30,
"banned_patterns": ["lol"],
"keyword_position": "end",
"number_policy": "avoid",
"required_signal": "curiosity",
"banned_openers": ["Playing"],
},
"hook_patterns": ["glitch at frame 0"],
"hook_style": "visual impossibility",
"description_template": "{hook}",
"links": ["https://example.test/a"],
"thumbnail": {
"role_order": ["curiosity", "technical"],
"palette": ["green"],
"presenter_photo": True,
},
"default_postiz_integration_id": "int_1",
"published_video_history": [{"id": "vid_1"}],
}
def _write(dir_, slug, data):
path = dir_ / f"{slug}.json"
path.write_text(json.dumps(data), encoding="utf-8")
return path
# --- happy path ------------------------------------------------------------
def test_full_profile_round_trips(tmp_path):
_write(tmp_path, "full", FULL)
p = profiles.load_profile("full", channels_dir=tmp_path)
assert p.slug == "full"
assert p.name == "Full Channel"
assert p.language == "pt"
assert p.tone == "calm"
assert p.title_rules.max_chars == 55
assert p.title_rules.mobile_chars == 30
assert p.title_rules.banned_patterns == ["lol"]
assert p.title_rules.keyword_position == "end"
assert p.title_rules.number_policy == "avoid"
assert p.title_rules.required_signal == "curiosity"
assert p.title_rules.banned_openers == ["Playing"]
assert p.hook_patterns == ["glitch at frame 0"]
assert p.hook_style == "visual impossibility"
assert p.description_template == "{hook}"
assert p.links == ["https://example.test/a"]
assert p.thumbnail.role_order == ["curiosity", "technical"]
assert p.thumbnail.palette == ["green"]
assert p.thumbnail.presenter_photo is True
assert p.default_postiz_integration_id == "int_1"
assert p.published_video_history == [{"id": "vid_1"}]
# --- defaults --------------------------------------------------------------
def test_minimal_profile_uses_defaults(tmp_path):
_write(tmp_path, "bare", {})
p = profiles.load_profile("bare", channels_dir=tmp_path)
assert p.language == "en"
assert p.name == ""
assert p.tone == ""
assert p.title_rules.max_chars == 70
assert p.title_rules.mobile_chars == 40
assert p.title_rules.banned_patterns == []
assert p.title_rules.keyword_position == "start"
assert p.title_rules.number_policy == "allow"
assert p.title_rules.required_signal == "either"
assert p.title_rules.banned_openers == []
assert p.hook_patterns == []
assert p.links == []
assert p.thumbnail.role_order == ["curiosity", "emotion", "technical"]
assert p.thumbnail.presenter_photo is False
assert p.default_postiz_integration_id is None
assert p.published_video_history == []
def test_partial_nested_section_keeps_sibling_defaults(tmp_path):
_write(tmp_path, "partial", {"title_rules": {"max_chars": 12}})
p = profiles.load_profile("partial", channels_dir=tmp_path)
assert p.title_rules.max_chars == 12
assert p.title_rules.mobile_chars == 40
# --- failure modes ---------------------------------------------------------
def test_missing_file_raises_clear_error(tmp_path):
with pytest.raises(profiles.ProfileError) as exc:
profiles.load_profile("nope", channels_dir=tmp_path)
assert "not found" in str(exc.value)
assert "nope.json" in str(exc.value)
def test_malformed_json_raises_clear_error(tmp_path):
(tmp_path / "broken.json").write_text("{ not json", encoding="utf-8")
with pytest.raises(profiles.ProfileError) as exc:
profiles.load_profile("broken", channels_dir=tmp_path)
assert "malformed JSON" in str(exc.value)
def test_top_level_must_be_an_object(tmp_path):
(tmp_path / "list.json").write_text("[]", encoding="utf-8")
with pytest.raises(profiles.ProfileError) as exc:
profiles.load_profile("list", channels_dir=tmp_path)
assert "must be a JSON object" in str(exc.value)
@pytest.mark.parametrize("bad", [
{"title_rules": {"max_chars": "big"}},
{"title_rules": []},
{"hook_patterns": "not-a-list"},
{"thumbnail": {"presenter_photo": "yes"}},
{"title_rules": {"required_signal": "profit"}},
{"published_video_history": ["not-an-object"]},
])
def test_wrong_types_and_enums_are_refused(tmp_path, bad):
_write(tmp_path, "bad", bad)
with pytest.raises(profiles.ProfileError):
profiles.load_profile("bad", channels_dir=tmp_path)
def test_slug_cannot_escape_the_channels_dir(tmp_path):
with pytest.raises(profiles.ProfileError) as exc:
profiles.load_profile("../../etc/passwd", channels_dir=tmp_path)
assert "invalid channel slug" in str(exc.value)
# --- env fallback ----------------------------------------------------------
def test_env_var_selects_the_profile_when_no_slug_given(tmp_path, monkeypatch):
_write(tmp_path, "fromenv", {"tone": "from env"})
monkeypatch.setenv(profiles.ENV_VAR, "fromenv")
p = profiles.load_profile(channels_dir=tmp_path)
assert p.slug == "fromenv"
assert p.tone == "from env"
def test_explicit_slug_wins_over_env(tmp_path, monkeypatch):
_write(tmp_path, "explicit", {"tone": "explicit"})
_write(tmp_path, "fromenv", {"tone": "from env"})
monkeypatch.setenv(profiles.ENV_VAR, "fromenv")
p = profiles.load_profile("explicit", channels_dir=tmp_path)
assert p.slug == "explicit"
def test_no_slug_and_no_env_is_an_error(tmp_path, monkeypatch):
monkeypatch.delenv(profiles.ENV_VAR, raising=False)
with pytest.raises(profiles.ProfileError):
profiles.load_profile(channels_dir=tmp_path)
# --- the seeded profiles ---------------------------------------------------
@pytest.mark.parametrize("slug", ["cseea", "pedro"])
def test_seeded_profiles_load(slug):
p = profiles.load_profile(slug)
assert p.slug == slug
assert p.language == "en"
assert p.title_rules.max_chars > 0
assert p.hook_patterns
# --- list_profiles (dashboard selector) ------------------------------------
def test_list_profiles_seeded_slugs_use_display_names():
listed = profiles.list_profiles()
by_slug = {item["slug"]: item["name"] for item in listed}
assert by_slug["cseea"] == "cseea"
assert by_slug["pedro"] == "Pedro Bento"
# Sorted by slug and every entry is exactly {slug, name}.
assert [item["slug"] for item in listed] == sorted(by_slug)
assert all(set(item) == {"slug", "name"} for item in listed)
def test_list_profiles_falls_back_to_slug_without_a_name(tmp_path):
_write(tmp_path, "noname", {"language": "en"})
assert profiles.list_profiles(channels_dir=tmp_path) == [
{"slug": "noname", "name": "noname"},
]
def test_list_profiles_skips_malformed_files(tmp_path):
_write(tmp_path, "good", {"name": "Good One"})
(tmp_path / "broken.json").write_text("{ not json", encoding="utf-8")
(tmp_path / "list.json").write_text("[]", encoding="utf-8")
(tmp_path / "not a slug.json").write_text("{}", encoding="utf-8")
assert profiles.list_profiles(channels_dir=tmp_path) == [
{"slug": "good", "name": "Good One"},
]
def test_list_profiles_missing_dir_is_empty(tmp_path):
assert profiles.list_profiles(channels_dir=tmp_path / "absent") == []