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" import monica_agent as ma from monica_agent import ( fusohorario_command, message_handler, load_user_profile, save_user_profile, get_now, ) 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_fusohorario_command_starts(): print("=== Testando /fusohorario inicia fluxo ===") ma.is_setting_timezone = False ma.timezone_step = 0 update = FakeUpdate("/fusohorario") context = FakeContext() try: await fusohorario_command(update, context) assert ma.is_setting_timezone is True assert ma.timezone_step == 1 print("✅ /fusohorario iniciou o fluxo corretamente") except Exception as e: print(f"❌ Erro: {e}") import traceback traceback.print_exc() async def test_fusohorario_valid_timezone(): print("\n=== Testando definicao de fuso horario valido ===") profile = load_user_profile() profile["timezone"] = "" save_user_profile(profile) ma.is_setting_timezone = False ma.timezone_step = 0 update = FakeUpdate("/fusohorario") context = FakeContext() await fusohorario_command(update, context) update = FakeUpdate("America/Sao_Paulo") context = FakeContext() await message_handler(update, context) assert ma.is_setting_timezone is False assert ma.timezone_step == 0 profile = load_user_profile() assert profile.get("timezone") == "America/Sao_Paulo" print(f"✅ Fuso horario salvo: {profile.get('timezone')}") now = get_now() assert now.tzinfo is not None print(f"✅ get_now() retorna datetime com timezone: {now}") async def test_fusohorario_invalid_timezone(): print("\n=== Testando fuso horario invalido ===") profile = load_user_profile() profile["timezone"] = "" save_user_profile(profile) ma.is_setting_timezone = False ma.timezone_step = 0 update = FakeUpdate("/fusohorario") context = FakeContext() await fusohorario_command(update, context) update = FakeUpdate("Fuso/Invalido") context = FakeContext() await message_handler(update, context) assert ma.is_setting_timezone is True profile = load_user_profile() assert profile.get("timezone") == "" print("✅ Fuso horario invalido rejeitado e fluxo mantido") async def test_fusohorario_cancelar(): print("\n=== Testando cancelar durante /fusohorario ===") profile = load_user_profile() profile["timezone"] = "" save_user_profile(profile) ma.is_setting_timezone = False ma.timezone_step = 0 update = FakeUpdate("/fusohorario") context = FakeContext() await fusohorario_command(update, context) update = FakeUpdate("cancelar") context = FakeContext() await message_handler(update, context) assert ma.is_setting_timezone is False assert ma.timezone_step == 0 print("✅ Cancelar encerrou o fluxo de fuso horario") async def test_fusohorario_affects_time(): print("\n=== Testando que get_now usa o fuso horario ===") profile = load_user_profile() profile["timezone"] = "UTC" save_user_profile(profile) now = get_now() assert now.tzinfo is not None print(f"✅ get_now() com UTC: {now}") profile["timezone"] = "America/Sao_Paulo" save_user_profile(profile) now_sp = get_now() assert now_sp.tzinfo is not None print(f"✅ get_now() com America/Sao_Paulo: {now_sp}") profile["timezone"] = "" save_user_profile(profile) now_naive = get_now() assert now_naive.tzinfo is None print(f"✅ get_now() sem timezone retorna naive: {now_naive}") async def test_fusohorario_in_help(): print("\n=== Verificando /help contem /fusohorario ===") from monica_agent import help_command update = FakeUpdate("/help") context = FakeContext() responses = [] original_reply = FakeMessage.reply_text async def capture_reply(self, text, **kwargs): responses.append(text) print(f"BOT RESPONSE: {text[:300]}") FakeMessage.reply_text = capture_reply try: await help_command(update, context) help_text = responses[0] if responses else "" if "/fusohorario" in help_text: print("✅ /help menciona /fusohorario") else: print("❌ /help nao menciona /fusohorario") print(f" Help text: {help_text[:200]}") finally: FakeMessage.reply_text = original_reply async def main(): await test_fusohorario_command_starts() await test_fusohorario_valid_timezone() await test_fusohorario_invalid_timezone() await test_fusohorario_cancelar() await test_fusohorario_affects_time() await test_fusohorario_in_help() print("\n=== Todos os testes de fusohorario concluidos ===") if __name__ == "__main__": asyncio.run(main())