clipwright/tests/test_layout_picker.py
juancarlos.cavero 3f7ac4da18 feat(reframe): pick the layout from 12 frames instead of uploading the video
Gemini bills video at ~300 tokens per second. An hour of source is ~1.08M
tokens (past a 1M context window) and a 1-2GB upload to get one word back. This
pipeline ingests hour-long podcasts, so uploading the file failed in exactly the
normal case.

Measured over the same 48 clips against the hand-checked labels:

  whole video          94% / 92% / 96%   18,17,18 of 20 found
  12 frames @ 640px    90% / 90%         15,16 of 20
  12 frames @ 1024px   92%               17 of 20
  24 frames @ 1024px   90%               16 of 20

Resolution was the gap, not frame count: at 640px a spreadsheet is unreadable,
and doubling the frames made it slightly worse. At 1024px the difference from
sending the whole video sits inside the run-to-run variance the whole-video mode
already has (92-96%), at 2.2s per clip instead of ~15s, with a cost that no
longer depends on the source duration.

It also removes the Files API dance: no upload, no waiting for ACTIVE.
2026-07-31 13:28:45 +02:00

117 lines
4.8 KiB
Python

import sys
import types
import layout_picker
from layout_picker import _module_flags, apply, pick
def fake_modules(monkeypatch, **initial):
"""Install stand-in layout modules so apply() can be tested without them."""
made = {}
for name in ("split_layout", "screencast_layout", "active_speaker"):
module = types.ModuleType(name)
module.ENABLED = initial.get(name, False)
monkeypatch.setitem(sys.modules, name, module)
made[name] = module
return made
class TestDecisionMapping:
def test_none_enables_nothing(self):
assert _module_flags("none") == []
def test_screencast_enables_its_module(self):
assert _module_flags("screencast") == ["screencast_layout"]
def test_split_also_needs_the_speaker_signal(self):
# Stacking without knowing who talks is the failure mode the corpus
# run showed: half a frame given to someone who never speaks.
assert "active_speaker" in _module_flags("split")
def test_unknown_and_empty_decisions_are_inert(self):
for value in ("gameplay", "", None, "SPLIT-SCREEN"):
assert _module_flags(value) == []
def test_decision_is_case_and_space_insensitive(self):
assert _module_flags(" SCREENCAST ") == ["screencast_layout"]
class TestApply:
def test_apply_switches_the_right_module_on(self, monkeypatch):
mods = fake_modules(monkeypatch)
touched = apply("screencast")
assert touched == ["screencast_layout"]
assert mods["screencast_layout"].ENABLED is True
assert mods["split_layout"].ENABLED is False
def test_apply_none_touches_nothing(self, monkeypatch):
mods = fake_modules(monkeypatch)
assert apply("none") == []
assert not any(m.ENABLED for m in mods.values())
def test_apply_never_disables_an_explicit_choice(self, monkeypatch):
# An operator who set SPLIT_LAYOUT=1 gets stacking even if the model
# says "none". The picker only ever adds.
mods = fake_modules(monkeypatch, split_layout=True)
apply("none")
assert mods["split_layout"].ENABLED is True
def test_already_enabled_modules_are_not_reported_as_touched(self, monkeypatch):
fake_modules(monkeypatch, screencast_layout=True)
assert apply("screencast") == []
def test_split_enables_both_modules(self, monkeypatch):
mods = fake_modules(monkeypatch)
assert set(apply("split")) == {"split_layout", "active_speaker"}
assert mods["split_layout"].ENABLED is True
assert mods["active_speaker"].ENABLED is True
class TestPick:
def test_disabled_picker_never_calls_out(self, monkeypatch):
monkeypatch.setattr(layout_picker, "ENABLED", False)
monkeypatch.setenv("GEMINI_API_KEY", "x")
assert pick("video.mp4", 60) == "none"
def test_missing_api_key_degrades_to_none(self, monkeypatch):
monkeypatch.setattr(layout_picker, "ENABLED", True)
monkeypatch.delenv("GEMINI_API_KEY", raising=False)
assert pick("video.mp4", 60) == "none"
def test_a_failed_call_degrades_to_none(self, monkeypatch):
# Import failure inside pick() stands in for any API error; the job
# must keep rendering with today's routing rather than die.
monkeypatch.setattr(layout_picker, "ENABLED", True)
monkeypatch.setenv("GEMINI_API_KEY", "x")
monkeypatch.setitem(sys.modules, "google.genai", None)
assert pick("video.mp4", 60) == "none"
class TestValidDecisions:
def test_every_valid_decision_maps_to_known_modules(self):
known = {"split_layout", "screencast_layout", "active_speaker"}
for decision in layout_picker.VALID:
assert set(_module_flags(decision)) <= known
def test_none_is_a_valid_decision(self):
# The model answers "none" for most videos; it must not be treated as
# an unrecognised value and logged as a warning on every job.
assert "none" in layout_picker.VALID
class TestSampleFrames:
def test_unreadable_video_yields_no_frames(self):
# A path that cannot be opened must come back empty, not raise: pick()
# turns "no frames" into "none" and the job carries on.
assert layout_picker.sample_frames("/nonexistent/video.mp4") == []
def test_no_frames_degrades_to_none(self, monkeypatch):
monkeypatch.setattr(layout_picker, "ENABLED", True)
monkeypatch.setenv("GEMINI_API_KEY", "x")
monkeypatch.setattr(layout_picker, "sample_frames", lambda *a, **k: [])
assert pick("video.mp4", 60) == "none"
def test_sample_size_is_configurable(self, monkeypatch):
# The 12/1024px pair is measured; keep it overridable without a deploy.
monkeypatch.setenv("LAYOUT_SAMPLE_FRAMES", "6")
assert layout_picker.SAMPLE_FRAMES == 12 # read at import, not per call