#!/usr/bin/env python3
"""main() 함수 직접 호출 테스트 - 커버리지 개선용"""

import io
import os
import sys
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch

sys.path.insert(0, str(Path(__file__).parent.parent))

# 모듈 직접 import
import base_reviewer
import code_reviewer
import red_team_orchestrator
import security_patterns


class TestBaseReviewerMain(unittest.TestCase):
    """base_reviewer.py main() 함수 테스트"""

    def test_main_with_test_arg(self):
        """main() 함수 - test 인자"""
        with patch("sys.argv", ["base_reviewer.py", "test"]):
            with patch("sys.stdout", new_callable=io.StringIO):
                base_reviewer.main()

    def test_main_without_args(self):
        """main() 함수 - 인자 없음"""
        with patch("sys.argv", ["base_reviewer.py"]):
            with patch("sys.stdout", new_callable=io.StringIO):
                base_reviewer.main()


class TestCodeReviewerMain(unittest.TestCase):
    """code_reviewer.py main() 함수 테스트"""

    def test_main_without_args(self):
        """main() 함수 - 인자 없음"""
        with patch("sys.argv", ["code_reviewer.py"]):
            with self.assertRaises(SystemExit):
                code_reviewer.main()

    def test_main_test_command(self):
        """main() 함수 - test 명령"""
        with patch("sys.argv", ["code_reviewer.py", "test"]):
            with patch("sys.stdout", new_callable=io.StringIO):
                code_reviewer.main()

    def test_main_scan_without_file(self):
        """main() 함수 - scan 명령 (파일 없음)"""
        with patch("sys.argv", ["code_reviewer.py", "scan"]):
            with self.assertRaises(SystemExit):
                code_reviewer.main()

    def test_main_scan_with_file(self):
        """main() 함수 - scan 명령 (파일 있음)"""
        with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
            f.write("# test\npass\n")
            temp_path = f.name

        try:
            with patch("sys.argv", ["code_reviewer.py", "scan", temp_path]):
                with patch("sys.stdout", new_callable=io.StringIO):
                    code_reviewer.main()
        finally:
            os.unlink(temp_path)

    def test_main_unknown_command(self):
        """main() 함수 - 알 수 없는 명령"""
        with patch("sys.argv", ["code_reviewer.py", "unknown_cmd"]):
            with self.assertRaises(SystemExit):
                code_reviewer.main()


class TestRedTeamOrchestratorMain(unittest.TestCase):
    """red_team_orchestrator.py main() 함수 테스트"""

    def test_main_without_args(self):
        """main() 함수 - 인자 없음"""
        with patch("sys.argv", ["red_team_orchestrator.py"]):
            with self.assertRaises(SystemExit):
                red_team_orchestrator.main()

    def test_main_types_command(self):
        """main() 함수 - types 명령"""
        with patch("sys.argv", ["red_team_orchestrator.py", "types"]):
            with patch("sys.stdout", new_callable=io.StringIO):
                red_team_orchestrator.main()

    def test_main_test_command(self):
        """main() 함수 - test 명령"""
        with patch("sys.argv", ["red_team_orchestrator.py", "test"]):
            with patch("sys.stdout", new_callable=io.StringIO):
                red_team_orchestrator.main()

    def test_main_review_without_args(self):
        """main() 함수 - review 명령 (인자 부족)"""
        with patch("sys.argv", ["red_team_orchestrator.py", "review"]):
            with self.assertRaises(SystemExit):
                red_team_orchestrator.main()

    def test_main_review_with_args(self):
        """main() 함수 - review 명령 (정상)"""
        with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
            f.write("# test\npass\n")
            temp_path = f.name

        try:
            with patch("sys.argv", ["red_team_orchestrator.py", "review", "code", temp_path]):
                with patch("sys.stdout", new_callable=io.StringIO):
                    red_team_orchestrator.main()
        finally:
            os.unlink(temp_path)

    def test_main_unknown_command(self):
        """main() 함수 - 알 수 없는 명령"""
        with patch("sys.argv", ["red_team_orchestrator.py", "unknown_cmd"]):
            with self.assertRaises(SystemExit):
                red_team_orchestrator.main()


class TestSecurityPatternsMain(unittest.TestCase):
    """security_patterns.py main() 함수 테스트"""

    def test_main_with_test_arg(self):
        """main() 함수 - test 인자"""
        with patch("sys.argv", ["security_patterns.py", "test"]):
            with patch("sys.stdout", new_callable=io.StringIO):
                security_patterns.main()

    def test_main_without_args(self):
        """main() 함수 - 인자 없음"""
        with patch("sys.argv", ["security_patterns.py"]):
            with patch("sys.stdout", new_callable=io.StringIO):
                security_patterns.main()


if __name__ == "__main__":
    unittest.main()
