"""Tests for lapis_spec.utils."""

from lapis_spec.utils import (
    generate_operation_id,
    pick_shorter,
    to_snake_case,
    truncate_description,
)


class TestTruncateDescription:
    def test_none_returns_none(self):
        assert truncate_description(None) is None

    def test_empty_string_returns_none(self):
        assert truncate_description("") is None

    def test_short_string_unchanged(self):
        assert truncate_description("Hello world") == "Hello world"

    def test_long_string_truncated(self):
        text = "A" * 100
        result = truncate_description(text)
        assert result is not None
        assert len(result) <= 80
        assert result.endswith("...")

    def test_multiline_flattened(self):
        text = "Line one.\nLine two.\nLine three."
        result = truncate_description(text)
        assert result is not None
        assert "\n" not in result

    def test_whitespace_collapsed(self):
        text = "  hello   world  "
        assert truncate_description(text) == "hello world"

    def test_custom_max_len(self):
        result = truncate_description("Hello world, this is long", max_len=10)
        assert result is not None
        assert len(result) <= 10


class TestToSnakeCase:
    def test_camel_case(self):
        assert to_snake_case("camelCase") == "camel_case"

    def test_pascal_case(self):
        assert to_snake_case("PascalCase") == "pascal_case"

    def test_kebab_case(self):
        assert to_snake_case("kebab-case") == "kebab_case"

    def test_path(self):
        assert to_snake_case("/invoices/{id}") == "invoices_id"

    def test_already_snake(self):
        assert to_snake_case("already_snake") == "already_snake"

    def test_braces_removed(self):
        assert to_snake_case("{param}") == "param"

    def test_consecutive_caps(self):
        assert to_snake_case("HTTPSConnection") == "https_connection"


class TestGenerateOperationId:
    def test_simple(self):
        assert generate_operation_id("get", "/invoices") == "get_invoices"

    def test_with_param(self):
        result = generate_operation_id("get", "/invoices/{invoice_id}")
        assert result == "get_invoices_invoice_id"

    def test_nested_path(self):
        result = generate_operation_id("post", "/invoices/{id}/send")
        assert result == "post_invoices_id_send"


class TestPickShorter:
    def test_both_none(self):
        assert pick_shorter(None, None) is None

    def test_first_none(self):
        assert pick_shorter(None, "hello") == "hello"

    def test_second_none(self):
        assert pick_shorter("hello", None) == "hello"

    def test_first_shorter(self):
        assert pick_shorter("hi", "hello") == "hi"

    def test_second_shorter(self):
        assert pick_shorter("hello", "hi") == "hi"

    def test_equal_length(self):
        assert pick_shorter("abc", "xyz") == "abc"
