"""tests for familiar-gemini"""

from __future__ import annotations

from unittest.mock import patch

from familiar.agents import Agent
from familiar_gemini import GeminiAgent


class TestGeminiAgent:
    """tests for gemini agent"""

    def test_is_agent_subclass(self):
        assert issubclass(GeminiAgent, Agent)

    def test_name(self):
        agent = GeminiAgent()
        assert agent.name == "gemini"

    def test_output_file(self):
        agent = GeminiAgent()
        assert agent.output_file == "GEMINI.md"

    def test_supports_skills(self):
        agent = GeminiAgent()
        assert agent.supports_skills() is True

    def test_skill_path(self, tmp_path):
        agent = GeminiAgent()
        path = agent.skill_path(tmp_path, "review")
        assert path == tmp_path / ".gemini" / "skills" / "review" / "SKILL.md"

    def test_supports_subagents(self):
        agent = GeminiAgent()
        assert agent.supports_subagents() is True

    def test_subagent_path(self, tmp_path):
        agent = GeminiAgent()
        path = agent.subagent_path(tmp_path, "python_sec")
        assert path == tmp_path / ".gemini" / "subagents" / "python_sec" / "AGENT.md"

    def test_run_headless(self, tmp_path):
        agent = GeminiAgent()

        with patch("familiar_gemini.subprocess.call", return_value=0) as mock_call:
            result = agent.run(tmp_path, "test prompt", headless=True)
            assert result == 0
            mock_call.assert_called_once_with(
                ["gemini", "-p", "test prompt"], cwd=tmp_path
            )

    def test_run_interactive(self, tmp_path):
        agent = GeminiAgent()

        with patch("familiar_gemini.subprocess.call", return_value=0) as mock_call:
            result = agent.run(tmp_path, "test prompt", headless=False)
            assert result == 0
            mock_call.assert_called_once_with(
                ["gemini", "-i", "test prompt"], cwd=tmp_path
            )

    def test_run_auto(self, tmp_path):
        agent = GeminiAgent()

        with patch("familiar_gemini.subprocess.call", return_value=0) as mock_call:
            result = agent.run(tmp_path, "test prompt", headless=False, auto=True)
            assert result == 0
            mock_call.assert_called_once_with(
                ["gemini", "--approval-mode=yolo", "-i", "test prompt"],
                cwd=tmp_path,
            )

    def test_run_returns_exit_code(self, tmp_path):
        agent = GeminiAgent()

        with patch("familiar_gemini.subprocess.call", return_value=42):
            result = agent.run(tmp_path, "prompt", headless=True)
            assert result == 42
