#!/usr/bin/env python3
"""회귀 테스트 - 기존 기능이 깨지지 않는지 확인"""

import sys
import tempfile
from pathlib import Path

import pytest

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

from code_reviewer import CodeReviewer


class TestRegression:
    """회귀 테스트 클래스"""

    @pytest.fixture(autouse=True)
    def setup(self):
        """테스트 설정"""
        self.reviewer = CodeReviewer()

    def test_sql_injection_still_detected(self):
        """회귀: SQL Injection이 여전히 감지되는지 확인"""
        with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
            f.write('query = "SELECT * FROM users WHERE id = " + user_input\n')
            temp_path = f.name

        try:
            result = self.reviewer.review(temp_path)

            # 회귀 체크: SQL Injection이 감지되어야 함
            assert any(vuln["type"] == "SQL Injection" for vuln in result["vulnerabilities"])
            assert result["risk_level"] in ["high", "critical"]
        finally:
            Path(temp_path).unlink()

    def test_hardcoded_secret_still_detected(self):
        """회귀: Hardcoded Secret이 여전히 감지되는지 확인"""
        with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
            f.write("password = '123456'\n")
            temp_path = f.name

        try:
            result = self.reviewer.review(temp_path)

            # 회귀 체크: Hardcoded Secret이 감지되어야 함
            assert any(vuln["type"] == "Hardcoded Secret" for vuln in result["vulnerabilities"])
            assert result["risk_level"] == "high"
        finally:
            Path(temp_path).unlink()

    def test_xss_still_detected(self):
        """회귀: XSS가 여전히 감지되는지 확인"""
        with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
            # Flask render_template_string 패턴 (실제 XSS 감지 패턴)
            f.write("from flask import render_template_string\nrender_template_string(user_input)\n")
            temp_path = f.name

        try:
            result = self.reviewer.review(temp_path)

            # 회귀 체크: XSS가 감지되어야 함
            assert any(vuln["type"] == "XSS (Cross-Site Scripting)" for vuln in result["vulnerabilities"])
        finally:
            Path(temp_path).unlink()

    def test_normal_file_still_passes(self):
        """회귀: 정상 파일이 여전히 통과하는지 확인"""
        with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
            f.write("# Normal code\npass\n")
            temp_path = f.name

        try:
            result = self.reviewer.review(temp_path)

            # 회귀 체크: 정상 파일은 passed=True여야 함
            assert result["passed"] is True
        finally:
            Path(temp_path).unlink()
