import pytest

from a2a_agent.invite import InviteCodeError
from a2a_agent.invite import PAIRING_ALPHABET
from a2a_agent.invite import create_invite_code
from a2a_agent.invite import generate_pairing_code
from a2a_agent.invite import parse_invite_code
from a2a_agent.invite import validate_pairing_code


def test_pairing_code_has_six_unambiguous_characters() -> None:
    code = generate_pairing_code()

    assert len(code) == 6
    assert set(code) <= set(PAIRING_ALPHABET)
    assert not set(code) & {"O", "0", "I", "1", "l"}


def test_invite_round_trip_validates_short_code() -> None:
    code = create_invite_code(
        broker_host="ignored",
        broker_port=8883,
        room="ignored",
        device_name="ignored",
        tls=True,
    )

    assert parse_invite_code(code) == {"code": code}
    assert validate_pairing_code(code.lower()) == code


def test_parse_invite_rejects_broken_code() -> None:
    with pytest.raises(InviteCodeError, match="6 characters"):
        parse_invite_code("TOO-LONG")

    with pytest.raises(InviteCodeError, match="invalid character"):
        parse_invite_code("O0I1LA")
