"""Tests for FirehoseEffect plugin."""

import pytest

from engine.effects.plugins.firehose import FirehoseEffect
from engine.effects.types import EffectContext


@pytest.fixture(autouse=True)
def patch_config(monkeypatch):
    """Patch config globals for firehose tests."""
    import engine.config as config

    monkeypatch.setattr(config, "FIREHOSE", False)
    monkeypatch.setattr(config, "FIREHOSE_H", 12)
    monkeypatch.setattr(config, "MODE", "news")
    monkeypatch.setattr(config, "GLITCH", "░▒▓█▌▐╌╍╎╏┃┆┇┊┋")
    monkeypatch.setattr(config, "KATA", "ﾊﾐﾋｰｳｼﾅﾓﾆｻﾜﾂｵﾘｱﾎﾃﾏｹﾒｴｶｷﾑﾕﾗｾﾈｽﾀﾇﾍ")


def test_firehose_disabled_returns_input():
    """Firehose disabled returns input buffer unchanged."""
    effect = FirehoseEffect()
    effect.configure(effect.config)
    buf = ["line1", "line2"]
    ctx = EffectContext(
        terminal_width=80,
        terminal_height=24,
        scroll_cam=0,
        ticker_height=0,
        items=[("Title", "Source", "2025-01-01T00:00:00")],
    )
    import engine.config as config

    config.FIREHOSE = False
    result = effect.process(buf, ctx)
    assert result == buf


def test_firehose_enabled_adds_lines():
    """Firehose enabled adds FIREHOSE_H lines to output."""
    effect = FirehoseEffect()
    effect.configure(effect.config)
    buf = ["line1"]
    ctx = EffectContext(
        terminal_width=80,
        terminal_height=24,
        scroll_cam=0,
        ticker_height=0,
        items=[("Title", "Source", "2025-01-01T00:00:00")] * 10,
    )
    import engine.config as config

    config.FIREHOSE = True
    config.FIREHOSE_H = 3
    result = effect.process(buf, ctx)
    assert len(result) == 4
    assert any("\033[" in line for line in result[1:])


def test_firehose_respects_terminal_width():
    """Firehose lines are truncated to terminal width."""
    effect = FirehoseEffect()
    effect.configure(effect.config)
    ctx = EffectContext(
        terminal_width=40,
        terminal_height=24,
        scroll_cam=0,
        ticker_height=0,
        items=[("A" * 100, "Source", "2025-01-01T00:00:00")],
    )
    import engine.config as config

    config.FIREHOSE = True
    config.FIREHOSE_H = 2
    result = effect.process([], ctx)
    firehose_lines = [line for line in result if "\033[" in line]
    for line in firehose_lines:
        # Strip all ANSI escape sequences (CSI sequences ending with letter)
        import re

        plain = re.sub(r"\x1b\[[^a-zA-Z]*[a-zA-Z]", "", line)
        # Extract content after position code
        content = plain.split("H", 1)[1] if "H" in plain else plain
        assert len(content) <= 40


def test_firehose_zero_height_noop():
    """Firehose with zero height returns buffer unchanged."""
    effect = FirehoseEffect()
    effect.configure(effect.config)
    buf = ["line1"]
    ctx = EffectContext(
        terminal_width=80,
        terminal_height=24,
        scroll_cam=0,
        ticker_height=0,
        items=[("Title", "Source", "2025-01-01T00:00:00")],
    )
    import engine.config as config

    config.FIREHOSE = True
    config.FIREHOSE_H = 0
    result = effect.process(buf, ctx)
    assert result == buf


def test_firehose_with_no_items():
    """Firehose with no content items returns buffer unchanged."""
    effect = FirehoseEffect()
    effect.configure(effect.config)
    buf = ["line1"]
    ctx = EffectContext(
        terminal_width=80,
        terminal_height=24,
        scroll_cam=0,
        ticker_height=0,
        items=[],
    )
    import engine.config as config

    config.FIREHOSE = True
    config.FIREHOSE_H = 3
    result = effect.process(buf, ctx)
    assert result == buf
