"""Tests for ResolutionEngine decision branches.""" import pytest from src.context.context_manager import ConversationContext from src.context.resolution_engine import ( ResolutionEngine, ResolutionType, UserInput, AMBIGUITY_THRESHOLD, ) # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def make_candidates(*score_pairs: tuple[str, float]) -> list[dict]: """Build a candidate list from (intent, score) pairs.""" return [{"intent": i, "confidence": s} for i, s in score_pairs] def make_input( candidates, entities=None, keywords=None, history_enabled=True, context=None, ): ctx = context or ConversationContext() ctx.history_enabled = history_enabled return UserInput( question="test question", entities=entities or [], keywords=keywords or [], candidate_intents=candidates, context=ctx, history_enabled=history_enabled, ) @pytest.fixture def engine(): return ResolutionEngine(threshold=AMBIGUITY_THRESHOLD) # --------------------------------------------------------------------------- # Branch 1: DIRECT (high confidence) # --------------------------------------------------------------------------- def test_direct_resolution_high_diff(engine): """Score diff >= threshold → DIRECT.""" candidates = make_candidates(("algorithm_definition", 0.92), ("time_complexity", 0.41)) resolved = engine.resolve(make_input(candidates)) assert resolved.resolution_type == ResolutionType.DIRECT assert resolved.final_intent == "algorithm_definition" assert resolved.context_used is False def test_direct_resolution_at_or_above_threshold(engine): """Score diff clearly >= threshold → DIRECT.""" candidates = make_candidates(("algorithm_definition", 0.65), ("time_complexity", 0.45)) resolved = engine.resolve(make_input(candidates)) assert resolved.resolution_type == ResolutionType.DIRECT # --------------------------------------------------------------------------- # Branch 2: CONTEXT_RESOLVED # --------------------------------------------------------------------------- def test_context_resolved_follow_up(engine): """No current entities + context has entities → CONTEXT_RESOLVED.""" ctx = ConversationContext() ctx.recent_entities = ["binary_search"] ctx.recent_intents = ["algorithm_definition"] ctx.history_enabled = True # Close scores → would normally be AMBIGUOUS candidates = make_candidates(("time_complexity", 0.56), ("implementation", 0.53)) resolved = engine.resolve(make_input(candidates, entities=[], context=ctx)) assert resolved.resolution_type == ResolutionType.CONTEXT_RESOLVED assert resolved.context_used is True assert "algorithm_definition" in resolved.previous_intents_used def test_context_resolved_entity_overlap(engine): """Current entity overlaps with context entity → CONTEXT_RESOLVED.""" ctx = ConversationContext() ctx.recent_entities = ["binary_search"] ctx.history_enabled = True candidates = make_candidates(("time_complexity", 0.56), ("implementation", 0.53)) resolved = engine.resolve(make_input(candidates, entities=["binary_search"], context=ctx)) assert resolved.resolution_type == ResolutionType.CONTEXT_RESOLVED def test_context_not_used_when_history_disabled(engine): """history_enabled=False → must NOT use context, should be AMBIGUOUS.""" ctx = ConversationContext() ctx.recent_entities = ["binary_search"] ctx.history_enabled = False candidates = make_candidates(("time_complexity", 0.56), ("implementation", 0.53)) resolved = engine.resolve(make_input(candidates, entities=[], history_enabled=False, context=ctx)) assert resolved.resolution_type == ResolutionType.AMBIGUOUS assert resolved.context_used is False # --------------------------------------------------------------------------- # Branch 3: AMBIGUOUS # --------------------------------------------------------------------------- def test_ambiguous_when_scores_close_no_context(engine): """Close scores + no context → AMBIGUOUS with fallback options.""" candidates = make_candidates(("time_complexity", 0.56), ("implementation", 0.53)) resolved = engine.resolve(make_input(candidates)) assert resolved.resolution_type == ResolutionType.AMBIGUOUS assert resolved.fallback_options is not None assert len(resolved.fallback_options) >= 2 def test_fallback_options_at_most_3(engine): candidates = make_candidates( ("a", 0.35), ("b", 0.34), ("c", 0.33), ("d", 0.32) ) resolved = engine.resolve(make_input(candidates)) if resolved.fallback_options: assert len(resolved.fallback_options) <= 3 # --------------------------------------------------------------------------- # Branch 4: FALLBACK # --------------------------------------------------------------------------- def test_fallback_on_empty_candidates(engine): """No candidates → FALLBACK with 'unknown' intent.""" resolved = engine.resolve(make_input([])) assert resolved.resolution_type == ResolutionType.FALLBACK assert resolved.final_intent == "unknown" def test_fallback_has_zero_confidence(engine): resolved = engine.resolve(make_input([])) assert resolved.confidence == 0.0 # --------------------------------------------------------------------------- # Single candidate (no top-2 comparison) # --------------------------------------------------------------------------- def test_single_candidate_is_direct(engine): """Only one candidate → diff = score - 0 >= threshold → DIRECT.""" candidates = make_candidates(("algorithm_definition", 0.85)) resolved = engine.resolve(make_input(candidates)) assert resolved.resolution_type == ResolutionType.DIRECT def test_ambiguous_low_scores_shows_2_outputs(engine): """When top score is low and difference is less, show 2 outputs with confidence levels.""" candidates = make_candidates(("Quick_Sort_Algorithm", 0.28), ("Merge_Sort_Algorithm", 0.24), ("Bubble_Sort", 0.15)) resolved = engine.resolve(make_input(candidates)) assert resolved.resolution_type == ResolutionType.AMBIGUOUS assert resolved.fallback_options is not None assert len(resolved.fallback_options) == 2 # Check that labels contain confidence percentage assert "28.0%" in resolved.fallback_options[0][0] assert "24.0%" in resolved.fallback_options[1][0] assert resolved.fallback_options[0][1] == "Quick_Sort_Algorithm" assert resolved.fallback_options[1][1] == "Merge_Sort_Algorithm" def test_noise_floor_triggers_fallback(engine): """Very low noise scores (e.g. out of domain) must trigger FALLBACK with no options.""" candidates = make_candidates(("random_intent_1", 0.04), ("random_intent_2", 0.03)) resolved = engine.resolve(make_input(candidates)) assert resolved.resolution_type == ResolutionType.FALLBACK assert resolved.final_intent == "out_of_scope" assert resolved.fallback_options is None