import asyncio import unittest from types import SimpleNamespace from typing import Any from unittest.mock import patch from assistant_bot.jobs import post_init, post_shutdown class ReminderJobLifecycleTests(unittest.IsolatedAsyncioTestCase): async def test_reminder_task_is_cancelled_on_shutdown(self) -> None: started = asyncio.Event() async def fake_reminder_loop(_application) -> None: started.set() await asyncio.Event().wait() application: Any = SimpleNamespace(bot_data={}) with patch("assistant_bot.jobs.reminder_loop", fake_reminder_loop): await post_init(application) await started.wait() task = application.bot_data["reminder_task"] self.assertFalse(task.done()) await post_shutdown(application) self.assertTrue(task.cancelled()) self.assertNotIn("reminder_task", application.bot_data) if __name__ == "__main__": unittest.main()