import datetime as dt

from fxhnt.adapters.persistence.forward_nav import ForwardNavRepo
from fxhnt.application.forward_models import ForwardSummary


def test_upsert_summary_stamps_policy_version_and_hash(tmp_path):
    repo = ForwardNavRepo(f"sqlite:///{tmp_path}/s.db")
    repo.migrate()
    at = dt.datetime(2026, 7, 9, 23, 30)
    sm = ForwardSummary(strategy_id="x", as_of="2026-07-01", days=15, nav=1.02, total_return=0.02,
                        sharpe=1.0, maxdd=-0.01)
    repo.upsert_summary(sm, "PASS", "ok", at, policy_version=3, policy_hash="abc123")
    row = {s.strategy_id: s for s in repo.all_summaries()}["x"]
    assert row.policy_version == 3 and row.policy_hash == "abc123"


def test_upsert_summary_stamp_defaults_none(tmp_path):
    repo = ForwardNavRepo(f"sqlite:///{tmp_path}/s2.db")
    repo.migrate()
    at = dt.datetime(2026, 7, 9, 23, 30)
    sm = ForwardSummary(strategy_id="y", as_of="2026-07-01", days=1, nav=1.0, total_return=0.0,
                        sharpe=0.0, maxdd=0.0)
    repo.upsert_summary(sm, "WAIT", "building", at)   # no stamp passed → columns nullable
    row = {s.strategy_id: s for s in repo.all_summaries()}["y"]
    assert row.policy_version is None and row.policy_hash is None
