"""Unit tests for data_layer.reference_values (summary assembly, no DB)."""

from data_layer.reference_values import build_summary_tiles_from_ranked_rows


def test_build_summary_tiles_single_type_two_rows():
    raw = [
        {
            "type_key": "hr_max",
            "type_label": "HF max",
            "type_sort_order": 1,
            "value_data_type": "decimal",
            "rn": 1,
            "id": 2,
            "effective_date": "2026-04-01",
            "value_numeric": 180.0,
            "value_text": None,
            "unit": "bpm",
        },
        {
            "type_key": "hr_max",
            "type_label": "HF max",
            "type_sort_order": 1,
            "value_data_type": "decimal",
            "rn": 2,
            "id": 1,
            "effective_date": "2026-03-01",
            "value_numeric": 175.0,
            "value_text": None,
            "unit": "bpm",
        },
    ]
    tiles = build_summary_tiles_from_ranked_rows(raw)
    assert len(tiles) == 1
    t = tiles[0]
    assert t["type_key"] == "hr_max"
    assert t["latest"]["value_numeric"] == 180.0
    assert t["previous"]["value_numeric"] == 175.0
    assert "sort_key" not in t


def test_build_summary_tiles_multi_type_order():
    raw = [
        {"type_key": "b", "type_label": "B", "type_sort_order": 2, "value_data_type": "decimal", "rn": 1, "id": 1},
        {"type_key": "a", "type_label": "A", "type_sort_order": 1, "value_data_type": "decimal", "rn": 1, "id": 2},
    ]
    tiles = build_summary_tiles_from_ranked_rows(raw)
    assert [x["type_key"] for x in tiles] == ["a", "b"]
