"""Hermes Agent 전수조사 검증 테스트 (task-850.1)

보고서의 핵심 내용이 올바르게 작성되었는지 검증합니다.
"""

import os
from pathlib import Path

import pytest

_WORKSPACE = Path(os.environ.get("WORKSPACE_ROOT", "/home/jay/workspace"))


class TestHermesAuditReportExists:
    """보고서 파일 존재 여부 검증"""

    def test_report_file_exists(self) -> None:
        """보고서 파일이 생성되어야 한다."""
        report_path = _WORKSPACE / "memory/research/hermes-agent-full-audit.md"
        assert report_path.exists(), f"Report file not found: {report_path}"

    def test_report_has_content(self) -> None:
        """보고서에 내용이 있어야 한다."""
        report_path = _WORKSPACE / "memory/research/hermes-agent-full-audit.md"
        content = report_path.read_text()
        assert len(content) > 10000, "Report content too short"
        assert "Hermes" in content, "Report missing 'Hermes' keyword"


class TestHermesAuditReportStructure:
    """보고서 섹션 구조 검증"""

    @pytest.fixture
    def report_content(self) -> str:
        report_path = _WORKSPACE / "memory/research/hermes-agent-full-audit.md"
        return report_path.read_text()

    def test_has_core_agent_section(self, report_content: str) -> None:
        """Core Agent 섹션이 있어야 한다."""
        assert "Core Agent" in report_content

    def test_has_memory_section(self, report_content: str) -> None:
        """Memory & Learning 섹션이 있어야 한다."""
        assert "Memory & Learning" in report_content or "Memory" in report_content

    def test_has_skills_section(self, report_content: str) -> None:
        """Skills & Tools 섹션이 있어야 한다."""
        assert "Skills & Tools" in report_content or "Skills" in report_content

    def test_has_security_section(self, report_content: str) -> None:
        """Security 섹션이 있어야 한다."""
        assert "Security" in report_content

    def test_has_infrastructure_section(self, report_content: str) -> None:
        """Infrastructure 섹션이 있어야 한다."""
        assert "Infrastructure" in report_content

    def test_has_ux_section(self, report_content: str) -> None:
        """UX & Interface 섹션이 있어야 한다."""
        assert "UX & Interface" in report_content or "UX" in report_content

    def test_has_summary_section(self, report_content: str) -> None:
        """도입 권장 항목 요약 섹션이 있어야 한다."""
        assert "도입 권장" in report_content or "요약" in report_content


class TestHermesAuditReportCompleteness:
    """전수조사 완전성 검증"""

    @pytest.fixture
    def report_content(self) -> str:
        report_path = _WORKSPACE / "memory/research/hermes-agent-full-audit.md"
        return report_path.read_text()

    def test_has_all_categories(self, report_content: str) -> None:
        """모든 카테고리가 있어야 한다."""
        categories = [
            "1. Core Agent",
            "2. Memory & Learning",
            "3. Skills & Tools",
            "4. Skills 카탈로그 (전수)",
            "5. Security",
            "6. Infrastructure",
            "7. UX & Interface (Gateway)",
            "8. Analytics & Observability",
        ]
        for cat in categories:
            assert cat in report_content, f"Missing category: {cat}"

    def test_has_value_ratings(self, report_content: str) -> None:
        """도입 가치 평가가 있어야 한다."""
        assert "상" in report_content
        assert "중" in report_content
        assert "하" in report_content

    def test_has_difficulty_ratings(self, report_content: str) -> None:
        """도입 난이도 평가가 있어야 한다."""
        # 난이도 키워드 확인
        assert "난이도" in report_content

    def test_has_comparison_tables(self, report_content: str) -> None:
        """우리 시스템과의 비교표가 있어야 한다."""
        assert "우리 시스템" in report_content or "비교" in report_content

    def test_item_count_sufficient(self, report_content: str) -> None:
        """항목 수가 충분해야 한다 (최소 50개)."""
        # ### 로 시작하는 항목 카운트
        item_count = report_content.count("### ")
        assert item_count >= 50, f"Expected at least 50 items, found {item_count}"

    def test_has_implementation_details(self, report_content: str) -> None:
        """구현 방식 설명이 있어야 한다."""
        assert "구현 방식" in report_content or "구현" in report_content


class TestHermesAuditKeyConcepts:
    """핵심 개념 포함 여부 검증"""

    @pytest.fixture
    def report_content(self) -> str:
        report_path = _WORKSPACE / "memory/research/hermes-agent-full-audit.md"
        return report_path.read_text()

    def test_prompt_injection_detection_mentioned(self, report_content: str) -> None:
        """프롬프트 인젝션 탐지가 언급되어야 한다."""
        assert "인젝션" in report_content or "injection" in report_content.lower()

    def test_context_compression_mentioned(self, report_content: str) -> None:
        """컨텍스트 압축이 언급되어야 한다."""
        assert "압축" in report_content or "compress" in report_content.lower()

    def test_frozen_snapshot_mentioned(self, report_content: str) -> None:
        """Frozen Snapshot 패턴이 언급되어야 한다."""
        assert "Frozen" in report_content or "Snapshot" in report_content or "스냅샷" in report_content

    def test_fts5_search_mentioned(self, report_content: str) -> None:
        """FTS5 검색이 언급되어야 한다."""
        assert "FTS5" in report_content or "전문 검색" in report_content

    def test_skill_system_mentioned(self, report_content: str) -> None:
        """스킬 시스템이 언급되어야 한다."""
        assert "skill" in report_content.lower() or "스킬" in report_content

    def test_subagent_delegation_mentioned(self, report_content: str) -> None:
        """서브에이전트 위임이 언급되어야 한다."""
        assert "서브에이전트" in report_content or "위임" in report_content or "delegate" in report_content.lower()

    def test_security_scanning_mentioned(self, report_content: str) -> None:
        """보안 스캔이 언급되어야 한다."""
        assert "보안" in report_content or "security" in report_content.lower()

    def test_summary_table_exists(self, report_content: str) -> None:
        """요약 테이블이 있어야 한다."""
        assert "가치 '상'" in report_content or "가치 상" in report_content
