import pandas as pd
import pytest

from app.screener.nodes.base import percentile_rank


def test_percentile_rank_basic():
    s = pd.Series([10, 20, 30, 40, 50])
    out = percentile_rank(s)
    assert (out >= 0).all() and (out <= 100).all()
    assert out.iloc[0] < out.iloc[-1]                  # smallest gets lowest rank


def test_percentile_rank_all_equal_returns_50():
    s = pd.Series([42, 42, 42, 42])
    out = percentile_rank(s)
    assert (out == 50.0).all()


def test_percentile_rank_handles_nan():
    s = pd.Series([1.0, float("nan"), 3.0, 5.0])
    out = percentile_rank(s)
    assert pd.isna(out.iloc[1])
    assert (out.dropna() >= 0).all()
