"""T5: integrazione worker honest/TSM01 nel PortfolioRunner."""
from src.portfolio.runner import build_worker_for, _STRAT_MODULE, _MULTI_KINDS
from src.portfolio.base import SleeveSpec
from src.live.basket_trend_worker import BasketTrendWorker
from src.live.rotation_worker import RotationWorker
from src.live.tsmom_worker import TsmomWorker
from src.live.strategy_worker import StrategyWorker
from scripts.portfolios._defs import PORTFOLIOS


def test_build_basket_worker(tmp_path):
    spec = SleeveSpec(kind="basket", name="TR01", sid="TR01_basket",
                      params={"universe": ["BNB", "BTC"], "tf": "4h"})
    w = build_worker_for(spec, alloc_capital=120.0, leverage=2.0, data_dir=tmp_path)
    assert isinstance(w, BasketTrendWorker) and w.capital == 120.0


def test_build_rotation_and_tsmom(tmp_path):
    rot = SleeveSpec(kind="rotation", name="ROT02", sid="ROT02_rot",
                     params={"universe": ["BTC", "ETH"], "tf": "1d", "top_k": 1, "gross": 0.45})
    tsm = SleeveSpec(kind="tsmom", name="TSM01", sid="TSM01",
                     params={"universe": ["BTC", "ETH"], "tf": "1d", "gross": 0.30})
    wr = build_worker_for(rot, 100.0, 2.0, data_dir=tmp_path)
    wt = build_worker_for(tsm, 100.0, 2.0, data_dir=tmp_path)
    assert isinstance(wr, RotationWorker) and wr.capital == 100.0
    assert isinstance(wt, TsmomWorker) and wt.capital == 100.0


def test_dip01_builds_as_strategy_worker(tmp_path):
    spec = SleeveSpec(kind="single", name="DIP01", sid="DIP01_BTC", asset="BTC")
    w = build_worker_for(spec, 80.0, 2.0, data_dir=tmp_path)
    assert isinstance(w, StrategyWorker) and w.capital == 80.0


def test_port06_has_no_unsupported_sleeves():
    p = PORTFOLIOS["PORT06"]
    unsupported = [s.sid for s in p.sleeves
                   if not (s.kind in ("pairs",) + _MULTI_KINDS or s.name in _STRAT_MODULE)]
    assert unsupported == []
