from fxhnt.application.promotion_corr import corr_to_book, deploy_book_returns, passes_backstop


def test_deploy_book_is_equal_weight_per_date_average():
    book = deploy_book_returns({"a": {"2026-06-01": 0.02, "2026-06-02": 0.00},
                                "b": {"2026-06-01": 0.00, "2026-06-02": 0.04}})
    assert book == {"2026-06-01": 0.01, "2026-06-02": 0.02}


def test_corr_needs_min_overlap():
    cand = {"2026-06-01": 0.01, "2026-06-02": -0.01}
    book = {"2026-06-01": 0.01, "2026-06-02": -0.01}
    assert corr_to_book(cand, book, min_overlap=5) is None            # only 2 overlapping < 5
    assert abs(corr_to_book(cand, book, min_overlap=2) - 1.0) < 1e-9  # perfectly correlated


def test_backstop_rules():
    assert passes_backstop(None, book_empty=True, max_corr=0.5) is True     # first edge → pass
    assert passes_backstop(None, book_empty=False, max_corr=0.5) is False   # indeterminate → withhold
    assert passes_backstop(0.30, book_empty=False, max_corr=0.5) is True    # low corr → pass
    assert passes_backstop(0.60, book_empty=False, max_corr=0.5) is False   # high corr → withhold
