"""
Tests for engine.types module.
"""

from engine.types import (
    Block,
    FetchResult,
    HeadlineItem,
    items_to_tuples,
    tuples_to_items,
)


class TestHeadlineItem:
    """Tests for HeadlineItem dataclass."""

    def test_create_headline_item(self):
        """Can create HeadlineItem with required fields."""
        item = HeadlineItem(title="Test", source="Source", timestamp="12:00")
        assert item.title == "Test"
        assert item.source == "Source"
        assert item.timestamp == "12:00"

    def test_to_tuple(self):
        """to_tuple returns correct tuple."""
        item = HeadlineItem(title="Test", source="Source", timestamp="12:00")
        assert item.to_tuple() == ("Test", "Source", "12:00")

    def test_from_tuple(self):
        """from_tuple creates HeadlineItem from tuple."""
        item = HeadlineItem.from_tuple(("Test", "Source", "12:00"))
        assert item.title == "Test"
        assert item.source == "Source"
        assert item.timestamp == "12:00"


class TestItemsConversion:
    """Tests for list conversion functions."""

    def test_items_to_tuples(self):
        """Converts list of HeadlineItem to list of tuples."""
        items = [
            HeadlineItem(title="A", source="S", timestamp="10:00"),
            HeadlineItem(title="B", source="T", timestamp="11:00"),
        ]
        result = items_to_tuples(items)
        assert result == [("A", "S", "10:00"), ("B", "T", "11:00")]

    def test_tuples_to_items(self):
        """Converts list of tuples to list of HeadlineItem."""
        tuples = [("A", "S", "10:00"), ("B", "T", "11:00")]
        result = tuples_to_items(tuples)
        assert len(result) == 2
        assert result[0].title == "A"
        assert result[1].title == "B"


class TestFetchResult:
    """Tests for FetchResult dataclass."""

    def test_create_fetch_result(self):
        """Can create FetchResult."""
        items = [HeadlineItem(title="Test", source="Source", timestamp="12:00")]
        result = FetchResult(items=items, linked=1, failed=0)
        assert len(result.items) == 1
        assert result.linked == 1
        assert result.failed == 0

    def test_to_legacy_tuple(self):
        """to_legacy_tuple returns correct format."""
        items = [HeadlineItem(title="Test", source="Source", timestamp="12:00")]
        result = FetchResult(items=items, linked=1, failed=0)
        legacy = result.to_legacy_tuple()
        assert legacy[0] == [("Test", "Source", "12:00")]
        assert legacy[1] == 1
        assert legacy[2] == 0


class TestBlock:
    """Tests for Block dataclass."""

    def test_create_block(self):
        """Can create Block."""
        block = Block(
            content=["line1", "line2"], color="\033[38;5;46m", meta_row_index=1
        )
        assert len(block.content) == 2
        assert block.color == "\033[38;5;46m"
        assert block.meta_row_index == 1

    def test_to_legacy_tuple(self):
        """to_legacy_tuple returns correct format."""
        block = Block(content=["line1"], color="green", meta_row_index=0)
        legacy = block.to_legacy_tuple()
        assert legacy == (["line1"], "green", 0)
