"""The director chat edits the script too: a script note persists the new words (and the shot list with them); a bad hook pick is refused before anything is saved.""" from types import SimpleNamespace from uuid import uuid4 import pytest from app.schemas import ( DirectedCreative, FinalCandidate, FinalOutput, GenerationRequest, ScriptDraft, VisualChatResult, VisualScene, ) from app.services import visual_chat as VC def _script(hook="a", words="a b c"): return ScriptDraft(concept_title="c", hook_options=["a", "b", "c"], selected_hook=hook, full_spoken_script=words, estimated_word_count=3, on_screen_text_plan=[], cta="c") def _plan(line): scene = VisualScene(scene_number=1, start_second=0, end_second=3, shot_type="s", camera_movement="c", subject_action="x", setting="y", lighting="z", spoken_line=line, visual_direction="v", purpose="p", transition="cut", editing_notes="") return DirectedCreative(concept_title="c", production_style="UGC", scenes=[scene], audio_direction="a", music_direction="m", production_notes=[], model_generation_notes=[]) def _final(): return FinalOutput(asset_id=uuid4(), status="completed", recommended_candidate_number=1, generated_at="2026-10-16T00:00:00Z", candidates=[FinalCandidate(candidate_number=1, script=_script(), visual_direction=_plan("a b c"), final_score=80.0)]) def test_a_script_note_saves_the_words_and_the_scenes_that_say_them(monkeypatch): saved, actions = {}, [] store = SimpleNamespace(update_asset=lambda aid, v: saved.update(v), log_human_action=lambda *a, **k: actions.append(k["detail"])) monkeypatch.setattr(VC, "snapshot_candidate", lambda *a, **k: None) final = _final() VC._apply(store, {"id": str(uuid4())}, final, 0, GenerationRequest(niche="n", platform="meta", duration_seconds=30), _plan("new words"), _script(hook="b", words="new words")) stored = FinalOutput.model_validate(saved["agent_outputs"]).candidates[0] assert stored.script.selected_hook == "b" and stored.script.full_spoken_script == "new words" assert stored.visual_direction.scenes[0].spoken_line == "new words" assert actions[0]["fields"] == ["script", "visual_direction"] def test_a_rewritten_script_must_pick_one_of_its_own_hooks(monkeypatch): bad = VisualChatResult(reply="done", script=_script().model_copy(update={"selected_hook": "zzz"})) class Gemini: def structured(self, **k): return bad final = _final() store = SimpleNamespace(get_asset=lambda aid: {"id": aid, "agent_outputs": final.model_dump(mode="json")}, update_asset=lambda *a: pytest.fail("saved a bad script")) monkeypatch.setattr(VC, "SupabaseStore", lambda: store) monkeypatch.setattr(VC, "GeminiService", lambda: Gemini()) monkeypatch.setattr(VC, "_brief_from_asset", lambda a: GenerationRequest(niche="n", platform="meta", duration_seconds=30)) with pytest.raises(VC.VisualChatError, match="hook"): VC.chat_about_visuals(str(uuid4()), 1, "shorten the hook") assert "SCRIPT INSTRUCTION" in VC.INSTRUCTION def test_a_shot_list_that_does_not_say_the_new_script_is_dropped_for_a_resync(monkeypatch): # The bizops incident: the chat rewrote the script, the scenes kept the old lines, # and the render spoke the old lines. stale = VisualChatResult(reply="done", script=_script(words="new words"), visual_direction=_plan("a b c")) class Gemini: def structured(self, **k): return stale saved = {} final = _final() store = SimpleNamespace(get_asset=lambda aid: {"id": aid, "agent_outputs": final.model_dump(mode="json")}, update_asset=lambda aid, v: saved.update(v), log_human_action=lambda *a, **k: None) monkeypatch.setattr(VC, "SupabaseStore", lambda: store) monkeypatch.setattr(VC, "GeminiService", lambda: Gemini()) monkeypatch.setattr(VC, "snapshot_candidate", lambda *a, **k: None) monkeypatch.setattr(VC, "_brief_from_asset", lambda a: GenerationRequest(niche="n", platform="meta", duration_seconds=30)) res = VC.chat_about_visuals(str(uuid4()), 1, "use this script") assert res.script is not None and res.visual_direction is None assert FinalOutput.model_validate(saved["agent_outputs"]).candidates[0].script.full_spoken_script == "new words"