#!/usr/bin/env python3 """ Test script to verify chat fixes are working correctly. Run this after starting the backend server. """ import asyncio import sys import os # Add the app directory to Python path sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'app')) from motor.motor_asyncio import AsyncIOMotorClient from datetime import datetime from services.llama_processor import llama_processor from services.chat_context_service import chat_context_service async def test_chat_fixes(): print("🧪 Testing Chat Fixes...") # Test 1: Check if LlamaProcessor methods work print("\n1. Testing LlamaProcessor collection methods...") test_index_id = "test-index-123" # Test collection existence check exists = llama_processor.check_collection_exists(test_index_id) print(f" Collection exists: {exists}") # Test collection info info = llama_processor.get_collection_info(test_index_id) print(f" Collection info: {info}") # Test 2: Check MongoDB connection print("\n2. Testing MongoDB connection...") try: client = AsyncIOMotorClient("mongodb://localhost:27017") db = client.contract_analysis # Test document count doc_count = await db.documents.count_documents({}) print(f" Total documents in DB: {doc_count}") # Test indices count indices_count = await db.indices.count_documents({}) print(f" Total indices in DB: {indices_count}") await client.close() except Exception as e: print(f" MongoDB connection failed: {e}") # Test 3: Test timestamp generation print("\n3. Testing timestamp generation...") now = datetime.utcnow() print(f" Current UTC timestamp: {now}") print(f" Formatted: {now.strftime('%Y-%m-%d %H:%M:%S')}") # Test 4: Test context service print("\n4. Testing context service...") try: # Test context formatting test_messages = [ {"query": "What is this document about?", "response": "This document is about contracts.", "created_at": now}, {"query": "Tell me more", "response": "It contains legal terms and conditions.", "created_at": now} ] formatted_context = chat_context_service.format_context_for_ai(test_messages) print(f" Context formatted successfully: {len(formatted_context)} characters") except Exception as e: print(f" Context service test failed: {e}") print("\n✅ Chat fixes test completed!") if __name__ == "__main__": asyncio.run(test_chat_fixes())