"""'Match the reference closely' swaps the 80/20 rule for a rebuild and keeps the cast.""" from types import SimpleNamespace from uuid import uuid4 from app.schemas import EvidenceItem, EvidencePack, GenerationRequest from app.services import workflow as wf def _runner(**brief): runner = wf.CreativeWorkflow(SimpleNamespace(), uuid4(), GenerationRequest(niche="ecom", platform="meta", prompt="x" * 20, **brief)) runner.reference = EvidenceItem(id="reference_ad", kind="concept", title="r", content="c", relevance=1.0, metadata={"reference": {"cast": "two people arguing on a street"}}) return runner def test_mirror_rebuilds_the_reference_and_keeps_its_cast(): rule = _runner(mirror_reference=True)._pacing()["reference_ad"]["rule"] assert "same cast" in rule and "shot for shot" in rule and "80%" not in rule loose = _runner()._pacing()["reference_ad"]["rule"] assert "80%" in loose and "Keep the reference's cast" in loose def test_mirror_trims_the_brain_to_a_sliver(): item = lambda i: EvidenceItem(id=str(i), kind="k", title="t", content="c", relevance=1.0) # noqa: E731 pack = EvidencePack(hook_bank=[item(i) for i in range(20)]) wf._weight_to_reference(pack, item("ref"), 0.05) assert len(pack.hook_bank) == 1 and pack.concepts[0].id == "ref" pack = EvidencePack(hook_bank=[item(i) for i in range(20)]) wf._weight_to_reference(pack, item("ref")) assert len(pack.hook_bank) == 4 def test_a_typed_brief_outranks_the_brain(): runner = wf.CreativeWorkflow(SimpleNamespace(), uuid4(), GenerationRequest(niche="ecom", platform="meta", prompt="x" * 20)) assert "brief_rule" not in runner._pacing() # not decided until execute() reads the brief runner.brief_typed = True assert "the brief wins" in runner._pacing()["brief_rule"] item = lambda i: EvidenceItem(id=str(i), kind="k", title="t", content="c", relevance=1.0) # noqa: E731 pack = EvidencePack(hook_bank=[item(i) for i in range(20)], concepts=[item("c")]) wf._trim_inspiration(pack, wf.TYPED_BRIEF_SHARE) assert len(pack.hook_bank) == 10 and len(pack.concepts) == 1 assert wf._is_placeholder_brief("Make like a reference video") and not wf._is_placeholder_brief("x" * 20) def test_keep_reference_script_skips_the_writers(monkeypatch): """The reference's transcript IS the script: no hook agent, no script agent, no revision pass, one candidate.""" from app.schemas import ReferenceTranscript, Strategy runner = _runner(keep_reference_script=True) runner.reference_script = ReferenceTranscript( spoken="Stop scrolling. This fixed my sleep in a week! Try it tonight.", on_screen_text=["7 nights"], cta="Try it tonight.", ) strategy = Strategy( concept_title="t", one_line_idea="i", audience_awareness="a", primary_pain_point="p", primary_desire="d", angle="x", hook_type="h", hook_idea="h", emotional_arc=[], proof_mechanism="p", narrative_structure=[], cta_strategy="c", evidence_ids=[], patterns_used=[], patterns_avoided=[], ) script = runner._kept_script(strategy) assert script.selected_hook == "Stop scrolling." and script.hook_options == ["Stop scrolling."] * 3 assert script.full_spoken_script.endswith("Try it tonight.") and script.cta == "Try it tonight." assert script.estimated_word_count == 12 and script.on_screen_text_plan == ["7 nights"] assert "FIXED" in runner._pacing()["reference_ad"]["script_rule"] assert "script_rule" not in _runner()._pacing()["reference_ad"] # The writers are never called. monkeypatch.setattr(wf, "HookAgent", lambda *a, **k: (_ for _ in ()).throw(AssertionError("hook agent ran"))) monkeypatch.setattr(wf, "ScriptAgent", lambda *a, **k: (_ for _ in ()).throw(AssertionError("script agent ran"))) monkeypatch.setattr(wf.StrategyAgent, "run", lambda self, *a, **k: strategy) seen = {} monkeypatch.setattr(wf.CreativeWorkflow, "_direct_visuals", lambda self, st, sc, *a, **k: (seen.setdefault("script", sc), SimpleNamespace(scenes=[], production_notes=[]))[1]) monkeypatch.setattr(wf.CreativeWorkflow, "_qa", lambda self, *a, **k: SimpleNamespace(passed=False, overall_score=40, issues=[])) monkeypatch.setattr(wf.CreativeWorkflow, "_revise", lambda self, *a, **k: (_ for _ in ()).throw(AssertionError("revised"))) monkeypatch.setattr(wf.CreativeWorkflow, "_within_budget", lambda self, attempts, visual: attempts[-1]) monkeypatch.setattr(wf, "split_overlong_scenes", lambda *a, **k: []) monkeypatch.setattr(wf.CreativeWorkflow, "_build_broll", lambda self, *a, **k: None) monkeypatch.setattr(wf.CreativeWorkflow, "_bake_broll", lambda self, *a, **k: None) monkeypatch.setattr(wf.CreativeWorkflow, "_save_revision_counts", lambda self, *a, **k: None) monkeypatch.setattr(wf.CreativeWorkflow, "_product", lambda self, **k: {}) monkeypatch.setattr(wf.CreativeWorkflow, "_trend_payload", lambda self, *a: {}) monkeypatch.setattr(wf, "CreativeCandidate", lambda **k: k) out = runner._build_candidate(1, EvidencePack(), None, []) assert out["script"] is seen["script"] and out["hook"] is None and out["script"].full_spoken_script == script.full_spoken_script