Add multibot functionality

This commit is contained in:
Jack Kingsman
2026-01-27 17:23:38 -08:00
parent c46128e2cd
commit 530179fde1
18 changed files with 1599 additions and 816 deletions
+265 -12
View File
@@ -13,6 +13,7 @@ from app.bot import (
process_bot_response,
run_bot_for_message,
)
from app.models import BotConfig
class TestExecuteBotCode:
@@ -359,12 +360,13 @@ class TestRunBotForMessage:
mock_repo.get.assert_not_called()
@pytest.mark.asyncio
async def test_skips_when_bot_disabled(self):
"""Bot is not triggered when disabled in settings."""
async def test_skips_when_no_enabled_bots(self):
"""Bot is not triggered when no bots are enabled."""
with patch("app.repository.AppSettingsRepository") as mock_repo:
mock_settings = MagicMock()
mock_settings.bot_enabled = False
mock_settings.bot_code = "def bot(): pass"
mock_settings.bots = [
BotConfig(id="1", name="Bot 1", enabled=False, code="def bot(): pass")
]
mock_repo.get = AsyncMock(return_value=mock_settings)
with patch("app.bot.execute_bot_code") as mock_exec:
@@ -379,12 +381,33 @@ class TestRunBotForMessage:
mock_exec.assert_not_called()
@pytest.mark.asyncio
async def test_skips_when_bot_code_empty(self):
"""Bot is not triggered when code is empty."""
async def test_skips_when_bots_array_empty(self):
"""Bot is not triggered when bots array is empty."""
with patch("app.repository.AppSettingsRepository") as mock_repo:
mock_settings = MagicMock()
mock_settings.bot_enabled = True
mock_settings.bot_code = ""
mock_settings.bots = []
mock_repo.get = AsyncMock(return_value=mock_settings)
with patch("app.bot.execute_bot_code") as mock_exec:
await run_bot_for_message(
sender_name="Alice",
sender_key="abc123",
message_text="Hello",
is_dm=True,
channel_key=None,
)
mock_exec.assert_not_called()
@pytest.mark.asyncio
async def test_skips_bot_with_empty_code(self):
"""Bot with empty code is skipped even if enabled."""
with patch("app.repository.AppSettingsRepository") as mock_repo:
mock_settings = MagicMock()
mock_settings.bots = [
BotConfig(id="1", name="Empty Bot", enabled=True, code=""),
BotConfig(id="2", name="Whitespace Bot", enabled=True, code=" "),
]
mock_repo.get = AsyncMock(return_value=mock_settings)
with patch("app.bot.execute_bot_code") as mock_exec:
@@ -405,12 +428,14 @@ class TestRunBotForMessage:
# First call: bot enabled
# Second call (after sleep): bot disabled
mock_settings_enabled = MagicMock()
mock_settings_enabled.bot_enabled = True
mock_settings_enabled.bot_code = "def bot(): return 'hi'"
mock_settings_enabled.bots = [
BotConfig(id="1", name="Bot 1", enabled=True, code="def bot(): return 'hi'")
]
mock_settings_disabled = MagicMock()
mock_settings_disabled.bot_enabled = False
mock_settings_disabled.bot_code = "def bot(): return 'hi'"
mock_settings_disabled.bots = [
BotConfig(id="1", name="Bot 1", enabled=False, code="def bot(): return 'hi'")
]
mock_repo.get = AsyncMock(side_effect=[mock_settings_enabled, mock_settings_disabled])
@@ -433,6 +458,199 @@ class TestRunBotForMessage:
mock_exec.assert_not_called()
class TestMultipleBots:
"""Test multiple bots functionality."""
@pytest.fixture(autouse=True)
def reset_semaphore(self):
"""Reset semaphore state between tests."""
while _bot_semaphore.locked():
_bot_semaphore.release()
yield
@pytest.fixture(autouse=True)
def reset_rate_limit_state(self):
"""Reset rate limiting state between tests."""
bot_module._last_bot_send_time = 0.0
yield
bot_module._last_bot_send_time = 0.0
@pytest.mark.asyncio
async def test_multiple_bots_execute_serially(self):
"""Multiple enabled bots execute serially in order."""
executed_bots = []
def mock_execute(code, *args, **kwargs):
# Extract bot identifier from the code
if "Bot 1" in code:
executed_bots.append("Bot 1")
return "Response 1"
elif "Bot 2" in code:
executed_bots.append("Bot 2")
return "Response 2"
return None
with patch("app.repository.AppSettingsRepository") as mock_repo:
mock_settings = MagicMock()
mock_settings.bots = [
BotConfig(id="1", name="Bot 1", enabled=True, code="# Bot 1\ndef bot(): pass"),
BotConfig(id="2", name="Bot 2", enabled=True, code="# Bot 2\ndef bot(): pass"),
]
mock_repo.get = AsyncMock(return_value=mock_settings)
with (
patch("app.bot.asyncio.sleep", new_callable=AsyncMock),
patch("app.bot.execute_bot_code", side_effect=mock_execute),
patch("app.bot.process_bot_response", new_callable=AsyncMock),
):
await run_bot_for_message(
sender_name="Alice",
sender_key="abc123" + "0" * 58,
message_text="Hello",
is_dm=True,
channel_key=None,
)
# Both bots should have executed in order
assert executed_bots == ["Bot 1", "Bot 2"]
@pytest.mark.asyncio
async def test_disabled_bots_are_skipped(self):
"""Disabled bots in the array are skipped."""
executed_bots = []
def mock_execute(code, *args, **kwargs):
if "Bot 1" in code:
executed_bots.append("Bot 1")
elif "Bot 2" in code:
executed_bots.append("Bot 2")
elif "Bot 3" in code:
executed_bots.append("Bot 3")
return None
with patch("app.repository.AppSettingsRepository") as mock_repo:
mock_settings = MagicMock()
mock_settings.bots = [
BotConfig(id="1", name="Bot 1", enabled=True, code="# Bot 1\ndef bot(): pass"),
BotConfig(id="2", name="Bot 2", enabled=False, code="# Bot 2\ndef bot(): pass"),
BotConfig(id="3", name="Bot 3", enabled=True, code="# Bot 3\ndef bot(): pass"),
]
mock_repo.get = AsyncMock(return_value=mock_settings)
with (
patch("app.bot.asyncio.sleep", new_callable=AsyncMock),
patch("app.bot.execute_bot_code", side_effect=mock_execute),
):
await run_bot_for_message(
sender_name="Alice",
sender_key="abc123" + "0" * 58,
message_text="Hello",
is_dm=True,
channel_key=None,
)
# Only enabled bots should have executed
assert executed_bots == ["Bot 1", "Bot 3"]
@pytest.mark.asyncio
async def test_error_in_one_bot_doesnt_stop_others(self):
"""Error in one bot doesn't prevent other bots from running."""
executed_bots = []
def mock_execute(code, *args, **kwargs):
if "Bot 1" in code:
executed_bots.append("Bot 1")
raise ValueError("Bot 1 crashed!")
elif "Bot 2" in code:
executed_bots.append("Bot 2")
return "Response 2"
elif "Bot 3" in code:
executed_bots.append("Bot 3")
return "Response 3"
return None
with patch("app.repository.AppSettingsRepository") as mock_repo:
mock_settings = MagicMock()
mock_settings.bots = [
BotConfig(id="1", name="Bot 1", enabled=True, code="# Bot 1\ndef bot(): pass"),
BotConfig(id="2", name="Bot 2", enabled=True, code="# Bot 2\ndef bot(): pass"),
BotConfig(id="3", name="Bot 3", enabled=True, code="# Bot 3\ndef bot(): pass"),
]
mock_repo.get = AsyncMock(return_value=mock_settings)
with (
patch("app.bot.asyncio.sleep", new_callable=AsyncMock),
patch("app.bot.execute_bot_code", side_effect=mock_execute),
patch("app.bot.process_bot_response", new_callable=AsyncMock) as mock_respond,
):
await run_bot_for_message(
sender_name="Alice",
sender_key="abc123" + "0" * 58,
message_text="Hello",
is_dm=True,
channel_key=None,
)
# All bots should have been attempted
assert executed_bots == ["Bot 1", "Bot 2", "Bot 3"]
# Responses from successful bots should have been sent
assert mock_respond.call_count == 2
@pytest.mark.asyncio
async def test_timeout_in_one_bot_doesnt_stop_others(self):
"""Timeout in one bot doesn't prevent other bots from running."""
executed_bots = []
async def mock_wait_for(coro, timeout):
result = await coro
# Simulate timeout for Bot 2
if len(executed_bots) == 2 and executed_bots[-1] == "Bot 2":
raise asyncio.TimeoutError()
return result
def mock_execute(code, *args, **kwargs):
if "Bot 1" in code:
executed_bots.append("Bot 1")
return "Response 1"
elif "Bot 2" in code:
executed_bots.append("Bot 2")
return "Response 2" # This will be "timed out"
elif "Bot 3" in code:
executed_bots.append("Bot 3")
return "Response 3"
return None
with patch("app.repository.AppSettingsRepository") as mock_repo:
mock_settings = MagicMock()
mock_settings.bots = [
BotConfig(id="1", name="Bot 1", enabled=True, code="# Bot 1\ndef bot(): pass"),
BotConfig(id="2", name="Bot 2", enabled=True, code="# Bot 2\ndef bot(): pass"),
BotConfig(id="3", name="Bot 3", enabled=True, code="# Bot 3\ndef bot(): pass"),
]
mock_repo.get = AsyncMock(return_value=mock_settings)
with (
patch("app.bot.asyncio.sleep", new_callable=AsyncMock),
patch("app.bot.execute_bot_code", side_effect=mock_execute),
patch("app.bot.asyncio.wait_for", side_effect=mock_wait_for),
patch("app.bot.process_bot_response", new_callable=AsyncMock) as mock_respond,
):
await run_bot_for_message(
sender_name="Alice",
sender_key="abc123" + "0" * 58,
message_text="Hello",
is_dm=True,
channel_key=None,
)
# All bots should have been attempted
assert executed_bots == ["Bot 1", "Bot 2", "Bot 3"]
# Only responses from non-timed-out bots (Bot 1 and Bot 3)
assert mock_respond.call_count == 2
class TestBotCodeValidation:
"""Test bot code syntax validation on save."""
@@ -455,6 +673,18 @@ class TestBotCodeValidation:
assert exc_info.value.status_code == 400
assert "syntax error" in exc_info.value.detail.lower()
def test_syntax_error_includes_bot_name(self):
"""Syntax error message includes bot name when provided."""
from fastapi import HTTPException
from app.routers.settings import validate_bot_code
with pytest.raises(HTTPException) as exc_info:
validate_bot_code("def bot(:\n return 'broken'", bot_name="My Test Bot")
assert exc_info.value.status_code == 400
assert "My Test Bot" in exc_info.value.detail
def test_empty_code_passes(self):
"""Empty code passes validation (disables bot)."""
from app.routers.settings import validate_bot_code
@@ -463,6 +693,29 @@ class TestBotCodeValidation:
validate_bot_code("")
validate_bot_code(" ")
def test_validate_all_bots(self):
"""validate_all_bots validates all bots' code."""
from fastapi import HTTPException
from app.routers.settings import validate_all_bots
# Valid bots should pass
valid_bots = [
BotConfig(id="1", name="Bot 1", enabled=True, code="def bot(): return 'hi'"),
BotConfig(id="2", name="Bot 2", enabled=False, code="def bot(): return 'hello'"),
]
validate_all_bots(valid_bots) # Should not raise
# Invalid code should raise with bot name
invalid_bots = [
BotConfig(id="1", name="Good Bot", enabled=True, code="def bot(): return 'hi'"),
BotConfig(id="2", name="Bad Bot", enabled=True, code="def bot(:"),
]
with pytest.raises(HTTPException) as exc_info:
validate_all_bots(invalid_bots)
assert "Bad Bot" in exc_info.value.detail
class TestBotMessageRateLimiting:
"""Test bot message rate limiting for repeater compatibility."""
+101 -7
View File
@@ -100,8 +100,8 @@ class TestMigration001:
# Run migrations
applied = await run_migrations(conn)
assert applied == 12 # All 11 migrations run
assert await get_version(conn) == 12
assert applied == 13 # All 13 migrations run
assert await get_version(conn) == 13
# Verify columns exist by inserting and selecting
await conn.execute(
@@ -183,9 +183,9 @@ class TestMigration001:
applied1 = await run_migrations(conn)
applied2 = await run_migrations(conn)
assert applied1 == 12 # All 11 migrations run
assert applied1 == 13 # All 13 migrations run
assert applied2 == 0 # No migrations on second run
assert await get_version(conn) == 12
assert await get_version(conn) == 13
finally:
await conn.close()
@@ -245,9 +245,9 @@ class TestMigration001:
# Run migrations - should not fail
applied = await run_migrations(conn)
# All 11 migrations applied (version incremented) but no error
assert applied == 12
assert await get_version(conn) == 12
# All 13 migrations applied (version incremented) but no error
assert applied == 13
assert await get_version(conn) == 13
finally:
await conn.close()
@@ -337,3 +337,97 @@ class TestMigration001:
assert row["last_read_at"] is None
finally:
await conn.close()
class TestMigration013:
"""Test migration 013: convert bot_enabled/bot_code to multi-bot format."""
@pytest.mark.asyncio
async def test_migration_converts_existing_bot_to_array(self):
"""Migration converts existing bot_enabled/bot_code to bots array."""
import json
conn = await aiosqlite.connect(":memory:")
conn.row_factory = aiosqlite.Row
try:
# Set version to 12 (just before migration 13)
await set_version(conn, 12)
# Create app_settings with old bot columns
await conn.execute("""
CREATE TABLE app_settings (
id INTEGER PRIMARY KEY,
max_radio_contacts INTEGER DEFAULT 50,
favorites TEXT DEFAULT '[]',
auto_decrypt_dm_on_advert INTEGER DEFAULT 0,
sidebar_sort_order TEXT DEFAULT 'recent',
last_message_times TEXT DEFAULT '{}',
preferences_migrated INTEGER DEFAULT 0,
advert_interval INTEGER DEFAULT 0,
last_advert_time INTEGER DEFAULT 0,
bot_enabled INTEGER DEFAULT 0,
bot_code TEXT DEFAULT ''
)
""")
await conn.execute(
"INSERT INTO app_settings (id, bot_enabled, bot_code) VALUES (1, 1, 'def bot(): return \"hello\"')"
)
await conn.commit()
# Run migration 13
applied = await run_migrations(conn)
assert applied == 1
assert await get_version(conn) == 13
# Verify bots array was created with migrated data
cursor = await conn.execute("SELECT bots FROM app_settings WHERE id = 1")
row = await cursor.fetchone()
bots = json.loads(row["bots"])
assert len(bots) == 1
assert bots[0]["name"] == "Bot 1"
assert bots[0]["enabled"] is True
assert bots[0]["code"] == 'def bot(): return "hello"'
assert "id" in bots[0] # Should have a UUID
finally:
await conn.close()
@pytest.mark.asyncio
async def test_migration_creates_empty_array_when_no_bot(self):
"""Migration creates empty bots array when no existing bot data."""
import json
conn = await aiosqlite.connect(":memory:")
conn.row_factory = aiosqlite.Row
try:
await set_version(conn, 12)
await conn.execute("""
CREATE TABLE app_settings (
id INTEGER PRIMARY KEY,
max_radio_contacts INTEGER DEFAULT 50,
favorites TEXT DEFAULT '[]',
auto_decrypt_dm_on_advert INTEGER DEFAULT 0,
sidebar_sort_order TEXT DEFAULT 'recent',
last_message_times TEXT DEFAULT '{}',
preferences_migrated INTEGER DEFAULT 0,
advert_interval INTEGER DEFAULT 0,
last_advert_time INTEGER DEFAULT 0,
bot_enabled INTEGER DEFAULT 0,
bot_code TEXT DEFAULT ''
)
""")
await conn.execute(
"INSERT INTO app_settings (id, bot_enabled, bot_code) VALUES (1, 0, '')"
)
await conn.commit()
await run_migrations(conn)
cursor = await conn.execute("SELECT bots FROM app_settings WHERE id = 1")
row = await cursor.fetchone()
bots = json.loads(row["bots"])
assert bots == []
finally:
await conn.close()