"""
Tests for the FigmentOverlayEffect plugin.
"""

import pytest

from engine.effects.plugins import discover_plugins
from engine.effects.registry import get_registry
from engine.effects.types import EffectConfig, create_effect_context
from engine.pipeline.adapters import EffectPluginStage


class TestFigmentEffect:
    """Tests for FigmentOverlayEffect."""

    def setup_method(self):
        """Discover plugins before each test."""
        discover_plugins()

    def test_figment_plugin_discovered(self):
        """Figment plugin is discovered and registered."""
        registry = get_registry()
        figment = registry.get("figment")
        assert figment is not None
        assert figment.name == "figment"

    def test_figment_plugin_enabled_by_default(self):
        """Figment plugin is enabled by default."""
        registry = get_registry()
        figment = registry.get("figment")
        assert figment.config.enabled is True

    def test_figment_renders_overlay(self):
        """Figment renders SVG overlay after interval."""
        registry = get_registry()
        figment = registry.get("figment")

        # Configure with short interval for testing
        config = EffectConfig(
            enabled=True,
            intensity=1.0,
            params={
                "interval_secs": 0.1,  # 100ms
                "display_secs": 1.0,
                "figment_dir": "figments",
            },
        )
        figment.configure(config)

        # Create test buffer
        buf = [" " * 80 for _ in range(24)]

        # Create context
        ctx = create_effect_context(
            terminal_width=80,
            terminal_height=24,
            frame_number=0,
        )

        # Process frames until figment renders
        for i in range(20):
            result = figment.process(buf, ctx)
            if len(result) > len(buf):
                # Figment rendered overlay
                assert len(result) > len(buf)
                # Check that overlay lines contain ANSI escape codes
                overlay_lines = result[len(buf) :]
                assert len(overlay_lines) > 0
                # First overlay line should contain cursor positioning
                assert "\x1b[" in overlay_lines[0]
                assert "H" in overlay_lines[0]
                return
            ctx.frame_number += 1

        pytest.fail("Figment did not render in 20 frames")

    def test_figment_stage_capabilities(self):
        """EffectPluginStage wraps figment correctly."""
        registry = get_registry()
        figment = registry.get("figment")

        stage = EffectPluginStage(figment, name="figment")
        assert "effect.figment" in stage.capabilities

    def test_figment_configure_preserves_params(self):
        """Figment configuration preserves figment_dir."""
        registry = get_registry()
        figment = registry.get("figment")

        # Configure without figment_dir
        config = EffectConfig(
            enabled=True,
            intensity=1.0,
            params={
                "interval_secs": 30.0,
                "display_secs": 3.0,
            },
        )
        figment.configure(config)

        # figment_dir should be preserved
        assert "figment_dir" in figment.config.params
        assert figment.config.params["figment_dir"] == "figments"
