|
| 1 | +"""direct lambda invocation - validates bypassing slack infrastructure entirely""" |
| 2 | + |
| 3 | +from unittest.mock import Mock, patch |
| 4 | +from app.handler import handle_direct_invocation |
| 5 | + |
| 6 | + |
| 7 | +class TestDirectInvocation: |
| 8 | + |
| 9 | + @patch("app.services.ai_processor.process_ai_query") |
| 10 | + def test_successful_direct_invocation_without_session(self, mock_process_ai_query): |
| 11 | + """new conversation: no session context from previous queries""" |
| 12 | + mock_process_ai_query.return_value = { |
| 13 | + "text": "AI response about EPS API authentication", |
| 14 | + "session_id": "new-session-123", |
| 15 | + "citations": [{"title": "EPS API Guide", "uri": "https://example.com"}], |
| 16 | + "kb_response": {"sessionId": "new-session-123"}, |
| 17 | + } |
| 18 | + |
| 19 | + event = {"invocation_type": "direct", "query": "How do I authenticate with EPS API?"} |
| 20 | + |
| 21 | + result = handle_direct_invocation(event, Mock()) |
| 22 | + |
| 23 | + assert result["statusCode"] == 200 |
| 24 | + assert result["response"]["text"] == "AI response about EPS API authentication" |
| 25 | + assert result["response"]["session_id"] == "new-session-123" |
| 26 | + assert len(result["response"]["citations"]) == 1 |
| 27 | + assert "timestamp" in result["response"] |
| 28 | + |
| 29 | + mock_process_ai_query.assert_called_once_with("How do I authenticate with EPS API?", None) |
| 30 | + |
| 31 | + @patch("app.services.ai_processor.process_ai_query") |
| 32 | + def test_successful_direct_invocation_with_session(self, mock_process_ai_query): |
| 33 | + """conversation continuity: session maintained across direct calls""" |
| 34 | + mock_process_ai_query.return_value = { |
| 35 | + "text": "Follow-up response", |
| 36 | + "session_id": "existing-session-456", |
| 37 | + "citations": [], |
| 38 | + "kb_response": {"sessionId": "existing-session-456"}, |
| 39 | + } |
| 40 | + |
| 41 | + event = {"invocation_type": "direct", "query": "What about rate limits?", "session_id": "existing-session-456"} |
| 42 | + |
| 43 | + result = handle_direct_invocation(event, Mock()) |
| 44 | + |
| 45 | + assert result["statusCode"] == 200 |
| 46 | + assert result["response"]["text"] == "Follow-up response" |
| 47 | + assert result["response"]["session_id"] == "existing-session-456" |
| 48 | + assert result["response"]["citations"] == [] |
| 49 | + assert "timestamp" in result["response"] |
| 50 | + |
| 51 | + mock_process_ai_query.assert_called_once_with("What about rate limits?", "existing-session-456") |
| 52 | + |
| 53 | + def test_direct_invocation_missing_query(self): |
| 54 | + """input validation: query field required for processing""" |
| 55 | + event = {"invocation_type": "direct"} |
| 56 | + |
| 57 | + result = handle_direct_invocation(event, Mock()) |
| 58 | + |
| 59 | + assert result["statusCode"] == 400 |
| 60 | + assert "Missing required field: query" in result["response"]["error"] |
| 61 | + assert "timestamp" in result["response"] |
| 62 | + |
| 63 | + def test_direct_invocation_empty_query(self): |
| 64 | + """edge case: empty string treated same as missing query""" |
| 65 | + event = {"invocation_type": "direct", "query": ""} |
| 66 | + |
| 67 | + result = handle_direct_invocation(event, Mock()) |
| 68 | + |
| 69 | + assert result["statusCode"] == 400 |
| 70 | + assert "Missing required field: query" in result["response"]["error"] |
| 71 | + assert "timestamp" in result["response"] |
| 72 | + |
| 73 | + @patch("app.services.ai_processor.process_ai_query") |
| 74 | + def test_direct_invocation_processing_error(self, mock_process_ai_query): |
| 75 | + """ai service failure: graceful error response to caller""" |
| 76 | + mock_process_ai_query.side_effect = Exception("Bedrock service unavailable") |
| 77 | + |
| 78 | + event = {"invocation_type": "direct", "query": "How do I authenticate with EPS API?"} |
| 79 | + |
| 80 | + result = handle_direct_invocation(event, Mock()) |
| 81 | + |
| 82 | + assert result["statusCode"] == 500 |
| 83 | + assert result["response"]["error"] == "Internal server error" |
| 84 | + assert "timestamp" in result["response"] |
| 85 | + |
| 86 | + @patch("app.services.ai_processor.process_ai_query") |
| 87 | + def test_direct_invocation_with_none_query(self, mock_process_ai_query): |
| 88 | + """edge case: None query handled same as empty string""" |
| 89 | + event = {"invocation_type": "direct", "query": None} |
| 90 | + |
| 91 | + result = handle_direct_invocation(event, Mock()) |
| 92 | + |
| 93 | + assert result["statusCode"] == 400 |
| 94 | + assert "Missing required field: query" in result["response"]["error"] |
| 95 | + |
| 96 | + @patch("app.services.ai_processor.process_ai_query") |
| 97 | + def test_direct_invocation_whitespace_query(self, mock_process_ai_query): |
| 98 | + """input sanitization: whitespace-only queries rejected""" |
| 99 | + event = {"invocation_type": "direct", "query": " \n\t "} |
| 100 | + |
| 101 | + result = handle_direct_invocation(event, Mock()) |
| 102 | + |
| 103 | + assert result["statusCode"] == 400 |
| 104 | + assert "Missing required field: query" in result["response"]["error"] |
| 105 | + |
| 106 | + @patch("app.services.ai_processor.process_ai_query") |
| 107 | + def test_direct_invocation_response_structure(self, mock_process_ai_query): |
| 108 | + """api contract: response structure matches expected format""" |
| 109 | + mock_process_ai_query.return_value = { |
| 110 | + "text": "Test response", |
| 111 | + "session_id": "test-session", |
| 112 | + "citations": [ |
| 113 | + {"title": "Doc 1", "uri": "https://example.com/1"}, |
| 114 | + {"title": "Doc 2", "uri": "https://example.com/2"}, |
| 115 | + ], |
| 116 | + "kb_response": {"sessionId": "test-session"}, |
| 117 | + } |
| 118 | + |
| 119 | + event = {"invocation_type": "direct", "query": "Test query"} |
| 120 | + |
| 121 | + result = handle_direct_invocation(event, Mock()) |
| 122 | + |
| 123 | + # api contract validation: all required fields present |
| 124 | + assert "statusCode" in result |
| 125 | + assert "response" in result |
| 126 | + assert "text" in result["response"] |
| 127 | + assert "session_id" in result["response"] |
| 128 | + assert "citations" in result["response"] |
| 129 | + assert "timestamp" in result["response"] |
| 130 | + |
| 131 | + # citation passthrough: bedrock data structure preserved |
| 132 | + assert len(result["response"]["citations"]) == 2 |
| 133 | + assert result["response"]["citations"][0]["title"] == "Doc 1" |
| 134 | + assert result["response"]["citations"][1]["uri"] == "https://example.com/2" |
| 135 | + |
| 136 | + @patch("app.services.ai_processor.process_ai_query") |
| 137 | + def test_direct_invocation_timestamp_format(self, mock_process_ai_query): |
| 138 | + """timestamp format: iso8601 with Z suffix for consistency""" |
| 139 | + mock_process_ai_query.return_value = { |
| 140 | + "text": "Test response", |
| 141 | + "session_id": None, |
| 142 | + "citations": [], |
| 143 | + "kb_response": {}, |
| 144 | + } |
| 145 | + |
| 146 | + event = {"invocation_type": "direct", "query": "Test query"} |
| 147 | + |
| 148 | + result = handle_direct_invocation(event, Mock()) |
| 149 | + |
| 150 | + timestamp = result["response"]["timestamp"] |
| 151 | + # iso8601 validation: parseable datetime with utc marker |
| 152 | + assert timestamp.endswith("Z") |
| 153 | + assert "T" in timestamp |
| 154 | + # format verification: datetime parsing confirms structure |
| 155 | + from datetime import datetime |
| 156 | + |
| 157 | + datetime.fromisoformat(timestamp.rstrip("Z")) |
0 commit comments