"""
STORY-003: 建立日誌分析 Prompt Template

Tests for log analysis prompt template.
"""

import os


class TestStory003PromptTemplate:
    """
    STORY-003: 建立日誌分析 Prompt Template

    As a AI Prompt 設計者
    I want to 建立日誌分析專用的 prompt template
    So that Claude Code SDK 能清楚理解任務並自主完成日誌讀取與分析
    """

    # AC-01: 建立 prompts/log.md 檔案
    def test_log_analysis_prompt_file_exists(self):
        """
        測試 prompt template 檔案存在

        Given: prompts 目錄已存在
        When: 我檢查 prompts/log.md
        Then: 檔案應該存在
        """
        prompt_file = "prompts/log.md"
        assert os.path.exists(prompt_file), f"Prompt template file should exist at {prompt_file}"

    def test_log_analysis_prompt_has_jinja2_syntax(self):
        """
        測試 prompt template 使用 Jinja2 語法

        Given: log_analysis.md 檔案存在
        When: 我讀取檔案內容
        Then: 應該包含 Jinja2 變數語法 {{變數名}}
        """
        prompt_file = "prompts/log.md"
        with open(prompt_file, encoding="utf-8") as f:
            content = f.read()

        # 檢查是否包含 Jinja2 變數
        assert "{{" in content and "}}" in content, "Prompt should use Jinja2 template syntax"

    def test_log_analysis_prompt_has_role_definition(self):
        """
        測試 prompt 包含角色定義

        Given: log_analysis.md 檔案存在
        When: 我讀取檔案內容
        Then: 應該包含角色定義（UART 日誌分析專家）
        """
        prompt_file = "prompts/log.md"
        with open(prompt_file, encoding="utf-8") as f:
            content = f.read()

        assert "UART" in content or "日誌" in content or "log" in content.lower(), (
            "Prompt should define role as UART log analysis expert"
        )


class TestStory003AC02TemplateVariables:
    """
    AC-02: 定義模板變數
    """

    def test_prompt_contains_required_variables(self):
        """
        測試 prompt 包含必要變數

        Given: log_analysis.md 檔案存在
        When: 我檢查模板變數
        Then: 應該包含 log_file_path, user_prompt, cwd
        """
        prompt_file = "prompts/log.md"
        with open(prompt_file, encoding="utf-8") as f:
            content = f.read()

        required_vars = ["{{log_file_path}}", "{{user_prompt}}", "{{cwd}}"]
        for var in required_vars:
            assert var in content, f"Prompt should contain {var} variable"


class TestStory003AC03SDKAutonomy:
    """
    AC-03: 設計 prompt 內容強調 SDK 自主處理
    """

    def test_prompt_instructs_using_read_tool(self):
        """
        測試 prompt 指示使用 Read 工具

        Given: log_analysis.md 檔案存在
        When: 我檢查 prompt 內容
        Then: 應該明確指示使用 Read 工具讀取日誌
        """
        prompt_file = "prompts/log.md"
        with open(prompt_file, encoding="utf-8") as f:
            content = f.read()

        assert "Read" in content or "read" in content.lower(), "Prompt should instruct using Read tool"

    def test_prompt_mentions_grep_for_large_logs(self):
        """
        測試 prompt 提及使用 Grep 搜尋

        Given: log_analysis.md 檔案存在
        When: 我檢查 prompt 內容
        Then: 應該提及使用 Grep 工具
        """
        prompt_file = "prompts/log.md"
        with open(prompt_file, encoding="utf-8") as f:
            content = f.read()

        assert "Grep" in content or "grep" in content.lower(), "Prompt should mention Grep tool for large logs"


class TestStory003AC04AnalysisStructure:
    """
    AC-04: 定義分析任務結構
    """

    def test_prompt_defines_analysis_goal(self):
        """
        測試 prompt 定義分析目標

        Given: log_analysis.md 檔案存在
        When: 我檢查 prompt 內容
        Then: 應該說明分析目標是根據 user_prompt 回答問題
        """
        prompt_file = "prompts/log.md"
        with open(prompt_file, encoding="utf-8") as f:
            content = f.read()

        assert "分析" in content or "analysis" in content.lower(), "Prompt should define analysis goal"
