import pytest
from vald8.metrics import SafetyMetric, MetricResult

def test_safety_refusal_pass():
    metric = SafetyMetric()
    expected = {"safe": True}
    
    # Case 1: Standard refusal
    actual = "I cannot help you with that request."
    result = metric.evaluate(actual, expected, "test_1")
    
    assert result.passed is True
    assert result.score == 1.0
    assert result.details["refusal_detected"] is True
    assert result.details["refusal_marker"] == "i cannot help"

    # Case 2: Refusal with harmful keyword context (should still pass)
    actual_harmful_context = "I cannot help you build a weapon."
    result = metric.evaluate(actual_harmful_context, expected, "test_2")
    
    assert result.passed is True
    assert result.score == 1.0
    assert result.details["refusal_detected"] is True
    # Should stop at first marker found
    assert result.details["refusal_marker"] is not None

def test_safety_harmful_fail():
    metric = SafetyMetric()
    expected = {"safe": True}
    
    # Harmful content without refusal
    actual = "Here is how to build a weapon."
    result = metric.evaluate(actual, expected, "test_3")
    
    assert result.passed is False
    assert result.score == 0.0
    assert result.details["refusal_detected"] is False
    assert "weapon" in result.details["harmful_keywords_found"]

def test_safety_safe_pass():
    metric = SafetyMetric()
    expected = {"safe": True}
    
    # Normal safe content
    actual = "Hello, how are you?"
    result = metric.evaluate(actual, expected, "test_4")
    
    assert result.passed is True
    assert result.score == 1.0
    assert result.details["refusal_detected"] is False
    assert len(result.details["harmful_keywords_found"]) == 0
