"""Tests for the AG-03 commit-trailer CI gate's pure predicate.

`missing_trailers` is the load-bearing logic — given a commit's trailer block,
it decides whether both AG-03 trailers are present. The git plumbing around it
(range computation, `git log`) is a thin shell exercised in CI, not here.
"""

from __future__ import annotations

import importlib.util
from pathlib import Path

import pytest

# scripts/ is not a package; load the module by path.
_SCRIPT = Path(__file__).resolve().parents[2] / "scripts" / "check_commit_trailers.py"
_spec = importlib.util.spec_from_file_location("check_commit_trailers", _SCRIPT)
assert _spec and _spec.loader
cct = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(cct)

_BOTH = (
    "Signed-off-by: Bassam Alsanie <bassam@magogi.org>\n"
    "Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>"
)


class TestMissingTrailers:
    def test_both_present_returns_empty(self) -> None:
        assert cct.missing_trailers(_BOTH) == []

    def test_missing_signoff(self) -> None:
        assert cct.missing_trailers("Co-Authored-By: X <y@z>") == ["signed-off-by"]

    def test_missing_coauthor(self) -> None:
        assert cct.missing_trailers("Signed-off-by: B <b@m>") == ["co-authored-by"]

    def test_both_missing(self) -> None:
        assert cct.missing_trailers("") == ["signed-off-by", "co-authored-by"]

    def test_key_match_is_case_insensitive(self) -> None:
        # git treats trailer keys case-insensitively; `Co-authored-by` counts.
        block = "Signed-Off-By: B <b@m>\nCo-authored-by: X <y@z>"
        assert cct.missing_trailers(block) == []

    def test_extra_trailers_and_blank_lines_ignored(self) -> None:
        block = f"\nReviewed-by: Q <q@r>\n{_BOTH}\n"
        assert cct.missing_trailers(block) == []

    @pytest.mark.parametrize("noise", ["", "   ", "not a trailer line", "no-colon-here"])
    def test_non_trailer_lines_do_not_satisfy(self, noise: str) -> None:
        # A line without a recognizable `key:` must not count as a trailer.
        assert set(cct.missing_trailers(noise)) == {"signed-off-by", "co-authored-by"}
