Files
uPortal2/bots/_smoke_test.py
T
2026-10-24 20:01:40 +03:00

136 lines
3.7 KiB
Python

"""Offline smoke test for the bot (no network). Run: python bots/_smoke_test.py"""
import os
import sys
import tempfile
from datetime import datetime, timedelta, timezone
try:
sys.stdout.reconfigure(encoding="utf-8")
except (AttributeError, ValueError):
pass
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from meetings_telegram_bot.api import Meeting
from meetings_telegram_bot.bot import MeetingsBot
from meetings_telegram_bot.config import Config
from meetings_telegram_bot.state import StateStore
now = datetime.now(timezone.utc)
def mk(i, title="T", room=None, sched=None):
return Meeting(
id=i,
title=title,
intent="about " + title,
room=room or ("room-%d" % i),
scheduled_at=sched or (now + timedelta(hours=1)),
created_by=1,
created_by_name="Roman",
created_at=now,
)
class FakeApi:
session_token = "sess"
csrf_token = "csrf"
has_session = True
def __init__(self, meetings):
self._m = meetings
def restore_session(self, a, b):
pass
def still_authenticated(self):
return True
def signin(self, *a, **k):
raise AssertionError("should not sign in")
def list_meetings(self, limit=200, offset=0):
return list(self._m)
class FakeBot:
def __init__(self):
self.sent = []
def send_message(self, chat_id, text, **kw):
self.sent.append((chat_id, text))
return {"message_id": len(self.sent)}
def test_polling():
tmp = tempfile.mkdtemp()
cfg = Config(
api_base_url="http://x",
api_login="u",
api_password="p",
bot_token="t",
chat_id="@g",
state_file=os.path.join(tmp, "state.json"),
)
api = FakeApi([mk(1), mk(2), mk(3)])
tb = FakeBot()
b = MeetingsBot(cfg, api=api, bot=tb, store=StateStore(cfg.state_file))
b.start()
assert b.poll_once() == 0 and len(tb.sent) == 0 # first run seeds silently
print("run1 (seed) announced 0 sent 0")
api._m.append(mk(4, title="Design review"))
b2 = MeetingsBot(cfg, api=api, bot=tb, store=StateStore(cfg.state_file))
b2.start()
assert b2.poll_once() == 1 and len(tb.sent) == 1 # one new meeting posted
print("run2 (new) announced 1 sent 1")
print("--- message ---")
print(tb.sent[0][1])
print("--- end ---")
b3 = MeetingsBot(cfg, api=api, bot=tb, store=StateStore(cfg.state_file))
b3.start()
assert b3.poll_once() == 0 and len(tb.sent) == 1 # no change, no post
print("run3 (no change) announced 0 sent 1")
api._m.append(mk(5, title="Stale", sched=now - timedelta(days=3)))
b4 = MeetingsBot(cfg, api=api, bot=tb, store=StateStore(cfg.state_file))
b4.start()
assert b4.poll_once() == 0 and len(tb.sent) == 1 # stale not posted
print("run4 (stale) announced 0 sent 1")
print("ALL GOOD")
def test_proxy():
import telebot.apihelper as apihelper
from meetings_telegram_bot.api import GoChatApi
from meetings_telegram_bot.config import load_config
os.environ.update(
API_BASE_URL="http://x",
API_LOGIN="u",
API_PASSWORD="p",
TELEGRAM_BOT_TOKEN="t",
TELEGRAM_CHAT_ID="@g",
SOCKS5_PROXY="socks5://user:pass@127.0.0.1:1080",
)
cfg = load_config()
assert cfg.socks5_proxy == "socks5://user:pass@127.0.0.1:1080"
api = GoChatApi(cfg.api_base_url, proxy=cfg.socks5_proxy)
assert api._http.proxies.get("http") == cfg.socks5_proxy
assert api._http.proxies.get("https") == cfg.socks5_proxy
apihelper.proxy = {"http": cfg.socks5_proxy, "https": cfg.socks5_proxy}
assert apihelper.proxy["https"] == cfg.socks5_proxy
print("proxy wiring OK")
if __name__ == "__main__":
test_polling()
test_proxy()