# tests/test_main.py
import sys
import pytest
from unittest.mock import patch
from ctf.main import main

def test_main_entry_point_help():
    """
    Verifies that calling main() executes the Click CLI app and responds
    to '--help' by listing registration groups.
    """
    # Mock sys.argv to simulate running 'ctf --help' from the shell
    with patch.object(sys, "argv", ["ctf", "--help"]):
        # Click calls sys.exit() after displaying help, raising SystemExit
        with pytest.raises(SystemExit) as exc_info:
            main()
        
        # Verify it exits with a successful exit code (0)
        assert exc_info.value.code == 0

# {{{ test_debug_exception_handler
def test_debug_exception_handler():
    """
    Verifies that debug_exception_handler prints traceback and invokes pdb.post_mortem.
    """
    from ctf.main import debug_exception_handler
    from unittest.mock import patch, MagicMock
    
    mock_tb = MagicMock()
    exc_val = ValueError("test error")
    with patch("traceback.print_exception") as mock_print, \
         patch("pdb.post_mortem") as mock_pm:
         
        debug_exception_handler(ValueError, exc_val, mock_tb)
        
        mock_print.assert_called_once_with(ValueError, exc_val, mock_tb)
        mock_pm.assert_called_once_with(mock_tb)
# }}}

