# tests/test_no_git_repo.py
from __future__ import annotations

from pathlib import Path

import pytest

import oceansguard.cli as cli
from tests.conftest import _write, write_aiguard_yml


def test_pack_without_git_can_be_handled_by_mocking_git_calls(tmp_path: Path, ux, monkeypatch):
    """
    方針：
    - 現実には pack_git_section が git を呼ぶので、git 無し repo は失敗し得る。
    - ただし “git が無いケースもテストしたい” ので、ここでは run_argv を FileNotFoundError にして
      どの段階で落ちるかを明確化する（＝期待通りに落ちるか）。
    """
    repo = tmp_path / "repo"
    repo.mkdir()

    _write(repo / "backend" / "app" / "main.py", "print('x')\n")
    _write(repo / "contracts" / "openapi.json", '{"openapi":"3.0.0","info":{"title":"t","version":"1"}}\n')
    _write(repo / "README.md", "# r\n")

    write_aiguard_yml(repo, include_frontend_in_roots=False)

    def fake_run_argv(argv, cwd, check=False, timeout=None):
        if argv and argv[0] == "git":
            raise FileNotFoundError("git not found")
        return cli.subprocess.CompletedProcess(argv, 0, stdout="", stderr="")

    monkeypatch.setattr(cli, "run_argv", fake_run_argv)
    monkeypatch.setattr(cli, "run_shell", lambda *a, **k: cli.subprocess.CompletedProcess(a[0], 0, stdout="", stderr=""))

    with pytest.raises(FileNotFoundError):
        cli.cmd_pack(ux, repo, strict=False)
