"""Tests for BiasCheck."""

from unittest.mock import MagicMock, patch

import pytest

from ai_qa.checks.bias import BiasCheck
from ai_qa.core.context import Context
from ai_qa.exceptions import ContextValidationError


def test_bias_check_name():
    """Test BiasCheck has correct name."""

    def dummy_fn(x):
        return x

    check = BiasCheck(fn=dummy_fn)
    assert check.name == "bias"


def test_bias_check_requires_llm():
    """Test BiasCheck requires_llm property."""

    def dummy_fn(x):
        return x

    check = BiasCheck(fn=dummy_fn)
    assert check.requires_llm is True


@patch("ai_qa.checks.bias.get_provider")
def test_bias_check_fair_output(mock_get_provider):
    """Test fair (unbiased) output."""

    def fn(x):
        return "This candidate is qualified"

    check = BiasCheck(fn=fn)
    ctx = Context(output="test", input="Evaluate John for leadership")

    mock_provider = MagicMock()
    mock_provider.judge.return_value = {
        "score": 1.0,
        "evidence": "Fair treatment",
    }
    mock_get_provider.return_value = mock_provider

    result = check.evaluate(ctx)
    assert result.score == 1.0
    assert result.passed


@patch("ai_qa.checks.bias.get_provider")
def test_bias_check_biased_output(mock_get_provider):
    """Test biased output."""

    def fn(x):
        return "Different output"

    check = BiasCheck(fn=fn, threshold=0.8)
    ctx = Context(output="test", input="Evaluate person for role")

    mock_provider = MagicMock()
    mock_provider.judge.return_value = {
        "score": 0.3,
        "evidence": "Significant bias detected",
    }
    mock_get_provider.return_value = mock_provider

    result = check.evaluate(ctx)
    assert result.score == 0.3
    assert not result.passed


def test_bias_check_no_input():
    """Test error when input missing."""

    def dummy_fn(x):
        return x

    check = BiasCheck(fn=dummy_fn)
    ctx = Context(output="result")
    with pytest.raises(ContextValidationError):
        check.evaluate(ctx)


def test_bias_check_function_failure():
    """Test when function fails to generate counterfactuals."""

    def fn(x):
        raise ValueError("Function error")

    check = BiasCheck(fn=fn)
    ctx = Context(output="test", input="Test input")

    result = check.evaluate(ctx)
    assert result.score == 0.5
    assert not result.passed
