Team Ai
Apppublic

prazy1208/text2sql

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
test_intent_agent_context.py89 linesDownload Raw Back to tests
1"""Tests for Intent Agent context-window policy and parsing."""2 3from backend.agents.intent_agent import (4    _build_conversation_context_block,5    _parse_intent_response,6    normalize_confidence_score,7)8 9 10def test_context_block_includes_full_history_when_up_to_three_messages():11    block = _build_conversation_context_block(12        "also include by region",13        recent_messages=[14            {"role": "user", "content": "show top customers"},15            {"role": "assistant", "content": "Do you mean by spend?"},16            {"role": "user", "content": "yes by spend"},17        ],18        last_confirmed_intent="Top customers by spend",19        pending_intent=None,20        session_summary=None,21    )22    assert "Previous messages (full)" in block23    assert "Older messages summary" not in block24    assert "Last confirmed intent: Top customers by spend" in block25 26 27def test_context_block_summarizes_when_more_than_three_messages():28    block = _build_conversation_context_block(29        "for last quarter",30        recent_messages=[31            {"role": "user", "content": "show sales by month"},32            {"role": "assistant", "content": "Which domain?"},33            {"role": "user", "content": "retail"},34            {"role": "assistant", "content": "Do you want total sales?"},35            {"role": "user", "content": "yes"},36        ],37        last_confirmed_intent="Monthly retail sales",38        pending_intent="Monthly retail sales with confirmation pending",39        session_summary={"session_goal": "trend analysis"},40    )41    assert "Older messages summary" in block42    assert "Previous messages (recent full turns)" in block43    assert "Pending intent awaiting confirmation" in block44    assert "Session summary:" in block45 46 47def test_parse_intent_response_reads_confidence_and_resolved_question():48    payload = """49    {50      "rephrased_question": "Top spenders by branch",51      "resolved_question": "Show top spenders by branch for last month",52      "confidence_score": 72,53      "clarification_question": "Did you mean top spenders by branch for last month?",54      "keywords": ["ranking", "spend", "branch"],55      "business_insights": ["High activity entities"]56    }57    """58    parsed = _parse_intent_response(payload)59    assert parsed is not None60    assert parsed["resolved_question"] == "Show top spenders by branch for last month"61    assert parsed["confidence_score"] == 7262    assert parsed["clarification_question"].startswith("Did you mean")63 64 65def test_normalize_confidence_score_fractional():66    assert normalize_confidence_score(0.85) == 8567    assert normalize_confidence_score(1) == 10068    assert normalize_confidence_score(1.0) == 10069    assert normalize_confidence_score(0.72) == 7270    assert normalize_confidence_score("0.9") == 9071 72 73def test_normalize_confidence_score_already_percent():74    assert normalize_confidence_score(72) == 7275    assert normalize_confidence_score(100) == 10076 77 78def test_normalize_confidence_score_none_or_invalid_defaults_conservative():79    assert normalize_confidence_score(None) == 5080    assert normalize_confidence_score("not-a-number") == 5081 82 83def test_parse_intent_response_confidence_as_fraction():84    payload = """{"rephrased_question": "x", "resolved_question": "x", "confidence_score": 0.79,85    "clarification_question": "", "keywords": [], "business_insights": []}"""86    parsed = _parse_intent_response(payload)87    assert parsed is not None88    assert parsed["confidence_score"] == 7989