"""test_logic.py — `pytest -q` (offline). Tests the UI-agnostic chat_logic layer
(no browser, no streamlit needed). Proves the PoC upholds the same behavior as
the capstone, including the human-approval handshake the UI surfaces."""
from __future__ import annotations

from chat_logic import ChatSession


def test_policy_answer_is_grounded():
    s = ChatSession()
    r = s.send("How long do I have to dispute a transaction?")
    assert "60" in r["answer"]
    assert ("user", "How long do I have to dispute a transaction?") in s.history


def test_balance_answer():
    s = ChatSession()
    r = s.send("what's my balance?")
    assert "1250" in r["answer"]


def test_transfer_requires_then_completes_approval():
    s = ChatSession()
    r = s.send("transfer 100 to AE_friend")
    assert r["pending"] is True
    assert s.awaiting_approval is True
    # the UI cannot send another message until the transfer is decided
    blocked = s.send("what's my balance?")
    assert blocked["pending"] is True
    # approve -> executes, and the session is no longer awaiting
    res = s.approve(True)
    assert "Done" in res["answer"] or "Transferred" in res["answer"]
    assert s.awaiting_approval is False


def test_transfer_can_be_declined():
    s = ChatSession()
    s.send("transfer 50 to AE_x")
    res = s.approve(False)
    assert "cancel" in res["answer"].lower()
    assert s.awaiting_approval is False


def test_refusal_for_out_of_scope():
    s = ChatSession()
    r = s.send("Should I invest in stocks?")
    assert r.get("refused") is True


def test_history_accumulates_for_the_ui():
    s = ChatSession()
    s.send("what's my balance?")
    roles = [role for role, _ in s.history]
    assert roles == ["user", "assistant"]
