# tests/test_guard_full_rewrite.py
from __future__ import annotations

from pathlib import Path

import pytest

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


def test_guard_detects_full_rewrite(repo_ctx, ux, monkeypatch):
    repo = repo_ctx.repo
    write_aiguard_yml(repo)

    # git diff --numstat を偽装して「大規模 rewrite」に見せる
    def fake_run_argv(argv, cwd, check=False, timeout=None):
        if argv[:2] == ["git", "diff"] and "--numstat" in argv:
            out = "500\t500\tbackend/app/main.py\n"
            return cli.subprocess.CompletedProcess(argv, 0, stdout=out, stderr="")
        if argv[:2] == ["git", "ls-files"]:
            return cli.subprocess.CompletedProcess(argv, 0, stdout="", stderr="")
        return cli.subprocess.CompletedProcess(argv, 0, stdout="", stderr="")

    monkeypatch.setattr(cli, "run_argv", fake_run_argv)

    # checks は走らせない（guard で落ちるのが正）
    with pytest.raises(SystemExit) as ex:
        cli.cmd_check(ux, repo, strict=False)

    assert ex.value.code == 1

    report = read_json(repo / "ai_check_report.json")
    assert report["status"] == "fail"
    assert report["guard"]["full_rewrite"] is not None
    assert "Possible full rewrite detected" in report["guard"]["full_rewrite"]
