
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x11f7450d0>

    def test_consult_bull_leg_flatten_proceeds(monkeypatch):
        """A held BULL leg: strict-majority FLATTEN (confirmed rollover) → the mechanical exit proceeds."""
        facts = dict(_EXIT_FACTS_LONG_HOLD)
        facts.update(breadth_now=0.40, dist_from_high=0.22, roc_now=-0.30, roc_shallow=False)   # rolled over
        monkeypatch.setattr(TR, "build_mktstate_exit_facts", lambda *a, **k: facts)
        monkeypatch.setattr(TR, "reason_mktstate_exit_ensemble",
                            lambda *a, **k: {"exit_action": "flatten_now", "conviction": 0.85,
                                             "rationale": "rolled over off high, momentum down", "ensemble_n": 5,
                                             "votes": {"flatten_now": 4, "hold_toward_target": 1}, "parse_failed": False})
        db = _RangeDB()
        decision, doc = TR.consult_mktstate_exit(date(2026, 7, 13), "12:30", "bull",
                                                 account_nick="cuixia", symbol="TNA", mongo=db, origin="live")
        assert decision == "flatten" and doc["leg"] == "bull"
>       assert db[TR.MKTSTATE_EXIT_DECISIONS].docs[0]["decision"] == "flatten"
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E       IndexError: list index out of range

tests/services/test_tide_etf_reasoning.py:2534: IndexError
