# retoor <retoor@molodetz.nl>

from tests.conftest import run_async
from devplacepy.services.devii.interaction.broker import InteractionBroker


def test_answer_text_completes_open_prompt():
    captured = {}

    async def wait_site(interaction_id, frame, timeout):
        captured["id"] = interaction_id
        outcome = broker.answer_text("13/01/1990")
        assert outcome["handled"] is True
        assert outcome["status"] == "submitted"
        return outcome["result"]

    broker = InteractionBroker("main", wait_site=wait_site)
    result = run_async(
        broker.prompt(
            {
                "title": "When?",
                "widgets": [{"type": "date", "name": "day", "label": "Day"}],
            }
        )
    )
    assert result.status == "submitted"
    assert result.values == {"day": "13/01/1990"}
    assert result.meta.get("via") == "text"


def test_answer_text_rejects_free_chat_as_date():
    async def wait_site(interaction_id, frame, timeout):
        bad = broker.answer_text("Show me number input then.")
        assert bad["status"] == "error"
        good = broker.answer_text("13/01/1990")
        return good["result"]

    broker = InteractionBroker("main", wait_site=wait_site)
    result = run_async(
        broker.prompt(
            {
                "title": "When?",
                "widgets": [{"type": "date", "name": "day", "label": "Day"}],
            }
        )
    )
    assert result.status == "submitted"
    assert result.values == {"day": "13/01/1990"}
