# retoor <retoor@molodetz.nl>

import pytest

from devplacepy.services.devii.errors import ToolInputError
from devplacepy.services.devii.interaction.schema import (
    validate_prompt_args,
)


def test_valid_confirm_prompt():
    req = validate_prompt_args(
        {
            "title": "Delete archive row?",
            "description": "Remote files are untouched.",
            "widgets": [
                {
                    "type": "confirm",
                    "name": "delete",
                    "label": "Delete archive row",
                    "default": False,
                }
            ],
            "submit_label": "Delete",
            "cancel_label": "Keep",
        }
    )
    assert req.title.startswith("Delete")
    assert req.widgets[0].type == "confirm"
    assert req.widgets[0].name == "delete"


def test_choice_requires_options():
    with pytest.raises(ToolInputError):
        validate_prompt_args(
            {
                "title": "Env?",
                "widgets": [{"type": "choice", "name": "env", "label": "Env"}],
            }
        )


def test_rejects_empty_widgets():
    with pytest.raises(ToolInputError):
        validate_prompt_args({"title": "x", "widgets": []})


def test_rejects_bad_option_value():
    with pytest.raises(ToolInputError):
        validate_prompt_args(
            {
                "title": "Pick",
                "widgets": [
                    {
                        "type": "choice",
                        "name": "x",
                        "label": "X",
                        "options": [{"value": "BAD VALUE!", "label": "Bad"}],
                    }
                ],
            }
        )


def test_group_nesting_limit():
    leaf = {
        "type": "text",
        "name": "a",
        "label": "A",
        "required": False,
    }
    deep = {
        "type": "group",
        "label": "G1",
        "widgets": [
            {
                "type": "group",
                "label": "G2",
                "widgets": [
                    {
                        "type": "group",
                        "label": "G3",
                        "widgets": [
                            {
                                "type": "group",
                                "label": "G4",
                                "widgets": [leaf],
                            }
                        ],
                    }
                ],
            }
        ],
    }
    with pytest.raises(ToolInputError):
        validate_prompt_args({"title": "Deep", "widgets": [deep]})
