import sys
from pathlib import Path
import pytest

# Añadir raíz del proyecto al path
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))


from src.risk.sizing.percent_risk import PercentRiskSizer


def test_percent_risk_basic():
    sizer = PercentRiskSizer(risk_fraction=0.01)  # 1%

    capital = 10_000
    entry_price = 100
    stop_price = 95

    units = sizer.calculate_size(
        capital=capital,
        entry_price=entry_price,
        stop_price=stop_price
    )

    # riesgo = 100€
    # riesgo por unidad = 5
    # unidades = 100 / 5 = 20
    assert units == 20


def test_percent_risk_zero_distance():
    sizer = PercentRiskSizer(0.01)

    units = sizer.calculate_size(
        capital=10_000,
        entry_price=100,
        stop_price=100
    )

    assert units == 0.0


def test_percent_risk_requires_stop():
    sizer = PercentRiskSizer(0.01)

    with pytest.raises(ValueError):
        sizer.calculate_size(
            capital=10_000,
            entry_price=100
        )
