import numpy as np

from fxhnt.application.stablecoin_runner import StableReversionRunner
from fxhnt.domain.stablecoin_reversion import short_rich_weights


def test_shorts_only_rich_equal_weight():
    w = short_rich_weights({"A": 0.006, "B": 0.001, "C": 0.008}, thresh=0.005)
    assert set(w) == {"A", "C"}
    assert all(v == -0.5 for v in w.values())


def test_never_longs_cheap_and_empty_when_none_rich():
    assert short_rich_weights({"A": -0.02, "B": -0.006}, thresh=0.005) == {}
    assert short_rich_weights({"A": 0.001}, thresh=0.005) == {}


def test_runner_books_short_reversion_profit():
    panel = {"S0": {0: 1.01, 1: 1.00}, "S1": {0: 1.00, 1: 1.00}}
    res = StableReversionRunner(panel, thresh_bp=50.0, cost_bps=4.0).run()
    assert res.dates == [1]
    expected = -1.0 * (1.00 / 1.01 - 1.0) - 1.0 * (4.0 / 1e4) / 2.0
    assert abs(res.returns[0] - expected) < 1e-12


def test_at_threshold_excluded_strict_inequality():
    # exactly at threshold is NOT rich (strict >) -> no position (death-spiral guard boundary)
    assert short_rich_weights({"A": 0.005}, thresh=0.005) == {}
