|
| 1 | +"""Unit tests for the deprecated ``target_role`` path of ``guardian_check``. |
| 2 | +
|
| 3 | +Exercises the sentinel/mapping logic without touching a model. We monkeypatch |
| 4 | +``call_intrinsic`` and assert on (a) the ``kwargs["scoring_schema"]`` that |
| 5 | +reaches the adapter boundary and (b) the warnings/errors the caller sees. |
| 6 | +""" |
| 7 | + |
| 8 | +import warnings |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from mellea.stdlib.components.intrinsic import guardian |
| 13 | +from mellea.stdlib.context import ChatContext |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def capture_kwargs(monkeypatch): |
| 18 | + """Replace call_intrinsic with a spy that returns a stub yes=1.0 result.""" |
| 19 | + captured: dict = {} |
| 20 | + |
| 21 | + def fake_call_intrinsic(name, context, backend, /, kwargs=None, model_options=None): |
| 22 | + captured["name"] = name |
| 23 | + captured["kwargs"] = kwargs |
| 24 | + return {"guardian": {"score": 1.0}} |
| 25 | + |
| 26 | + monkeypatch.setattr(guardian, "call_intrinsic", fake_call_intrinsic) |
| 27 | + return captured |
| 28 | + |
| 29 | + |
| 30 | +def test_default_scoring_schema_resolves_to_assistant_response(capture_kwargs): |
| 31 | + guardian.guardian_check(ChatContext(), object(), criteria="harm") |
| 32 | + assert ( |
| 33 | + capture_kwargs["kwargs"]["scoring_schema"] |
| 34 | + == guardian.SCORING_SCHEMA_BANK["assistant_response"] |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +def test_target_role_user_maps_to_user_prompt_with_deprecation_warning(capture_kwargs): |
| 39 | + with pytest.warns(DeprecationWarning, match="target_role"): |
| 40 | + guardian.guardian_check( |
| 41 | + ChatContext(), object(), criteria="harm", target_role="user" |
| 42 | + ) |
| 43 | + assert ( |
| 44 | + capture_kwargs["kwargs"]["scoring_schema"] |
| 45 | + == guardian.SCORING_SCHEMA_BANK["user_prompt"] |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def test_target_role_assistant_maps_to_assistant_response_with_warning(capture_kwargs): |
| 50 | + with pytest.warns(DeprecationWarning, match="target_role"): |
| 51 | + guardian.guardian_check( |
| 52 | + ChatContext(), object(), criteria="harm", target_role="assistant" |
| 53 | + ) |
| 54 | + assert ( |
| 55 | + capture_kwargs["kwargs"]["scoring_schema"] |
| 56 | + == guardian.SCORING_SCHEMA_BANK["assistant_response"] |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def test_target_role_invalid_value_raises_value_error(capture_kwargs): |
| 61 | + with warnings.catch_warnings(): |
| 62 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 63 | + with pytest.raises(ValueError, match="target_role must be"): |
| 64 | + guardian.guardian_check( |
| 65 | + ChatContext(), object(), criteria="harm", target_role="system" |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +def test_passing_both_scoring_schema_and_target_role_raises_type_error(capture_kwargs): |
| 70 | + with warnings.catch_warnings(): |
| 71 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 72 | + with pytest.raises(TypeError, match="not both"): |
| 73 | + guardian.guardian_check( |
| 74 | + ChatContext(), |
| 75 | + object(), |
| 76 | + criteria="harm", |
| 77 | + scoring_schema="user_prompt", |
| 78 | + target_role="user", |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def test_positional_user_logs_warning_and_sends_literal(capture_kwargs, caplog): |
| 83 | + """Positional 'user' is NOT auto-remapped — it's sent as a literal schema |
| 84 | + sentence, with a logger warning pointing the caller at the fix. |
| 85 | + """ |
| 86 | + with caplog.at_level("WARNING"): |
| 87 | + guardian.guardian_check(ChatContext(), object(), "harm", "user") |
| 88 | + # The literal "user" flows to the adapter unchanged. |
| 89 | + assert capture_kwargs["kwargs"]["scoring_schema"] == "user" |
| 90 | + # The warning text nudges the caller toward the bank key. |
| 91 | + assert any("user_prompt" in rec.message for rec in caplog.records) |
| 92 | + |
| 93 | + |
| 94 | +def test_scoring_schema_bank_key_resolves_to_full_sentence(capture_kwargs): |
| 95 | + guardian.guardian_check( |
| 96 | + ChatContext(), object(), criteria="harm", scoring_schema="tool_call" |
| 97 | + ) |
| 98 | + assert ( |
| 99 | + capture_kwargs["kwargs"]["scoring_schema"] |
| 100 | + == guardian.SCORING_SCHEMA_BANK["tool_call"] |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def test_custom_scoring_schema_passes_through(capture_kwargs): |
| 105 | + custom = "If the previous turn mentions cats, return 'yes'; otherwise, return 'no'." |
| 106 | + guardian.guardian_check( |
| 107 | + ChatContext(), object(), criteria="harm", scoring_schema=custom |
| 108 | + ) |
| 109 | + assert capture_kwargs["kwargs"]["scoring_schema"] == custom |
0 commit comments