a digital entity named phi that roams bsky
at mcp-refactor 143 lines 4.4 kB view raw
1#!/usr/bin/env python 2"""Test AI integration without posting to Bluesky""" 3 4import asyncio 5 6import pytest 7 8from bot.config import settings 9from bot.response_generator import ResponseGenerator 10 11 12@pytest.mark.asyncio 13async def test_response_generator(): 14 """Test the response generator with various inputs""" 15 print("🧪 Testing AI Integration") 16 print(f" Bot name: {settings.bot_name}") 17 print(f" AI enabled: {'Yes' if settings.anthropic_api_key else 'No'}") 18 print() 19 20 # Create response generator 21 generator = ResponseGenerator() 22 23 # Test cases 24 test_cases = [ 25 { 26 "mention": f"@{settings.bot_name} What's your favorite color?", 27 "author": "test.user", 28 "description": "Simple question", 29 }, 30 { 31 "mention": f"@{settings.bot_name} Can you help me understand integrated information theory?", 32 "author": "curious.scientist", 33 "description": "Complex topic", 34 }, 35 { 36 "mention": f"@{settings.bot_name} hello!", 37 "author": "friendly.person", 38 "description": "Simple greeting", 39 }, 40 { 41 "mention": f"@{settings.bot_name} What do you think about consciousness?", 42 "author": "philosopher", 43 "description": "Philosophical question", 44 }, 45 ] 46 47 # Run tests 48 for i, test in enumerate(test_cases, 1): 49 print(f"Test {i}: {test['description']}") 50 print(f" From: @{test['author']}") 51 print(f" Raw text: {test['mention']}") 52 53 # In real AT Protocol, mentions are facets with structured data 54 # For testing, we pass the full text (bot can parse if needed) 55 print( 56 f" (Note: In production, @{settings.bot_name} would be a structured mention)" 57 ) 58 59 try: 60 response = await generator.generate( 61 mention_text=test["mention"], 62 author_handle=test["author"], 63 thread_context="", 64 ) 65 print(f" Response: {response}") 66 print(f" Length: {len(response)} chars") 67 68 # Verify response is within Bluesky limit 69 if len(response) > 300: 70 print(" ⚠️ WARNING: Response exceeds 300 character limit!") 71 else: 72 print(" ✅ Response within limit") 73 74 except Exception as e: 75 print(f" ❌ ERROR: {e}") 76 import traceback 77 78 traceback.print_exc() 79 80 print() 81 82 # Test response consistency 83 if generator.agent: 84 print("🔄 Testing response consistency...") 85 test_mention = f"@{settings.bot_name} What are you?" 86 responses = [] 87 88 for i in range(3): 89 response = await generator.generate( 90 mention_text=test_mention, 91 author_handle="consistency.tester", 92 thread_context="", 93 ) 94 responses.append(response) 95 print(f" Response {i + 1}: {response[:50]}...") 96 97 # Check if responses are different (they should be somewhat varied) 98 if len(set(responses)) == 1: 99 print(" ⚠️ All responses are identical - might want more variation") 100 else: 101 print(" ✅ Responses show variation") 102 103 print("\n✨ Test complete!") 104 105 106@pytest.mark.asyncio 107async def test_direct_agent(): 108 """Test the Anthropic agent directly""" 109 if not settings.anthropic_api_key: 110 print("⚠️ No Anthropic API key found - skipping direct agent test") 111 return 112 113 print("\n🤖 Testing Anthropic Agent Directly") 114 115 try: 116 from bot.agents.anthropic_agent import AnthropicAgent 117 118 agent = AnthropicAgent() 119 120 # Test a simple response 121 response = await agent.generate_response( 122 mention_text=f"@{settings.bot_name} explain your name", 123 author_handle="name.curious", 124 thread_context="", 125 ) 126 127 print(f"Direct agent response: {response}") 128 print(f"Response length: {len(response)} chars") 129 130 except Exception as e: 131 print(f"❌ Direct agent test failed: {e}") 132 import traceback 133 134 traceback.print_exc() 135 136 137if __name__ == "__main__": 138 print("=" * 60) 139 print(f"{settings.bot_name} Bot - AI Integration Test") 140 print("=" * 60) 141 142 asyncio.run(test_response_generator()) 143 asyncio.run(test_direct_agent())