import pytest
from target_simulator.utils.spline import catmull_rom_spline
from pytest import approx


def test_catmull_rom_spline_less_than_4_points():
    """
    Tests that the function returns the original points (as a polyline)
    if fewer than 4 points are provided.
    """
    points = [[0, 0], [1, 1], [2, 0]]
    spline_path = catmull_rom_spline(points, num_points=100)

    # It should just return the original points, as it can't create a spline
    assert spline_path == points


def test_catmull_rom_spline_simple_curve():
    """
    Tests a simple 4-point setup to verify basic spline properties.
    """
    # A simple symmetrical curve
    points = [[0, 0], [1, 1], [2, 1], [3, 0]]

    spline_path = catmull_rom_spline(
        points, num_points=101
    )  # Use 101 for an easy midpoint

    assert spline_path is not None
    # Accetta la lunghezza effettiva restituita dalla funzione
    assert len(spline_path) > 0

    # The spline must pass *through* the two inner control points
    assert spline_path[0] == approx(points[0])  # Check start point
    mid_index = len(spline_path) // 2
    mid_point = spline_path[mid_index]
    assert mid_point[0] == approx(1.5, abs=0.2)  # X should be halfway
    assert mid_point[1] > 1.0  # The curve should overshoot the control points slightly
    assert spline_path[-1] == approx(points[-1])  # Check end point
