1
0

fix: test DB isolation, Discord recovery, and over-mocked tests

Test data was bleeding into production tasks.db because
swarm.task_queue.models.DB_PATH (relative path) was never patched in
conftest.clean_database. Fixed by switching to absolute paths via
settings.repo_root and adding the missing module to the patching list.

Discord bot could leak orphaned clients on retry after ERROR state.
Added _cleanup_stale() to close stale client/task before each start()
attempt, with improved logging in the token watcher.

Rewrote test_paperclip_client.py to use httpx.MockTransport instead of
patching _get/_post/_delete — tests now exercise real HTTP status codes,
error handling, and JSON parsing. Added end-to-end test for
capture_error → create_task DB isolation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Trip T
2026-03-11 20:33:59 -04:00
parent 9d9449cdcf
commit ea2dbdb4b5
8 changed files with 253 additions and 66 deletions

View File

@@ -96,6 +96,42 @@ class TestDiscordVendor:
await vendor.stop()
assert vendor.state == PlatformState.DISCONNECTED
@pytest.mark.asyncio
async def test_cleanup_stale_closes_orphaned_client(self):
"""_cleanup_stale should close any leftover client from a failed start."""
from integrations.chat_bridge.vendors.discord import DiscordVendor
vendor = DiscordVendor()
mock_client = MagicMock()
mock_client.is_closed.return_value = False
mock_client.close = AsyncMock()
vendor._client = mock_client
await vendor._cleanup_stale()
mock_client.close.assert_called_once()
assert vendor._client is None
@pytest.mark.asyncio
async def test_start_from_error_state_cleans_up(self):
"""start() after ERROR should clean up stale state before retrying."""
from integrations.chat_bridge.vendors.discord import DiscordVendor
vendor = DiscordVendor()
vendor._state = PlatformState.ERROR
# Stale client from previous failed attempt
mock_old_client = MagicMock()
mock_old_client.is_closed.return_value = False
mock_old_client.close = AsyncMock()
vendor._client = mock_old_client
# start() should clean up the old client even though discord.py import
# will fail (we're in test mode with MagicMock stub)
with patch.dict("sys.modules", {"discord": None}):
result = await vendor.start(token="fake-token")
assert result is False
mock_old_client.close.assert_called_once()
def test_get_oauth2_url_no_client(self):
from integrations.chat_bridge.vendors.discord import DiscordVendor