from __future__ import annotations

import pytest

from llmdebug.mcp_git_context import collect_git_evidence_refs, parse_max_commits


@pytest.mark.parametrize(
    ("raw", "expected"),
    [
        (5, 5),
        ("3", 3),
        (0, 1),
        (-99, 1),
        (999, 50),
    ],
)
def test_parse_max_commits_clamps_to_supported_range(raw: object, expected: int) -> None:
    assert parse_max_commits(raw) == expected


def test_parse_max_commits_rejects_invalid_values() -> None:
    with pytest.raises(ValueError, match="Invalid max_commits value"):
        parse_max_commits("abc")


def test_collect_git_evidence_refs_extracts_head_and_crash_refs() -> None:
    enhanced = {
        "head": {"commit": "abc1234", "branch": "main"},
        "crash_file": {
            "file_rel": "src/app.py",
            "blame": {"commit": "def5678"},
        },
    }
    assert collect_git_evidence_refs(enhanced) == [
        "git_head_commit:abc1234",
        "git_branch:main",
        "git_crash_file:src/app.py",
        "git_blame_commit:def5678",
    ]


def test_collect_git_evidence_refs_ignores_malformed_sections() -> None:
    enhanced = {"head": "not-a-dict", "crash_file": {"blame": "not-a-dict"}}
    assert collect_git_evidence_refs(enhanced) == []
