#!/usr/bin/env python3
"""
test_qc_false_positive_fix.py - task-2072 오탐 수정 검증 테스트

tdd_check.py의 _is_non_code_file() / _verify_by_check_files() 수정과
git_evidence.py의 _is_system_auto_file() / SYSTEM_AUTO_FILES 수정을 검증합니다.
"""

import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))

# ---------------------------------------------------------------------------
# tdd_check 임포트
# ---------------------------------------------------------------------------
from verifiers.tdd_check import _is_non_code_file, verify as tdd_verify  # type: ignore[import-not-found]

# ---------------------------------------------------------------------------
# git_evidence 임포트
# ---------------------------------------------------------------------------
from verifiers.git_evidence import _is_system_auto_file, SYSTEM_AUTO_FILES  # type: ignore[import-not-found]


# ===========================================================================
# tdd_check 테스트
# ===========================================================================


class TestIsNonCodeFile:
    """_is_non_code_file() 함수 테스트."""

    def test_md_is_non_code(self):
        """.md 파일은 비코드 파일"""
        assert _is_non_code_file("report.md") is True

    def test_json_is_non_code(self):
        """.json 파일은 비코드 파일"""
        assert _is_non_code_file("config.json") is True

    def test_yaml_is_non_code(self):
        """.yaml 파일은 비코드 파일"""
        assert _is_non_code_file("settings.yaml") is True

    def test_py_is_code(self):
        """.py 파일은 코드 파일"""
        assert _is_non_code_file("main.py") is False

    def test_ts_is_code(self):
        """.ts 파일은 코드 파일"""
        assert _is_non_code_file("app.ts") is False

    def test_go_is_code(self):
        """.go 파일은 코드 파일"""
        assert _is_non_code_file("server.go") is False

    def test_full_path_report_md(self):
        """경로 포함 report.md → True"""
        assert _is_non_code_file("report.md") is True

    def test_full_path_main_py(self):
        """경로 포함 main.py → False"""
        assert _is_non_code_file("main.py") is False


class TestVerifyCheckFilesOnlyMdJson:
    """check_files에 .md/.json만 있을 때 SKIP 반환 테스트."""

    def test_only_md_json_returns_skip(self):
        """check_files에 비코드 파일만 있으면 SKIP"""
        result = tdd_verify(
            "task-test-noncode",
            check_files=["report.md", "config.json"],
            audit_trail_path="/tmp/nonexistent-audit.jsonl",
        )
        assert result["status"] == "SKIP", (
            f"Expected SKIP but got {result['status']}: {result.get('details')}"
        )


class TestVerifyCheckFilesWithCode:
    """check_files에 코드 파일이 있으면 기존대로 동작하는지 테스트."""

    def test_code_only_returns_fail(self):
        """테스트 파일 없이 구현 파일만 있으면 FAIL"""
        result = tdd_verify(
            "task-test-code",
            check_files=["src/main.py"],
            audit_trail_path="/tmp/nonexistent-audit.jsonl",
        )
        assert result["status"] == "FAIL", (
            f"Expected FAIL but got {result['status']}: {result.get('details')}"
        )


class TestVerifyCheckFilesCodeAndTest:
    """코드 + 테스트 파일 모두 있으면 PASS 테스트."""

    def test_code_and_test_returns_pass(self):
        """구현 파일과 테스트 파일이 모두 있으면 PASS"""
        result = tdd_verify(
            "task-test-both",
            check_files=["src/main.py", "tests/test_main.py"],
            audit_trail_path="/tmp/nonexistent-audit.jsonl",
        )
        assert result["status"] == "PASS", (
            f"Expected PASS but got {result['status']}: {result.get('details')}"
        )


class TestVerifyCheckFilesMdAndTest:
    """.md + 테스트만 있으면 PASS (impl 0건) 테스트."""

    def test_md_and_test_returns_pass(self):
        """.md와 테스트 파일만 있으면 구현 파일 0건으로 PASS"""
        result = tdd_verify(
            "task-test-md-test",
            check_files=["README.md", "tests/test_main.py"],
            audit_trail_path="/tmp/nonexistent-audit.jsonl",
        )
        assert result["status"] == "PASS", (
            f"Expected PASS but got {result['status']}: {result.get('details')}"
        )


# ===========================================================================
# git_evidence 테스트
# ===========================================================================


class TestIsSystemAutoFile:
    """_is_system_auto_file() 함수 테스트."""

    def test_heartbeat_dir(self):
        """memory/heartbeats/ 경로 → True"""
        assert _is_system_auto_file("memory/heartbeats/dev1.heartbeat") is True

    def test_events_dir(self):
        """memory/events/ 경로 → True"""
        assert _is_system_auto_file("memory/events/task-123.done") is True

    def test_logs_dir(self):
        """logs/ 경로 → True"""
        assert _is_system_auto_file("logs/app.log") is True

    def test_bot_activity_json(self):
        """bot-activity.json → True"""
        assert _is_system_auto_file("bot-activity.json") is True

    def test_token_ledger_json(self):
        """token-ledger.json → True"""
        assert _is_system_auto_file("token-ledger.json") is True

    def test_whisper_dir(self):
        """whisper/ 경로 → True"""
        assert _is_system_auto_file("whisper/msg.txt") is True

    def test_memory_daily_dir(self):
        """memory/daily/ 경로 → True"""
        assert _is_system_auto_file("memory/daily/2026-04-22.md") is True

    def test_dashboard_data_refine_status(self):
        """dashboard/data/refine-status.json → True"""
        assert _is_system_auto_file("dashboard/data/refine-status.json") is True

    def test_nested_bot_activity_json(self):
        """경로 포함 bot-activity.json → True"""
        assert _is_system_auto_file("some/path/bot-activity.json") is True

    def test_heartbeat_extension(self):
        """확장자 .heartbeat → True"""
        assert _is_system_auto_file("file.heartbeat") is True

    def test_src_main_py_is_not_system(self):
        """src/main.py → False"""
        assert _is_system_auto_file("src/main.py") is False

    def test_verifiers_tdd_check_is_not_system(self):
        """teams/shared/verifiers/tdd_check.py → False"""
        assert _is_system_auto_file("teams/shared/verifiers/tdd_check.py") is False

    def test_projects_server_py_is_not_system(self):
        """projects/insuro/server.py → False"""
        assert _is_system_auto_file("projects/insuro/server.py") is False


class TestSystemAutoFilesConstant:
    """SYSTEM_AUTO_FILES 상수 유효성 테스트."""

    def test_is_list(self):
        """SYSTEM_AUTO_FILES가 리스트 타입"""
        assert isinstance(SYSTEM_AUTO_FILES, list)

    def test_minimum_ten_entries(self):
        """SYSTEM_AUTO_FILES 항목이 최소 10개 이상"""
        assert len(SYSTEM_AUTO_FILES) >= 10, (
            f"SYSTEM_AUTO_FILES에 항목이 {len(SYSTEM_AUTO_FILES)}개뿐 (최소 10개 필요)"
        )
