EmperorFred/tests/test_twitch_bot_load_commands.py
2025-12-05 20:21:47 -06:00

51 lines
1.7 KiB
Python

import asyncio
import os
from libs.Db import Db
from data.models.TwitchComponent import TwitchComponent
def test_twitch_bot_loads_ping_and_restart_components():
async def _run():
# Import after fixtures have set environment variables
from twitch_bot.bot import TwitchBot
# Ensure both the bot and the test share the same DB session/engine
db = Db(os.environ["DB_CONN_STR"], echo=False)
bot = TwitchBot(restart_controller=None, tokens={}, db=db)
try:
await bot.load_commands()
# Commands registered from components
assert "ping" in bot.commands
assert "restart" in bot.commands
finally:
await bot.close()
asyncio.run(_run())
def test_twitch_bot_respects_db_activation(env_tmp_db):
async def _run():
# Mark the ping component inactive in DB before loading
db = Db(env_tmp_db, echo=False)
name = "twitch_bot.commands.ping"
rec = db.session.query(TwitchComponent).filter_by(name=name).first()
if not rec:
rec = TwitchComponent(name=name, active=False)
db.session.add(rec)
else:
rec.active = False
db.session.commit()
# Import after fixtures have set environment variables
from twitch_bot.bot import TwitchBot
bot = TwitchBot(restart_controller=None, tokens={}, db=db)
try:
await bot.load_commands()
# ping should be skipped due to inactive flag; restart should load
assert "ping" not in bot.commands
assert "restart" in bot.commands
finally:
await bot.close()
asyncio.run(_run())