"""Factory store: persist a cycle's funnel counts + the deployed allocation (for the cockpit)."""
from __future__ import annotations

import datetime as dt

from fxhnt.adapters.persistence.factory_store import FactoryStore


def test_funnel_and_allocation_roundtrip() -> None:
    st = FactoryStore("sqlite://")
    at = dt.datetime(2026, 6, 15, 0, 0)
    st.record_cycle(cycle_id="c1", proposed=12, tested=12, survived=3, forward=8, deployed=2,
                    retired=1, global_trials=5000, at=at)
    st.record_allocation("c1", {"A": 0.6, "B": 0.4}, at)
    funnel = st.latest_cycle()
    assert funnel.proposed == 12 and funnel.deployed == 2 and funnel.global_trials == 5000
    alloc = {a.strategy_id: a.weight for a in st.allocation("c1")}
    assert abs(alloc["A"] - 0.6) < 1e-9 and abs(alloc["B"] - 0.4) < 1e-9


def test_kill_switch_persists() -> None:
    """set_kill(True) → is_killed() True; set_kill(False) → is_killed() False."""
    st = FactoryStore("sqlite://")
    assert st.is_killed() is False  # default: no row → False
    st.set_kill(True)
    assert st.is_killed() is True
    st.set_kill(False)
    assert st.is_killed() is False
