"""Tests for the evaluation module."""

import re
from unittest.mock import patch

from texer.eval import evaluate, _generate_header, _get_git_sha, _get_version


class TestHeader:
    def test_generate_header_with_git(self):
        """Test that header includes version, timestamp, and git SHA when available."""
        with patch("texer.eval._get_git_sha", return_value="abc1234def5678"):
            header = _generate_header()

        assert "% Generated by texer v" in header
        assert "% Git commit: abc1234def5678" in header
        # Check timestamp format (YYYY-MM-DD HH:MM:SS)
        assert re.search(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}", header)

    def test_generate_header_without_git(self):
        """Test that header works when git is not available."""
        with patch("texer.eval._get_git_sha", return_value=None):
            header = _generate_header()

        assert "% Generated by texer v" in header
        assert "Git commit" not in header

    def test_generate_header_includes_version(self):
        """Test that header includes the texer version."""
        header = _generate_header()
        version = _get_version()
        assert f"v{version}" in header

    def test_get_git_sha_returns_full_sha(self):
        """Test that _get_git_sha returns the full SHA or None."""
        sha = _get_git_sha()
        # In a git repo, should return a full SHA (40 chars)
        # Outside a git repo, returns None
        if sha is not None:
            assert isinstance(sha, str)
            assert len(sha) == 40
            assert re.match(r"^[0-9a-f]+$", sha)

    def test_get_version_returns_string(self):
        """Test that _get_version returns a version string."""
        version = _get_version()
        assert isinstance(version, str)
        assert len(version) > 0

    def test_evaluate_without_header(self):
        """Test that evaluate with header=False returns plain content."""
        result = evaluate("hello", header=False)
        assert result == "hello"
        assert "Generated by texer" not in result

    def test_evaluate_default_includes_header(self):
        """Test that evaluate includes header by default."""
        result = evaluate("hello")
        assert "% Generated by texer v" in result
        assert result.endswith("hello")

    def test_evaluate_with_header(self):
        """Test that evaluate with header flag includes the header."""
        with patch("texer.eval._get_git_sha", return_value="abc1234def5678901234567890abcdef12345678"):
            result = evaluate("hello", header=True)

        assert "% Generated by texer v" in result
        assert "% Git commit: abc1234def5678901234567890abcdef12345678" in result
        assert result.endswith("hello")

    def test_header_is_latex_comment(self):
        """Test that header lines are valid LaTeX comments."""
        with patch("texer.eval._get_git_sha", return_value="abc1234def5678901234567890abcdef12345678"):
            header = _generate_header()

        for line in header.strip().split("\n"):
            assert line.startswith("%"), f"Line should be a LaTeX comment: {line}"
