"""Tests for build utility helper functions.

Generated by our friend.
"""

from ax_prover.utils.build import _format_lean_errors, _trim_warnings


class TestTrimWarnings:
    """Tests for _trim_warnings function."""

    def test_removes_warning_lines(self):
        """Lines containing 'warning:' are removed."""
        output = "error: type mismatch\nwarning: unused import\nother line"
        result = _trim_warnings(output)
        assert "warning:" not in result
        assert "error: type mismatch" in result
        assert "other line" in result

    def test_removes_sorry_warnings(self):
        """Lines about sorry usage are removed."""
        output = "declaration uses 'sorry'\nerror: real error"
        result = _trim_warnings(output)
        assert "sorry" not in result
        assert "real error" in result

    def test_removes_unused_variable_warnings(self):
        """Lines about unused variables are removed."""
        output = "unused variable `h`\ndef foo := 1"
        result = _trim_warnings(output)
        assert "unused variable" not in result

    def test_preserves_error_lines(self):
        """Error lines without warning keywords are preserved."""
        output = "error: unknown identifier 'foo'\nerror: type mismatch"
        result = _trim_warnings(output)
        assert "unknown identifier" in result
        assert "type mismatch" in result

    def test_empty_input(self):
        """Empty string returns empty string."""
        assert _trim_warnings("") == ""

    def test_removes_trace_and_note(self):
        """Lines containing 'trace:' or 'note:' are removed."""
        output = "trace: [debug] some info\nnote: see declaration\nerror: real"
        result = _trim_warnings(output)
        assert "trace:" not in result
        assert "note:" not in result
        assert "real" in result


class TestFormatLeanErrors:
    """Tests for _format_lean_errors function."""

    def test_formats_error_with_context(self):
        """Error at a specific line gets formatted with code context."""
        file_content = "line 1\nline 2\nlet x := bad_expr\nline 4"
        error_output = "MyFile.lean:3:10: error: unknown identifier 'bad_expr'"
        result = _format_lean_errors(error_output, "MyFile.lean", file_content)
        assert "Error at line 3:10" in result
        assert "bad_expr" in result
        assert "^^^" in result

    def test_preserves_non_matching_lines(self):
        """Lines not matching the file pattern are passed through."""
        error_output = "some other output\ngeneral info"
        result = _format_lean_errors(error_output, "MyFile.lean", "content")
        assert "some other output" in result
        assert "general info" in result

    def test_handles_out_of_range_line(self):
        """Line number beyond file content doesn't crash."""
        file_content = "line 1"
        error_output = "MyFile.lean:99:1: error: something"
        # Should not raise
        result = _format_lean_errors(error_output, "MyFile.lean", file_content)
        assert result is not None

    def test_empty_output(self):
        """Empty error output returns empty string."""
        assert _format_lean_errors("", "MyFile.lean", "content") == ""
