import datetime as dt
from fxhnt.adapters.persistence.paper_repo import PaperRepo

def _repo():
    r = PaperRepo("sqlite://"); r.migrate(); return r

def test_upsert_and_read_trailing_returns():
    r = _repo(); at = dt.datetime(2026, 1, 2, tzinfo=dt.UTC)
    r.upsert_sleeve_ret("2026-01-01", "ts", 0.01, at=at)
    r.upsert_sleeve_ret("2026-01-02", "ts", 0.02, at=at)
    r.upsert_sleeve_ret("2026-01-02", "sc", -0.01, at=at)
    out = r.sleeve_returns(before="2026-01-03")
    ed1 = dt.date(2026, 1, 1).toordinal(); ed2 = dt.date(2026, 1, 2).toordinal()
    assert out["ts"] == {ed1: 0.01, ed2: 0.02}
    assert out["sc"] == {ed2: -0.01}

def test_before_is_strict_and_idempotent():
    r = _repo(); at = dt.datetime(2026, 1, 2, tzinfo=dt.UTC)
    r.upsert_sleeve_ret("2026-01-02", "ts", 0.02, at=at)
    r.upsert_sleeve_ret("2026-01-02", "ts", 0.05, at=at)   # overwrite same (date,sleeve)
    assert r.sleeve_returns(before="2026-01-02") == {}      # strict < before
    out = r.sleeve_returns(before="2026-01-03")
    assert out["ts"][dt.date(2026, 1, 2).toordinal()] == 0.05
