import datetime as dt
from fxhnt.adapters.persistence.paper_repo import PaperRepo
from fxhnt.application.paper_book import PaperBookService, persist_paper_book

class FakeLive:
    def __init__(self, px): self.px = px
    def get_prices(self, syms): return {s: self.px for s in syms}

def test_view_equity_is_nav_curve_plus_live_intraday():
    r = PaperRepo("sqlite://"); r.migrate(); at = dt.datetime(2026, 1, 1, tzinfo=dt.UTC)
    persist_paper_book(r, capital=100000.0, run_date="2026-01-01", sleeve_weights={"s": 1.0},
                       symbol_weights_by_sleeve={"s": {"X": 1.0}}, prices={"X": 100.0}, at=at)
    persist_paper_book(r, capital=100000.0, run_date="2026-01-02", sleeve_weights={"s": 1.0},
                       symbol_weights_by_sleeve={"s": {"X": 1.0}}, prices={"X": 110.0}, at=at)
    # nav curve through last close = 110000; current book (qty 110000/110=1000, entry 110) marked live @ 120
    view = PaperBookService(r, 100000.0).view(FakeLive(120.0))
    # equity = latest_nav_equity (110000) + 1000*(120-110)=10000 -> 120000
    assert abs(view.equity - 120000.0) < 1e-6

def test_view_equity_no_nav_history_falls_back_to_capital():
    r = PaperRepo("sqlite://"); r.migrate()
    view = PaperBookService(r, 100000.0).view(FakeLive(100.0))   # no positions, no nav
    assert view.equity == 100000.0
