"""Tests para kde_density_levels."""

import sys
import os
import numpy as np

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from kde_density_levels import kde_density_levels


def test_kde_density_levels_returns_dict_for_50_points():
    rng = np.random.default_rng(42)
    xs = rng.normal(0, 1, 50).tolist()
    ys = rng.normal(0, 1, 50).tolist()
    result = kde_density_levels(xs, ys)
    assert result is not None
    assert "method" in result
    assert result["method"] in ("kde", "hist")
    assert "densities" in result
    assert len(result["densities"]) == 50
    assert "abs_level" in result
    assert "dense_level" in result


def test_kde_density_levels_none_for_few_points():
    result = kde_density_levels([1.0, 2.0, 3.0], [1.0, 2.0, 3.0])
    assert result is None


def test_kde_density_levels_none_for_4_points():
    result = kde_density_levels([1, 2, 3, 4], [1, 2, 3, 4])
    assert result is None


def test_kde_density_levels_levels_ordered():
    rng = np.random.default_rng(0)
    xs = rng.uniform(0, 10, 100).tolist()
    ys = rng.uniform(0, 10, 100).tolist()
    result = kde_density_levels(xs, ys, abs_quantile=0.1, dense_quantile=0.85)
    assert result is not None
    assert result["abs_level"] <= result["dense_level"]


def test_kde_density_levels_mismatched_lengths():
    result = kde_density_levels([1, 2, 3, 4, 5], [1, 2, 3])
    assert result is None
