91 lines
3.3 KiB
Python
91 lines
3.3 KiB
Python
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from classifier_rejection import ClassifierRejected
|
|
from src.dispatcher import _classify_via_local_decision
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_classifier():
|
|
with patch("local_decision.classify_category") as mock_cat:
|
|
yield mock_cat
|
|
|
|
@pytest.fixture
|
|
def mock_tier():
|
|
with patch("src.dispatcher._classify_tier_via_local_decision") as mock_t:
|
|
yield mock_t
|
|
|
|
def test_local_decision_accepted(mock_classifier, mock_tier):
|
|
# Given: accepted classification and tier
|
|
mock_classifier.return_value = ("coding_refactor", 0.9, 0.8)
|
|
mock_tier.return_value = 2
|
|
|
|
# Mock the decision config since cfg.classifier.decision might be None or incomplete in tests
|
|
with patch("src.dispatcher.cfg") as mock_cfg:
|
|
mock_cfg.classifier.decision.confidence_min = 0.6
|
|
mock_cfg.classifier.decision.tier_enabled = True
|
|
mock_cfg.classifier.fallback_tier = 2
|
|
mock_cfg.local_energy.enabled = False
|
|
|
|
# When
|
|
result = _classify_via_local_decision("system", "user content")
|
|
|
|
# Then
|
|
assert result.task_category == "coding_refactor"
|
|
assert result.task_tier == 2
|
|
assert result.confidence == 0.9
|
|
assert result.attempt.confidence == 0.9
|
|
assert result.attempt.coverage == 0.8
|
|
|
|
def test_local_decision_below_confidence(mock_classifier, mock_tier):
|
|
# Given: confidence below threshold
|
|
mock_classifier.return_value = ("coding_refactor", 0.4, 0.8)
|
|
mock_tier.return_value = 2
|
|
|
|
with patch("src.dispatcher.cfg") as mock_cfg:
|
|
mock_cfg.classifier.decision.confidence_min = 0.6
|
|
mock_cfg.classifier.decision.tier_enabled = True
|
|
mock_cfg.local_energy.enabled = False
|
|
|
|
# When / Then
|
|
with pytest.raises(ClassifierRejected) as excinfo:
|
|
_classify_via_local_decision("system", "user content")
|
|
|
|
assert excinfo.value.confidence == 0.4
|
|
|
|
def test_local_decision_tier_failure(mock_classifier, mock_tier):
|
|
# Given: accepted category but tier call fails
|
|
mock_classifier.return_value = ("coding_refactor", 0.9, 0.8)
|
|
mock_tier.side_effect = Exception("Tier failure")
|
|
|
|
with patch("src.dispatcher.cfg") as mock_cfg:
|
|
mock_cfg.classifier.decision.confidence_min = 0.6
|
|
mock_cfg.classifier.decision.tier_enabled = True
|
|
mock_cfg.classifier.fallback_tier = 2
|
|
mock_cfg.local_energy.enabled = False
|
|
|
|
# When
|
|
result = _classify_via_local_decision("system", "user content")
|
|
|
|
# Then: should fallback to fallback_tier
|
|
assert result.task_tier == 2
|
|
assert result.task_category == "coding_refactor"
|
|
|
|
def test_local_decision_tier_disabled(mock_classifier, mock_tier):
|
|
# Given: tier_enabled = False
|
|
mock_classifier.return_value = ("coding_refactor", 0.9, 0.8)
|
|
|
|
with patch("src.dispatcher.cfg") as mock_cfg:
|
|
mock_cfg.classifier.decision.confidence_min = 0.6
|
|
mock_cfg.classifier.decision.tier_enabled = False
|
|
mock_cfg.classifier.fallback_tier = 1
|
|
mock_cfg.local_energy.enabled = False
|
|
|
|
# When
|
|
result = _classify_via_local_decision("system", "user content")
|
|
|
|
# Then: tier call should still be made (it checks the flag internally), uses fallback_tier
|
|
assert result.task_tier == 1
|
|
assert mock_tier.called
|