refactor: improve code readability and structure in various components
- Refactored the `mini_app_short_name` assignment in `config.py` for better clarity. - Enhanced the `app_config_js` function in `app.py` to improve formatting of the JavaScript response body. - Added per-chat locks in `group_duty_pin.py` to prevent concurrent refreshes, improving message handling. - Updated `_schedule_next_update` to include optional jitter for scheduling, enhancing performance during high-load scenarios. - Cleaned up test files by removing unused imports and improving test descriptions for clarity.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
"""Tests for FastAPI app /api/duties."""
|
||||
|
||||
import time
|
||||
from pathlib import Path
|
||||
from unittest.mock import ANY, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -93,8 +93,9 @@ def test_get_contact_button_markup_returns_markup_when_username_set():
|
||||
"""_get_contact_button_markup: BOT_USERNAME set, no short name -> t.me bot link with startapp=duty."""
|
||||
from telegram import InlineKeyboardMarkup
|
||||
|
||||
with patch.object(config, "BOT_USERNAME", "MyDutyBot"), patch.object(
|
||||
config, "MINI_APP_SHORT_NAME", ""
|
||||
with (
|
||||
patch.object(config, "BOT_USERNAME", "MyDutyBot"),
|
||||
patch.object(config, "MINI_APP_SHORT_NAME", ""),
|
||||
):
|
||||
with patch.object(mod, "t", return_value="View contacts"):
|
||||
result = mod._get_contact_button_markup("en")
|
||||
@@ -113,8 +114,9 @@ def test_get_contact_button_markup_with_short_name_uses_direct_miniapp_link():
|
||||
"""_get_contact_button_markup: MINI_APP_SHORT_NAME set -> direct Mini App URL with startapp=duty."""
|
||||
from telegram import InlineKeyboardMarkup
|
||||
|
||||
with patch.object(config, "BOT_USERNAME", "MyDutyBot"), patch.object(
|
||||
config, "MINI_APP_SHORT_NAME", "DutyApp"
|
||||
with (
|
||||
patch.object(config, "BOT_USERNAME", "MyDutyBot"),
|
||||
patch.object(config, "MINI_APP_SHORT_NAME", "DutyApp"),
|
||||
):
|
||||
with patch.object(mod, "t", return_value="View contacts"):
|
||||
result = mod._get_contact_button_markup("en")
|
||||
@@ -294,8 +296,8 @@ async def test_update_group_pin_send_raises_no_unpin_pin_schedule_still_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_group_pin_repin_raises_still_schedules_next():
|
||||
"""update_group_pin: send_message ok, unpin or pin raises -> no _sync_save_pin, schedule still called, log."""
|
||||
async def test_update_group_pin_unpin_raises_pin_succeeds_saves_and_schedules():
|
||||
"""update_group_pin: send_message ok, unpin raises (e.g. no pinned message), pin succeeds -> save_pin and schedule called."""
|
||||
new_msg = MagicMock()
|
||||
new_msg.message_id = 888
|
||||
context = MagicMock()
|
||||
@@ -304,9 +306,45 @@ async def test_update_group_pin_repin_raises_still_schedules_next():
|
||||
context.bot = MagicMock()
|
||||
context.bot.send_message = AsyncMock(return_value=new_msg)
|
||||
context.bot.unpin_chat_message = AsyncMock(
|
||||
side_effect=Forbidden("Not enough rights")
|
||||
side_effect=BadRequest("Chat has no pinned message")
|
||||
)
|
||||
context.bot.pin_chat_message = AsyncMock()
|
||||
context.bot.delete_message = AsyncMock()
|
||||
context.application = MagicMock()
|
||||
|
||||
with patch.object(config, "DUTY_PIN_NOTIFY", True):
|
||||
with patch.object(mod, "_sync_is_trusted", return_value=True):
|
||||
with patch.object(
|
||||
mod, "_sync_get_pin_refresh_data", return_value=(3, "Text", None)
|
||||
):
|
||||
with patch.object(
|
||||
mod, "_schedule_next_update", AsyncMock()
|
||||
) as mock_schedule:
|
||||
with patch.object(mod, "_sync_save_pin") as mock_save:
|
||||
await mod.update_group_pin(context)
|
||||
context.bot.send_message.assert_called_once_with(
|
||||
chat_id=222, text="Text", reply_markup=None
|
||||
)
|
||||
context.bot.unpin_chat_message.assert_called_once_with(chat_id=222)
|
||||
context.bot.pin_chat_message.assert_called_once_with(
|
||||
chat_id=222, message_id=888, disable_notification=False
|
||||
)
|
||||
mock_save.assert_called_once_with(222, 888)
|
||||
mock_schedule.assert_called_once_with(context.application, 222, None)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_group_pin_pin_raises_no_save_still_schedules_next():
|
||||
"""update_group_pin: send_message ok, unpin ok, pin raises -> no _sync_save_pin, schedule still called, log."""
|
||||
new_msg = MagicMock()
|
||||
new_msg.message_id = 888
|
||||
context = MagicMock()
|
||||
context.job = MagicMock()
|
||||
context.job.data = {"chat_id": 222}
|
||||
context.bot = MagicMock()
|
||||
context.bot.send_message = AsyncMock(return_value=new_msg)
|
||||
context.bot.unpin_chat_message = AsyncMock()
|
||||
context.bot.pin_chat_message = AsyncMock(side_effect=Forbidden("Not enough rights"))
|
||||
context.application = MagicMock()
|
||||
|
||||
with patch.object(config, "DUTY_PIN_NOTIFY", True):
|
||||
@@ -325,7 +363,7 @@ async def test_update_group_pin_repin_raises_still_schedules_next():
|
||||
)
|
||||
mock_save.assert_not_called()
|
||||
mock_logger.warning.assert_called_once()
|
||||
assert "Unpin or pin" in mock_logger.warning.call_args[0][0]
|
||||
assert "Pin after refresh failed" in mock_logger.warning.call_args[0][0]
|
||||
mock_schedule.assert_called_once_with(context.application, 222, None)
|
||||
|
||||
|
||||
@@ -388,7 +426,7 @@ async def test_pin_duty_cmd_group_only_reply():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_pin_duty_cmd_group_pins_and_replies_pinned():
|
||||
"""pin_duty_cmd in group with existing pin record -> pin and reply pinned."""
|
||||
"""pin_duty_cmd in group with existing pin record -> pin, schedule next update, reply pinned."""
|
||||
update = MagicMock()
|
||||
update.message = MagicMock()
|
||||
update.message.reply_text = AsyncMock()
|
||||
@@ -399,16 +437,22 @@ async def test_pin_duty_cmd_group_pins_and_replies_pinned():
|
||||
context = MagicMock()
|
||||
context.bot = MagicMock()
|
||||
context.bot.pin_chat_message = AsyncMock()
|
||||
context.application = MagicMock()
|
||||
|
||||
with patch("duty_teller.handlers.group_duty_pin.get_lang", return_value="en"):
|
||||
with patch.object(mod, "_sync_is_trusted", return_value=True):
|
||||
with patch.object(mod, "_sync_get_message_id", return_value=5):
|
||||
with patch("duty_teller.handlers.group_duty_pin.t") as mock_t:
|
||||
mock_t.return_value = "Pinned"
|
||||
await mod.pin_duty_cmd(update, context)
|
||||
with patch.object(mod, "_get_next_shift_end_sync", return_value=None):
|
||||
with patch.object(
|
||||
mod, "_schedule_next_update", AsyncMock()
|
||||
) as mock_schedule:
|
||||
with patch("duty_teller.handlers.group_duty_pin.t") as mock_t:
|
||||
mock_t.return_value = "Pinned"
|
||||
await mod.pin_duty_cmd(update, context)
|
||||
context.bot.pin_chat_message.assert_called_once_with(
|
||||
chat_id=100, message_id=5, disable_notification=True
|
||||
)
|
||||
mock_schedule.assert_called_once_with(context.application, 100, None)
|
||||
update.message.reply_text.assert_called_once_with("Pinned")
|
||||
|
||||
|
||||
@@ -908,8 +952,8 @@ async def test_my_chat_member_handler_bot_removed_deletes_pin_and_jobs():
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_restore_group_pin_jobs_calls_schedule_for_each_chat():
|
||||
"""restore_group_pin_jobs: for each chat_id from _get_all_pin_chat_ids_sync, calls _schedule_next_update."""
|
||||
async def test_restore_group_pin_jobs_calls_schedule_for_each_chat_with_jitter():
|
||||
"""restore_group_pin_jobs: for each chat_id calls _schedule_next_update with jitter_seconds=60."""
|
||||
application = MagicMock()
|
||||
application.job_queue = MagicMock()
|
||||
application.job_queue.get_jobs_by_name = MagicMock(return_value=[])
|
||||
@@ -922,8 +966,8 @@ async def test_restore_group_pin_jobs_calls_schedule_for_each_chat():
|
||||
) as mock_schedule:
|
||||
await mod.restore_group_pin_jobs(application)
|
||||
assert mock_schedule.call_count == 2
|
||||
mock_schedule.assert_any_call(application, 10, None)
|
||||
mock_schedule.assert_any_call(application, 20, None)
|
||||
mock_schedule.assert_any_call(application, 10, None, jitter_seconds=60.0)
|
||||
mock_schedule.assert_any_call(application, 20, None, jitter_seconds=60.0)
|
||||
|
||||
|
||||
# --- _refresh_pin_for_chat untrusted ---
|
||||
|
||||
Reference in New Issue
Block a user