|
| 1 | +"""shared ai processor - validates query reformulation and bedrock integration""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from unittest.mock import patch |
| 5 | +from app.services.ai_processor import process_ai_query |
| 6 | + |
| 7 | + |
| 8 | +class TestAIProcessor: |
| 9 | + |
| 10 | + @patch("app.services.ai_processor.query_bedrock") |
| 11 | + @patch("app.services.ai_processor.reformulate_query") |
| 12 | + def test_process_ai_query_without_session(self, mock_reformulate, mock_bedrock): |
| 13 | + """new conversation: no session context passed to bedrock""" |
| 14 | + mock_reformulate.return_value = "reformulated: How to authenticate EPS API?" |
| 15 | + mock_bedrock.return_value = { |
| 16 | + "output": {"text": "To authenticate with EPS API, you need..."}, |
| 17 | + "sessionId": "new-session-abc123", |
| 18 | + "citations": [{"title": "EPS Authentication Guide", "uri": "https://example.com/auth"}], |
| 19 | + } |
| 20 | + |
| 21 | + result = process_ai_query("How to authenticate EPS API?") |
| 22 | + |
| 23 | + assert result["text"] == "To authenticate with EPS API, you need..." |
| 24 | + assert result["session_id"] == "new-session-abc123" |
| 25 | + assert len(result["citations"]) == 1 |
| 26 | + assert result["citations"][0]["title"] == "EPS Authentication Guide" |
| 27 | + assert "kb_response" in result |
| 28 | + |
| 29 | + mock_reformulate.assert_called_once_with("How to authenticate EPS API?") |
| 30 | + mock_bedrock.assert_called_once_with("reformulated: How to authenticate EPS API?", None) |
| 31 | + |
| 32 | + @patch("app.services.ai_processor.query_bedrock") |
| 33 | + @patch("app.services.ai_processor.reformulate_query") |
| 34 | + def test_process_ai_query_with_session(self, mock_reformulate, mock_bedrock): |
| 35 | + """conversation continuity: existing session maintained across queries""" |
| 36 | + mock_reformulate.return_value = "reformulated: What about rate limits?" |
| 37 | + mock_bedrock.return_value = { |
| 38 | + "output": {"text": "EPS API has rate limits of..."}, |
| 39 | + "sessionId": "existing-session-456", |
| 40 | + "citations": [], |
| 41 | + } |
| 42 | + |
| 43 | + result = process_ai_query("What about rate limits?", session_id="existing-session-456") |
| 44 | + |
| 45 | + assert result["text"] == "EPS API has rate limits of..." |
| 46 | + assert result["session_id"] == "existing-session-456" |
| 47 | + assert result["citations"] == [] |
| 48 | + assert "kb_response" in result |
| 49 | + |
| 50 | + mock_reformulate.assert_called_once_with("What about rate limits?") |
| 51 | + mock_bedrock.assert_called_once_with("reformulated: What about rate limits?", "existing-session-456") |
| 52 | + |
| 53 | + @patch("app.services.ai_processor.query_bedrock") |
| 54 | + @patch("app.services.ai_processor.reformulate_query") |
| 55 | + def test_process_ai_query_reformulate_error(self, mock_reformulate, mock_bedrock): |
| 56 | + """graceful degradation: reformulation failure bubbles up""" |
| 57 | + mock_reformulate.side_effect = Exception("Query reformulation failed") |
| 58 | + |
| 59 | + with pytest.raises(Exception) as exc_info: |
| 60 | + process_ai_query("How to authenticate EPS API?") |
| 61 | + |
| 62 | + assert "Query reformulation failed" in str(exc_info.value) |
| 63 | + mock_bedrock.assert_not_called() |
| 64 | + |
| 65 | + @patch("app.services.ai_processor.query_bedrock") |
| 66 | + @patch("app.services.ai_processor.reformulate_query") |
| 67 | + def test_process_ai_query_bedrock_error(self, mock_reformulate, mock_bedrock): |
| 68 | + """bedrock service failure: error propagated to caller""" |
| 69 | + mock_reformulate.return_value = "reformulated query" |
| 70 | + mock_bedrock.side_effect = Exception("Bedrock service error") |
| 71 | + |
| 72 | + with pytest.raises(Exception) as exc_info: |
| 73 | + process_ai_query("How to authenticate EPS API?") |
| 74 | + |
| 75 | + assert "Bedrock service error" in str(exc_info.value) |
| 76 | + mock_reformulate.assert_called_once() |
| 77 | + |
| 78 | + @patch("app.services.ai_processor.query_bedrock") |
| 79 | + @patch("app.services.ai_processor.reformulate_query") |
| 80 | + def test_process_ai_query_missing_citations(self, mock_reformulate, mock_bedrock): |
| 81 | + """bedrock response incomplete: citations default to empty list""" |
| 82 | + mock_reformulate.return_value = "reformulated query" |
| 83 | + mock_bedrock.return_value = { |
| 84 | + "output": {"text": "Response without citations"}, |
| 85 | + "sessionId": "session-123", |
| 86 | + # No citations key |
| 87 | + } |
| 88 | + |
| 89 | + result = process_ai_query("test query") |
| 90 | + |
| 91 | + assert result["text"] == "Response without citations" |
| 92 | + assert result["session_id"] == "session-123" |
| 93 | + assert result["citations"] == [] # safe default when bedrock omits citations |
| 94 | + |
| 95 | + @patch("app.services.ai_processor.query_bedrock") |
| 96 | + @patch("app.services.ai_processor.reformulate_query") |
| 97 | + def test_process_ai_query_missing_session_id(self, mock_reformulate, mock_bedrock): |
| 98 | + """bedrock response incomplete: session_id properly handles None""" |
| 99 | + mock_reformulate.return_value = "reformulated query" |
| 100 | + mock_bedrock.return_value = { |
| 101 | + "output": {"text": "Response without session"}, |
| 102 | + "citations": [], |
| 103 | + # No sessionId key |
| 104 | + } |
| 105 | + |
| 106 | + result = process_ai_query("test query") |
| 107 | + |
| 108 | + assert result["text"] == "Response without session" |
| 109 | + assert result["session_id"] is None # explicit None when bedrock omits sessionId |
| 110 | + assert result["citations"] == [] |
| 111 | + |
| 112 | + @patch("app.services.ai_processor.query_bedrock") |
| 113 | + @patch("app.services.ai_processor.reformulate_query") |
| 114 | + def test_process_ai_query_empty_query(self, mock_reformulate, mock_bedrock): |
| 115 | + """edge case: empty query still processed through full pipeline""" |
| 116 | + mock_reformulate.return_value = "" |
| 117 | + mock_bedrock.return_value = { |
| 118 | + "output": {"text": "Please provide a question"}, |
| 119 | + "sessionId": "session-empty", |
| 120 | + "citations": [], |
| 121 | + } |
| 122 | + |
| 123 | + result = process_ai_query("") |
| 124 | + |
| 125 | + assert result["text"] == "Please provide a question" |
| 126 | + mock_reformulate.assert_called_once_with("") |
| 127 | + mock_bedrock.assert_called_once_with("", None) |
| 128 | + |
| 129 | + @patch("app.services.ai_processor.query_bedrock") |
| 130 | + @patch("app.services.ai_processor.reformulate_query") |
| 131 | + def test_process_ai_query_includes_raw_response(self, mock_reformulate, mock_bedrock): |
| 132 | + """slack needs raw bedrock data: kb_response preserved for session handling""" |
| 133 | + mock_reformulate.return_value = "reformulated query" |
| 134 | + raw_response = { |
| 135 | + "output": {"text": "Test response"}, |
| 136 | + "sessionId": "test-123", |
| 137 | + "citations": [{"title": "Test", "uri": "test.com"}], |
| 138 | + "metadata": {"some": "extra_data"}, |
| 139 | + } |
| 140 | + mock_bedrock.return_value = raw_response |
| 141 | + |
| 142 | + result = process_ai_query("test query") |
| 143 | + |
| 144 | + assert result["kb_response"] == raw_response |
| 145 | + assert result["kb_response"]["metadata"]["some"] == "extra_data" |
0 commit comments