forked from Rockachopa/Timmy-time-dashboard
feat: code quality audit + autoresearch integration + infra hardening (#150)
This commit is contained in:
committed by
GitHub
parent
fd0ede0d51
commit
ae3bb1cc21
@@ -6,7 +6,6 @@ from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
# ── TelegramBot unit tests ────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -17,6 +16,7 @@ class TestTelegramBotTokenHelpers:
|
||||
monkeypatch.setattr("integrations.telegram_bot.bot._STATE_FILE", state_file)
|
||||
|
||||
from integrations.telegram_bot.bot import TelegramBot
|
||||
|
||||
bot = TelegramBot()
|
||||
|
||||
bot.save_token("test-token-123")
|
||||
@@ -38,6 +38,7 @@ class TestTelegramBotTokenHelpers:
|
||||
with patch("integrations.telegram_bot.bot._load_token_from_file", return_value=None):
|
||||
with patch("config.settings", mock_settings):
|
||||
from integrations.telegram_bot.bot import TelegramBot
|
||||
|
||||
bot = TelegramBot()
|
||||
result = bot.load_token()
|
||||
assert result is None
|
||||
@@ -45,6 +46,7 @@ class TestTelegramBotTokenHelpers:
|
||||
def test_token_set_property(self):
|
||||
"""token_set reflects whether a token has been applied."""
|
||||
from integrations.telegram_bot.bot import TelegramBot
|
||||
|
||||
bot = TelegramBot()
|
||||
assert not bot.token_set
|
||||
bot._token = "tok"
|
||||
@@ -52,6 +54,7 @@ class TestTelegramBotTokenHelpers:
|
||||
|
||||
def test_is_running_property(self):
|
||||
from integrations.telegram_bot.bot import TelegramBot
|
||||
|
||||
bot = TelegramBot()
|
||||
assert not bot.is_running
|
||||
bot._running = True
|
||||
@@ -66,6 +69,7 @@ class TestTelegramBotLifecycle:
|
||||
monkeypatch.setattr("integrations.telegram_bot.bot._STATE_FILE", state_file)
|
||||
|
||||
from integrations.telegram_bot.bot import TelegramBot
|
||||
|
||||
bot = TelegramBot()
|
||||
with patch.object(bot, "load_token", return_value=None):
|
||||
result = await bot.start()
|
||||
@@ -75,6 +79,7 @@ class TestTelegramBotLifecycle:
|
||||
@pytest.mark.asyncio
|
||||
async def test_start_already_running_returns_true(self):
|
||||
from integrations.telegram_bot.bot import TelegramBot
|
||||
|
||||
bot = TelegramBot()
|
||||
bot._running = True
|
||||
result = await bot.start(token="any")
|
||||
@@ -84,10 +89,12 @@ class TestTelegramBotLifecycle:
|
||||
async def test_start_import_error_returns_false(self):
|
||||
"""start() returns False gracefully when python-telegram-bot absent."""
|
||||
from integrations.telegram_bot.bot import TelegramBot
|
||||
|
||||
bot = TelegramBot()
|
||||
|
||||
with patch.object(bot, "load_token", return_value="tok"), \
|
||||
patch.dict("sys.modules", {"telegram": None, "telegram.ext": None}):
|
||||
with patch.object(bot, "load_token", return_value="tok"), patch.dict(
|
||||
"sys.modules", {"telegram": None, "telegram.ext": None}
|
||||
):
|
||||
result = await bot.start(token="tok")
|
||||
assert result is False
|
||||
assert not bot.is_running
|
||||
@@ -95,6 +102,7 @@ class TestTelegramBotLifecycle:
|
||||
@pytest.mark.asyncio
|
||||
async def test_stop_when_not_running_is_noop(self):
|
||||
from integrations.telegram_bot.bot import TelegramBot
|
||||
|
||||
bot = TelegramBot()
|
||||
# Should not raise
|
||||
await bot.stop()
|
||||
@@ -103,6 +111,7 @@ class TestTelegramBotLifecycle:
|
||||
async def test_stop_calls_shutdown(self):
|
||||
"""stop() invokes the Application shutdown sequence."""
|
||||
from integrations.telegram_bot.bot import TelegramBot
|
||||
|
||||
bot = TelegramBot()
|
||||
bot._running = True
|
||||
|
||||
@@ -126,6 +135,7 @@ class TestTelegramRoutes:
|
||||
def test_status_not_running(self, client):
|
||||
"""GET /telegram/status returns running=False when bot is idle."""
|
||||
from integrations.telegram_bot.bot import telegram_bot
|
||||
|
||||
telegram_bot._running = False
|
||||
telegram_bot._token = None
|
||||
|
||||
@@ -138,6 +148,7 @@ class TestTelegramRoutes:
|
||||
def test_status_running(self, client):
|
||||
"""GET /telegram/status returns running=True when bot is active."""
|
||||
from integrations.telegram_bot.bot import telegram_bot
|
||||
|
||||
telegram_bot._running = True
|
||||
telegram_bot._token = "tok"
|
||||
|
||||
@@ -164,8 +175,9 @@ class TestTelegramRoutes:
|
||||
from integrations.telegram_bot.bot import telegram_bot
|
||||
|
||||
telegram_bot._running = False
|
||||
with patch.object(telegram_bot, "save_token") as mock_save, \
|
||||
patch.object(telegram_bot, "start", new_callable=AsyncMock, return_value=True):
|
||||
with patch.object(telegram_bot, "save_token") as mock_save, patch.object(
|
||||
telegram_bot, "start", new_callable=AsyncMock, return_value=True
|
||||
):
|
||||
resp = client.post("/telegram/setup", json={"token": "bot123:abc"})
|
||||
|
||||
assert resp.status_code == 200
|
||||
@@ -178,8 +190,9 @@ class TestTelegramRoutes:
|
||||
from integrations.telegram_bot.bot import telegram_bot
|
||||
|
||||
telegram_bot._running = False
|
||||
with patch.object(telegram_bot, "save_token"), \
|
||||
patch.object(telegram_bot, "start", new_callable=AsyncMock, return_value=False):
|
||||
with patch.object(telegram_bot, "save_token"), patch.object(
|
||||
telegram_bot, "start", new_callable=AsyncMock, return_value=False
|
||||
):
|
||||
resp = client.post("/telegram/setup", json={"token": "bad-token"})
|
||||
|
||||
assert resp.status_code == 200
|
||||
@@ -190,11 +203,14 @@ class TestTelegramRoutes:
|
||||
def test_setup_stops_running_bot_first(self, client):
|
||||
"""POST /telegram/setup stops any running bot before starting new one."""
|
||||
from integrations.telegram_bot.bot import telegram_bot
|
||||
|
||||
telegram_bot._running = True
|
||||
|
||||
with patch.object(telegram_bot, "save_token"), \
|
||||
patch.object(telegram_bot, "stop", new_callable=AsyncMock) as mock_stop, \
|
||||
patch.object(telegram_bot, "start", new_callable=AsyncMock, return_value=True):
|
||||
with patch.object(telegram_bot, "save_token"), patch.object(
|
||||
telegram_bot, "stop", new_callable=AsyncMock
|
||||
) as mock_stop, patch.object(
|
||||
telegram_bot, "start", new_callable=AsyncMock, return_value=True
|
||||
):
|
||||
resp = client.post("/telegram/setup", json={"token": "new-token"})
|
||||
|
||||
mock_stop.assert_awaited_once()
|
||||
@@ -207,5 +223,6 @@ class TestTelegramRoutes:
|
||||
|
||||
def test_module_singleton_exists():
|
||||
"""telegram_bot module exposes a singleton TelegramBot instance."""
|
||||
from integrations.telegram_bot.bot import telegram_bot, TelegramBot
|
||||
from integrations.telegram_bot.bot import TelegramBot, telegram_bot
|
||||
|
||||
assert isinstance(telegram_bot, TelegramBot)
|
||||
|
||||
Reference in New Issue
Block a user