"""Test beat-policy-aware element management.

Verifies:
1. VOICE beat with 2 characters → only 1 element (primary)
2. ENTRY_IMAGE beat → 0 character elements
3. BREAK beat → 2 character elements allowed
4. Legacy mode (no policy) → all characters included
"""

import sys
from pathlib import Path

RECOIL_ROOT = Path(__file__).resolve().parent.parent
if str(RECOIL_ROOT) not in sys.path:
    sys.path.insert(0, str(RECOIL_ROOT))

from visual.format_interface import BeatPolicy  # noqa: E402
from visual.elements import ElementManager  # noqa: E402


def test_split_face_dominant():
    """VOICE beat: 2 chars → 1 element, 1 text-only."""
    policy = BeatPolicy(
        max_loras=1, composition_style="FACE_DOMINANT",
        allow_kinetic_prompting=False,
    )
    elem, text = ElementManager.split_characters_by_policy(
        ["SADIE", "DUSTY"], beat_policy=policy,
    )
    assert elem == ["SADIE"]
    assert text == ["DUSTY"]
    print("  [OK] FACE_DOMINANT split (1 element, 1 text)")


def test_split_environment():
    """ENTRY_IMAGE beat: all chars → text-only."""
    policy = BeatPolicy(
        max_loras=0, composition_style="ENVIRONMENT",
        allow_kinetic_prompting=False,
    )
    elem, text = ElementManager.split_characters_by_policy(
        ["SADIE"], beat_policy=policy,
    )
    assert elem == []
    assert text == ["SADIE"]
    print("  [OK] ENVIRONMENT split (0 elements)")


def test_split_ensemble():
    """BREAK beat: 2 chars → 2 elements."""
    policy = BeatPolicy(
        max_loras=2, composition_style="ENSEMBLE",
        allow_kinetic_prompting=True,
    )
    elem, text = ElementManager.split_characters_by_policy(
        ["SADIE", "DUSTY"], beat_policy=policy,
    )
    assert elem == ["SADIE", "DUSTY"]
    assert text == []
    print("  [OK] ENSEMBLE split (2 elements)")


def test_split_legacy():
    """No policy → all characters as elements."""
    elem, text = ElementManager.split_characters_by_policy(
        ["CHAR_A", "CHAR_B", "CHAR_C"], beat_policy=None,
    )
    assert elem == ["CHAR_A", "CHAR_B", "CHAR_C"]
    assert text == []
    print("  [OK] Legacy split (no policy)")


if __name__ == "__main__":
    print("Elements Beat Policy Tests")
    print("=" * 40)
    test_split_face_dominant()
    test_split_environment()
    test_split_ensemble()
    test_split_legacy()
    print("=" * 40)
    print("ALL TESTS PASSED")
