"""
Tests for CropEffect.
"""

from engine.effects.plugins.crop import CropEffect
from engine.effects.types import EffectContext


def make_ctx(terminal_width: int = 80, terminal_height: int = 24) -> EffectContext:
    """Create a mock EffectContext."""
    return EffectContext(
        terminal_width=terminal_width,
        terminal_height=terminal_height,
        scroll_cam=0,
        ticker_height=terminal_height,
    )


class TestCropEffect:
    """Tests for CropEffect."""

    def test_basic_init(self):
        """CropEffect initializes with defaults."""
        effect = CropEffect()
        assert effect.name == "crop"
        assert effect.config.enabled is True

    def test_crop_wider_buffer(self):
        """CropEffect crops wide buffer to terminal width."""
        effect = CropEffect()
        buf = [
            "This is a very long line that exceeds the terminal width of eighty characters!",
            "Another long line that should also be cropped to fit within the terminal bounds!",
            "Short",
        ]
        ctx = make_ctx(terminal_width=40, terminal_height=10)

        result = effect.process(buf, ctx)

        # Lines should be cropped to 40 chars
        assert len(result[0]) == 40
        assert len(result[1]) == 40
        assert result[2] == "Short" + " " * 35  # padded to width

    def test_crop_taller_buffer(self):
        """CropEffect crops tall buffer to terminal height."""
        effect = CropEffect()
        buf = ["line"] * 30  # 30 lines
        ctx = make_ctx(terminal_width=80, terminal_height=10)

        result = effect.process(buf, ctx)

        # Should be cropped to 10 lines
        assert len(result) == 10

    def test_pad_shorter_lines(self):
        """CropEffect pads lines shorter than width."""
        effect = CropEffect()
        buf = ["short", "medium length", ""]
        ctx = make_ctx(terminal_width=20, terminal_height=5)

        result = effect.process(buf, ctx)

        assert len(result[0]) == 20  # padded
        assert len(result[1]) == 20  # padded
        assert len(result[2]) == 20  # padded (was empty)

    def test_pad_to_height(self):
        """CropEffect pads with empty lines if buffer is too short."""
        effect = CropEffect()
        buf = ["line1", "line2"]
        ctx = make_ctx(terminal_width=20, terminal_height=10)

        result = effect.process(buf, ctx)

        # Should have 10 lines
        assert len(result) == 10
        # Last 8 should be empty padding
        for i in range(2, 10):
            assert result[i] == " " * 20

    def test_empty_buffer(self):
        """CropEffect handles empty buffer."""
        effect = CropEffect()
        ctx = make_ctx()

        result = effect.process([], ctx)

        assert result == []

    def test_uses_context_dimensions(self):
        """CropEffect uses context terminal_width/terminal_height."""
        effect = CropEffect()
        buf = ["x" * 100]
        ctx = make_ctx(terminal_width=50, terminal_height=1)

        result = effect.process(buf, ctx)

        assert len(result[0]) == 50
