clipwright/tests/test_profile_prompts.py

260 lines
10 KiB
Python

"""Profile -> prompt mapping (PLAN #1, full).
Two things are pinned here:
1. each ``profile_prompts`` helper encodes the right fields, and the seeded
cseea/pedro profiles produce visibly different blocks;
2. a profile actually reaches the clipping detail prompt and each Studio
generator, while a profile-less call renders the prompt it did before.
"""
from types import SimpleNamespace
import pytest
from clipwright.media import thumbnail
from clipwright.pipeline import gemini_worker, profile_prompts, profiles
@pytest.fixture(scope="module")
def cseea():
return profiles.load_profile("cseea")
@pytest.fixture(scope="module")
def pedro():
return profiles.load_profile("pedro")
# --- pure mapping ----------------------------------------------------------
def test_no_profile_is_the_empty_block():
assert profile_prompts.title_rules_block(None) == ""
assert profile_prompts.hook_block(None) == ""
assert profile_prompts.description_block(None) == ""
assert profile_prompts.thumbnail_block(None) == ""
assert profile_prompts.clip_rules_block(None) == ""
def test_title_rules_block_encodes_every_field(cseea):
block = profile_prompts.title_rules_block(cseea)
assert "CHANNEL TITLE RULES" in block
assert "60" in block and "38" in block # max_chars / mobile_chars
assert "nostalgic and calm" in block # tone
assert "first 3 words" in block # keyword_position=start
assert "curiosity gap" in block # required_signal=curiosity
assert "Avoid numbers" in block # number_policy=avoid
assert '"in this video"' in block # banned_patterns
assert '"Exploring"' in block # banned_openers
def test_title_rules_block_differs_per_channel(cseea, pedro):
cseea_block = profile_prompts.title_rules_block(cseea)
pedro_block = profile_prompts.title_rules_block(pedro)
assert cseea_block != pedro_block
assert "consequence or stake" in pedro_block # required_signal=consequence
assert "technical, direct" in pedro_block
assert '"for beginners"' in pedro_block
assert "Avoid numbers" not in pedro_block # pedro allows numbers
def test_hook_block_carries_style_and_patterns(cseea, pedro):
cseea_block = profile_prompts.hook_block(cseea)
assert "CHANNEL HOOK RULES" in cseea_block
assert "visual impossibility" in cseea_block
assert '"this shouldn\'t be possible in Alpha"' in cseea_block
assert "working demo" in profile_prompts.hook_block(pedro)
def test_description_block_carries_template_links_and_tone(cseea, pedro):
block = profile_prompts.description_block(cseea)
assert "CHANNEL DESCRIPTION RULES" in block
assert "#minecraft #alpha #retro" in block # template, newlines escaped
assert "youtube.com/@cseea" in block # links
assert "nostalgic and calm" in block # tone
assert "github.com/pbento" in profile_prompts.description_block(pedro)
def test_thumbnail_block_carries_roles_palette_and_presenter(cseea, pedro):
block = profile_prompts.thumbnail_block(cseea)
assert "curiosity, emotion, technical" in block
assert "mossy green" in block
assert "not expected" in block # presenter_photo=false
pedro_block = profile_prompts.thumbnail_block(pedro)
assert "technical, curiosity, emotion" in pedro_block
assert "available and encouraged" in pedro_block
def test_clip_rules_block_composes_hook_then_title(cseea):
block = profile_prompts.clip_rules_block(cseea)
assert block == profile_prompts.hook_block(cseea) + profile_prompts.title_rules_block(cseea)
assert block.index("CHANNEL HOOK RULES") < block.index("CHANNEL TITLE RULES")
# --- the detail template is byte-identical without a profile ----------------
def _detail_kwargs():
return dict(video_duration=100, language="es", min_clips=2, max_clips=4,
min_secs=10.0, max_secs=20.0, windows_json="[]")
def test_detail_template_empty_block_matches_the_old_template():
# The old template is exactly the new one with the placeholder token removed;
# formatting both with an empty block must be byte-identical.
kwargs = _detail_kwargs()
old = gemini_worker.DETAIL_PROMPT_TEMPLATE.replace("{profile_rules}", "").format(**kwargs)
new = gemini_worker.DETAIL_PROMPT_TEMPLATE.format(profile_rules="", **kwargs)
assert new == old
assert "CHANNEL" not in new
def test_detail_template_block_changes_the_prompt(cseea):
kwargs = _detail_kwargs()
plain = gemini_worker.DETAIL_PROMPT_TEMPLATE.format(profile_rules="", **kwargs)
profiled = gemini_worker.DETAIL_PROMPT_TEMPLATE.format(
profile_rules=profile_prompts.clip_rules_block(cseea), **kwargs)
assert profiled != plain
assert "CHANNEL TITLE RULES" in profiled
assert "CHANNEL HOOK RULES" in profiled
assert "{profile_rules}" not in profiled
# --- the clipping detail call uses ACTIVE_PROFILE ---------------------------
def _dense_transcript():
return {
"text": "alpha beta gamma delta",
"language": "en",
"segments": [
{"start": 0, "end": 30, "text": "alpha",
"words": [{"word": " alpha", "start": 0.0, "end": 1.0}]},
{"start": 30, "end": 60, "text": "beta",
"words": [{"word": " beta", "start": 30.0, "end": 31.0}]},
{"start": 60, "end": 90, "text": "gamma",
"words": [{"word": " gamma", "start": 60.0, "end": 61.0}]},
{"start": 90, "end": 120, "text": "delta",
"words": [{"word": " delta", "start": 90.0, "end": 91.0}]},
],
}
def _capture_detail_prompt(monkeypatch, main, profile):
captured = []
def fake_run_stage(client, model_name, items, build_prompt, schema, key, costs, label):
if key == "shorts":
captured.append(build_prompt(items))
return []
monkeypatch.setattr(main, "_run_stage_split", fake_run_stage)
monkeypatch.setattr(main, "ACTIVE_PROFILE", profile)
monkeypatch.setenv("GEMINI_API_KEY", "test")
monkeypatch.setattr(main.llm_backend, "active", lambda: False)
monkeypatch.setattr(main.genai, "Client", lambda **kwargs: object())
main.get_viral_clips(_dense_transcript(), 120)
return captured
def test_active_profile_reaches_the_clip_detail_prompt(monkeypatch, cseea):
main = pytest.importorskip("clipwright.pipeline.main")
captured = _capture_detail_prompt(monkeypatch, main, cseea)
assert captured
assert "CHANNEL TITLE RULES" in captured[0]
assert "CHANNEL HOOK RULES" in captured[0]
def test_no_active_profile_leaves_the_clip_detail_prompt_plain(monkeypatch):
main = pytest.importorskip("clipwright.pipeline.main")
captured = _capture_detail_prompt(monkeypatch, main, None)
assert captured
assert "CHANNEL" not in captured[0]
# --- the Studio generators ------------------------------------------------
class _RecordingModels:
def __init__(self, responses):
self.prompts = []
self.responses = list(responses)
def generate_content(self, model=None, contents=None, config=None):
self.prompts.append(_prompt_text(contents))
return SimpleNamespace(text=self.responses.pop(0))
class _RecordingClient:
def __init__(self, responses):
self.models = _RecordingModels(responses)
def _prompt_text(contents):
if isinstance(contents, str):
return contents
return "\n".join(c if isinstance(c, str) else (getattr(c, "text", "") or "")
for c in (contents or []))
_BRAINSTORM = '{"transcript_summary": "sum", "candidates": ["Alpha", "Beta"]}'
_CRITIC = ('{"titles": ["Why this broke everything"], "thumbnail_texts": ["A"], '
'"recommended": []}')
_CONCEPTS = ('{"concepts": [{"text": "BIG", "text_position": "left", '
'"text_color": "yellow", "scene": "a desk", "why": "w"}]}')
def _analyze_prompts(monkeypatch, profile):
client = _RecordingClient([_BRAINSTORM, _CRITIC])
monkeypatch.setattr(thumbnail, "_frame_parts", lambda *a, **k: [])
monkeypatch.setattr(thumbnail, "genai", SimpleNamespace(
Client=lambda api_key=None, **k: client))
transcript = {"language": "en", "text": "hello world",
"segments": [{"start": 0.0, "end": 10.0, "text": "hello"}]}
thumbnail.analyze_video_for_titles("key", "video.mp4", transcript=transcript, profile=profile)
return client.models.prompts
def test_titles_brainstorm_and_critic_are_profile_tuned(monkeypatch, pedro):
plain = _analyze_prompts(monkeypatch, None)
profiled = _analyze_prompts(monkeypatch, pedro)
assert len(plain) == 2 and len(profiled) == 2
assert all("CHANNEL" not in p for p in plain)
# The blank line before OUTPUT JSON is preserved without a profile.
assert "no emojis.\n\nOUTPUT JSON" in plain[0]
assert "will win the click.\n\nOUTPUT JSON" in plain[1]
for prompt in profiled:
assert "CHANNEL TITLE RULES" in prompt
assert "CHANNEL HOOK RULES" in prompt
assert profiled != plain
def test_thumbnail_concepts_are_profile_tuned(monkeypatch, cseea):
plain_client = _RecordingClient([_CONCEPTS])
thumbnail.plan_thumbnail_concepts(plain_client, "A title", 1)
profiled_client = _RecordingClient([_CONCEPTS])
thumbnail.plan_thumbnail_concepts(profiled_client, "A title", 1, profile=cseea)
plain = plain_client.models.prompts[0]
profiled = profiled_client.models.prompts[0]
assert "CHANNEL" not in plain
assert "same layout.\n\nPer concept give:" in plain
assert "CHANNEL THUMBNAIL RULES" in profiled
assert "mossy green" in profiled
assert profiled != plain
def test_description_is_profile_tuned(monkeypatch, cseea):
plain_client = _RecordingClient(["DESC"])
monkeypatch.setattr(thumbnail, "genai", SimpleNamespace(
Client=lambda api_key=None, **k: plain_client))
thumbnail.generate_youtube_description(
"key", "A title", [{"start": 0, "end": 1, "text": "hi"}], "en", 60)
plain = plain_client.models.prompts[0]
profiled_client = _RecordingClient(["DESC"])
monkeypatch.setattr(thumbnail, "genai", SimpleNamespace(
Client=lambda api_key=None, **k: profiled_client))
thumbnail.generate_youtube_description(
"key", "A title", [{"start": 0, "end": 1, "text": "hi"}], "en", 60, profile=cseea)
profiled = profiled_client.models.prompts[0]
assert "CHANNEL" not in plain
assert "5000 characters\n\nOUTPUT:" in plain
assert "CHANNEL DESCRIPTION RULES" in profiled
assert "youtube.com/@cseea" in profiled
assert profiled != plain