This commit is contained in:
kandrusyak
2026-07-25 15:39:03 +03:00
parent 0163208484
commit 3f63d0305c
6 changed files with 148 additions and 37 deletions

32
tests/test_jobs.py Normal file
View File

@@ -0,0 +1,32 @@
import asyncio
import unittest
from types import SimpleNamespace
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 = 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()

View File

@@ -7,6 +7,7 @@ from assistant_bot.yandex_ai import (
YandexAIClient,
YandexSpeechRecognizer,
describe_yandex_error,
prepare_chat_messages,
)
@@ -38,6 +39,28 @@ class FakeChatCompletions:
class YandexAIClientTests(unittest.IsolatedAsyncioTestCase):
def test_multiple_system_messages_are_merged_at_the_beginning(self) -> None:
self.assertEqual(
prepare_chat_messages(
[
{"role": "system", "content": "Primary instructions"},
{"role": "user", "content": "Earlier question"},
{"role": "system", "content": "Runtime context"},
{"role": "assistant", "content": "Earlier answer"},
{"role": "user", "content": "Current question"},
]
),
[
{
"role": "system",
"content": "Primary instructions\n\nRuntime context",
},
{"role": "user", "content": "Earlier question"},
{"role": "assistant", "content": "Earlier answer"},
{"role": "user", "content": "Current question"},
],
)
def test_http_error_includes_safe_response_body(self) -> None:
error = RuntimeError("forbidden")
error.response = SimpleNamespace(