"""Unit-Tests ohne DB: abgeleitete Trainingseinheit-Payload-Helfer."""
import pytest

from routers.training_planning import _flatten_exercises_from_sections


def test_flatten_exercises_from_sections_order():
    unit = {
        "sections": [
            {
                "order_index": 1,
                "items": [
                    {"order_index": 1, "item_type": "exercise", "exercise_id": 10},
                    {"order_index": 0, "item_type": "note"},
                    {"order_index": 2, "item_type": "exercise", "exercise_id": 20},
                ],
            },
            {
                "order_index": 0,
                "items": [{"order_index": 0, "item_type": "exercise", "exercise_id": 5}],
            },
        ]
    }
    _flatten_exercises_from_sections(unit)
    # Sektionen nach order_index; innerhalb nur exercise-Items nach order_index
    assert [x["exercise_id"] for x in unit["exercises"]] == [5, 10, 20]


def test_flatten_exercises_from_sections_empty():
    unit = {"sections": []}
    _flatten_exercises_from_sections(unit)
    assert unit["exercises"] == []
