"""Tests for benchmark return / alpha helper (pure, no DB)."""

from __future__ import annotations

from datetime import date

import pytest

from app.services.benchmark_service import benchmark_return_pct


def test_benchmark_return_basic():
    closes = {date(2026, 1, 2): 100.0, date(2026, 1, 5): 110.0}
    assert benchmark_return_pct(closes, date(2026, 1, 2), date(2026, 1, 5)) == pytest.approx(10.0)


def test_benchmark_return_uses_nearest_prior_trading_day():
    # No bar on the 4th (weekend) → falls back to the 2nd; as-of the 12th → the 9th.
    closes = {date(2026, 1, 2): 100.0, date(2026, 1, 9): 120.0}
    assert benchmark_return_pct(closes, date(2026, 1, 4), date(2026, 1, 12)) == pytest.approx(20.0)


def test_benchmark_return_none_when_empty():
    assert benchmark_return_pct({}, date(2026, 1, 2), date(2026, 1, 5)) is None


def test_benchmark_return_none_when_open_before_history():
    closes = {date(2026, 1, 10): 100.0}
    assert benchmark_return_pct(closes, date(2026, 1, 2), date(2026, 1, 12)) is None
