#!/usr/bin/env python3
"""
Real-world test: Search a non-existent index to verify server returns suggestions.
"""
import asyncio
import sys
import os
# Add the parent directory to the Python path to access src
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
from src.elasticsearch_handlers import handle_search
async def test_real_search_nonexistent_index():
"""Test real search with non-existent index to verify suggestions are returned."""
print("π§ͺ REAL-WORLD TEST: Search Non-Existent Index")
print("=" * 60)
print("Testing if server returns 4-step agent guidance for non-existent index...")
print()
# Test with completely non-existent index
test_index = "nonexistent_test_index_12345"
print(f"π Testing search with non-existent index: '{test_index}'")
print(f"π Query: 'test search'")
print()
try:
# Perform the actual search
result = await handle_search({
"index": test_index,
"query": "test search",
"size": 10
})
print("π SERVER RESPONSE:")
print("-" * 40)
for content in result:
response_text = content.text
print(response_text)
# Check if response contains the expected 4-step guidance
has_suggestions = "Suggestions for agents" in response_text
has_4_steps = all(step in response_text for step in ["1.", "2.", "3.", "4."])
has_list_indices = "list_indices" in response_text
has_index_error = "Index Error" in response_text or "Index Not Found" in response_text
print()
print("β
VALIDATION RESULTS:")
print(f" π― Contains agent suggestions: {'β
' if has_suggestions else 'β'}")
print(f" π Has 4-step guidance: {'β
' if has_4_steps else 'β'}")
print(f" π οΈ Mentions list_indices tool: {'β
' if has_list_indices else 'β'}")
print(f" π Identifies as index error: {'β
' if has_index_error else 'β'}")
all_checks_pass = has_suggestions and has_4_steps and has_list_indices and has_index_error
print(f" π OVERALL TEST RESULT: {'β
PASS' if all_checks_pass else 'β FAIL'}")
if all_checks_pass:
print()
print("π SUCCESS: Server correctly returns 4-step agent guidance!")
print("π Error handling enhancement is working as expected.")
else:
print()
print("β οΈ WARNING: Some expected suggestions are missing.")
print("π Error handling may need further improvement.")
except Exception as e:
print(f"β TEST ERROR: {str(e)}")
print("π Could not complete the test due to technical issues.")
print()
print("=" * 60)
print("π Test completed!")
if __name__ == "__main__":
asyncio.run(test_real_search_nonexistent_index())