"""
Integration tests for figment effect in the pipeline.
"""

from engine.effects.plugins import discover_plugins
from engine.effects.registry import get_registry
from engine.pipeline import Pipeline, PipelineConfig, get_preset
from engine.pipeline.adapters import (
    EffectPluginStage,
    SourceItemsToBufferStage,
    create_stage_from_display,
)
from engine.pipeline.controller import PipelineRunner


class TestFigmentPipeline:
    """Tests for figment effect in pipeline integration."""

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

    def test_figment_in_pipeline(self):
        """Figment effect can be added to pipeline."""
        registry = get_registry()
        figment = registry.get("figment")

        pipeline = Pipeline(
            config=PipelineConfig(
                source="empty",
                display="null",
                camera="feed",
                effects=["figment"],
            )
        )

        # Add source stage
        from engine.data_sources.sources import EmptyDataSource
        from engine.pipeline.adapters import DataSourceStage

        empty_source = EmptyDataSource(width=80, height=24)
        pipeline.add_stage("source", DataSourceStage(empty_source, name="empty"))

        # Add render stage
        pipeline.add_stage("render", SourceItemsToBufferStage(name="items-to-buffer"))

        # Add figment effect stage
        pipeline.add_stage("effect_figment", EffectPluginStage(figment, name="figment"))

        # Add display stage
        from engine.display import DisplayRegistry

        display = DisplayRegistry.create("null")
        display.init(0, 0)
        pipeline.add_stage("display", create_stage_from_display(display, "null"))

        # Build and initialize pipeline
        pipeline.build()
        assert pipeline.initialize()

        # Use PipelineRunner to step through frames
        runner = PipelineRunner(pipeline)
        runner.start()

        # Run pipeline for a few frames
        for i in range(10):
            runner.step()
            # Result might be None for null display, but that's okay

        # Verify pipeline ran without errors
        assert pipeline.context.params.frame_number == 10

    def test_figment_preset(self):
        """Figment preset is properly configured."""
        preset = get_preset("test-figment")
        assert preset is not None
        assert preset.source == "empty"
        assert preset.display == "terminal"
        assert "figment" in preset.effects
