from __future__ import annotations

import pytest

from botindex_aar.encoding import (
    decode_base64,
    decode_base64url,
    encode_base64,
    encode_base64url,
    utf8_decode,
    utf8_encode,
)


def test_utf8_roundtrip() -> None:
    source = "AAR ✓ 日本語"
    assert utf8_decode(utf8_encode(source)) == source


def test_encode_decode_base64_roundtrip() -> None:
    data = b"\x00\x01\xfe\xff"
    encoded = encode_base64(data)
    assert encoded == "AAH+/w=="
    assert decode_base64(encoded) == data


def test_decode_base64_strips_whitespace() -> None:
    assert decode_base64(" YW Jj\n") == b"abc"


def test_decode_base64_invalid_length() -> None:
    with pytest.raises(ValueError, match="Invalid base64 length"):
        decode_base64("abc")


def test_decode_base64_invalid_input() -> None:
    with pytest.raises(ValueError, match="Invalid base64 input"):
        decode_base64("ab!#")


def test_encode_decode_base64url_roundtrip() -> None:
    data = b"\xfb\xff\x00"
    encoded = encode_base64url(data)
    assert encoded == "-_8A"
    assert decode_base64url(encoded) == data
