from recoil.pipeline._lib.scene_clusterer import _build_batch
from types import SimpleNamespace


def _shot(characters, dur=3.0, scene_index=1, shot_id="EP001_SH99", location_id="X"):
    return SimpleNamespace(
        shot_id=shot_id,
        characters=characters,
        duration_s=dur,
        scene_index=scene_index,
        location_id=location_id,
    )


def test_clusterer_tolerates_none_character_entry():
    shots = [_shot([None, "JADE"])]
    batch = _build_batch(shots, idx=0, min_batch_size=1)
    assert batch.shared_characters == ["JADE"]


def test_clusterer_tolerates_dict_character_entry():
    shots = [_shot([{"char_id": "JADE", "role": "protagonist"}, {"char_id": "WREN"}])]
    batch = _build_batch(shots, idx=0, min_batch_size=1)
    assert sorted(batch.shared_characters) == ["JADE", "WREN"]


def test_clusterer_string_path_still_works():
    shots = [_shot(["JADE", "WREN"])]
    batch = _build_batch(shots, idx=0, min_batch_size=1)
    assert sorted(batch.shared_characters) == ["JADE", "WREN"]
