"""Tests.unit.infrastructure.repositories.test_git_hook_repository_runtime
Where: Unit tests for GitHookRepository helpers.
What: Validates interpreter resolution and shell formatting for generated hooks.
Why: Ensures Windows互換のpython実行ファイル検出ロジックが正しく動作する。
"""

from pathlib import Path

import pytest

import noveler.infrastructure.repositories.git_hook_repository as git_hook_module
from noveler.infrastructure.repositories.git_hook_repository import GitHookRepository


@pytest.fixture
def repo_root(tmp_path: Path) -> Path:
    """Provide a temporary repository root for GitHookRepository tests."""

    (tmp_path / ".git").mkdir()
    return tmp_path

@pytest.mark.skip(reason="_resolve_python_executable method not implemented in GitHookRepository")
def test_resolve_python_executable_prefers_existing_sys_executable(monkeypatch: pytest.MonkeyPatch, repo_root: Path) -> None:
    """When python3が未配置でもsys.executableが存在すればそれを返す。"""

    target_python = repo_root / "venv" / "Scripts" / "python.exe"
    target_python.parent.mkdir(parents=True, exist_ok=True)
    target_python.write_text("", encoding="utf-8")

    monkeypatch.setattr(
        git_hook_module.sys,
        "executable",
        str(target_python),
        raising=False,
    )

    def fake_which(candidate: str) -> str | None:
        return None

    monkeypatch.setattr(git_hook_module.shutil, "which", fake_which, raising=False)

    repo = GitHookRepository(repo_root)

    resolved = repo._resolve_python_executable("python3")

    assert resolved == target_python.as_posix()

@pytest.mark.skip(reason="_resolve_python_executable method not implemented in GitHookRepository")
def test_resolve_python_executable_falls_back_to_python_on_path(monkeypatch: pytest.MonkeyPatch, repo_root: Path) -> None:
    """python3もsys.executableも利用できない場合はPATH上のpythonにフォールバックする。"""

    monkeypatch.setattr(
        git_hook_module.sys,
        "executable",
        "",
        raising=False,
    )

    path_python = Path("C:/Tools/Python/python.exe")

    def fake_which(candidate: str) -> str | None:
        if candidate == "python":
            return str(path_python)
        return None

    monkeypatch.setattr(git_hook_module.shutil, "which", fake_which, raising=False)

    repo = GitHookRepository(repo_root)

    resolved = repo._resolve_python_executable("python3")

    assert resolved == path_python.as_posix()
