Files
MonicaAgentIA/test_start_context.py
T
2026-09-03 15:34:28 -03:00

110 lines
2.8 KiB
Python
Executable File

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 (
start_command,
load_user_profile,
save_user_profile,
load_all_contexts,
add_to_context,
ensure_contexts_dir,
AIProvider,
DEFAULT_MODEL,
)
from pathlib import Path
class FakeMessage:
def __init__(self, text):
self.text = text
self.chat_id = 7889499475
async def reply_text(self, text, **kwargs):
print(f"BOT RESPONSE: {text[:500]}")
class FakeUpdate:
def __init__(self, text):
self.message = FakeMessage(text)
self.effective_chat = self
self.effective_user = self
self.effective_message = self.message
@property
def id(self):
return self.message.chat_id
@property
def username(self):
return "test_user"
@property
def first_name(self):
return "Marco"
class FakeContext:
def __init__(self):
self.bot = self
async def send_message(self, **kwargs):
print(f"BOT SEND: {kwargs.get('text', '')[:500]}")
async def test_start_without_context():
print("=== Testando /start SEM contextos ===")
profile = load_user_profile()
profile["name"] = "Marco"
save_user_profile(profile)
from monica_agent import CONTEXTS_DIR
contexts_dir = Path(CONTEXTS_DIR)
for file_path in contexts_dir.glob("*.json"):
file_path.unlink()
update = FakeUpdate("/start")
context = FakeContext()
try:
await start_command(update, context)
print("✅ start_command executou sem erros")
except Exception as e:
print(f"❌ Erro: {e}")
import traceback
traceback.print_exc()
async def test_start_with_context():
print("\n=== Testando /start COM contextos ===")
profile = load_user_profile()
profile["name"] = "Marco"
save_user_profile(profile)
add_to_context("user", "Olá Mônica!")
add_to_context("assistant", "Oi, Marco! Como você está?")
def mock_chat(model, messages, temperature=0.8, max_tokens=2000):
return "Que bom ver você de novo! Continue a conversa baseado no contexto anterior."
with patch.object(AIProvider, 'chat', mock_chat):
update = FakeUpdate("/start")
context = FakeContext()
try:
await start_command(update, context)
print("✅ start_command executou sem erros")
except Exception as e:
print(f"❌ Erro: {e}")
import traceback
traceback.print_exc()
async def main():
await test_start_without_context()
await test_start_with_context()
print("\n=== Todos os testes concluídos ===")
if __name__ == "__main__":
asyncio.run(main())