# tests/test_helpers.py
from pathlib import Path
from ctf.helpers import check_for_flag

# {{{ test_check_for_flag
def test_check_for_flag():
    """
    Verifies that check_for_flag extracts multiple matching flags or returns empty list.
    """
    res = check_for_flag("flag{one} and flag{two}")
    assert set(res) == {"flag{one}", "flag{two}"}
    res_custom = check_for_flag("custom{first} custom{second}", r"custom\{[a-z]+\}")
    assert set(res_custom) == {"custom{first}", "custom{second}"}
    assert check_for_flag("no flag matches here") == []
# }}}

# {{{ test_is_valid_flag
def test_is_valid_flag():
    """
    Verifies that is_valid_flag validates flags and supports uniqueness checks.
    """
    from ctf.helpers import is_valid_flag
    # Valid flag
    assert is_valid_flag("flag{test}")
    # Invalid flag
    assert not is_valid_flag("no flag here")
    # Uniqueness check (success)
    assert is_valid_flag("flag{new}", original="flag{old}")
    # Uniqueness check (failure)
    assert not is_valid_flag("flag{same}", original="flag{same}")
# }}}

# {{{ test_flag_detector_stream_detection
def test_flag_detector_stream_detection():
    """
    Verifies that FlagDetectorStream successfully intercepts writes to output potential flags.
    """
    from io import StringIO
    from ctf.helpers import FlagDetectorStream
    
    out = StringIO()
    stream = FlagDetectorStream(out, r"flag\{[a-z_]+\}")
    
    stream.write("Some text before flag{my_flag_here} and text after.")
    output = out.getvalue()
    assert "Potential flag(s) detected in command output" in output
    assert "flag{my_flag_here}" in output
# }}}

# {{{ test_flag_detector_stream_anchored_stripping
def test_flag_detector_stream_anchored_stripping():
    """
    Verifies that FlagDetectorStream strips standard anchors ^ and $ to support substring searches.
    """
    from io import StringIO
    from ctf.helpers import FlagDetectorStream
    
    out = StringIO()
    stream = FlagDetectorStream(out, r"^flag\{[a-z_]+\}$")
    
    stream.write("random flag{my_flag_here} data")
    output = out.getvalue()
    assert "Potential flag(s) detected in command output" in output
    assert "flag{my_flag_here}" in output
# }}}

# {{{ test_flag_detector_stream_no_pattern
def test_flag_detector_stream_no_pattern():
    """
    Verifies that FlagDetectorStream passes through text unmodified if no flag pattern is configured.
    """
    from io import StringIO
    from ctf.helpers import FlagDetectorStream
    
    out = StringIO()
    stream = FlagDetectorStream(out, "")
    
    stream.write("normal output flag{hello}")
    assert out.getvalue() == "normal output flag{hello}"
# }}}

# {{{ test_flag_detector_stream_persistence
def test_flag_detector_stream_persistence():
    """
    Verifies that FlagDetectorStream writes the detected flag to config.toml.
    """
    from io import StringIO
    from ctf.helpers import FlagDetectorStream
    from ctf.config import Config
    
    out = StringIO()
    stream = FlagDetectorStream(out, r"flag\{[a-z_]+\}")
    stream.write("found flag{persisted_flag} in stdout")
    
    # Reload config and check last_flag
    cfg = Config()
    assert cfg.data.get("Competition", {}).get("last_flag") == "flag{persisted_flag}"
# }}}


