# tests/test_explorer_labels.py
"""Tests for SVD axis label derivation in explorer.py."""

import pytest


def test_derive_labels_flip_true():
    """Labels should always reflect what's on each side, regardless of flip.
    negative_pole describes LEFT, positive_pole describes RIGHT."""
    theme = {
        "positive_pole": "Right-wing parties",
        "negative_pole": "Left-wing parties",
        "flip": True,
    }
    flip = theme.get("flip", False)
    pos_pole = theme.get("positive_pole", "")
    neg_pole = theme.get("negative_pole", "")

    # Fixed logic: labels don't depend on flip
    left_pole = neg_pole
    right_pole = pos_pole

    assert left_pole == "Left-wing parties"
    assert right_pole == "Right-wing parties"


def test_derive_labels_flip_false():
    """Labels should always reflect what's on each side, regardless of flip.
    negative_pole describes LEFT, positive_pole describes RIGHT."""
    theme = {
        "positive_pole": "Right-wing parties",
        "negative_pole": "Left-wing parties",
        "flip": False,
    }
    flip = theme.get("flip", False)
    pos_pole = theme.get("positive_pole", "")
    neg_pole = theme.get("negative_pole", "")

    # Fixed logic: labels don't depend on flip
    left_pole = neg_pole
    right_pole = pos_pole

    assert left_pole == "Left-wing parties"
    assert right_pole == "Right-wing parties"


def test_derive_labels_missing_theme():
    """When theme is None, use empty strings as fallback."""
    theme = None
    flip = theme.get("flip", False) if theme else False
    pos_pole = theme.get("positive_pole", "") if theme else ""
    neg_pole = theme.get("negative_pole", "") if theme else ""

    if flip:
        left_pole, right_pole = pos_pole, neg_pole
    else:
        left_pole, right_pole = neg_pole, pos_pole

    assert left_pole == ""
    assert right_pole == ""


def test_motion_assignment_flip_true():
    """When flip=True, positive motions go to left column."""
    pos_motions = [{"motion_id": 1, "score": 0.5}]
    neg_motions = [{"motion_id": 2, "score": -0.5}]
    flip = True

    if flip:
        left_motions, right_motions = pos_motions, neg_motions
    else:
        left_motions, right_motions = neg_motions, pos_motions

    assert left_motions == pos_motions
    assert right_motions == neg_motions


def test_motion_assignment_flip_false():
    """When flip=False, negative motions go to left column."""
    pos_motions = [{"motion_id": 1, "score": 0.5}]
    neg_motions = [{"motion_id": 2, "score": -0.5}]
    flip = False

    if flip:
        left_motions, right_motions = pos_motions, neg_motions
    else:
        left_motions, right_motions = neg_motions, pos_motions

    assert left_motions == neg_motions
    assert right_motions == pos_motions
