First Commit
This commit is contained in:
Executable
+97
@@ -0,0 +1,97 @@
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, "/home/marco/MonicaAgentIA/app")
|
||||
|
||||
os.environ["TELEGRAM_BOT_TOKEN"] = "test"
|
||||
os.environ["TELEGRAM_CHAT_ID"] = "123"
|
||||
|
||||
from unittest.mock import patch, AsyncMock
|
||||
from monica_agent import get_monica_response, AIProvider
|
||||
|
||||
async def test_retry_success_after_failures():
|
||||
print("=== Testando retry: sucesso após falhas ===")
|
||||
|
||||
call_count = 0
|
||||
def mock_chat(model, messages, temperature=0.8, max_tokens=2000):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
if call_count < 3:
|
||||
print(f" Tentativa {call_count}: falha (simulada)")
|
||||
return None
|
||||
print(f" Tentativa {call_count}: sucesso")
|
||||
return "Resposta após retry"
|
||||
|
||||
async def mock_ensure_min_lines(response, *args, **kwargs):
|
||||
return response
|
||||
|
||||
with patch.object(AIProvider, 'chat', mock_chat), \
|
||||
patch('monica_agent.ensure_min_lines', mock_ensure_min_lines):
|
||||
response = await get_monica_response("Olá")
|
||||
|
||||
print(f" Total de chamadas: {call_count}")
|
||||
print(f" Resposta: {response}")
|
||||
|
||||
if call_count == 3 and response == "Resposta após retry":
|
||||
print("✅ Retry funcionou: sucesso após 2 falhas")
|
||||
else:
|
||||
print(f"❌ Retry não funcionou como esperado: {call_count} chamadas")
|
||||
|
||||
async def test_retry_exhausted():
|
||||
print("\n=== Testando retry: esgotamento das 5 tentativas ===")
|
||||
|
||||
call_count = 0
|
||||
def mock_chat(model, messages, temperature=0.8, max_tokens=2000):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
print(f" Tentativa {call_count}: falha")
|
||||
return None
|
||||
|
||||
async def mock_ensure_min_lines(response, *args, **kwargs):
|
||||
return response
|
||||
|
||||
with patch.object(AIProvider, 'chat', mock_chat), \
|
||||
patch('monica_agent.ensure_min_lines', mock_ensure_min_lines):
|
||||
response = await get_monica_response("Olá")
|
||||
|
||||
print(f" Total de chamadas: {call_count}")
|
||||
print(f" Resposta: {response}")
|
||||
|
||||
if call_count == 5 and response is None:
|
||||
print("✅ Retry esgotado corretamente após 5 tentativas")
|
||||
else:
|
||||
print(f"❌ Esperado 5 chamadas, got {call_count}")
|
||||
|
||||
async def test_no_retry_on_success():
|
||||
print("\n=== Testando: sem retry quando sucesso na primeira tentativa ===")
|
||||
|
||||
call_count = 0
|
||||
def mock_chat(model, messages, temperature=0.8, max_tokens=2000):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
print(f" Tentativa {call_count}: sucesso")
|
||||
return "Resposta imediata"
|
||||
|
||||
async def mock_ensure_min_lines(response, *args, **kwargs):
|
||||
return response
|
||||
|
||||
with patch.object(AIProvider, 'chat', mock_chat), \
|
||||
patch('monica_agent.ensure_min_lines', mock_ensure_min_lines):
|
||||
response = await get_monica_response("Olá")
|
||||
|
||||
print(f" Total de chamadas: {call_count}")
|
||||
print(f" Resposta: {response}")
|
||||
|
||||
if call_count == 1 and response == "Resposta imediata":
|
||||
print("✅ Sem retry desnecessário quando sucesso na primeira tentativa")
|
||||
else:
|
||||
print(f"❌ Esperado 1 chamada, got {call_count}")
|
||||
|
||||
async def main():
|
||||
await test_retry_success_after_failures()
|
||||
await test_retry_exhausted()
|
||||
await test_no_retry_on_success()
|
||||
print("\n=== Todos os testes concluídos ===")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user